Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame^] | 1 | var express = require('express'); |
| 2 | var router = express.Router(); |
| 3 | var exec = require('child_process').exec; |
| 4 | var util = require('util'); |
| 5 | var fs = require('fs.extra'); |
| 6 | var dbRoutes = require('./dbRoutes'); |
| 7 | var csp = require('./csp'); |
| 8 | var multer = require('multer'); |
| 9 | var bodyParser = require('body-parser'); |
| 10 | var sax = require('sax'),strict=true,parser = sax.parser(strict); |
| 11 | var async = require('async'); |
| 12 | var l_ = require('lodash'); |
| 13 | var dateFormat = require('dateformat'); |
| 14 | var properties = require(process.env.SDNC_CONFIG_DIR + '/admportal.json'); |
| 15 | |
| 16 | |
| 17 | // pass host, username and password to ODL |
| 18 | // target host for ODL request |
| 19 | var username = properties.odlUser; |
| 20 | var password = properties.odlPasswd; |
| 21 | var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); |
| 22 | var host = properties.odlHost; |
| 23 | var port = properties.odlPort; |
| 24 | |
| 25 | var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'application/json'}; |
| 26 | var options = { |
| 27 | host : host, |
| 28 | headers : header, |
| 29 | port : port, |
| 30 | rejectUnauthorized:false, |
| 31 | strictSSL: false |
| 32 | }; |
| 33 | |
| 34 | // Connection to OpenDaylight |
| 35 | Odl = require('./Odl'); |
| 36 | |
| 37 | // used for file upload button, retain original file name |
| 38 | //router.use(bodyParser()); |
| 39 | //router.use(bodyParser.urlencoded({ |
| 40 | //extended: true |
| 41 | //})); |
| 42 | |
| 43 | //var upload = multer({ dest: process.cwd() + '/uploads/', rename: function(fieldname,filename){ return filename; } }); |
| 44 | |
| 45 | // multer 1.1 |
| 46 | var storage = multer.diskStorage({ |
| 47 | destination: function (req, file, cb) { |
| 48 | cb(null, process.cwd() + '/uploads/') |
| 49 | }, |
| 50 | filename: function (req, file, cb) { |
| 51 | cb(null, file.originalname ) |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | var upload = multer({ |
| 56 | storage: storage |
| 57 | }); |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | // GET |
| 63 | router.get('/getVnfData', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 64 | dbRoutes.getVnfData(req,res, {code:'', msg:''}, req.session.loggedInAdmin); |
| 65 | }); |
| 66 | router.get('/getVmNetworks', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 67 | dbRoutes.getVmNetworks(req,res, {code:'', msg:''}, req.session.loggedInAdmin); |
| 68 | }); |
| 69 | router.get('/getVnfProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 70 | dbRoutes.getVnfProfile(req,res, {code:'', msg:''}, req.session.loggedInAdmin); |
| 71 | }); |
| 72 | router.get('/getVnfNetworks', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 73 | dbRoutes.getVnfNetworks(req,res, {code:'', msg:''}, req.session.loggedInAdmin); |
| 74 | }); |
| 75 | router.get('/getVmProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 76 | dbRoutes.getVmProfile(req,res, {code:'', msg:''}, req.session.loggedInAdmin); |
| 77 | }); |
| 78 | //////// |
| 79 | router.get('/getVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 80 | dbRoutes.getVnfNetworkData(req,res, {code:'', msg:''}, req.session.loggedInAdmin); |
| 81 | }); |
| 82 | |
| 83 | router.get('/viewVnfNetworkData', csp.checkAuth, function(req,res) |
| 84 | { |
| 85 | var privilegeObj = req.session.loggedInAdmin; |
| 86 | var resp_msg = ''; |
| 87 | var network_name = req.query.network_name; |
| 88 | var network_type = req.query.network_type; |
| 89 | var tasks = []; |
| 90 | |
| 91 | tasks.push(function(callback){ |
| 92 | Odl.GetPreloadVnfData('/restconf/config/VNF-API:preload-vnfs/vnf-preload-list/' |
| 93 | + encodeURIComponent(network_name) + '/' + encodeURIComponent(network_type) + '/', options,res,callback); |
| 94 | |
| 95 | }); |
| 96 | async.series(tasks, function(err,result) |
| 97 | { |
| 98 | var msgArray = new Array(); |
| 99 | if(err){ |
| 100 | resp_msg = err; |
| 101 | res.render('mobility/displayVnfNetworkData', {result:{code:'failure', msg:resp_msg}, header:process.env.MAIN_MENU}); |
| 102 | return; |
| 103 | } |
| 104 | else{ |
| 105 | resp_msg = JSON.stringify(JSON.parse(result[0],null,4)); |
| 106 | res.render('mobility/displayVnfNetworkData', {result:{code:'success', msg:JSON.parse(result[0])}, header:process.env.MAIN_MENU}); |
| 107 | return; |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | }); |
| 112 | |
| 113 | router.get('/viewVnfData', csp.checkAuth, function(req,res) |
| 114 | { |
| 115 | var privilegeObj = req.session.loggedInAdmin; |
| 116 | var resp_msg = ''; |
| 117 | var vnf_name = req.query.vnf_name; |
| 118 | var vnf_type = req.query.vnf_type; |
| 119 | var tasks = []; |
| 120 | |
| 121 | tasks.push(function(callback){ |
| 122 | Odl.GetPreloadVnfData('/restconf/config/VNF-API:preload-vnfs/vnf-preload-list/' |
| 123 | + encodeURIComponent(vnf_name) + '/' + encodeURIComponent(vnf_type) + '/', options,res,callback); |
| 124 | |
| 125 | }); |
| 126 | async.series(tasks, function(err,result) |
| 127 | { |
| 128 | var msgArray = new Array(); |
| 129 | if(err){ |
| 130 | resp_msg = err; |
| 131 | res.render('mobility/displayVnfData', {result:{code:'failure', msg:resp_msg}, header:process.env.MAIN_MENU}); |
| 132 | return; |
| 133 | } |
| 134 | else{ |
| 135 | resp_msg = JSON.stringify(JSON.parse(result[0],null,4)); |
| 136 | res.render('mobility/displayVnfData', {result:{code:'success', msg:JSON.parse(result[0])}, header:process.env.MAIN_MENU}); |
| 137 | return; |
| 138 | } |
| 139 | }); |
| 140 | |
| 141 | }); |
| 142 | |
| 143 | router.get('/loadVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, function(req,res) |
| 144 | { |
| 145 | |
| 146 | var privilegeObj = req.session.loggedInAdmin; |
| 147 | var msgArray = new Array(); |
| 148 | |
| 149 | if ( req.query.status != 'pending' ) |
| 150 | { |
| 151 | msgArray.push("Upload Status must be in 'pending' state."); |
| 152 | dbRoutes.getVnfNetworkData(req,res, {code:'failure', msg:msgArray}, privilegeObj); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | // build request-id |
| 157 | var now = new Date(); |
| 158 | var df = dateFormat(now,"isoDateTime"); |
| 159 | var rnum = Math.floor((Math.random() * 9999) +1); |
| 160 | var svc_req_id = req.query.id + "-" + df + "-" + rnum; |
| 161 | |
| 162 | var tasks = []; |
| 163 | |
| 164 | // first get the contents of the file from the db |
| 165 | tasks.push(function(callback){ |
| 166 | dbRoutes.getVnfPreloadData(req,res,"PRE_LOAD_VNF_NETWORK_DATA",callback); |
| 167 | }); |
| 168 | |
| 169 | // then format the request and send it using the arg1 parameter |
| 170 | // which is the contents of the file returned from the previous function |
| 171 | // call in the tasks array |
| 172 | tasks.push(function(arg1,callback){ |
| 173 | |
| 174 | var s_file = JSON.stringify(arg1); |
| 175 | |
| 176 | // remove the last two braces, going to add the headers there |
| 177 | // will add them back later. |
| 178 | s_file = s_file.substring(0, (s_file.length-2)); |
| 179 | |
| 180 | // add the request-information header |
| 181 | s_file = s_file.concat(',"request-information": {"request-action": "PreloadNetworkRequest"}'); |
| 182 | |
| 183 | // add the sdnc-request-header |
| 184 | s_file = s_file.concat(',"sdnc-request-header": {"svc-request-id":"'); |
| 185 | s_file = s_file.concat(svc_req_id); |
| 186 | s_file = s_file.concat('","svc-action": "reserve"}'); |
| 187 | |
| 188 | // add the two curly braces at the end that we stripped off |
| 189 | s_file = s_file.concat('}}'); |
| 190 | |
| 191 | Odl.Post('/restconf/operations/VNF-API:preload-network-topology-operation', |
| 192 | options,s_file,res,callback); |
| 193 | }); |
| 194 | |
| 195 | // if successful then update the status |
| 196 | tasks.push(function(arg1,callback){ |
| 197 | dbRoutes.executeSQL("UPDATE PRE_LOAD_VNF_NETWORK_DATA SET status='uploaded',svc_request_id='" |
| 198 | + svc_req_id + "',svc_action='reserve' WHERE id="+req.query.id,req,res,callback); |
| 199 | }); |
| 200 | |
| 201 | // use the waterfall method of making calls |
| 202 | async.waterfall(tasks, function(err,result) |
| 203 | { |
| 204 | var msgArray = new Array(); |
| 205 | if(err){ |
| 206 | msgArray.push("Error posting pre-load data to ODL: "+err); |
| 207 | dbRoutes.getVnfNetworkData(req,res, {code:'failure', msg:msgArray}, privilegeObj); |
| 208 | return; |
| 209 | } |
| 210 | else{ |
| 211 | msgArray.push('Successfully loaded VNF pre-loaded data.'); |
| 212 | dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 213 | return; |
| 214 | } |
| 215 | }); |
| 216 | }); |
| 217 | |
| 218 | |
| 219 | router.get('/loadVnfData', csp.checkAuth, dbRoutes.checkDB, function(req,res) |
| 220 | { |
| 221 | var privilegeObj = req.session.loggedInAdmin; |
| 222 | var full_path_file_name = process.cwd() + "/uploads/" + req.query.filename |
| 223 | var msgArray = new Array(); |
| 224 | |
| 225 | if ( req.query.status != 'pending' ) |
| 226 | { |
| 227 | msgArray.push("Upload Status must be in 'pending' state."); |
| 228 | dbRoutes.getVnfData(req,res, {code:'failure', msg:msgArray}, privilegeObj); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | // build request-id |
| 233 | var now = new Date(); |
| 234 | var df = dateFormat(now,"isoDateTime"); |
| 235 | var rnum = Math.floor((Math.random() * 9999) +1); |
| 236 | var svc_req_id = req.query.id + "-" + df + "-" + rnum; |
| 237 | |
| 238 | var tasks = []; |
| 239 | |
| 240 | // first get the contents of the file from the db |
| 241 | tasks.push(function(callback){ |
| 242 | dbRoutes.getVnfPreloadData(req,res,"PRE_LOAD_VNF_DATA",callback); |
| 243 | }); |
| 244 | |
| 245 | // then format the request and send it using the arg1 parameter |
| 246 | // which is the contents of the file returned from the previous function |
| 247 | // call in the tasks array |
| 248 | tasks.push(function(arg1,callback){ |
| 249 | |
| 250 | var s1_file = JSON.stringify(arg1); |
| 251 | var s_file = decodeURI(s1_file); |
| 252 | |
| 253 | |
| 254 | // remove the last two braces, going to add the headers there |
| 255 | // will add them back later. |
| 256 | s_file = s_file.substring(0, (s_file.length-2)); |
| 257 | |
| 258 | // add the request-information header |
| 259 | s_file = s_file.concat(',"request-information": {"request-action": "PreloadVNFRequest"}'); |
| 260 | |
| 261 | // add the sdnc-request-header |
| 262 | s_file = s_file.concat(',"sdnc-request-header": {"svc-request-id":"'); |
| 263 | s_file = s_file.concat(svc_req_id); |
| 264 | s_file = s_file.concat('","svc-action": "reserve"}'); |
| 265 | |
| 266 | // add the two curly braces at the end that we stripped off |
| 267 | s_file = s_file.concat('}}'); |
| 268 | |
| 269 | Odl.Post('/restconf/operations/VNF-API:preload-vnf-topology-operation', |
| 270 | options,s_file,res,callback); |
| 271 | }); |
| 272 | |
| 273 | // if successful then update the status |
| 274 | tasks.push(function(arg1,callback){ |
| 275 | dbRoutes.executeSQL("UPDATE PRE_LOAD_VNF_DATA SET status='uploaded',svc_request_id='" |
| 276 | + svc_req_id + "',svc_action='reserve' WHERE id="+req.query.id,req,res,callback); |
| 277 | }); |
| 278 | |
| 279 | // use the waterfall method of making calls |
| 280 | async.waterfall(tasks, function(err,result) |
| 281 | { |
| 282 | var msgArray = new Array(); |
| 283 | if(err){ |
| 284 | msgArray.push("Error posting pre-load data to ODL: "+err); |
| 285 | dbRoutes.getVnfData(req,res, {code:'failure', msg:msgArray}, privilegeObj); |
| 286 | return; |
| 287 | } |
| 288 | else{ |
| 289 | msgArray.push('Successfully loaded VNF pre-loaded data.'); |
| 290 | dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 291 | return; |
| 292 | } |
| 293 | }); |
| 294 | }); |
| 295 | |
| 296 | |
| 297 | router.get('/deleteVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 298 | |
| 299 | var privilegeObj = req.session.loggedInAdmin; |
| 300 | var tasks = []; |
| 301 | var sql = 'DELETE FROM PRE_LOAD_VNF_NETWORK_DATA WHERE id=' + req.query.id; |
| 302 | |
| 303 | // if status is pending, then we do not have to call |
| 304 | // ODL, just remove from db |
| 305 | if (req.query.status == 'pending'){ |
| 306 | tasks.push(function(callback) { |
| 307 | dbRoutes.executeSQL(sql,req,res,callback); |
| 308 | }); |
| 309 | } else { |
| 310 | // format the request to ODL |
| 311 | var inputString = '{"input":{"network-topology-information":{"network-topology-identifier":{"service-type":"SDN-MOBILITY","network-name": "'; |
| 312 | inputString = inputString.concat(req.query.network_name); |
| 313 | inputString = inputString.concat('","network-type":"'); |
| 314 | inputString = inputString.concat(req.query.network_type); |
| 315 | inputString = inputString.concat('"}},'); |
| 316 | |
| 317 | // add the request-information header |
| 318 | inputString = inputString.concat('"request-information": {"request-action": "DeletePreloadNetworkRequest"},'); |
| 319 | |
| 320 | // add the sdnc-request-header |
| 321 | inputString = inputString.concat('"sdnc-request-header": {"svc-request-id":"'); |
| 322 | inputString = inputString.concat(req.query.svc_request_id); |
| 323 | inputString = inputString.concat('","svc-action": "delete"}}}'); |
| 324 | |
| 325 | tasks.push(function(callback) { |
| 326 | Odl.Post('/restconf/operations/VNF-API:preload-network-topology-operation', |
| 327 | options,inputString,res,callback); |
| 328 | }); |
| 329 | tasks.push(function(callback) { |
| 330 | dbRoutes.executeSQL(sql,req,res,callback); |
| 331 | }); |
| 332 | } |
| 333 | async.series(tasks, function(err,result){ |
| 334 | |
| 335 | var msgArray = new Array(); |
| 336 | if(err){ |
| 337 | msgArray.push(err); |
| 338 | dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 339 | return; |
| 340 | } |
| 341 | else { |
| 342 | msgArray.push('Row successfully deleted from PRE_LOAD_VNF_NETWORK_DATA table and ODL.'); |
| 343 | dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 344 | return; |
| 345 | } |
| 346 | }); |
| 347 | }); |
| 348 | |
| 349 | |
| 350 | router.get('/deleteVnfData', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 351 | |
| 352 | var privilegeObj = req.session.loggedInAdmin; |
| 353 | var tasks = []; |
| 354 | var sql = 'DELETE FROM PRE_LOAD_VNF_DATA WHERE id=' + req.query.id; |
| 355 | |
| 356 | // if status is pending, then we do not have to call |
| 357 | // ODL, just remove from db |
| 358 | if (req.query.status == 'pending'){ |
| 359 | tasks.push(function(callback) { |
| 360 | dbRoutes.executeSQL(sql,req,res,callback); |
| 361 | }); |
| 362 | } else { |
| 363 | var inputString = '{"input":{"vnf-topology-information":{"vnf-topology-identifier":{"service-type":"SDN-MOBILITY","vnf-name": "'; |
| 364 | inputString = inputString.concat(req.query.vnf_name); |
| 365 | inputString = inputString.concat('","vnf-type":"'); |
| 366 | inputString = inputString.concat(req.query.vnf_type); |
| 367 | inputString = inputString.concat('"}},'); |
| 368 | |
| 369 | // add the request-information header |
| 370 | inputString = inputString.concat('"request-information": {"request-action": "DeletePreloadVNFRequest"},'); |
| 371 | |
| 372 | // add the request-information header |
| 373 | //inputString = inputString.concat('"request-information": {"request-id": "259c0f93-23cf-46ad-84dc-162ea234fff1",'); |
| 374 | //inputString = inputString.concat('"source": "ADMINPORTAL",'); |
| 375 | //inputString = inputString.concat('"order-version": "1",'); |
| 376 | //inputString = inputString.concat('"notification-url": "notused-this would be infrastructure portal",'); |
| 377 | //inputString = inputString.concat('"order-number": "1",'); |
| 378 | //inputString = inputString.concat('"request-action": "DeletePreloadVNFRequest"},'); |
| 379 | |
| 380 | // add the sdnc-request-header |
| 381 | inputString = inputString.concat('"sdnc-request-header": {"svc-request-id":"'); |
| 382 | inputString = inputString.concat(req.query.svc_request_id); |
| 383 | inputString = inputString.concat('","svc-action": "delete"}}}'); |
| 384 | |
| 385 | //inputString = inputString.concat('"sdnc-request-header":{'); |
| 386 | //inputString = inputString.concat('"svc-request-id": "2015-01-15T14:34:54.st1101a",'); |
| 387 | //inputString = inputString.concat('"svc-notification-url": "not used",'); |
| 388 | //inputString = inputString.concat('"svc-action": "delete"}}}'); |
| 389 | |
| 390 | tasks.push(function(callback) { |
| 391 | Odl.Post('/restconf/operations/VNF-API:preload-vnf-topology-operation', |
| 392 | options,inputString,res,callback); |
| 393 | }); |
| 394 | tasks.push(function(callback) { |
| 395 | dbRoutes.executeSQL(sql,req,res,callback); |
| 396 | }); |
| 397 | } |
| 398 | async.series(tasks, function(err,result){ |
| 399 | |
| 400 | var msgArray = new Array(); |
| 401 | if(err){ |
| 402 | msgArray.push(err); |
| 403 | dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 404 | return; |
| 405 | } |
| 406 | else { |
| 407 | msgArray.push('Row successfully deleted from PRE_LOAD_VNF_DATA table and ODL.'); |
| 408 | dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 409 | return; |
| 410 | } |
| 411 | }); |
| 412 | }); |
| 413 | |
| 414 | |
| 415 | router.get('/deleteVmProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 416 | |
| 417 | var privilegeObj = req.session.loggedInAdmin; |
| 418 | var tasks = []; |
| 419 | var sql = ''; |
| 420 | |
| 421 | sql = "DELETE FROM VM_PROFILE WHERE vnf_type='" + req.query.vnf_type + "'" |
| 422 | + " AND vm_type='" + req.query.vm_type + "'"; |
| 423 | |
| 424 | tasks.push(function(callback) { |
| 425 | dbRoutes.executeSQL(sql,req,res,callback); |
| 426 | }); |
| 427 | async.series(tasks, function(err,result) |
| 428 | { |
| 429 | var msgArray = new Array(); |
| 430 | if(err){ |
| 431 | msgArray.push(err); |
| 432 | dbRoutes.getVmProfile(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 433 | return; |
| 434 | } |
| 435 | else { |
| 436 | msgArray.push('Row successfully deleted from VM_PROFILE table.'); |
| 437 | dbRoutes.getVmProfile(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 438 | return; |
| 439 | } |
| 440 | }); |
| 441 | }); |
| 442 | |
| 443 | |
| 444 | router.get('/deleteVnfNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 445 | |
| 446 | var privilegeObj = req.session.loggedInAdmin; |
| 447 | var tasks = []; |
| 448 | var sql = ''; |
| 449 | |
| 450 | sql = "DELETE FROM VNF_NETWORKS WHERE vnf_type='" + req.query.vnf_type + "'" |
| 451 | + " AND network_role='" + req.query.network_role + "'"; |
| 452 | |
| 453 | tasks.push(function(callback) { |
| 454 | dbRoutes.executeSQL(sql,req,res,callback); |
| 455 | }); |
| 456 | async.series(tasks, function(err,result) |
| 457 | { |
| 458 | var msgArray = new Array(); |
| 459 | if(err){ |
| 460 | msgArray.push(err); |
| 461 | dbRoutes.getVnfNetwork(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 462 | return; |
| 463 | } |
| 464 | else { |
| 465 | msgArray.push('Row successfully deleted from VNF_NETWORKS table.'); |
| 466 | dbRoutes.getVnfNetworks(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 467 | return; |
| 468 | } |
| 469 | }); |
| 470 | }); |
| 471 | |
| 472 | router.get('/deleteVnfProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 473 | |
| 474 | var privilegeObj = req.session.loggedInAdmin; |
| 475 | var tasks = []; |
| 476 | var sql = ''; |
| 477 | |
| 478 | sql = "DELETE FROM VNF_PROFILE WHERE vnf_type='" + req.query.vnf_type + "'"; |
| 479 | |
| 480 | tasks.push(function(callback) { |
| 481 | dbRoutes.executeSQL(sql,req,res,callback); |
| 482 | }); |
| 483 | async.series(tasks, function(err,result) |
| 484 | { |
| 485 | var msgArray = new Array(); |
| 486 | if(err){ |
| 487 | msgArray.push(err); |
| 488 | dbRoutes.getVnfProfile(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 489 | return; |
| 490 | } |
| 491 | else { |
| 492 | msgArray.push('Row successfully deleted from VNF_PROFILE table.'); |
| 493 | dbRoutes.getVnfProfile(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 494 | return; |
| 495 | } |
| 496 | }); |
| 497 | }); |
| 498 | |
| 499 | router.get('/deleteVmNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res) { |
| 500 | |
| 501 | var privilegeObj = req.session.loggedInAdmin; |
| 502 | var tasks = []; |
| 503 | var sql = ''; |
| 504 | |
| 505 | sql = "DELETE FROM VM_NETWORKS WHERE vnf_type='" + req.query.vnf_type |
| 506 | + "' AND vm_type='" + req.query.vm_type + "' AND network_role='" |
| 507 | + req.query.network_role + "'"; |
| 508 | |
| 509 | tasks.push(function(callback) { |
| 510 | dbRoutes.executeSQL(sql,req,res,callback); |
| 511 | }); |
| 512 | async.series(tasks, function(err,result) |
| 513 | { |
| 514 | var msgArray = new Array(); |
| 515 | if(err){ |
| 516 | msgArray.push(err); |
| 517 | dbRoutes.getVmNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 518 | return; |
| 519 | } |
| 520 | else { |
| 521 | msgArray.push('Row successfully deleted from VM_NETWORKS table.'); |
| 522 | dbRoutes.getVmNetworks(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 523 | return; |
| 524 | } |
| 525 | }); |
| 526 | }); |
| 527 | |
| 528 | |
| 529 | // POST |
| 530 | router.post('/addVmProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 531 | |
| 532 | var privilegeObj = req.session.loggedInAdmin; |
| 533 | var tasks = []; |
| 534 | var sql; |
| 535 | |
| 536 | |
| 537 | if ( req.body.nf_vm_count.length > 0 ) |
| 538 | { |
| 539 | sql = "INSERT INTO VM_PROFILE (vnf_type,vm_type,vm_count) VALUES (" |
| 540 | + "'" + req.body.nf_vnf_type + "'," |
| 541 | + "'" + req.body.nf_vm_type + "'," |
| 542 | + req.body.nf_vm_count + ")"; |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | sql = "INSERT INTO VM_PROFILE (vnf_type,vm_type) VALUES (" |
| 547 | + "'" + req.body.nf_vnf_type + "'," |
| 548 | + "'" + req.body.nf_vm_type + "')"; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | console.log("SQL: " + sql); |
| 553 | |
| 554 | tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } ); |
| 555 | async.series(tasks, function(err,result){ |
| 556 | var msgArray = new Array(); |
| 557 | if(err){ |
| 558 | msgArray.push(err); |
| 559 | dbRoutes.getVmProfile(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 560 | return; |
| 561 | } |
| 562 | else { |
| 563 | msgArray.push('Successfully added VM Profile'); |
| 564 | dbRoutes.getVmProfile(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 565 | return; |
| 566 | } |
| 567 | }); |
| 568 | }); |
| 569 | |
| 570 | |
| 571 | router.post('/addVnfNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 572 | |
| 573 | var privilegeObj = req.session.loggedInAdmin; |
| 574 | var tasks = []; |
| 575 | |
| 576 | var sql = "INSERT INTO VNF_NETWORKS (vnf_type,network_role) VALUES (" |
| 577 | + "'" + req.body.nf_vnf_type + "'," |
| 578 | + "'" + req.body.nf_network_role + "')"; |
| 579 | |
| 580 | console.log("SQL: " + sql); |
| 581 | |
| 582 | tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } ); |
| 583 | async.series(tasks, function(err,result){ |
| 584 | var msgArray = new Array(); |
| 585 | if(err){ |
| 586 | msgArray.push(err); |
| 587 | dbRoutes.getVnfNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 588 | return; |
| 589 | } |
| 590 | else { |
| 591 | msgArray.push('Successfully added VNF Network'); |
| 592 | dbRoutes.getVnfNetworks(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 593 | return; |
| 594 | } |
| 595 | }); |
| 596 | }); |
| 597 | |
| 598 | router.post('/addVnfProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 599 | |
| 600 | var privilegeObj = req.session.loggedInAdmin; |
| 601 | var tasks = []; |
| 602 | var sql; |
| 603 | |
| 604 | sql = "INSERT INTO VNF_PROFILE (vnf_type,availability_zone_count,equipment_role) VALUES (" |
| 605 | + "'" + req.body.nf_vnf_type + "'," |
| 606 | + req.body.nf_availability_zone_count |
| 607 | + ",'" + req.body.nf_equipment_role + "')"; |
| 608 | |
| 609 | console.log(sql); |
| 610 | |
| 611 | tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } ); |
| 612 | async.series(tasks, function(err,result){ |
| 613 | var msgArray = new Array(); |
| 614 | if(err){ |
| 615 | msgArray.push(err); |
| 616 | dbRoutes.getVnfProfile(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 617 | return; |
| 618 | } |
| 619 | else { |
| 620 | msgArray.push('Successfully added VNF Profile'); |
| 621 | dbRoutes.getVnfProfile(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 622 | return; |
| 623 | } |
| 624 | }); |
| 625 | }); |
| 626 | |
| 627 | router.post('/addVmNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res){ |
| 628 | |
| 629 | var privilegeObj = req.session.loggedInAdmin; |
| 630 | var tasks = []; |
| 631 | var msgArray = new Array(); |
| 632 | |
| 633 | // convert true|false to 1|0 |
| 634 | var assign_ips = (req.body.nf_assign_ips == 'true') ? 1 : 0; |
| 635 | var assign_macs = (req.body.nf_assign_macs == 'true') ? 1 : 0; |
| 636 | var assign_floating_ip = (req.body.nf_assign_floating_ip == 'true') ? 1 : 0; |
| 637 | |
| 638 | |
| 639 | if ((req.body.nf_assign_ips == 'true' && |
| 640 | (typeof req.body.nf_ip_count == 'undefined' || req.body.nf_ip_count.length <=0))) |
| 641 | { |
| 642 | msgArray.push("If assign_ips equals 'true', ip_count must be populated with a number."); |
| 643 | dbRoutes.getVmNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | if ( req.body.nf_ip_count.length >0 ) |
| 649 | { |
| 650 | var sql = "INSERT INTO VM_NETWORKS (vnf_type,vm_type,network_role,ip_count,assign_ips,assign_macs,assign_floating_ip) VALUES (" |
| 651 | + "'" + req.body.nf_vnf_type + "'," |
| 652 | + "'" + req.body.nf_vm_type + "'," |
| 653 | + "'" + req.body.nf_network_role + "'," |
| 654 | + req.body.nf_ip_count + "," |
| 655 | + assign_ips + "," |
| 656 | + assign_macs + "," |
| 657 | + assign_floating_ip + ")"; |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | var sql = "INSERT INTO VM_NETWORKS (vnf_type,vm_type,network_role,assign_ips,assign_macs,assign_floating_ip) VALUES (" |
| 662 | + "'" + req.body.nf_vnf_type + "'," |
| 663 | + "'" + req.body.nf_vm_type + "'," |
| 664 | + "'" + req.body.nf_network_role + "'," |
| 665 | + assign_ips + "," |
| 666 | + assign_macs + "," |
| 667 | + assign_floating_ip + ")"; |
| 668 | } |
| 669 | |
| 670 | tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } ); |
| 671 | async.series(tasks, function(err,result){ |
| 672 | msgArray = new Array(); |
| 673 | if(err){ |
| 674 | msgArray.push(err); |
| 675 | dbRoutes.getVmNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 676 | return; |
| 677 | } |
| 678 | else { |
| 679 | msgArray.push('Successfully added VM Network'); |
| 680 | var message = ''; |
| 681 | if (req.body.nf_ip_count.length >0) |
| 682 | { |
| 683 | message = req.body.nf_vnf_type |
| 684 | + ',' + req.body.nf_vm_type |
| 685 | + ',' + req.body.nf_network_role |
| 686 | + ',' + req.body.nf_ip_count |
| 687 | + ',' + req.body.nf_assign_ips |
| 688 | + ',' + req.body.nf_assign_macs |
| 689 | + ',' + req.body.nf_assign_floating_ip; |
| 690 | } |
| 691 | else |
| 692 | { |
| 693 | message = req.body.nf_vnf_type |
| 694 | + ',' + req.body.nf_vm_type |
| 695 | + ',' + req.body.nf_network_role |
| 696 | + ',' + req.body.nf_assign_ips |
| 697 | + ',' + req.body.nf_assign_macs |
| 698 | + ',' + req.body.nf_assign_floating_ip; |
| 699 | } |
| 700 | dbRoutes.getVmNetworks(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 701 | return; |
| 702 | } |
| 703 | }); |
| 704 | }); |
| 705 | |
| 706 | // POST |
| 707 | router.post('/uploadVnfData', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res) |
| 708 | { |
| 709 | console.log('filename:'+ JSON.stringify(req.file.originalname)); |
| 710 | var msgArray = new Array(); |
| 711 | var privilegeObj = req.session.loggedInAdmin; |
| 712 | |
| 713 | if(req.file.originalname) |
| 714 | { |
| 715 | if (req.file.originalname.size == 0) { |
| 716 | msgArray.push('There was an error uploading the file.'); |
| 717 | dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 718 | return; |
| 719 | } |
| 720 | fs.exists(req.file.path, function(exists) |
| 721 | { |
| 722 | if(exists) |
| 723 | { |
| 724 | var str = req.file.originalname; |
| 725 | var content; |
| 726 | var enc_content; |
| 727 | |
| 728 | try{ |
| 729 | content = fs.readFileSync(req.file.path); |
| 730 | enc_content = encodeURI(content); |
| 731 | |
| 732 | |
| 733 | var sql = "INSERT INTO PRE_LOAD_VNF_DATA " |
| 734 | + "(filename,preload_data) VALUES (" |
| 735 | + "'"+ str + "'," + "'" + enc_content + "')"; |
| 736 | |
| 737 | var privilegeObj = req.session.loggedInAdmin; |
| 738 | var tasks = []; |
| 739 | tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } ); |
| 740 | async.series(tasks, function(err,result) |
| 741 | { |
| 742 | if(err){ |
| 743 | msgArray.push(err); |
| 744 | dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 745 | return; |
| 746 | } |
| 747 | else { |
| 748 | msgArray.push('Successfully uploaded ' + str); |
| 749 | dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 750 | return; |
| 751 | } |
| 752 | }); |
| 753 | } |
| 754 | catch(error){ |
| 755 | fs.removeSync(req.file.path); // remove bad file that was uploaded |
| 756 | console.error("There was an error reading the file '"+str+"'. Error: " + error); |
| 757 | msgArray.push("There was an error reading the file '"+str+"'. Error: " + error); |
| 758 | dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 759 | return; |
| 760 | } |
| 761 | } else { |
| 762 | msgArray.length = 0; |
| 763 | msgArray.push('There was an error uploading the file.'); |
| 764 | dbRoutes.getVnfData(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 765 | return; |
| 766 | } |
| 767 | }); |
| 768 | } |
| 769 | else |
| 770 | { |
| 771 | msgArray.length = 0; |
| 772 | msgArray.push('There was an error uploading the file.'); |
| 773 | dbRoutes.getVnfData(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | } ); |
| 778 | |
| 779 | router.post('/uploadVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res) |
| 780 | { |
| 781 | var msgArray = new Array(); |
| 782 | var privilegeObj = req.session.loggedInAdmin; |
| 783 | |
| 784 | if(req.file.originalname) |
| 785 | { |
| 786 | if (req.file.originalname.size == 0) { |
| 787 | msgArray.push('There was an error uploading the file.'); |
| 788 | dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 789 | return; |
| 790 | } |
| 791 | fs.exists(req.file.path, function(exists) |
| 792 | { |
| 793 | if(exists) |
| 794 | { |
| 795 | var str = req.file.originalname; |
| 796 | var content; |
| 797 | var enc_content; |
| 798 | |
| 799 | try{ |
| 800 | content = fs.readFileSync(req.file.path); |
| 801 | enc_content = encodeURI(content); |
| 802 | |
| 803 | var sql = "INSERT INTO PRE_LOAD_VNF_NETWORK_DATA " |
| 804 | + "(filename,preload_data) VALUES (" |
| 805 | + "'"+ str + "'," + "'" + enc_content + "')"; |
| 806 | |
| 807 | var privilegeObj = req.session.loggedInAdmin; |
| 808 | var tasks = []; |
| 809 | tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } ); |
| 810 | async.series(tasks, function(err,result) |
| 811 | { |
| 812 | if(err){ |
| 813 | msgArray.push(err); |
| 814 | dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 815 | return; |
| 816 | } |
| 817 | else { |
| 818 | msgArray.push('Successfully uploaded ' + str); |
| 819 | dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj); |
| 820 | return; |
| 821 | } |
| 822 | }); |
| 823 | } |
| 824 | catch(error){ |
| 825 | fs.removeSync(req.file.path); // remove bad file that was uploaded |
| 826 | msgArray.push("There was an error reading the file '"+str+"'. Error: " + error); |
| 827 | dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj); |
| 828 | return; |
| 829 | } |
| 830 | } else { |
| 831 | msgArray.length = 0; |
| 832 | msgArray.push('There was an error uploading the file.'); |
| 833 | dbRoutes.getVnfNetworkData(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 834 | return; |
| 835 | } |
| 836 | }); |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | msgArray.length = 0; |
| 841 | msgArray.push('There was an error uploading the file.'); |
| 842 | dbRoutes.getVnfNetworkData(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | } ); |
| 847 | |
| 848 | |
| 849 | router.post('/uploadVmNetworks', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){ |
| 850 | |
| 851 | var msgArray = new Array(); |
| 852 | var privilegeObj = req.session.loggedInAdmin; |
| 853 | |
| 854 | if(req.file.originalname){ |
| 855 | if (req.file.originalname.size == 0) { |
| 856 | dbRoutes.getVmNetworks(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj); |
| 857 | return; |
| 858 | } |
| 859 | fs.exists(req.file.path, function(exists) { |
| 860 | |
| 861 | if(exists) { |
| 862 | |
| 863 | var str = req.file.originalname; |
| 864 | |
| 865 | try { |
| 866 | var csv = require('csv'); |
| 867 | |
| 868 | // the job of the parser is to convert a CSV file |
| 869 | // to a list of rows (array of rows) |
| 870 | var parser = csv.parse({ |
| 871 | columns: function(line) { |
| 872 | // By defining this callback, we get handed the |
| 873 | // first line of the spreadsheet. Which we'll |
| 874 | // ignore and effectively skip this line from processing |
| 875 | }, |
| 876 | skip_empty_lines: true |
| 877 | }); |
| 878 | |
| 879 | var row = 0; |
| 880 | var f = new Array(); |
| 881 | var transformer = csv.transform(function(data){ |
| 882 | // this will get row by row data, so for example, |
| 883 | //logger.debug(data[0]+','+data[1]+','+data[2]); |
| 884 | |
| 885 | // build an array of rows |
| 886 | f[row] = new Array(); |
| 887 | for ( col=0; col<data.length; col++ ) |
| 888 | { |
| 889 | f[row][col] = data[col]; |
| 890 | } |
| 891 | row++; |
| 892 | }); |
| 893 | |
| 894 | // called when done with processing the CSV |
| 895 | transformer.on("finish", function() { |
| 896 | |
| 897 | var funcArray = new Array(); |
| 898 | |
| 899 | function createFunction(lrow,res) |
| 900 | { |
| 901 | return function(callback) { dbRoutes.addVmNetwork(lrow,res,callback); } |
| 902 | } |
| 903 | // loop for each row and create an array of callbacks for async.parallelLimit |
| 904 | // had to create a function above 'createFunction' to get |
| 905 | for (var x=0; x<f.length; x++) |
| 906 | { |
| 907 | funcArray.push( createFunction(f[x],res) ); |
| 908 | } |
| 909 | |
| 910 | // make db calls in parrallel |
| 911 | async.parallelLimit(funcArray, 50, function(err,result){ |
| 912 | |
| 913 | if ( err ) { |
| 914 | dbRoutes.getVmNetworks(req,res,result,privilegeObj); |
| 915 | return; |
| 916 | } |
| 917 | else { |
| 918 | // result array has an entry in it, success entries are blank, figure out |
| 919 | // how many are not blank, aka errors. |
| 920 | var rowError = 0; |
| 921 | for(var i=0;i<result.length;i++){ |
| 922 | if ( result[i].length > 0 ) |
| 923 | { |
| 924 | rowError++; |
| 925 | } |
| 926 | } |
| 927 | var rowsProcessed = f.length - rowError; |
| 928 | result.push(rowsProcessed + ' of ' + f.length + ' rows processed.'); |
| 929 | if ( rowError > 0 ) |
| 930 | { |
| 931 | result = {code:'failure', msg:result}; |
| 932 | } |
| 933 | else |
| 934 | { |
| 935 | result = {code:'success', msg:result}; |
| 936 | } |
| 937 | dbRoutes.getVmNetworks(req,res,result,privilegeObj); |
| 938 | return; |
| 939 | } |
| 940 | }); |
| 941 | }); |
| 942 | |
| 943 | var stream = fs.createReadStream(req.file.path, "utf8"); |
| 944 | stream.pipe(parser).pipe(transformer); |
| 945 | |
| 946 | } catch(ex) { |
| 947 | msgArray.length = 0; |
| 948 | msgArray.push('There was an error uploading the file. '+ex); |
| 949 | dbRoutes.getVmNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | } else { |
| 954 | msgArray.length = 0; |
| 955 | msgArray.push('There was an error uploading the file.'); |
| 956 | dbRoutes.getVmNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 957 | return; |
| 958 | } |
| 959 | }); |
| 960 | } |
| 961 | else { |
| 962 | msgArray.length = 0; |
| 963 | msgArray.push('There was an error uploading the file.'); |
| 964 | dbRoutes.getVmNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | } ); |
| 969 | |
| 970 | router.post('/uploadVnfProfile', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){ |
| 971 | |
| 972 | var msgArray = new Array(); |
| 973 | var privilegeObj = req.session.loggedInAdmin; |
| 974 | |
| 975 | if(req.file.originalname) |
| 976 | { |
| 977 | if (req.file.originalname.size == 0) { |
| 978 | dbRoutes.getVnfProfile(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj); |
| 979 | return; |
| 980 | } |
| 981 | fs.exists(req.file.path, function(exists) { |
| 982 | |
| 983 | if(exists) { |
| 984 | |
| 985 | var str = req.file.originalname; |
| 986 | |
| 987 | try { |
| 988 | var csv = require('csv'); |
| 989 | |
| 990 | // the job of the parser is to convert a CSV file |
| 991 | // to a list of rows (array of rows) |
| 992 | var parser = csv.parse({ |
| 993 | columns: function(line) { |
| 994 | // By defining this callback, we get handed the |
| 995 | // first line of the spreadsheet. Which we'll |
| 996 | // ignore and effectively skip this line from processing |
| 997 | }, |
| 998 | skip_empty_lines: true |
| 999 | }); |
| 1000 | |
| 1001 | var row = 0; |
| 1002 | var f = new Array(); |
| 1003 | var transformer = csv.transform(function(data){ |
| 1004 | // this will get row by row data, so for example, |
| 1005 | //logger.debug(data[0]+','+data[1]+','+data[2]); |
| 1006 | |
| 1007 | // build an array of rows |
| 1008 | f[row] = new Array(); |
| 1009 | for ( col=0; col<data.length; col++ ) |
| 1010 | { |
| 1011 | f[row][col] = data[col]; |
| 1012 | } |
| 1013 | row++; |
| 1014 | }); |
| 1015 | |
| 1016 | // called when done with processing the CSV |
| 1017 | transformer.on("finish", function() { |
| 1018 | |
| 1019 | var funcArray = new Array(); |
| 1020 | |
| 1021 | function createFunction(lrow,res) |
| 1022 | { |
| 1023 | return function(callback) { dbRoutes.addVnfProfile(lrow,res,callback); } |
| 1024 | } |
| 1025 | // loop for each row and create an array of callbacks for async.parallelLimit |
| 1026 | // had to create a function above 'createFunction' to get |
| 1027 | for (var x=0; x<f.length; x++) |
| 1028 | { |
| 1029 | funcArray.push( createFunction(f[x],res) ); |
| 1030 | } |
| 1031 | |
| 1032 | // make db calls in parrallel |
| 1033 | async.series(funcArray, function(err,result){ |
| 1034 | |
| 1035 | if ( err ) { |
| 1036 | dbRoutes.getVnfProfile(req,res,result,privilegeObj); |
| 1037 | return; |
| 1038 | } |
| 1039 | else { |
| 1040 | // result array has an entry in it, success entries are blank, figure out |
| 1041 | // how many are not blank, aka errors. |
| 1042 | var rowError = 0; |
| 1043 | for(var i=0;i<result.length;i++){ |
| 1044 | if ( result[i].length > 0 ) |
| 1045 | { |
| 1046 | rowError++; |
| 1047 | } |
| 1048 | } |
| 1049 | console.log('rowError='+rowError); |
| 1050 | var rowsProcessed = f.length - rowError; |
| 1051 | console.log('rowsProcessed='+rowsProcessed); |
| 1052 | result.push(rowsProcessed + ' of ' + f.length + ' rows processed.'); |
| 1053 | if ( rowError > 0 ) |
| 1054 | { |
| 1055 | result = {code:'failure', msg:result}; |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | result = {code:'success', msg:result}; |
| 1060 | } |
| 1061 | console.log('result='+JSON.stringify(result)); |
| 1062 | dbRoutes.getVnfProfile(req,res,result,privilegeObj); |
| 1063 | return; |
| 1064 | } |
| 1065 | }); |
| 1066 | }); |
| 1067 | |
| 1068 | var stream = fs.createReadStream(req.file.path, "utf8"); |
| 1069 | stream.pipe(parser).pipe(transformer); |
| 1070 | |
| 1071 | } catch(ex) { |
| 1072 | msgArray.length = 0; |
| 1073 | msgArray.push('There was an error uploading the file. '+ex); |
| 1074 | console.error('There was an error uploading the file. '+ex); |
| 1075 | dbRoutes.getVnfProfile(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1076 | return; |
| 1077 | } |
| 1078 | } else { |
| 1079 | msgArray.length = 0; |
| 1080 | msgArray.push('There was an error uploading the file.'); |
| 1081 | dbRoutes.getVnfProfile(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1082 | return; |
| 1083 | } |
| 1084 | }); |
| 1085 | } |
| 1086 | else { |
| 1087 | msgArray.length = 0; |
| 1088 | msgArray.push('There was an error uploading the file.'); |
| 1089 | dbRoutes.getVnfProfile(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1090 | return; |
| 1091 | } |
| 1092 | } ); |
| 1093 | |
| 1094 | |
| 1095 | router.post('/uploadVnfNetworks', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){ |
| 1096 | |
| 1097 | var msgArray = new Array(); |
| 1098 | var privilegeObj = req.session.loggedInAdmin; |
| 1099 | |
| 1100 | if(req.file.originalname) |
| 1101 | { |
| 1102 | if (req.file.originalname.size == 0) { |
| 1103 | dbRoutes.getVnfProfile(req,res, |
| 1104 | {code:'failure', msg:'There was an error uploading the file, please try again.'}, |
| 1105 | privilegeObj); |
| 1106 | return; |
| 1107 | } |
| 1108 | fs.exists(req.file.path, function(exists) { |
| 1109 | |
| 1110 | if(exists) { |
| 1111 | |
| 1112 | var str = req.file.originalname; |
| 1113 | |
| 1114 | try { |
| 1115 | var csv = require('csv'); |
| 1116 | |
| 1117 | // the job of the parser is to convert a CSV file |
| 1118 | // to a list of rows (array of rows) |
| 1119 | var parser = csv.parse({ |
| 1120 | columns: function(line) { |
| 1121 | // By defining this callback, we get handed the |
| 1122 | // first line of the spreadsheet. Which we'll |
| 1123 | // ignore and effectively skip this line from processing |
| 1124 | }, |
| 1125 | skip_empty_lines: true |
| 1126 | }); |
| 1127 | |
| 1128 | var row = 0; |
| 1129 | var f = new Array(); |
| 1130 | var transformer = csv.transform(function(data){ |
| 1131 | // this will get row by row data, so for example, |
| 1132 | //logger.debug(data[0]+','+data[1]+','+data[2]); |
| 1133 | |
| 1134 | // build an array of rows |
| 1135 | f[row] = new Array(); |
| 1136 | for ( col=0; col<data.length; col++ ) |
| 1137 | { |
| 1138 | f[row][col] = data[col]; |
| 1139 | } |
| 1140 | row++; |
| 1141 | }); |
| 1142 | |
| 1143 | // called when done with processing the CSV |
| 1144 | transformer.on("finish", function() { |
| 1145 | |
| 1146 | var funcArray = new Array(); |
| 1147 | |
| 1148 | function createFunction(lrow,res) |
| 1149 | { |
| 1150 | return function(callback) { dbRoutes.addVnfNetwork(lrow,res,callback); } |
| 1151 | } |
| 1152 | // loop for each row and create an array of callbacks for async.parallelLimit |
| 1153 | // had to create a function above 'createFunction' to get |
| 1154 | for (var x=0; x<f.length; x++) |
| 1155 | { |
| 1156 | funcArray.push( createFunction(f[x],res) ); |
| 1157 | } |
| 1158 | |
| 1159 | // make db calls in parrallel |
| 1160 | async.series(funcArray, function(err,result){ |
| 1161 | |
| 1162 | if ( err ) { |
| 1163 | dbRoutes.getVnfNetworks(req,res,result,privilegeObj); |
| 1164 | return; |
| 1165 | } |
| 1166 | else { |
| 1167 | // result array has an entry in it, success entries are blank, figure out |
| 1168 | // how many are not blank, aka errors. |
| 1169 | var rowError = 0; |
| 1170 | for(var i=0;i<result.length;i++){ |
| 1171 | if ( result[i].length > 0 ) |
| 1172 | { |
| 1173 | rowError++; |
| 1174 | } |
| 1175 | } |
| 1176 | var rowsProcessed = f.length - rowError; |
| 1177 | result.push(rowsProcessed + ' of ' + f.length + ' rows processed.'); |
| 1178 | if ( rowError > 0 ) |
| 1179 | { |
| 1180 | result = {code:'failure', msg:result}; |
| 1181 | } |
| 1182 | else |
| 1183 | { |
| 1184 | result = {code:'success', msg:result}; |
| 1185 | } |
| 1186 | dbRoutes.getVnfNetworks(req,res,result,privilegeObj); |
| 1187 | return; |
| 1188 | } |
| 1189 | }); |
| 1190 | }); |
| 1191 | |
| 1192 | var stream = fs.createReadStream(req.file.path, "utf8"); |
| 1193 | stream.pipe(parser).pipe(transformer); |
| 1194 | |
| 1195 | } catch(ex) { |
| 1196 | msgArray.length = 0; |
| 1197 | msgArray.push('There was an error uploading the file. '+ex); |
| 1198 | dbRoutes.getVnfNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1199 | return; |
| 1200 | } |
| 1201 | } else { |
| 1202 | msgArray.length = 0; |
| 1203 | msgArray.push('There was an error uploading the file.'); |
| 1204 | dbRoutes.getVnfNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1205 | return; |
| 1206 | } |
| 1207 | }); |
| 1208 | } |
| 1209 | else { |
| 1210 | msgArray.length = 0; |
| 1211 | msgArray.push('There was an error uploading the file.'); |
| 1212 | dbRoutes.getVnfNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1213 | return; |
| 1214 | } |
| 1215 | } ); |
| 1216 | |
| 1217 | router.post('/uploadVmProfile', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){ |
| 1218 | |
| 1219 | var msgArray = new Array(); |
| 1220 | var privilegeObj = req.session.loggedInAdmin; |
| 1221 | |
| 1222 | if(req.file.originalname) |
| 1223 | { |
| 1224 | if (req.file.originalname.size == 0) { |
| 1225 | dbRoutes.getVmProfile(req,res, |
| 1226 | {code:'failure', msg:'There was an error uploading the file, please try again.'}, |
| 1227 | privilegeObj); |
| 1228 | return; |
| 1229 | } |
| 1230 | fs.exists(req.file.path, function(exists) { |
| 1231 | |
| 1232 | if(exists) { |
| 1233 | |
| 1234 | var str = req.file.originalname; |
| 1235 | |
| 1236 | try { |
| 1237 | var csv = require('csv'); |
| 1238 | |
| 1239 | // the job of the parser is to convert a CSV file |
| 1240 | // to a list of rows (array of rows) |
| 1241 | var parser = csv.parse({ |
| 1242 | columns: function(line) { |
| 1243 | // By defining this callback, we get handed the |
| 1244 | // first line of the spreadsheet. Which we'll |
| 1245 | // ignore and effectively skip this line from processing |
| 1246 | }, |
| 1247 | skip_empty_lines: true |
| 1248 | }); |
| 1249 | |
| 1250 | var row = 0; |
| 1251 | var f = new Array(); |
| 1252 | var transformer = csv.transform(function(data){ |
| 1253 | // this will get row by row data, so for example, |
| 1254 | //logger.debug(data[0]+','+data[1]+','+data[2]); |
| 1255 | |
| 1256 | // build an array of rows |
| 1257 | f[row] = new Array(); |
| 1258 | for ( col=0; col<data.length; col++ ) |
| 1259 | { |
| 1260 | f[row][col] = data[col]; |
| 1261 | } |
| 1262 | row++; |
| 1263 | }); |
| 1264 | |
| 1265 | // called when done with processing the CSV |
| 1266 | transformer.on("finish", function() { |
| 1267 | |
| 1268 | var funcArray = new Array(); |
| 1269 | |
| 1270 | function createFunction(lrow,res) |
| 1271 | { |
| 1272 | return function(callback) { dbRoutes.addVmProfile(lrow,res,callback); } |
| 1273 | } |
| 1274 | // loop for each row and create an array of callbacks for async.parallelLimit |
| 1275 | // had to create a function above 'createFunction' to get |
| 1276 | for (var x=0; x<f.length; x++) |
| 1277 | { |
| 1278 | funcArray.push( createFunction(f[x],res) ); |
| 1279 | } |
| 1280 | |
| 1281 | // make db calls in parrallel |
| 1282 | async.series(funcArray, function(err,result){ |
| 1283 | |
| 1284 | if ( err ) { |
| 1285 | dbRoutes.getVmProfile(req,res,result,privilegeObj); |
| 1286 | return; |
| 1287 | } |
| 1288 | else { |
| 1289 | // result array has an entry in it, success entries are blank, figure out |
| 1290 | // how many are not blank, aka errors. |
| 1291 | var rowError = 0; |
| 1292 | for(var i=0;i<result.length;i++){ |
| 1293 | if ( result[i].length > 0 ) |
| 1294 | { |
| 1295 | rowError++; |
| 1296 | } |
| 1297 | } |
| 1298 | var rowsProcessed = f.length - rowError; |
| 1299 | result.push(rowsProcessed + ' of ' + f.length + ' rows processed.'); |
| 1300 | if ( rowError > 0 ) |
| 1301 | { |
| 1302 | result = {code:'failure', msg:result}; |
| 1303 | } |
| 1304 | else |
| 1305 | { |
| 1306 | result = {code:'success', msg:result}; |
| 1307 | } |
| 1308 | dbRoutes.getVmProfile(req,res,result,privilegeObj); |
| 1309 | return; |
| 1310 | } |
| 1311 | }); |
| 1312 | }); |
| 1313 | |
| 1314 | var stream = fs.createReadStream(req.file.path, "utf8"); |
| 1315 | stream.pipe(parser).pipe(transformer); |
| 1316 | |
| 1317 | } catch(ex) { |
| 1318 | msgArray.length = 0; |
| 1319 | msgArray.push('There was an error uploading the file. '+ex); |
| 1320 | dbRoutes.getVmProfile(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1321 | return; |
| 1322 | } |
| 1323 | } else { |
| 1324 | msgArray.length = 0; |
| 1325 | msgArray.push('There was an error uploading the file.'); |
| 1326 | dbRoutes.getVmProfile(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1327 | return; |
| 1328 | } |
| 1329 | }); |
| 1330 | } |
| 1331 | else { |
| 1332 | msgArray.length = 0; |
| 1333 | msgArray.push('There was an error uploading the file.'); |
| 1334 | dbRoutes.getVmProfile(req,res,{code:'danger', msg:msgArray},privilegeObj); |
| 1335 | return; |
| 1336 | } |
| 1337 | } ); |
| 1338 | |
| 1339 | module.exports = router; |