summaryrefslogtreecommitdiffstats
path: root/docs/guides/onap-operator/settingup/index.rst
diff options
context:
space:
mode:
Diffstat (limited to 'docs/guides/onap-operator/settingup/index.rst')
0 files changed, 0 insertions, 0 deletions
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
var log4js = require('log4js');
var http = require('http');
var async = require('async');
var properties = require(process.env.SDNC_CONFIG_DIR + '/netdb-updater.json');
var admProperties = require(process.env.SDNC_CONFIG_DIR + '/admportal.json');
var csvtojson = require('csvtojson');
var mysql = require('mysql');
var moment = require('moment');
var os = require('os');
var fs = require('fs.extra');

// Check to make sure SDNC_CONFIG_DIR is set
var sdnc_config_dir = process.env.SDNC_CONFIG_DIR;
if ( typeof sdnc_config_dir == 'undefined' )
{
    console.log('ERROR the SDNC_CONFIG_DIR environmental variable is not set.');
    return;
}

// SETUP LOGGER
log4js.configure(process.env.SDNC_CONFIG_DIR + '/netdb.log4js.json');
var logger = log4js.getLogger('netdb');
logger.setLevel(properties.netdbLogLevel);

var yargs = require('yargs')
  .usage("\nUsage: node netdb_updater -t link_master|router_master")
  .demand('t')
  .alias('t', 'table')
  .example("Example: node netdb_updater -t link_master","Update SDNC LINK_MASTER table from NetDB.")
  .argv;

var dbTable = yargs.table;
var debug = properties.netdbDebug;
var env = properties.netdbEnv; 
var retSuccess = false;

// DB Setup
var currentDB = '';
var dbConnection = '';
var db01 = '';
var db02 = '';
var count = 0;
var errorMsg = [];

var dbtasks = [];
dbtasks.push( function(callback) { checkParams(callback); } );
dbtasks.push( function(callback) { dbConnect(callback); } );
//dbtasks.push( function(callback) { netdb(callback); } );

logger.debug('\n\n********** START PROCESSING - Env=' + env + ' Debug=' + debug + ' **********');

async.series(dbtasks, function(err,result){
    if(err) {
		logger.error(err + ' COUNT: ' + count);
    }
    else {
		if ( errorMsg.length > 0 ){
			logger.error(errorMsg);
		}
	}
});


function checkParams(scb){
	if ( dbTable != 'link_master' && dbTable != 'router_master' ){
		scb("Invalid parameter passed in '" + dbTable + " ' exiting.'");
	}
	else{
		scb(null);
	}
}


async.whilst(
    	function () { return count < properties.netdbRetryInterval },
    	function (callback) {
			if ( dbTable == 'link_master' ){
				getLinkMaster(callback);
			}
			else if (dbTable == 'router_master'){
				getRouterMaster(callback);
			}
			else{ // should never hit this condition
				logger.debug("Invalid parameter passed in '" + dbTable + " ' exiting.'");
			}
    	},
    	function (err) {
logger.debug('whilst err function errorMsg = ' + errorMsg);
			// report error
			if ( errorMsg.length > 0 ){
				logger.debug(errorMsg + ' COUNT: ' + count);
				process.exit(1);
			}
			else{
				logger.debug('success');
				process.exit(0);
			}
    	}
);


function returnError(emsg, cb){
	retSuccess=false;
	errorMsg.push(emsg);
	if ( count == properties.netdbRetryInterval ) { logger.error(errorMsg); }
    setTimeout( function(){
    		cb(null);
		}, properties.netdbWaitTime);
}

function returnSuccess(cb){
logger.debug('inside returnSuccess');
	errorMsg = '';
	//var cnt = properties.netdbRetryInterval;
	//logger.debug('b4 inc returnSuccess count=' + count);
	//count = ++cnt;
	//logger.debug('after inc returnSuccess count=' + count);
	//cb(null);
	retSuccess=true;
process.exit(0);
} 

