Added new modules to help prevent Cross Site Request Forgery
[sdnc/oam.git] / admportal / server / router / routes / gamma.js
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');
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
14
15 // used for file upload button, retain original file name
16 //router.use(bodyParser());
17 router.use(bodyParser.urlencoded({
18   extended: true
19 }));
20
21 //var upload = multer({ dest: process.cwd() + '/uploads/', rename: function(fieldname,filename){ return filename; } });
22
23 // multer 1.1
24 var storage = multer.diskStorage({
25   destination: function (req, file, cb) {
26     cb(null, process.cwd() + '/uploads/')
27   },
28   filename: function (req, file, cb) {
29     cb(null, file.originalname )
30   }
31 });
32
33 var upload = multer({
34     storage: storage
35 });
36
37
38 //router.use(express.json());
39 //router.use(express.urlencoded());
40 //router.use(multer({ dest: './uploads/' }));
41
42 // 1604
43 var selectNetworkProfile = "SELECT network_type,technology FROM NETWORK_PROFILE ORDER BY network_type";
44
45 var selectNbVlanRange = "SELECT vlan_plan_id,plan_type,purpose,LPAD(range_start,4,0) range_start,LPAD(range_end,4,0) range_end,generated from VLAN_RANGES ORDER BY vlan_plan_id";
46
47 var selectNbVlanPool = "SELECT aic_site_id,availability_zone,vlan_plan_id,plan_type,purpose,LPAD(vlan_id,4,0) vlan_id,status FROM VLAN_POOL ORDER BY aic_site_id,availability_zone,vlan_plan_id,vlan_id";
48
49 router.get('/getNetworkProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
50         dbRoutes.getTable(req,res,selectNetworkProfile,'gamma/networkProfile',{code:'', msg:''}, req.session.loggedInAdmin);
51 });
52 router.get('/getNbVlanRange', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
53                 dbRoutes.getTable(req,res,selectNbVlanRange,'gamma/nbVlanRange',{code:'', msg:''}, req.session.loggedInAdmin);
54 });
55
56 // GET
57 router.get('/deleteSite', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
58
59         var privilegeObj = req.session.loggedInAdmin;
60         var tasks = [];
61         tasks.push(function(callback) {
62                 dbRoutes.deleteSite(req,res,callback);
63         });
64         async.series(tasks, function(err,result){
65         var msgArray = new Array();
66         if(err){
67             msgArray.push(err);
68             dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
69             return;
70         }
71         else {
72             msgArray.push('Row successfully deleted from AIC_SITE table.');
73             dbRoutes.getAicSite(req,res,{code:'success', msg:msgArray},privilegeObj);
74             return;
75         }
76     });
77 });
78
79 // DELETE AIC_SWITCH
80 router.get('/deleteSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
81
82     var privilegeObj = req.session.loggedInAdmin;
83     var tasks = [];
84     tasks.push(function(callback) {
85         dbRoutes.deleteSwitch(req,res,callback);
86     });
87     async.series(tasks, function(err,result){
88         var msgArray = new Array();
89         if(err){
90             msgArray.push(err);
91             dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
92             return;
93         }
94         else {
95             msgArray.push('Row successfully deleted from AIC_SWITCH table.');
96             dbRoutes.getAicSwitch(req,res,{code:'success', msg:msgArray},privilegeObj);
97             return;
98         }
99     });
100 });
101
102 // DELETE AIC_AVAIL_ZONE_POOL
103 router.get('/deleteZone', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
104
105     var privilegeObj = req.session.loggedInAdmin;
106     var tasks = [];
107     tasks.push(function(callback) {
108         dbRoutes.deleteZone(req,res,callback);
109     });
110     async.series(tasks, function(err,result){
111         var msgArray = new Array();
112         if(err){
113             msgArray.push(err);
114             dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
115             return;
116         }
117         else {
118             msgArray.push('Row successfully deleted from AIC_AVAIL_ZONE_POOL table.');
119             dbRoutes.getAicAvailZone(req,res,{code:'success', msg:msgArray},privilegeObj);
120             return;
121         }
122     });
123 });
124
125 // DELETE VLAN_ID_POOL
126 router.get('/deleteVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
127
128     var privilegeObj = req.session.loggedInAdmin;
129     var tasks = [];
130     tasks.push(function(callback) {
131         dbRoutes.deleteVlanPool(req,res,callback);
132     });
133     async.series(tasks, function(err,result){
134         var msgArray = new Array();
135         if(err){
136             msgArray.push(err);
137             dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
138             return;
139         }
140         else {
141             msgArray.push('Row successfully deleted from VLAN_ID_POOL table.');
142             dbRoutes.getVlanPool(req,res,{code:'success', msg:msgArray},privilegeObj);
143             return;
144         }
145     });
146 });
147
148 // DELETE VPE_POOL
149 router.get('/deleteVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
150
151     var privilegeObj = req.session.loggedInAdmin;
152     var tasks = [];
153     tasks.push(function(callback) {
154         dbRoutes.deleteVpePool(req,res,callback);
155     });
156     async.series(tasks, function(err,result){
157         var msgArray = new Array();
158         if(err){
159             msgArray.push(err);
160             dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
161             return;
162         }
163         else {
164             msgArray.push('Row successfully deleted from VPE_POOL table.');
165             dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
166             return;
167         }
168     });
169 });
170
171
172 // DELETE VPE_POOL
173 router.get('/deleteVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
174
175     var privilegeObj = req.session.loggedInAdmin;
176     var tasks = [];
177     tasks.push(function(callback) {
178         dbRoutes.deleteVplspePool(req,res,callback);
179     });
180     async.series(tasks, function(err,result){
181         var msgArray = new Array();
182         if(err){
183             msgArray.push(err);
184             dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
185             return;
186         }
187         else {
188             msgArray.push('Row successfully deleted from VPLSPE_POOL table.');
189             dbRoutes.getVplspePool(req,res,{code:'success', msg:msgArray},privilegeObj);
190             return;
191         }
192     });
193 });
194
195 // POST
196 router.post('/addServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res){
197
198     var privilegeObj = req.session.loggedInAdmin;
199     var tasks = [];
200     tasks.push( function(callback) { dbRoutes.addWebServiceHoming(req,res,callback); } );
201     async.series(tasks, function(err,result){
202         var msgArray = new Array();
203         if(err){
204             msgArray.push(err);
205             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
206             return;
207         }
208         else {
209             msgArray.push('Successfully added SERVICE_HOMING');
210             dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
211             return;
212         }
213     });
214 });
215
216
217 // gamma - updateProvStatus
218 router.post('/updateProvStatus', csp.checkAuth, dbRoutes.checkDB, function(req,res){
219
220
221         var privilegeObj = req.session.loggedInAdmin;
222         var tasks = [];
223     tasks.push( function(callback) { dbRoutes.updateProvStatus(req,res,callback); } );
224     async.series(tasks, function(err,result){
225                 var msgArray = new Array();
226                 if(err){
227                         msgArray.push(err);
228             dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
229                         return;
230         }
231         else {
232                         msgArray.push('Successfully updated Provisioning Status');
233             dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
234                         return;
235         }
236     });
237 });
238
239 // gamma - updateAicSite
240 router.post('/updateAicSite', csp.checkAuth, dbRoutes.checkDB, function(req,res){
241
242     var privilegeObj = req.session.loggedInAdmin;
243     var tasks = [];
244     tasks.push( function(callback) { dbRoutes.updateAicSite(req,res,callback); } );
245     async.series(tasks, function(err,result){
246         var msgArray = new Array();
247         if(err){
248             msgArray.push(err);
249             dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
250             return;
251         }
252         else {
253             msgArray.push('Successfully updated AIC_SITE table.');
254             dbRoutes.getAicSite(req,res,{code:'success', msg:msgArray},privilegeObj);
255             return;
256         }
257     });
258 });
259
260 // gamma - updateAicSwitch
261 router.post('/updateAicSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res){
262
263     var privilegeObj = req.session.loggedInAdmin;
264     var tasks = [];
265     tasks.push( function(callback) { dbRoutes.updateAicSwitch(req,res,callback); } );
266     async.series(tasks, function(err,result){
267         var msgArray = new Array();
268         if(err){
269             msgArray.push(err);
270             dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
271             return;
272         }
273         else {
274             msgArray.push('Successfully updated AIC_SWITCH table.');
275             dbRoutes.getAicSwitch(req,res,{code:'success', msg:msgArray},privilegeObj);
276             return;
277         }
278     });
279 });
280
281 // gamma - updateAicAvailZone
282 router.post('/updateAicAvailZone', csp.checkAuth, dbRoutes.checkDB, function(req,res){
283     var privilegeObj = req.session.loggedInAdmin;
284     var tasks = [];
285     tasks.push( function(callback) { dbRoutes.updateAicAvailZone(req,res,callback); } );
286     async.series(tasks, function(err,result){
287         var msgArray = new Array();
288         if(err){
289             msgArray.push(err);
290             dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
291             return;
292         }
293         else {
294             msgArray.push('Successfully updated AIC_AVAIL_ZONE_POOL table.');
295             dbRoutes.getAicAvailZone(req,res,{code:'success', msg:msgArray},privilegeObj);
296             return;
297         }
298     });
299 });
300
301 // gamma - updateVlanPool
302 router.post('/updateVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
303
304     var privilegeObj = req.session.loggedInAdmin;
305     var tasks = [];
306     tasks.push( function(callback) { dbRoutes.updateVlanPool(req,res,callback); } );
307     async.series(tasks, function(err,result){
308         var msgArray = new Array();
309         if(err){
310             msgArray.push(err);
311             dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
312             return;
313         }
314         else {
315             msgArray.push('Successfully updated VLAN_ID_POOL table.');
316             dbRoutes.getVlanPool(req,res,{code:'success', msg:msgArray},privilegeObj);
317             return;
318         }
319     });
320 });
321
322 // gamma - updateVpePool
323 router.post('/updateVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
324     var privilegeObj = req.session.loggedInAdmin;
325     var tasks = [];
326     tasks.push( function(callback) { dbRoutes.updateVpePool(req,res,callback); } );
327     async.series(tasks, function(err,result){
328         var msgArray = new Array();
329         if(err){
330             msgArray.push(err);
331             dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
332             return;
333         }
334         else {
335             msgArray.push('Successfully updated VPE_POOL table.');
336             dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
337             return;
338         }
339     });
340 });
341
342 // gamma - updateVplspePool
343 router.post('/updateVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
344
345     var privilegeObj = req.session.loggedInAdmin;
346     var tasks = [];
347     tasks.push( function(callback) { dbRoutes.updateVplspePool(req,res,callback); } );
348     async.series(tasks, function(err,result){
349         var msgArray = new Array();
350         if(err){
351             msgArray.push(err);
352             dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
353             return;
354         }
355         else {
356             msgArray.push('Successfully updated VPLSPE_POOL table.');
357             dbRoutes.getVplspePool(req,res,{code:'success', msg:msgArray},privilegeObj);
358             return;
359         }
360     });
361 });
362
363
364 // gamma - updateServiceHoming
365 router.post('/updateServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res){
366
367     var privilegeObj = req.session.loggedInAdmin;
368     var tasks = [];
369     tasks.push( function(callback) { dbRoutes.updateServiceHoming(req,res,callback); } );
370     async.series(tasks, function(err,result){
371         var msgArray = new Array();
372         if(err){
373             msgArray.push(err);
374             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
375             return;
376         }
377         else {
378             msgArray.push('Successfully updated SERVICE_HOMING table.');
379             dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
380             return;
381         }
382     });
383 });
384
385
386 router.post('/uploadVLAN', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res, next){
387
388         var msgArray = new Array();
389         var privilegeObj = req.session.loggedInAdmin;
390
391         if(req.file.originalname){
392         if (req.file.originalname.size == 0) {
393                         dbRoutes.getVlanPool(req,res,{code:'danger', msg:'There was an error uploading the file, please try again.'},privilegeObj);
394                         return;
395         }
396         fs.exists(req.file.path, function(exists) {
397             if(exists) {
398
399                                 var str = req.file.originalname;
400                                 
401                                 // check for valid filename format
402                                 var tagpos = str.search("_ctag_pool");
403                                 var csvpos = str.search(".csv");
404                                 if(tagpos != 13 || csvpos != 23){
405                                         var msgArray = new Array();
406                                         msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_ctag_pool.csv');
407                                         dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
408                                         return;
409                                 }
410                                 
411                                 try {
412                                         var csv = require('csv');
413
414                                         // the job of the parser is to convert a CSV file
415                                         // to a list of rows (array of rows)
416                                         var parser = csv.parse({
417                                         columns: function(line) {
418                                                 // By defining this callback, we get handed the
419                                                 // first line of the spreadsheet. Which we'll
420                                                 // ignore and effectively skip this line from processing
421                                         },
422                                         skip_empty_lines: true
423                                         });
424
425                                         var row = 0;
426                                         var f = new Array();
427                                         var transformer = csv.transform(function(data){
428                                                 // this will get row by row data, so for example,
429                                                 //logger.debug(data[0]+','+data[1]+','+data[2]);
430                                                 f[row] = new Array();
431                                                 for ( col=0; col<data.length; col++ )
432                                                 {
433                                                         f[row][col] = data[col];
434                                                 }
435                                                 row++;
436                                         });
437
438
439                                         // called when done with processing the CSV
440                     transformer.on("finish", function() {
441
442                         var funcArray = new Array();
443
444                         function createFunction(lrow,res)
445                         {
446                             return function(callback) { dbRoutes.addVLAN(lrow,res,callback); }
447                         }
448
449                         // loop for each row and create an array of callbacks for async.parallelLimit
450                         // had to create a function above 'createFunction' to get
451                         for (var x=0; x<f.length; x++)
452                         {
453                             funcArray.push( createFunction(f[x],res) );
454                         }
455
456                         // make db calls in parrallel
457                         //async.parallelLimit(funcArray, 5, function(err,result){
458                         async.series(funcArray, function(err,result){
459
460                             if ( err ) {
461                                 dbRoutes.getVlanPool(req,res, result,privilegeObj);
462                                 return;
463                             }
464                             else {
465                                 // result array has an entry in it, success entries are blank, figure out
466                                 // how many are not blank, aka errors.
467                                 var rowError = 0;
468                                 for(var i=0;i<result.length;i++){
469                                     if ( result[i].length > 0 )
470                                     {
471                                         rowError++;
472                                     }
473                                 }
474
475                                 var rowsProcessed = f.length - rowError;
476                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
477                                 if ( rowError > 0 )
478                                 {
479                                     result = {code:'failure', msg:result};
480                                 }
481                                 else
482                                 {
483                                     result = {code:'success', msg:result};
484                                 }
485                                 dbRoutes.getVlanPool(req,res,result,privilegeObj);
486                                 return;
487                             }
488                         });
489                     });
490
491
492                                 var stream = fs.createReadStream(req.file.path, "utf8");
493                                         stream.pipe(parser).pipe(transformer);
494
495
496                                 } catch(ex) {
497                                         console.error('error:'+ex);
498                                         msgArray = [];
499                                         msgArray.push('There was an error uploading the file. '+ex);
500                                         dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
501                                         return;
502                                 }
503
504             } else {
505                                 msgArray = [];
506                                 msgArray.push('There was an error uploading the file.');
507                                 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
508                                 return;
509             }
510         });
511         }
512         else {
513                 msgArray = [];
514                 msgArray.push('There was an error uploading the file.');
515                 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
516                 return;
517         }
518         
519 });
520
521
522 // POST
523 router.post('/uploadAicSite', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
524
525         var msgArray = new Array();
526         var privilegeObj = req.session.loggedInAdmin;
527
528         if(req.file.originalname){
529         if (req.file.originalname.size == 0) {
530                         dbRoutes.getAicSite(req,res, {code:'danger', msg:'There was an error uploading the file, please try again.'}, privilegeObj);
531                         return;
532         }
533         fs.exists(req.file.path, function(exists) {
534             if(exists) {
535
536                                 var str = req.file.originalname;
537                                 
538                                 // check for valid filename format
539                                 var tagpos = str.search("_site");
540                                 var csvpos = str.search(".csv");
541                                 if(tagpos != 13 || csvpos != 18){
542                                         msgArray.length = 0;
543                                         msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_site.csv');
544                                         dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
545                                         return;
546                                 }
547
548                                 try {
549                                         var csv = require('csv');
550
551                                         // the job of the parser is to convert a CSV file
552                                         // to a list of rows (array of rows)
553                                         var parser = csv.parse({
554                                         columns: function(line) {
555                                                 // By defining this callback, we get handed the
556                                                 // first line of the spreadsheet. Which we'll
557                                                 // ignore and effectively skip this line from processing
558                                         },
559                                         skip_empty_lines: true
560                                         });
561
562                                         var row = 0;
563                     var f = new Array();
564                     var transformer = csv.transform(function(data){
565                         // this will get row by row data, so for example,
566                         //logger.debug(data[0]+','+data[1]+','+data[2]);
567
568                                                 // build an array of rows
569                         f[row] = new Array();
570                         for ( col=0; col<data.length; col++ )
571                         {
572                             f[row][col] = data[col];
573                         }
574                         row++;
575                     });
576
577                                         // called when done with processing the CSV
578                                         transformer.on("finish", function() {
579
580                                                 var funcArray = new Array();
581
582                                                 function createFunction(lrow,res)
583                                                 {
584                                                         return function(callback) { dbRoutes.addAicSite(lrow,res,callback); }
585                                                 }
586
587                                                 // loop for each row and create an array of callbacks for async.parallelLimit
588                                                 // had to create a function above 'createFunction' to get
589                                                 for (var x=0; x<f.length; x++)
590                                                 {
591                                                         funcArray.push( createFunction(f[x],res) );
592                                                 }
593
594                                                 // make db calls in parrallel
595                                                 async.parallelLimit(funcArray, 50, function(err,result){
596
597                                                         if ( err ) {
598                                                                 dbRoutes.getAicSite(req,res, result,privilegeObj);
599                                                                 return;
600                                                         }
601                                                         else {
602                                                                 // result array has an entry in it, success entries are blank, figure out
603                                                                 // how many are not blank, aka errors.
604                                                                 var rowError = 0;
605                                                                 for(var i=0;i<result.length;i++){
606                                                                         if ( result[i].length > 0 )
607                                                                         {
608                                                                                 rowError++;
609                                                                         }
610                                                                 }
611                                                                 
612                                                                 var rowsProcessed = f.length - rowError;
613                                                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
614                                                                 if ( rowError > 0 )
615                                                                 {
616                                                                         result = {code:'failure', msg:result};
617                                                                 }
618                                                                 else
619                                                                 {
620                                                                         result = {code:'success', msg:result};
621                                                                 }
622                                                                 dbRoutes.getAicSite(req,res,result,privilegeObj);
623                                                                 return;
624                                                         }
625                                                 });
626                                         });
627
628                                 var stream = fs.createReadStream(req.file.path, "utf8");
629                                         stream.pipe(parser).pipe(transformer);
630
631
632                                 } catch(ex) {
633                                         msgArray.length = 0;
634                                         msgArray.push('There was an error uploading the file. '+ex);
635                                         dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
636                                         return;
637                                 }
638
639             } else {
640                                 msgArray.length = 0;
641                                 msgArray.push('There was an error uploading the file.');
642                                 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
643                                 return;
644             }
645         });
646         }
647         else {
648                 msgArray.length = 0;
649                 msgArray.push('There was an error uploading the file.');
650                 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
651         }
652         
653 } );
654
655 // POST
656 router.post('/uploadAicSwitch', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
657
658     var msgArray = new Array();
659         var privilegeObj = req.session.loggedInAdmin;
660
661     if(req.file.originalname){
662         if (req.file.originalname.size == 0) {
663             dbRoutes.getAicSwitch(req,res,{code:'danger', msg:'There was an error uploading the file, please try again.'},privilegeObj);
664                         return;
665         }
666         fs.exists(req.file.path, function(exists) {
667
668             if(exists) {
669
670                 var str = req.file.orignalname;
671
672                 // check for valid filename format
673                 var tagpos = str.search("_switch");
674                 var csvpos = str.search(".csv");
675                 if(tagpos != 13 || csvpos != 20){
676                     msgArray.length = 0;
677                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_switch.csv');
678                     dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
679                     return;
680                 }
681
682                 try {
683                     var csv = require('csv');
684
685                     // the job of the parser is to convert a CSV file
686                     // to a list of rows (array of rows)
687                     var parser = csv.parse({
688                         columns: function(line) {
689                             // By defining this callback, we get handed the
690                             // first line of the spreadsheet. Which we'll
691                             // ignore and effectively skip this line from processing
692                         },
693                         skip_empty_lines: true
694                     });
695
696                     var row = 0;
697                     var f = new Array();
698                     var transformer = csv.transform(function(data){
699                         // this will get row by row data, so for example,
700                         //logger.debug(data[0]+','+data[1]+','+data[2]);
701
702                         // build an array of rows
703                         f[row] = new Array();
704                         for ( col=0; col<data.length; col++ )
705                         {
706                             f[row][col] = data[col];
707                         }
708                         row++;
709                     });
710
711                     // called when done with processing the CSV
712                     transformer.on("finish", function() {
713
714                         var funcArray = new Array();
715
716                         function createFunction(lrow,res)
717                         {
718                             return function(callback) { dbRoutes.addAicSwitch(lrow,res,callback); }
719                         }
720
721                         // loop for each row and create an array of callbacks for async.parallelLimit
722                         // had to create a function above 'createFunction' to get
723                         for (var x=0; x<f.length; x++)
724                         {
725                             funcArray.push( createFunction(f[x],res) );
726                         }
727
728                         // make db calls in parrallel
729                         async.parallelLimit(funcArray, 50, function(err,result){
730
731                             if ( err ) {
732                                 dbRoutes.getAicSwitch(req,res,result,privilegeObj);
733                                                                 return;
734                             }
735                             else {
736                                 // result array has an entry in it, success entries are blank, figure out
737                                 // how many are not blank, aka errors.
738                                 var rowError = 0;
739                                 for(var i=0;i<result.length;i++){
740                                     if ( result[i].length > 0 )
741                                     {
742                                         rowError++;
743                                     }
744                                 }
745
746                                 var rowsProcessed = f.length - rowError;
747                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
748                                 if ( rowError > 0 )
749                                 {
750                                     result = {code:'failure', msg:result};
751                                 }
752                                 else
753                                 {
754                                     result = {code:'success', msg:result};
755                                 }
756                                 dbRoutes.getAicSwitch(req,res,result,privilegeObj);
757                                                                 return;
758                             }
759                         });
760                     });
761
762                     var stream = fs.createReadStream(req.file.path, "utf8");
763                     stream.pipe(parser).pipe(transformer);
764
765
766                 } catch(ex) {
767                     msgArray.length = 0;
768                     msgArray.push('There was an error uploading the file. '+ex);
769                     dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
770                                         return;
771                 }
772
773             } else {
774                 msgArray.length = 0;
775                 msgArray.push('There was an error uploading the file.');
776                 dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
777                                 return;
778             }
779         });
780     }
781     else {
782                 msgArray.length = 0;
783         msgArray.push('There was an error uploading the file.');
784         dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
785                 return;
786     }
787
788 } );
789
790 // POST
791 router.post('/uploadAicAvailZone', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
792
793     var msgArray = new Array();
794         var privilegeObj = req.session.loggedInAdmin;
795
796     if(req.file.originalname){
797         if (req.file.originalname.size == 0) {
798             dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
799                         return;
800         }
801         fs.exists(req.file.path, function(exists) {
802
803             if(exists) {
804
805                 var str = req.file.originalname;
806
807                 // check for valid filename format
808                 var tagpos = str.search("_availabilityzone");
809                 var csvpos = str.search(".csv");
810                 if(tagpos != 13 || csvpos != 30){
811                     msgArray.length = 0;
812                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_availabilityzone.csv');
813                     dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
814                     return;
815                 }
816
817                 try {
818                     var csv = require('csv');
819
820                     // the job of the parser is to convert a CSV file
821                     // to a list of rows (array of rows)
822                     var parser = csv.parse({
823                         columns: function(line) {
824                             // By defining this callback, we get handed the
825                             // first line of the spreadsheet. Which we'll
826                             // ignore and effectively skip this line from processing
827                         },
828                         skip_empty_lines: true
829                     });
830
831                     var row = 0;
832                     var f = new Array();
833                     var transformer = csv.transform(function(data){
834                         // this will get row by row data, so for example,
835                         //logger.debug(data[0]+','+data[1]+','+data[2]);
836
837                         // build an array of rows
838                         f[row] = new Array();
839                         for ( col=0; col<data.length; col++ )
840                         {
841                             f[row][col] = data[col];
842                         }
843                         row++;
844                     });
845
846                     // called when done with processing the CSV
847                     transformer.on("finish", function() {
848
849                         var funcArray = new Array();
850
851                         function createFunction(lrow,res)
852                                                 {
853                             return function(callback) { dbRoutes.addAicAvailZone(lrow,res,callback); }
854                         }
855
856                         // loop for each row and create an array of callbacks for async.parallelLimit
857                         // had to create a function above 'createFunction' to get
858                         for (var x=0; x<f.length; x++)
859                         {
860                             funcArray.push( createFunction(f[x],res) );
861                         }
862
863                         // make db calls in parrallel
864                         async.parallelLimit(funcArray, 50, function(err,result){
865
866                             if ( err ) {
867                                 dbRoutes.getAicAvailZone(req,res,result,privilegeObj);
868                                                                 return;
869                             }
870                             else {
871                                 // result array has an entry in it, success entries are blank, figure out
872                                 // how many are not blank, aka errors.
873                                 var rowError = 0;
874                                 for(var i=0;i<result.length;i++){
875                                     if ( result[i].length > 0 )
876                                     {
877                                         rowError++;
878                                     }
879                                 }
880
881                                 var rowsProcessed = f.length - rowError;
882                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
883                                 if ( rowError > 0 )
884                                 {
885                                     result = {code:'failure', msg:result};
886                                 }
887                                 else
888                                 {
889                                     result = {code:'success', msg:result};
890                                 }
891                                 dbRoutes.getAicAvailZone(req,res,result,privilegeObj);
892                                                                 return;
893                             }
894                         });
895                     });
896
897                     var stream = fs.createReadStream(req.file.path, "utf8");
898                     stream.pipe(parser).pipe(transformer);
899
900
901                 } catch(ex) {
902                     msgArray.length = 0;
903                     msgArray.push('There was an error uploading the file. '+ex);
904                     dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
905                                         return;
906                 }
907
908             } else {
909                 msgArray.length = 0;
910                 msgArray.push('There was an error uploading the file.');
911                 dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
912                                 return;
913             }
914         });
915  }
916     else {
917         msgArray.length = 0;
918         msgArray.push('There was an error uploading the file.');
919         dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
920                 return;
921     }
922
923 } );
924
925 // POST
926 router.post('/uploadVpePool', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
927
928     var msgArray = new Array();
929         var privilegeObj = req.session.loggedInAdmin;
930
931     if(req.file.originalname){
932         if (req.file.originalname.size == 0) {
933             dbRoutes.getVpePool(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
934                         return;
935         }
936         fs.exists(req.file.path, function(exists) {
937
938             if(exists) {
939
940                 var str = req.file.originalname;
941
942                 // check for valid filename format
943                 var tagpos = str.search("_vpe");
944                 var csvpos = str.search(".csv");
945                 if(tagpos != 13 || csvpos != 17){
946                     msgArray.length = 0;
947                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpe.csv');
948                                         var resultObj = {code:'failure', msg:msgArray};
949                     dbRoutes.getVpePool(req,res,resultObj,privilegeObj);
950                                         return;
951                 }
952
953                 try {
954                     var csv = require('csv');
955
956                     // the job of the parser is to convert a CSV file
957                     // to a list of rows (array of rows)
958                     var parser = csv.parse({
959                         columns: function(line) {
960                             // By defining this callback, we get handed the
961                             // first line of the spreadsheet. Which we'll
962                             // ignore and effectively skip this line from processing
963                         },
964                         skip_empty_lines: true
965                     });
966
967                     var row = 0;
968                     var f = new Array();
969                     var transformer = csv.transform(function(data){
970                         // this will get row by row data, so for example,
971                         //logger.debug(data[0]+','+data[1]+','+data[2]);
972
973                         // build an array of rows
974                         f[row] = new Array();
975                         for ( col=0; col<data.length; col++ )
976                         {
977                             f[row][col] = data[col];
978                         }
979                         row++;
980                     });
981
982                     // called when done with processing the CSV
983                     transformer.on("finish", function() {
984
985                         var funcArray = new Array();
986
987                         function createFunction(lrow,res)
988                         {
989                             return function(callback) { dbRoutes.addVpePool(lrow,res,callback); }
990                         }
991
992                         // loop for each row and create an array of callbacks for async.parallelLimit
993                                                 // had to create a function above 'createFunction' to get
994                         for (var x=0; x<f.length; x++)
995                         {
996                             funcArray.push( createFunction(f[x],res) );
997                         }
998
999                         // make db calls in parrallel
1000                         async.parallelLimit(funcArray, 50, function(err,result){
1001
1002                             if ( err ) {
1003                                 dbRoutes.getVpePool(req,res,result,privilegeObj);
1004                                                                 return;
1005                             }
1006                             else {
1007                                 // result array has an entry in it, success entries are blank, figure out
1008                                 // how many are not blank, aka errors.
1009                                 var rowError = 0;
1010                                 for(var i=0;i<result.length;i++){
1011                                     if ( result[i].length > 0 )
1012                                     {
1013                                         rowError++;
1014                                     }
1015                                 }
1016
1017                                 var rowsProcessed = f.length - rowError;
1018                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1019                                 if ( rowError > 0 )
1020                                 {
1021                                     result = {code:'failure', msg:result};
1022                                 }
1023                                 else
1024                                 {
1025                                     result = {code:'success', msg:result};
1026                                 }
1027                                 dbRoutes.getVpePool(req,res,result,privilegeObj);
1028                                                                 return;
1029                             }
1030                         });
1031                     });
1032
1033                     var stream = fs.createReadStream(req.file.path, "utf8");
1034                     stream.pipe(parser).pipe(transformer);
1035
1036
1037                 } catch(ex) {
1038                     msgArray.length = 0;
1039                     msgArray.push('There was an error uploading the file. '+ex);
1040                     dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1041                                         return;
1042                 }
1043
1044             } else {
1045                 msgArray.length = 0;
1046                 msgArray.push('There was an error uploading the file.');
1047                 dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1048                                 return;
1049             }
1050         });
1051  }
1052     else {
1053         msgArray.length = 0;
1054         msgArray.push('There was an error uploading the file.');
1055         dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1056                 return;
1057     }
1058
1059 } );
1060
1061 // POST
1062 router.post('/uploadVplspePool', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1063
1064     var msgArray = new Array();
1065         var privilegeObj = req.session.loggedInAdmin;
1066
1067     if(req.file.originalname){
1068         if (req.file.originalname.size == 0) {
1069             dbRoutes.getVplspePool(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1070                         return;
1071         }
1072         fs.exists(req.file.path, function(exists) {
1073
1074             if(exists) {
1075
1076                 var str = req.file.originalname;
1077
1078                 // check for valid filename format
1079                 var tagpos = str.search("_vpls");
1080                 var csvpos = str.search(".csv");
1081                 if(tagpos != 13 || csvpos != 18){
1082                     msgArray.length = 0;
1083                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpls.csv');
1084                     dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
1085                     return;
1086                 }
1087
1088                 try {
1089                     var csv = require('csv');
1090
1091                     // the job of the parser is to convert a CSV file
1092                     // to a list of rows (array of rows)
1093                     var parser = csv.parse({
1094                         columns: function(line) {
1095                             // By defining this callback, we get handed the
1096                             // first line of the spreadsheet. Which we'll
1097                             // ignore and effectively skip this line from processing
1098                         },
1099                         skip_empty_lines: true
1100                     });
1101
1102                     var row = 0;
1103                     var f = new Array();
1104                     var transformer = csv.transform(function(data){
1105                         // this will get row by row data, so for example,
1106                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1107
1108                         // build an array of rows
1109                         f[row] = new Array();
1110                         for ( col=0; col<data.length; col++ )
1111                         {
1112                             f[row][col] = data[col];
1113                         }
1114                         row++;
1115                     });
1116
1117                     // called when done with processing the CSV
1118                     transformer.on("finish", function() {
1119
1120                         var funcArray = new Array();
1121
1122                         function createFunction(lrow,res)
1123                         {
1124                             return function(callback) { dbRoutes.addVplspePool(lrow,res,callback); }
1125                         }
1126                                                 // loop for each row and create an array of callbacks for async.parallelLimit
1127                         // had to create a function above 'createFunction' to get
1128                         for (var x=0; x<f.length; x++)
1129                         {
1130                             funcArray.push( createFunction(f[x],res) );
1131                         }
1132
1133                         // make db calls in parrallel
1134                         async.parallelLimit(funcArray, 50, function(err,result){
1135
1136                             if ( err ) {
1137                                 dbRoutes.getVplspePool(req,res,result,privilegeObj);
1138                                                                 return;
1139                             }
1140                             else {
1141                                 // result array has an entry in it, success entries are blank, figure out
1142                                 // how many are not blank, aka errors.
1143                                 var rowError = 0;
1144                                 for(var i=0;i<result.length;i++){
1145                                     if ( result[i].length > 0 )
1146                                     {
1147                                         rowError++;
1148                                     }
1149                                 }
1150                                 var rowsProcessed = f.length - rowError;
1151                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1152                                 if ( rowError > 0 )
1153                                 {
1154                                     result = {code:'failure', msg:result};
1155                                 }
1156                                 else
1157                                 {
1158                                     result = {code:'success', msg:result};
1159                                 }
1160                                 dbRoutes.getVplspePool(req,res,result,privilegeObj);
1161                                                                 return;
1162                             }
1163                         });
1164                     });
1165
1166                     var stream = fs.createReadStream(req.file.path, "utf8");
1167                     stream.pipe(parser).pipe(transformer);
1168
1169
1170                 } catch(ex) {
1171                     msgArray.length = 0;
1172                     msgArray.push('There was an error uploading the file. '+ex);
1173                     dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1174                                         return;
1175                 }
1176
1177             } else {
1178                 msgArray.length = 0;
1179                 msgArray.push('There was an error uploading the file.');
1180                 dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1181                                 return;
1182             }
1183         });
1184  }
1185     else {
1186         msgArray.length = 0;
1187         msgArray.push('There was an error uploading the file.');
1188         dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1189                 return;
1190     }
1191
1192 } );
1193
1194 // POST
1195 router.post('/uploadServiceHoming', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res)
1196 {
1197     var msgArray = new Array();
1198     var privilegeObj = req.session.loggedInAdmin;
1199
1200     if(req.file.originalname)
1201         {
1202         if (req.file.originalname.size == 0) {
1203             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1204             return;
1205         }
1206         fs.exists(req.file.path, function(exists) 
1207                 {
1208             if(exists) 
1209                         {
1210                 var str = req.file.originalname;
1211
1212                 // check for valid filename format
1213                 var csvpos = str.search(".csv");
1214                 if( (l_.startsWith(str,'aichoming_') != true)  || csvpos != 18)
1215                                 {
1216                         msgArray.length = 0;
1217                         msgArray.push('Not a valid filename, format must be aichoming_mmddYYYY.csv');
1218                         //msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpls.csv');
1219                         dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
1220                         return;
1221                 }
1222
1223                 try 
1224                                 {
1225                     var csv = require('csv');
1226
1227                     // the job of the parser is to convert a CSV file
1228                     // to a list of rows (array of rows)
1229                     var parser = csv.parse({
1230                         columns: function(line) {
1231                             // By defining this callback, we get handed the
1232                             // first line of the spreadsheet. Which we'll
1233                             // ignore and effectively skip this line from processing
1234                         },
1235                         skip_empty_lines: true
1236                     });
1237
1238                     var row = 0;
1239                     var f = new Array();
1240                                         var csvrows = new Array();
1241                     var transformer = csv.transform(function(data){
1242                         // this will get row by row data, so for example,
1243                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1244
1245                         // build an array of rows
1246                         f[row] = new Array();
1247                         for ( col=0; col<data.length; col++ )
1248                         {
1249                             f[row][col] = data[col];
1250                         }
1251                         row++;
1252                     });
1253
1254                     // called when done with processing the CSV
1255                     transformer.on("finish", function() 
1256                                         {
1257                         var funcArray = new Array();
1258
1259                         function createFunction(lrow,res)
1260                         {
1261                             return function(callback) { dbRoutes.addServiceHoming(lrow,req,res,callback); }
1262                         }
1263                                                 funcArray.push(function(callback) {
1264                                                         dbRoutes.saveServiceHoming(req,res,callback);
1265                                                 });
1266                         // loop for each row and create an array of callbacks for async.parallelLimit
1267                         // had to create a function above 'createFunction' to get
1268                         for (var x=0; x<f.length; x++)
1269                         {
1270                                                         funcArray.push( createFunction(f[x],res) );
1271                                                 }
1272
1273                         // make db calls in series
1274                         async.series(funcArray, function(err,result)
1275                                                 {
1276                             if ( err ) 
1277                                                         {
1278                                 result = {code:'failure', msg:result};
1279                                 dbRoutes.getServiceHoming(req,res,result,privilegeObj);
1280                                 return;
1281                             }
1282                             else 
1283                                                         {       // result array has an entry in it, success entries are blank, figure out
1284                                 // how many are not blank, aka errors.
1285                                 var rowError = 0;
1286                                 for(var i=0;i<result.length;i++)
1287                                                                 {
1288                                     if ( result[i].length > 0 )
1289                                     {
1290                                         rowError++;
1291                                     }
1292                                 }
1293                                 var rowsProcessed = f.length - rowError;
1294                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1295                                 if ( rowError > 0 )
1296                                 {
1297                                     result = {code:'failure', msg:result};
1298                                 }
1299                                 else
1300                                 {
1301                                     result = {code:'success', msg:result};
1302                                 }
1303                                 dbRoutes.getServiceHoming(req,res,result,privilegeObj);
1304                                 return;
1305                             }
1306                         });
1307                     });
1308
1309                     var stream = fs.createReadStream(req.file.path, "utf8");
1310                     stream.pipe(parser).pipe(transformer);
1311
1312
1313                 } catch(ex) {
1314                     msgArray.length = 0;
1315                     msgArray.push('There was an error uploading the file. '+ex);
1316                     dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1317                     return;
1318                 }
1319
1320             } else {
1321                 msgArray.length = 0;
1322                 msgArray.push('There was an error uploading the file.');
1323                 dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1324                 return;
1325             }
1326         });
1327         }
1328     else 
1329         {
1330         msgArray.length = 0;
1331         msgArray.push('There was an error uploading the file.');
1332         dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1333         return;
1334     }
1335
1336 } );
1337 function removeNL(s) {
1338   /*
1339   ** Remove NewLine, CarriageReturn and Tab characters from a String
1340   **   s  string to be processed
1341   ** returns new string
1342   */
1343   r = "";
1344   for (i=0; i < s.length; i++)
1345   {
1346     if (s.charAt(i) != '\n' &&
1347         s.charAt(i) != '\r' &&
1348         s.charAt(i) != '\t')
1349     {
1350       r += s.charAt(i);
1351     }
1352   }
1353   return r;
1354 }
1355 function padLeft(nr, n, str){
1356     return Array(n-String(nr).length+1).join(str||'0')+nr;
1357 }
1358
1359
1360 module.exports = router;