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');
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;
25 var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'application/json'};
30 rejectUnauthorized:false,
34 // Connection to OpenDaylight
35 Odl = require('./Odl');
37 // used for file upload button, retain original file name
38 //router.use(bodyParser());
39 //router.use(bodyParser.urlencoded({
43 //var upload = multer({ dest: process.cwd() + '/uploads/', rename: function(fieldname,filename){ return filename; } });
46 var storage = multer.diskStorage({
47 destination: function (req, file, cb) {
48 cb(null, process.cwd() + '/uploads/')
50 filename: function (req, file, cb) {
51 cb(null, file.originalname )
63 router.get('/getVnfData', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
64 dbRoutes.getVnfData(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
66 router.get('/getVmNetworks', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
67 dbRoutes.getVmNetworks(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
69 router.get('/getVnfProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
70 dbRoutes.getVnfProfile(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
72 router.get('/getVnfNetworks', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
73 dbRoutes.getVnfNetworks(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
75 router.get('/getVmProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
76 dbRoutes.getVmProfile(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
79 router.get('/getVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
80 dbRoutes.getVnfNetworkData(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
83 router.get('/viewVnfNetworkData', csp.checkAuth, function(req,res)
85 var privilegeObj = req.session.loggedInAdmin;
87 var network_name = req.query.network_name;
88 var network_type = req.query.network_type;
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);
96 async.series(tasks, function(err,result)
98 var msgArray = new Array();
101 res.render('mobility/displayVnfNetworkData', {result:{code:'failure', msg:resp_msg}, header:process.env.MAIN_MENU});
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});
113 router.get('/viewVnfData', csp.checkAuth, function(req,res)
115 var privilegeObj = req.session.loggedInAdmin;
117 var vnf_name = req.query.vnf_name;
118 var vnf_type = req.query.vnf_type;
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);
126 async.series(tasks, function(err,result)
128 var msgArray = new Array();
131 res.render('mobility/displayVnfData', {result:{code:'failure', msg:resp_msg}, header:process.env.MAIN_MENU});
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});
143 router.get('/loadVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, function(req,res)
146 var privilegeObj = req.session.loggedInAdmin;
147 var msgArray = new Array();
149 if ( req.query.status != 'pending' )
151 msgArray.push("Upload Status must be in 'pending' state.");
152 dbRoutes.getVnfNetworkData(req,res, {code:'failure', msg:msgArray}, privilegeObj);
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;
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);
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){
174 var s_file = JSON.stringify(arg1);
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));
180 // add the request-information header
181 s_file = s_file.concat(',"request-information": {"request-action": "PreloadNetworkRequest"}');
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"}');
188 // add the two curly braces at the end that we stripped off
189 s_file = s_file.concat('}}');
191 Odl.Post('/restconf/operations/VNF-API:preload-network-topology-operation',
192 options,s_file,res,callback);
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);
201 // use the waterfall method of making calls
202 async.waterfall(tasks, function(err,result)
204 var msgArray = new Array();
206 msgArray.push("Error posting pre-load data to ODL: "+err);
207 dbRoutes.getVnfNetworkData(req,res, {code:'failure', msg:msgArray}, privilegeObj);
211 msgArray.push('Successfully loaded VNF pre-loaded data.');
212 dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj);
219 router.get('/loadVnfData', csp.checkAuth, dbRoutes.checkDB, function(req,res)
221 var privilegeObj = req.session.loggedInAdmin;
222 var full_path_file_name = process.cwd() + "/uploads/" + req.query.filename
223 var msgArray = new Array();
225 if ( req.query.status != 'pending' )
227 msgArray.push("Upload Status must be in 'pending' state.");
228 dbRoutes.getVnfData(req,res, {code:'failure', msg:msgArray}, privilegeObj);
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;
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);
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){
250 var s1_file = JSON.stringify(arg1);
251 var s_file = decodeURI(s1_file);
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));
258 // add the request-information header
259 s_file = s_file.concat(',"request-information": {"request-action": "PreloadVNFRequest"}');
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"}');
266 // add the two curly braces at the end that we stripped off
267 s_file = s_file.concat('}}');
269 Odl.Post('/restconf/operations/VNF-API:preload-vnf-topology-operation',
270 options,s_file,res,callback);
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);
279 // use the waterfall method of making calls
280 async.waterfall(tasks, function(err,result)
282 var msgArray = new Array();
284 msgArray.push("Error posting pre-load data to ODL: "+err);
285 dbRoutes.getVnfData(req,res, {code:'failure', msg:msgArray}, privilegeObj);
289 msgArray.push('Successfully loaded VNF pre-loaded data.');
290 dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj);
297 router.get('/deleteVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
299 var privilegeObj = req.session.loggedInAdmin;
301 var sql = 'DELETE FROM PRE_LOAD_VNF_NETWORK_DATA WHERE id=' + req.query.id;
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);
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('"}},');
317 // add the request-information header
318 inputString = inputString.concat('"request-information": {"request-action": "DeletePreloadNetworkRequest"},');
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"}}}');
325 tasks.push(function(callback) {
326 Odl.Post('/restconf/operations/VNF-API:preload-network-topology-operation',
327 options,inputString,res,callback);
329 tasks.push(function(callback) {
330 dbRoutes.executeSQL(sql,req,res,callback);
333 async.series(tasks, function(err,result){
335 var msgArray = new Array();
338 dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj);
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);
350 router.get('/deleteVnfData', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
352 var privilegeObj = req.session.loggedInAdmin;
354 var sql = 'DELETE FROM PRE_LOAD_VNF_DATA WHERE id=' + req.query.id;
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);
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('"}},');
369 // add the request-information header
370 inputString = inputString.concat('"request-information": {"request-action": "DeletePreloadVNFRequest"},');
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"},');
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"}}}');
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"}}}');
390 tasks.push(function(callback) {
391 Odl.Post('/restconf/operations/VNF-API:preload-vnf-topology-operation',
392 options,inputString,res,callback);
394 tasks.push(function(callback) {
395 dbRoutes.executeSQL(sql,req,res,callback);
398 async.series(tasks, function(err,result){
400 var msgArray = new Array();
403 dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj);
407 msgArray.push('Row successfully deleted from PRE_LOAD_VNF_DATA table and ODL.');
408 dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj);
415 router.get('/deleteVmProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
417 var privilegeObj = req.session.loggedInAdmin;
421 sql = "DELETE FROM VM_PROFILE WHERE vnf_type='" + req.query.vnf_type + "'"
422 + " AND vm_type='" + req.query.vm_type + "'";
424 tasks.push(function(callback) {
425 dbRoutes.executeSQL(sql,req,res,callback);
427 async.series(tasks, function(err,result)
429 var msgArray = new Array();
432 dbRoutes.getVmProfile(req,res,{code:'failure', msg:msgArray},privilegeObj);
436 msgArray.push('Row successfully deleted from VM_PROFILE table.');
437 dbRoutes.getVmProfile(req,res,{code:'success', msg:msgArray},privilegeObj);
444 router.get('/deleteVnfNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
446 var privilegeObj = req.session.loggedInAdmin;
450 sql = "DELETE FROM VNF_NETWORKS WHERE vnf_type='" + req.query.vnf_type + "'"
451 + " AND network_role='" + req.query.network_role + "'";
453 tasks.push(function(callback) {
454 dbRoutes.executeSQL(sql,req,res,callback);
456 async.series(tasks, function(err,result)
458 var msgArray = new Array();
461 dbRoutes.getVnfNetwork(req,res,{code:'failure', msg:msgArray},privilegeObj);
465 msgArray.push('Row successfully deleted from VNF_NETWORKS table.');
466 dbRoutes.getVnfNetworks(req,res,{code:'success', msg:msgArray},privilegeObj);
472 router.get('/deleteVnfProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
474 var privilegeObj = req.session.loggedInAdmin;
478 sql = "DELETE FROM VNF_PROFILE WHERE vnf_type='" + req.query.vnf_type + "'";
480 tasks.push(function(callback) {
481 dbRoutes.executeSQL(sql,req,res,callback);
483 async.series(tasks, function(err,result)
485 var msgArray = new Array();
488 dbRoutes.getVnfProfile(req,res,{code:'failure', msg:msgArray},privilegeObj);
492 msgArray.push('Row successfully deleted from VNF_PROFILE table.');
493 dbRoutes.getVnfProfile(req,res,{code:'success', msg:msgArray},privilegeObj);
499 router.get('/deleteVmNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
501 var privilegeObj = req.session.loggedInAdmin;
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 + "'";
509 tasks.push(function(callback) {
510 dbRoutes.executeSQL(sql,req,res,callback);
512 async.series(tasks, function(err,result)
514 var msgArray = new Array();
517 dbRoutes.getVmNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj);
521 msgArray.push('Row successfully deleted from VM_NETWORKS table.');
522 dbRoutes.getVmNetworks(req,res,{code:'success', msg:msgArray},privilegeObj);
530 router.post('/addVmProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res){
532 var privilegeObj = req.session.loggedInAdmin;
537 if ( req.body.nf_vm_count.length > 0 )
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 + ")";
546 sql = "INSERT INTO VM_PROFILE (vnf_type,vm_type) VALUES ("
547 + "'" + req.body.nf_vnf_type + "',"
548 + "'" + req.body.nf_vm_type + "')";
552 console.log("SQL: " + sql);
554 tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } );
555 async.series(tasks, function(err,result){
556 var msgArray = new Array();
559 dbRoutes.getVmProfile(req,res,{code:'failure', msg:msgArray},privilegeObj);
563 msgArray.push('Successfully added VM Profile');
564 dbRoutes.getVmProfile(req,res,{code:'success', msg:msgArray},privilegeObj);
571 router.post('/addVnfNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res){
573 var privilegeObj = req.session.loggedInAdmin;
576 var sql = "INSERT INTO VNF_NETWORKS (vnf_type,network_role) VALUES ("
577 + "'" + req.body.nf_vnf_type + "',"
578 + "'" + req.body.nf_network_role + "')";
580 console.log("SQL: " + sql);
582 tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } );
583 async.series(tasks, function(err,result){
584 var msgArray = new Array();
587 dbRoutes.getVnfNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj);
591 msgArray.push('Successfully added VNF Network');
592 dbRoutes.getVnfNetworks(req,res,{code:'success', msg:msgArray},privilegeObj);
598 router.post('/addVnfProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res){
600 var privilegeObj = req.session.loggedInAdmin;
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 + "')";
611 tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } );
612 async.series(tasks, function(err,result){
613 var msgArray = new Array();
616 dbRoutes.getVnfProfile(req,res,{code:'failure', msg:msgArray},privilegeObj);
620 msgArray.push('Successfully added VNF Profile');
621 dbRoutes.getVnfProfile(req,res,{code:'success', msg:msgArray},privilegeObj);
627 router.post('/addVmNetwork', csp.checkAuth, dbRoutes.checkDB, function(req,res){
629 var privilegeObj = req.session.loggedInAdmin;
631 var msgArray = new Array();
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;
639 if ((req.body.nf_assign_ips == 'true' &&
640 (typeof req.body.nf_ip_count == 'undefined' || req.body.nf_ip_count.length <=0)))
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);
648 if ( req.body.nf_ip_count.length >0 )
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 + ","
657 + assign_floating_ip + ")";
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 + "',"
667 + assign_floating_ip + ")";
670 tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } );
671 async.series(tasks, function(err,result){
672 msgArray = new Array();
675 dbRoutes.getVmNetworks(req,res,{code:'failure', msg:msgArray},privilegeObj);
679 msgArray.push('Successfully added VM Network');
681 if (req.body.nf_ip_count.length >0)
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;
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;
700 dbRoutes.getVmNetworks(req,res,{code:'success', msg:msgArray},privilegeObj);
707 router.post('/uploadVnfData', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res)
709 console.log('filename:'+ JSON.stringify(req.file.originalname));
710 var msgArray = new Array();
711 var privilegeObj = req.session.loggedInAdmin;
713 if(req.file.originalname)
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);
720 fs.exists(req.file.path, function(exists)
724 var str = req.file.originalname;
729 content = fs.readFileSync(req.file.path);
730 enc_content = encodeURI(content);
733 var sql = "INSERT INTO PRE_LOAD_VNF_DATA "
734 + "(filename,preload_data) VALUES ("
735 + "'"+ str + "'," + "'" + enc_content + "')";
737 var privilegeObj = req.session.loggedInAdmin;
739 tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } );
740 async.series(tasks, function(err,result)
744 dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj);
748 msgArray.push('Successfully uploaded ' + str);
749 dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj);
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);
763 msgArray.push('There was an error uploading the file.');
764 dbRoutes.getVnfData(req,res,{code:'danger', msg:msgArray},privilegeObj);
772 msgArray.push('There was an error uploading the file.');
773 dbRoutes.getVnfData(req,res,{code:'danger', msg:msgArray},privilegeObj);
779 router.post('/uploadVnfNetworkData', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res)
781 var msgArray = new Array();
782 var privilegeObj = req.session.loggedInAdmin;
784 if(req.file.originalname)
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);
791 fs.exists(req.file.path, function(exists)
795 var str = req.file.originalname;
800 content = fs.readFileSync(req.file.path);
801 enc_content = encodeURI(content);
803 var sql = "INSERT INTO PRE_LOAD_VNF_NETWORK_DATA "
804 + "(filename,preload_data) VALUES ("
805 + "'"+ str + "'," + "'" + enc_content + "')";
807 var privilegeObj = req.session.loggedInAdmin;
809 tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } );
810 async.series(tasks, function(err,result)
814 dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj);
818 msgArray.push('Successfully uploaded ' + str);
819 dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj);
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);
832 msgArray.push('There was an error uploading the file.');
833 dbRoutes.getVnfNetworkData(req,res,{code:'danger', msg:msgArray},privilegeObj);
841 msgArray.push('There was an error uploading the file.');
842 dbRoutes.getVnfNetworkData(req,res,{code:'danger', msg:msgArray},privilegeObj);
849 router.post('/uploadVmNetworks', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
851 var msgArray = new Array();
852 var privilegeObj = req.session.loggedInAdmin;
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);
859 fs.exists(req.file.path, function(exists) {
863 var str = req.file.originalname;
866 var csv = require('csv');
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
876 skip_empty_lines: true
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]);
885 // build an array of rows
886 f[row] = new Array();
887 for ( col=0; col<data.length; col++ )
889 f[row][col] = data[col];
894 // called when done with processing the CSV
895 transformer.on("finish", function() {
897 var funcArray = new Array();
899 function createFunction(lrow,res)
901 return function(callback) { dbRoutes.addVmNetwork(lrow,res,callback); }
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++)
907 funcArray.push( createFunction(f[x],res) );
910 // make db calls in parrallel
911 async.parallelLimit(funcArray, 50, function(err,result){
914 dbRoutes.getVmNetworks(req,res,result,privilegeObj);
918 // result array has an entry in it, success entries are blank, figure out
919 // how many are not blank, aka errors.
921 for(var i=0;i<result.length;i++){
922 if ( result[i].length > 0 )
927 var rowsProcessed = f.length - rowError;
928 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
931 result = {code:'failure', msg:result};
935 result = {code:'success', msg:result};
937 dbRoutes.getVmNetworks(req,res,result,privilegeObj);
943 var stream = fs.createReadStream(req.file.path, "utf8");
944 stream.pipe(parser).pipe(transformer);
948 msgArray.push('There was an error uploading the file. '+ex);
949 dbRoutes.getVmNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj);
955 msgArray.push('There was an error uploading the file.');
956 dbRoutes.getVmNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj);
963 msgArray.push('There was an error uploading the file.');
964 dbRoutes.getVmNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj);
970 router.post('/uploadVnfProfile', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
972 var msgArray = new Array();
973 var privilegeObj = req.session.loggedInAdmin;
975 if(req.file.originalname)
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);
981 fs.exists(req.file.path, function(exists) {
985 var str = req.file.originalname;
988 var csv = require('csv');
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
998 skip_empty_lines: true
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]);
1007 // build an array of rows
1008 f[row] = new Array();
1009 for ( col=0; col<data.length; col++ )
1011 f[row][col] = data[col];
1016 // called when done with processing the CSV
1017 transformer.on("finish", function() {
1019 var funcArray = new Array();
1021 function createFunction(lrow,res)
1023 return function(callback) { dbRoutes.addVnfProfile(lrow,res,callback); }
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++)
1029 funcArray.push( createFunction(f[x],res) );
1032 // make db calls in parrallel
1033 async.series(funcArray, function(err,result){
1036 dbRoutes.getVnfProfile(req,res,result,privilegeObj);
1040 // result array has an entry in it, success entries are blank, figure out
1041 // how many are not blank, aka errors.
1043 for(var i=0;i<result.length;i++){
1044 if ( result[i].length > 0 )
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.');
1055 result = {code:'failure', msg:result};
1059 result = {code:'success', msg:result};
1061 console.log('result='+JSON.stringify(result));
1062 dbRoutes.getVnfProfile(req,res,result,privilegeObj);
1068 var stream = fs.createReadStream(req.file.path, "utf8");
1069 stream.pipe(parser).pipe(transformer);
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);
1079 msgArray.length = 0;
1080 msgArray.push('There was an error uploading the file.');
1081 dbRoutes.getVnfProfile(req,res,{code:'danger', msg:msgArray},privilegeObj);
1087 msgArray.length = 0;
1088 msgArray.push('There was an error uploading the file.');
1089 dbRoutes.getVnfProfile(req,res,{code:'danger', msg:msgArray},privilegeObj);
1095 router.post('/uploadVnfNetworks', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1097 var msgArray = new Array();
1098 var privilegeObj = req.session.loggedInAdmin;
1100 if(req.file.originalname)
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.'},
1108 fs.exists(req.file.path, function(exists) {
1112 var str = req.file.originalname;
1115 var csv = require('csv');
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
1125 skip_empty_lines: true
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]);
1134 // build an array of rows
1135 f[row] = new Array();
1136 for ( col=0; col<data.length; col++ )
1138 f[row][col] = data[col];
1143 // called when done with processing the CSV
1144 transformer.on("finish", function() {
1146 var funcArray = new Array();
1148 function createFunction(lrow,res)
1150 return function(callback) { dbRoutes.addVnfNetwork(lrow,res,callback); }
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++)
1156 funcArray.push( createFunction(f[x],res) );
1159 // make db calls in parrallel
1160 async.series(funcArray, function(err,result){
1163 dbRoutes.getVnfNetworks(req,res,result,privilegeObj);
1167 // result array has an entry in it, success entries are blank, figure out
1168 // how many are not blank, aka errors.
1170 for(var i=0;i<result.length;i++){
1171 if ( result[i].length > 0 )
1176 var rowsProcessed = f.length - rowError;
1177 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1180 result = {code:'failure', msg:result};
1184 result = {code:'success', msg:result};
1186 dbRoutes.getVnfNetworks(req,res,result,privilegeObj);
1192 var stream = fs.createReadStream(req.file.path, "utf8");
1193 stream.pipe(parser).pipe(transformer);
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);
1202 msgArray.length = 0;
1203 msgArray.push('There was an error uploading the file.');
1204 dbRoutes.getVnfNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj);
1210 msgArray.length = 0;
1211 msgArray.push('There was an error uploading the file.');
1212 dbRoutes.getVnfNetworks(req,res,{code:'danger', msg:msgArray},privilegeObj);
1217 router.post('/uploadVmProfile', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1219 var msgArray = new Array();
1220 var privilegeObj = req.session.loggedInAdmin;
1222 if(req.file.originalname)
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.'},
1230 fs.exists(req.file.path, function(exists) {
1234 var str = req.file.originalname;
1237 var csv = require('csv');
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
1247 skip_empty_lines: true
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]);
1256 // build an array of rows
1257 f[row] = new Array();
1258 for ( col=0; col<data.length; col++ )
1260 f[row][col] = data[col];
1265 // called when done with processing the CSV
1266 transformer.on("finish", function() {
1268 var funcArray = new Array();
1270 function createFunction(lrow,res)
1272 return function(callback) { dbRoutes.addVmProfile(lrow,res,callback); }
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++)
1278 funcArray.push( createFunction(f[x],res) );
1281 // make db calls in parrallel
1282 async.series(funcArray, function(err,result){
1285 dbRoutes.getVmProfile(req,res,result,privilegeObj);
1289 // result array has an entry in it, success entries are blank, figure out
1290 // how many are not blank, aka errors.
1292 for(var i=0;i<result.length;i++){
1293 if ( result[i].length > 0 )
1298 var rowsProcessed = f.length - rowError;
1299 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1302 result = {code:'failure', msg:result};
1306 result = {code:'success', msg:result};
1308 dbRoutes.getVmProfile(req,res,result,privilegeObj);
1314 var stream = fs.createReadStream(req.file.path, "utf8");
1315 stream.pipe(parser).pipe(transformer);
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);
1324 msgArray.length = 0;
1325 msgArray.push('There was an error uploading the file.');
1326 dbRoutes.getVmProfile(req,res,{code:'danger', msg:msgArray},privilegeObj);
1332 msgArray.length = 0;
1333 msgArray.push('There was an error uploading the file.');
1334 dbRoutes.getVmProfile(req,res,{code:'danger', msg:msgArray},privilegeObj);
1339 module.exports = router;