function getRouterMaster(cb){

	logger.info('getRouterMaster debug=' + debug + ' count=' + count);

	// setup connection
    var netdbEnv = properties.netdbEnv;
    var auth_param = '';
    if ( netdbEnv == 'e2e' || netdbEnv == 'prod' ){
        // conexus network
        auth_param = '?auth=' + admProperties['ip-addresses']['eth2'] + ';'
    }else{
        // app network
        auth_param = '?auth=' + admProperties['ip-addresses']['eth1:0'] + ';'
    }
    var username = properties.netdbUser;;
    var password = properties.netdbPassword;
	var date = moment().format('YYYYMMDD');
    var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
    var host = properties.netdbHost;
    var port = properties.netdbPort;
    var path = '/' + properties.netdbPath
                + '/' + properties.netdbNetwork
                + '/' + properties.netdbApiName
                + auth_param
                + 'client=' + properties.netdbClientName + ';'
                + 'date=' + date + ';'
                + 'reportName=' + dbTable + ';'
                + 'type=' + properties.netdbType;

    var header = { 'Content-Type': 'text/csv' };
    //var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'text/csv' };
    var options = {
        method            : "GET",
        path              : path,
        host              : host,
        port              : port,
        headers           : header
    };

	logger.debug('options:\n' + JSON.stringify(options,null,2));

    var request = http.request(options, function(response) {

    	var response_str = '';
		if ( retSuccess == true ){
			var cnt = properties.netdbRetryInterval;
			count = ++cnt;
		}
		else{
			count++;
		}

        logger.debug('STATUS: ' + response.statusCode + ' content-type=' + response.headers['content-type']);

        // Read the response from ODL side
        response.on('data', function(chunk) {
            response_str += chunk;
        });

        response.on('end', function() {

			logger.debug('HEADERS:' + JSON.stringify(response.headers));

            if(response.statusCode == 200){

                if(response_str.length > 0){

					// save the upload
					try{
						fs.writeFileSync('/sdncvar/sdnc/files/netdb-updater/' + moment().unix() + ".netdb." + dbTable + '.csv', response_str);
					}
					catch(e){
						// this is not in reqs, if it fails keep on going.
						logger.error('Error writing NetDB file:' + e);
					}

					if (response.headers['content-type'].indexOf('html') > 0){
						returnError('Error:Unexpected content-type:' + response.headers['content-type'] + ' returned.\n', cb);
						return;
					}
                    // need to parse csv file
                    var Converter=csvtojson.Converter;
                    var csvConverter = new Converter({
                        noheader:true
                    });
                    var routerMasterSQL = '';

                    // end_parsed will be emitted once parsing is finished
                    csvConverter.on("end_parsed", function(respObj){

                        routerMasterSQL = routerMasterSQL.concat("INSERT INTO ROUTER_MASTER (crs_name, loopback_ip)");
                        for ( var x=0; x < respObj.length; x++ ){

                           	if ( respObj[x].field1.length == 0 ){
                               	returnError('Required field [crs_name] is null.', cb);
                           	}

							if (x!=0){
								routerMasterSQL = routerMasterSQL.concat(' union ');
							}
							routerMasterSQL = routerMasterSQL.concat(" SELECT " 
								+ "'" + respObj[x].field1 + "',"
                               	+ "'" + respObj[x].field2 + "' FROM DUAL ");
                        }
                        //logger.debug('SQL: ' + routerMasterSQL);
	
			if (debug != 'debug' && env != 'dev'){

                        	var tasks = [];
                        	tasks.push( function(callback) { updateRouterMaster(routerMasterSQL,callback); } );
                        	async.series(tasks, function(err,result){
                            	if(err) {
                                	returnError(err,cb);
					return;
                            	}
                            	else {
					logger.info('*** Router Master Table Replaced ***');
                                	returnSuccess(cb);
					return;
                            	}
                        	});
			}
			else{
logger.debug('*** debug ***');
                            	returnSuccess(cb);
					return;
			}

                    });
                    csvConverter.on("error",function(errMsg,errData){
                        returnError(errMsg,cb);
						return;
                    });
                    csvConverter.fromString(response_str, function(err,result){
                        if(err){
							returnError(err,cb);
							return;
                        }
                    });
                }
                else{
                    //logger.debug("no data");
					returnError('no data',cb);
					return;
                }
            }
            else if(response.statusCode == 404){
				returnError('Router Master Table for ' + date + ' is not Available.',cb);
				return;
			}
            else {
				returnError('Status Code:' + response.statudCode + ' returned for Router Master Table query.',cb);
				return;
            }
        });
    });
    request.on('error', function(e) {
	    if ( retSuccess == true ){
			var cnt = properties.netdbRetryInterval;
			count = ++cnt;
		}
		else{
			count++;
		}	
        returnError(e,cb);
		return;
    });
    request.end();
}

function getLinkMaster(cb){

	logger.info('getLinkMaster debug=' + debug + ' count=' + count);

    // setup connection
    var netdbEnv = properties.netdbEnv;
    var auth_param = '';
    if ( netdbEnv == 'e2e' || netdbEnv == 'prod' ){
        // conexus network
        auth_param = '?auth=' + admProperties['ip-addresses']['eth2'] + ';'
    }else{
        // app network
        auth_param = '?auth=' + admProperties['ip-addresses']['eth1:0'] + ';'
    }
    var username = properties.netdbUser;;
    var password = properties.netdbPassword;
    var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
    var host = properties.netdbHost;
    var port = properties.netdbPort;
	var date = moment().format('YYYYMMDD');
    var path = '/' + properties.netdbPath
                + '/' + properties.netdbNetwork
                + '/' + properties.netdbApiName
                + auth_param
                + 'client=' + properties.netdbClientName + ';'
                + 'date=' + date + ';'
                + 'reportName=' + dbTable + ';'
                + 'type=' + properties.netdbType;

    var header = { 'Content-Type': 'text/csv' };
    //var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'text/csv' };
    var options = {
        method            : "GET",
        path              : path,
        host              : host,
        port              : port,
        headers           : header
    };

    logger.debug('options:\n' + JSON.stringify(options,null,2));

	var request = http.request(options, function(response) {

        logger.debug('STATUS: ' + response.statusCode + ' content-type=' + response.headers['content-type']);

		if ( retSuccess == true ){
        	var cnt = properties.netdbRetryInterval;
        	count = ++cnt;
    	}
		else{
    		count++
		}

        var response_str = '';

        // Read the response from ODL side
        response.on('data', function(chunk) {
            response_str += chunk;
        });

        response.on('end', function() {

			logger.debug('HEADERS:' + JSON.stringify(response.headers));

            if(response.statusCode == 200){

				if(response_str.length > 0){

					//logger.debug('response_str=' + response_str);
					// save the upload
                    try{
						fs.writeFileSync('/sdncvar/sdnc/files/netdb-updater/' + moment().unix() + ".netdb." + dbTable + '.csv', response_str);
                    }
                    catch(e){
                        // this is not in reqs, if it fails keep on going.
                        logger.error('Error writing NetDB file:' + e);
                    }

					if (response.headers['content-type'].indexOf('html') > 0){
						returnError('Error:Unexpected content-type:' + response.headers['content-type'] + ' returned.\n', cb);
						return;
					}
					// need to parse csv file
					var Converter=csvtojson.Converter;
					var csvConverter = new Converter({
						noheader:true
					});

					var linkMasterSQL = '';

					// end_parsed will be emitted once parsing is finished
					csvConverter.on("end_parsed", function(jsonObj){

						linkMasterSQL = linkMasterSQL.concat("INSERT INTO LINK_MASTER (link_interface_ip, source_crs_name, destination_crs_name, link_speed, default_cost, bundle_name, shutdown)"); 
						for ( var x=0; x < jsonObj.length; x++ ){
							if ( jsonObj[x].field1.length == 0 ){
								returnError('Required field [link_interface_ip] is null.', cb);
								return;
							}
							if ( jsonObj[x].field2.length == 0 ){
								returnError('Required field [source_crs_name] is null.', cb);
								return;
							}
							if ( jsonObj[x].field3.length == 0 ){
								returnError('Required field [destination_crs_name] is null.', cb);
								return;
							}
							if (x!=0){
								linkMasterSQL = linkMasterSQL.concat(' union ');
							}

							linkMasterSQL = linkMasterSQL.concat(" SELECT " 
								+ "'" + jsonObj[x].field1 + "',"
								+ "'" + jsonObj[x].field2 + "',"
								+ "'" + jsonObj[x].field3 + "',"
								+ jsonObj[x].field4 + ","
								+ jsonObj[x].field5 + ","
								+ "'" + jsonObj[x].field6 + "',"
								+ "'" + jsonObj[x].field7 + "' FROM DUAL");
						}
						//logger.debug('SQL: ' + linkMasterSQL);

						if (debug != 'debug' && env != 'dev'){
                    		// update db
							var tasks = [];
							tasks.push( function(callback) { updateLinkMaster(linkMasterSQL,callback); } );
    						async.series(tasks, function(err,result){
								if(err)
                            	{
                                	returnError(err,cb);
									return;
                            	}
                            	else
                            	{
									logger.info('*** Link Master Table Replaced ***');
                                	returnSuccess(cb);
									return;
                            	}
    						});
						}
						else{
                        	returnSuccess(cb);
							return;
						}
					});
					csvConverter.on("error",function(errMsg,errData){
    					returnError(errMsg,cb);
						return;
  					});
					csvConverter.fromString(response_str, function(err,result){
						if(err){
    						returnError(errMsg,cb);
							return;
						}
					});
                }
                else{
    				returnError('no data',cb);
					return;
                }
            }
            else if(response.statusCode == 404){
				returnError('Link Master Table for ' + date + ' is not Available.',cb);
				return;
			}
            else {
				returnError('Status Code:' + response.statudCode + ' returned for Link Master Table query.',cb);
				return;
            }
    	});
	});
	request.on('error', function(e) {
		if ( retSuccess == true ){
        	var cnt = properties.netdbRetryInterval;
        	count = ++cnt;
    	}
		else{
    		count++
		}
        returnError(e,cb);
        return;
	});
	request.end();
}
			

function dbConnect(callback){

        var l_db01 = admProperties['databases']['0'];
	var db01Array = l_db01.split("|");
	db01 = db01Array[0];

        var l_db02 = admProperties['databases']['1'];
	var db02Array = l_db02.split("|");
	db02 = db02Array[0];

	if ( admProperties.dbFabric == 'true' )
	{
		logger.debug('connectFabric()');

    // testing 
    var fabric_connection = mysql.createConnection({
        host            : admProperties.dbFabricServer,
        user            : admProperties.dbFabricUser,
        password        : admProperties.dbFabricPassword,
        database        : admProperties.dbFabricDB,
        port            : admProperties.dbFabricPort
    });


    fabric_connection.connect( function(err) {

        if (err) {
            callback(err);
            return;
        }
        fabric_connection.query('CALL dump.servers()', function(err,rows) {

            var masterDB = '';

            if (err) {
                callback(err);
                return;
            }
            fabric_connection.end();
            logger.debug('rows: ' + JSON.stringify(rows,null,2));

            // fabric servers
            for ( var x=0; x<rows.length; x++)
            {
                // database servers
                for ( var y=0; y<rows[x].length; y++)
                {
                    var row = rows[x][y];
                    if (row.group_id == admProperties.dbFabricGroupId)
                    {
                        if (row.status == '3' && row.mode == '3'){
                            masterDB = row.host;
                        }
                    }
                }
            }
            logger.debug('currentDB: ' + currentDB);
            logger.debug('masterDB: ' + masterDB);

            if (masterDB.length <=0)
            {
                logger.debug('no writable master db');
                callback('no writable master db');
                return;
            }

            if ( currentDB != masterDB )
            {
                currentDB = masterDB;
                dbConnection = mysql.createConnection({
                    connectionLimit   : admProperties.dbConnLimit,
                    host              : currentDB,
                    user              : admProperties.dbUser,
                    password          : admProperties.dbPassword,
                    database          : admProperties.dbName,
                    multipleStatements: true,
                    debug             : false
                });
            }
            logger.debug('new currentDB: ' + currentDB);
            logger.debug('new masterDB: ' + masterDB);
            callback(null);
            return;
        });
        fabric_connection.on('error', function(err){
             logger.debug(err.code);
             callback(err);
		     return;
        });
    });
	}
	else
	{
    	currentDB = db01;

    	var dbConn = mysql.createConnection({
        	connectionLimit : admProperties.dbConnLimit,
        	host            : currentDB,
        	user            : admProperties.dbUser,
        	password        : admProperties.dbPassword,
        	database        : admProperties.dbName,
        	multipleStatements: true,
        	debug           : false
    	});
		logger.debug('initDB currentDB=' + currentDB);

    	dbConn.connect(function(err,connection){

        	if(err){
            	logger.debug( String(err) ); // ALARM
            	callback(err);
            	return;
        	}
        	var sql = 'select @@read_only';
        	dbConn.query(sql, function(err,result){
            	dbConn.end();

            	// @@read_only=0 means db is writable
            	logger.debug('@@read_only=' + result[0]['@@read_only']);
            	if ( result[0]['@@read_only'] != '0' )
            	{
                	if (currentDB == db01)
                	{
                    	currentDB = db02;
                	}
                	else
                	{
                    	currentDB = db01;
                	}
logger.debug('initDB reconnect to currentDB '+ currentDB);
                	var newConnection = mysql.createConnection({
                    	connectionLimit : admProperties.dbConnLimit,
                    	host            : currentDB,
                    	user            : admProperties.dbUser,
                    	password        : admProperties.dbPassword,
                    	database        : admProperties.dbName,
                    	multipleStatements: true,
                    	debug           : false
                	}); // end create
                	dbConnection = newConnection;
                	callback(null);
					return;
            	}
                dbConnection = dbConn;
            	callback(null);
				return;
        	});
    	});
	}
}

function updateLinkMaster(linkMasterSQL,callback){

logger.debug('updateLinkMaster');

    dbConnection.connect(function(err,connection)
    {
        if(err){
            logger.debug( String(err) ); // ALARM
            callback(err, 'Unable to get database connection.');
            return;
        }
    }); // end connection
    dbConnection.beginTransaction(function(err) {
    	if(err){
            //dbConnection.release();
            callback(err,String(err));
            return;
        }
        var sql = "DELETE FROM LINK_MASTER";
        dbConnection.query(sql,function(err,result)
        {
            if(err){
                //dbConnection.release();
                dbConnection.rollback( {rollback: 'NO RELEASE'},function(){
                    callback(err,String(err));
                    return;
                });
            }
            dbConnection.query(linkMasterSQL,function(err,result)
            {
                if(err){
                    //dbConnection.release();
                    dbConnection.rollback( {rollback: 'NO RELEASE'},function(){
                        callback(err,String(err));
                        return;
                    });
                }
                dbConnection.commit(function(err){
                    if(err){
                        //dbConnection.release();
                		dbConnection.rollback( {rollback: 'NO RELEASE'},function(){
                            callback(err,String(err));
                            return;
                        });
                    }
                    //dbConnection.release();
                    callback(null);
                });
            })
        });
    }); // end transaction
}

function updateRouterMaster(routerMasterSQL,callback){

logger.debug('updateRouterMaster');

    dbConnection.connect(function(err,connection)
    {
        if(err){
            logger.debug( String(err) ); // ALARM
            callback(err, 'Unable to get database connection.');
            return;
        }
    }); // end connection
    dbConnection.beginTransaction(function(err) {
        if(err){
            //dbConnection.release();
            callback(err,String(err));
            return;
        }
        var sql = "DELETE FROM ROUTER_MASTER";
        dbConnection.query(sql,function(err,result)
        {
            if(err){
                //dbConnection.release();
                dbConnection.rollback( function(){
                    callback(err,String(err));
                    return;
                });
            }
            dbConnection.query(routerMasterSQL,function(err,result)
            {
                if(err){
                    //dbConnection.release();
                	dbConnection.rollback( function(){
                        callback(err,String(err));
                        return;
                    });
                }
                dbConnection.commit(function(err){
                    if(err){
                        //dbConnection.release();
                		dbConnection.rollback( function(){
                            callback(err,String(err));
                            return;
                        });
                    }
                    //dbConnection.release();
                    callback(null);
                });
            })
        });
    }); // end transaction
}

/*

	logger.debug('getLinkMaster - count=' + count);
	if ( true ) {
		//fail
		setTimeout( function(){
			cb(null);}, properties.netdbWaitTime);
		return;
	}
	// success
	count = 10;
	cb(null);
}
*/