70e6713c58e6d5350e7be32797b24f02be3ddcdb
[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 router.get('/getNbVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
57         if (typeof req.query.vlan_plan_id == "undefined"){ 
58                 dbRoutes.getTable(req,res,selectNbVlanPool,'gamma/nbVlanPool',{code:'', msg:''}, req.session.loggedInAdmin);
59         }else{
60                 var sql = "SELECT aic_site_id,availability_zone,vlan_plan_id,plan_type,purpose,vlan_id,status FROM VLAN_POOL WHERE vlan_plan_id='" + req.query.vlan_plan_id + "' AND vlan_id BETWEEN "
61                         + req.query.range_start + " AND " + req.query.range_end;
62                 dbRoutes.getTable(req,res,sql,'gamma/nbVlanPool',{code:'', msg:''}, req.session.loggedInAdmin);
63         }
64 });
65
66 router.post('/addNetworkProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res){
67
68  var network_type = removeNL(req.body.nf_network_type);
69  var technology = removeNL(req.body.nf_technology);
70  var sql = "INSERT INTO NETWORK_PROFILE (network_type,technology) VALUES ("
71     + "'"+ network_type + "',"
72     + "'"+ technology + "')";
73
74     var privilegeObj = req.session.loggedInAdmin;
75     var tasks = [];
76     tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } );
77     async.series(tasks, function(err,result){
78         var msgArray = new Array();
79         if(err)
80         {
81             msgArray.push(err);
82             dbRoutes.getTable(req,res,ucpePhsCredentials, 'gamma/networkProfile', {code:'failure', msg:msgArray},privilegeObj);
83             return;
84         }
85         else
86         {
87             if ( result == 1 )
88             {
89                 msgArray.push('Successfully added Network Profile.');
90                 dbRoutes.getTable(req,res,selectNetworkProfile, 'gamma/networkProfile', {code:'success', msg:msgArray},privilegeObj);
91                 return;
92             }
93             else
94             {
95                 msgArray.push('Was not able to add Network Profile.');
96                 dbRoutes.getTable(req,res,ucpePhsCredentials, 'gamma/networkProfile', {code:'failure', msg:msgArray},privilegeObj);
97                 return;
98             }
99         }
100     });
101 });
102
103 router.post('/saveNbVlanRange', csp.checkAuth, dbRoutes.checkDB, function(req,res){
104
105         var plan_type = req.body.nf_plan_type;
106         var purpose = req.body.nf_purpose;
107         var range_start = padLeft(removeNL(req.body.nf_range_start),4);
108         var range_end = padLeft(removeNL(req.body.nf_range_end),4);
109         var tasks = [];
110         var privilegeObj = req.session.loggedInAdmin;
111
112         tasks.push( function(callback) { 
113                 dbRoutes.saveNbVlanRange(range_start,range_end,plan_type,purpose,req,res,callback); 
114         });
115
116         // will probably need to be a new call that is a transaction if i use a new
117         // plan_type-purpose-counter table.
118     //tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } );
119     async.series(tasks, function(err,result){
120         var msgArray = new Array();
121         if(err)
122         {
123             msgArray.push(err);
124             dbRoutes.getTable(req,res,selectNbVlanRange, 'gamma/nbVlanRange', {code:'failure', msg:msgArray},privilegeObj);
125             return;
126         }
127         else
128         {
129             msgArray.push('Successfully added VLAN Range.');
130             dbRoutes.getTable(req,res,selectNbVlanRange, 'gamma/nbVlanRange', {code:'success', msg:msgArray},privilegeObj);
131             return;
132         }
133     });
134 });
135
136 router.get('/deleteNetworkProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
137
138     var privilegeObj = req.session.loggedInAdmin;
139     var tasks = [];
140     tasks.push(function(callback){
141         dbRoutes.executeSQL("DELETE FROM NETWORK_PROFILE WHERE network_type = '" + req.query.network_type + "'", req,res,callback);
142
143     });
144     async.series(tasks, function(err,result)
145     {
146         var msgArray = new Array();
147         if(err){
148             msgArray.push("Error: " + err);
149             dbRoutes.getTable(req,res,selectNetworkProfile, 'gamma/networkProfile', {code:'failure', msg:msgArray},privilegeObj);
150             return;
151         }
152         else
153         {
154             if ( result[0] == 1 )
155             {
156                 msgArray.push('Successfully deleted Network Profile.');
157                 dbRoutes.getTable(req,res,selectNetworkProfile, 'gamma/networkProfile', {code:'success', msg:msgArray},privilegeObj);
158                 return;
159             }
160             else
161             {
162                 msgArray.push('No rows removed.');
163                 dbRoutes.getTable(req,res,selectNetworkProfile, 'gamma/networkProfile', {code:'failure', msg:msgArray},privilegeObj);
164                 return;
165             }
166         }
167     });
168 });
169
170 router.get('/deleteNbVlanRange', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
171
172     var privilegeObj = req.session.loggedInAdmin;
173     var tasks = [];
174
175     tasks.push(function(callback){
176         dbRoutes.deleteNbVlanRange(req.query.vlan_plan_id,req,res,callback);
177     });
178     async.series(tasks, function(err,result)
179     {
180         var msgArray = new Array();
181         if(err){
182             msgArray.push(err);
183             dbRoutes.getTable(req,res,selectNbVlanRange, 'gamma/nbVlanRange', {code:'failure', msg:msgArray},privilegeObj);
184             return;
185         }
186         else
187         {
188             msgArray.push('Successfully deleted Range.');
189             dbRoutes.getTable(req,res,selectNbVlanRange, 'gamma/nbVlanRange', {code:'success', msg:msgArray},privilegeObj);
190             return;
191         }
192     });
193 });
194
195 router.post('/updateNetworkProfile', csp.checkAuth, dbRoutes.checkDB, function(req,res){
196
197     var sql = "UPDATE NETWORK_PROFILE SET "
198             + "network_type='"+ removeNL(req.body.uf_network_type) + "', "
199             + "technology='" + removeNL(req.body.uf_technology) + "' "
200             + "WHERE network_type='" + removeNL(req.body.uf_key_network_type) + "'";
201
202
203     var privilegeObj = req.session.loggedInAdmin;
204     var tasks = [];
205     tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } );
206     async.series(tasks, function(err,result){
207         var msgArray = new Array();
208         if(err){
209             msgArray.push(err);
210             dbRoutes.getTable(req,res,selectNetworkProfile, 'gamma/networkProfile', {code:'failure', msg:msgArray},privilegeObj);
211             return;
212         }
213         else {
214             msgArray.push('Successfully updated Network Profile.');
215             dbRoutes.getTable(req,res,selectNetworkProfile, 'gamma/networkProfile', {code:'success', msg:msgArray},privilegeObj);
216             return;
217         }
218     });
219 });
220
221 router.post('/updateNbVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
222
223     var sql = "UPDATE VLAN_POOL SET "
224             + "status='"+ removeNL(req.body.uf_status) + "' "
225             + " WHERE aic_site_id='" + removeNL(req.body.uf_key_aic_site_id) + "'"
226             + " AND availability_zone='" + removeNL(req.body.uf_key_availability_zone) + "'"
227             + " AND vlan_plan_id='" + removeNL(req.body.uf_key_vlan_plan_id) + "'"
228             + " AND plan_type='" + removeNL(req.body.uf_key_plan_type) + "'"
229             + " AND purpose='" + removeNL(req.body.uf_key_purpose) + "'"
230             + " AND vlan_id=" + removeNL(req.body.uf_key_vlan_id); 
231
232
233     var privilegeObj = req.session.loggedInAdmin;
234     var tasks = [];
235     tasks.push( function(callback) { dbRoutes.executeSQL(sql,req,res,callback); } );
236     async.series(tasks, function(err,result){
237         var msgArray = new Array();
238         if(err){
239             msgArray.push(err);
240             dbRoutes.getTable(req,res,selectNbVlanPool, 'gamma/nbVlanPool', {code:'failure', msg:msgArray},privilegeObj);
241             return;
242         }
243         else {
244             msgArray.push('Successfully updated Network Profile.');
245             dbRoutes.getTable(req,res,selectNbVlanPool, 'gamma/nbVlanPool', {code:'success', msg:msgArray},privilegeObj);
246             return;
247         }
248     });
249 });
250
251 router.post('/updateNbVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
252 });
253 router.get('/generateNbVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
254
255     var vlan_plan_id = req.query.vlan_plan_id;
256     var plan_type = req.query.plan_type;
257     var purpose = req.query.purpose;
258     var range_start = req.query.range_start;
259     var range_end = req.query.range_end;
260     var tasks = [];
261     var privilegeObj = req.session.loggedInAdmin;
262
263     tasks.push( function(callback) {
264         dbRoutes.generateNbVlanPool(range_start,range_end,plan_type,purpose,vlan_plan_id,req,res,callback);
265     });
266
267     // will probably need to be a new call that is a transaction if i use a new
268     // plan_type-purpose-counter table.
269     //tasks.push( function(callback) { dbRoutes.addRow(sql,req,res,callback); } );
270     async.series(tasks, function(err,result){
271         var msgArray = new Array();
272         if(err)
273         {
274             msgArray.push(err);
275             dbRoutes.getTable(req,res,selectNbVlanRange, 'gamma/nbVlanRange', {code:'failure', msg:msgArray},privilegeObj);
276             return;
277         }
278         else
279         {
280             msgArray.push('Successfully added VLAN Range.');
281             dbRoutes.getTable(req,res,selectNbVlanRange, 'gamma/nbVlanRange', {code:'success', msg:msgArray},privilegeObj);
282             return;
283         }
284     });
285 });
286
287 ///// end 1604
288
289
290 // GET
291 router.get('/getServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
292         dbRoutes.getServiceHoming(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
293 });
294 router.get('/getServiceHomingRollback', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
295         dbRoutes.getServiceHomingRollback(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
296 });
297 router.get('/getVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
298         dbRoutes.getVlanPool(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
299 });
300 router.get('/getAicSite', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
301         dbRoutes.getAicSite(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
302 });
303 router.get('/getAicSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
304         dbRoutes.getAicSwitch(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
305 });
306 router.get('/getAicAvailZone', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
307         dbRoutes.getAicAvailZone(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
308 });
309 router.get('/getVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
310         dbRoutes.getVpePool(req,res,{code:'', msg:''}, req.session.loggedInAdmin);
311 });
312 router.get('/getVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
313         dbRoutes.getVplspePool(req,res, {code:'', msg:''}, req.session.loggedInAdmin);
314 });
315
316 // ROLLBACK SERVICE_HOMING
317 router.get('/rollbackServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
318
319     var privilegeObj = req.session.loggedInAdmin;
320     var tasks = [];
321     tasks.push(function(callback) {
322         dbRoutes.rollbackServiceHoming(req,res,callback);
323     });
324     async.series(tasks, function(err,result){
325         var msgArray = new Array();
326         if(err){
327             msgArray.push(err);
328             dbRoutes.getServiceHomingRollback(req,res,{code:'failure', msg:msgArray},privilegeObj);
329             return;
330         }
331         else {
332             msgArray.push('SERVICE_HOMING table successfully restored.');
333             dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
334             return;
335         }
336     });
337 });
338
339 // DELETE SERVICE_HOMING
340 router.get('/deleteServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
341
342     var privilegeObj = req.session.loggedInAdmin;
343     var tasks = [];
344     tasks.push(function(callback) {
345         dbRoutes.deleteServiceHoming(req,res,callback);
346     });
347     async.series(tasks, function(err,result){
348         var msgArray = new Array();
349         if(err){
350             msgArray.push(err);
351             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
352             return;
353         }
354         else {
355             msgArray.push('Row successfully deleted from SERVICE_HOMING table.');
356             dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
357             return;
358         }
359     });
360 });
361
362
363 // DELETE AIC_SITE
364 router.get('/deleteSite', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
365
366         var privilegeObj = req.session.loggedInAdmin;
367         var tasks = [];
368         tasks.push(function(callback) {
369                 dbRoutes.deleteSite(req,res,callback);
370         });
371         async.series(tasks, function(err,result){
372         var msgArray = new Array();
373         if(err){
374             msgArray.push(err);
375             dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
376             return;
377         }
378         else {
379             msgArray.push('Row successfully deleted from AIC_SITE table.');
380             dbRoutes.getAicSite(req,res,{code:'success', msg:msgArray},privilegeObj);
381             return;
382         }
383     });
384 });
385
386 // DELETE AIC_SWITCH
387 router.get('/deleteSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
388
389     var privilegeObj = req.session.loggedInAdmin;
390     var tasks = [];
391     tasks.push(function(callback) {
392         dbRoutes.deleteSwitch(req,res,callback);
393     });
394     async.series(tasks, function(err,result){
395         var msgArray = new Array();
396         if(err){
397             msgArray.push(err);
398             dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
399             return;
400         }
401         else {
402             msgArray.push('Row successfully deleted from AIC_SWITCH table.');
403             dbRoutes.getAicSwitch(req,res,{code:'success', msg:msgArray},privilegeObj);
404             return;
405         }
406     });
407 });
408
409 // DELETE AIC_AVAIL_ZONE_POOL
410 router.get('/deleteZone', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
411
412     var privilegeObj = req.session.loggedInAdmin;
413     var tasks = [];
414     tasks.push(function(callback) {
415         dbRoutes.deleteZone(req,res,callback);
416     });
417     async.series(tasks, function(err,result){
418         var msgArray = new Array();
419         if(err){
420             msgArray.push(err);
421             dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
422             return;
423         }
424         else {
425             msgArray.push('Row successfully deleted from AIC_AVAIL_ZONE_POOL table.');
426             dbRoutes.getAicAvailZone(req,res,{code:'success', msg:msgArray},privilegeObj);
427             return;
428         }
429     });
430 });
431
432 // DELETE VLAN_ID_POOL
433 router.get('/deleteVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
434
435     var privilegeObj = req.session.loggedInAdmin;
436     var tasks = [];
437     tasks.push(function(callback) {
438         dbRoutes.deleteVlanPool(req,res,callback);
439     });
440     async.series(tasks, function(err,result){
441         var msgArray = new Array();
442         if(err){
443             msgArray.push(err);
444             dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
445             return;
446         }
447         else {
448             msgArray.push('Row successfully deleted from VLAN_ID_POOL table.');
449             dbRoutes.getVlanPool(req,res,{code:'success', msg:msgArray},privilegeObj);
450             return;
451         }
452     });
453 });
454
455 // DELETE VPE_POOL
456 router.get('/deleteVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
457
458     var privilegeObj = req.session.loggedInAdmin;
459     var tasks = [];
460     tasks.push(function(callback) {
461         dbRoutes.deleteVpePool(req,res,callback);
462     });
463     async.series(tasks, function(err,result){
464         var msgArray = new Array();
465         if(err){
466             msgArray.push(err);
467             dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
468             return;
469         }
470         else {
471             msgArray.push('Row successfully deleted from VPE_POOL table.');
472             dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
473             return;
474         }
475     });
476 });
477
478
479 // DELETE VPE_POOL
480 router.get('/deleteVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res) {
481
482     var privilegeObj = req.session.loggedInAdmin;
483     var tasks = [];
484     tasks.push(function(callback) {
485         dbRoutes.deleteVplspePool(req,res,callback);
486     });
487     async.series(tasks, function(err,result){
488         var msgArray = new Array();
489         if(err){
490             msgArray.push(err);
491             dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
492             return;
493         }
494         else {
495             msgArray.push('Row successfully deleted from VPLSPE_POOL table.');
496             dbRoutes.getVplspePool(req,res,{code:'success', msg:msgArray},privilegeObj);
497             return;
498         }
499     });
500 });
501
502 // POST
503 router.post('/addServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res){
504
505     var privilegeObj = req.session.loggedInAdmin;
506     var tasks = [];
507     tasks.push( function(callback) { dbRoutes.addWebServiceHoming(req,res,callback); } );
508     async.series(tasks, function(err,result){
509         var msgArray = new Array();
510         if(err){
511             msgArray.push(err);
512             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
513             return;
514         }
515         else {
516             msgArray.push('Successfully added SERVICE_HOMING');
517             dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
518             return;
519         }
520     });
521 });
522
523
524 // gamma - updateProvStatus
525 router.post('/updateProvStatus', csp.checkAuth, dbRoutes.checkDB, function(req,res){
526
527
528         var privilegeObj = req.session.loggedInAdmin;
529         var tasks = [];
530     tasks.push( function(callback) { dbRoutes.updateProvStatus(req,res,callback); } );
531     async.series(tasks, function(err,result){
532                 var msgArray = new Array();
533                 if(err){
534                         msgArray.push(err);
535             dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
536                         return;
537         }
538         else {
539                         msgArray.push('Successfully updated Provisioning Status');
540             dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
541                         return;
542         }
543     });
544 });
545
546 // gamma - updateAicSite
547 router.post('/updateAicSite', csp.checkAuth, dbRoutes.checkDB, function(req,res){
548
549     var privilegeObj = req.session.loggedInAdmin;
550     var tasks = [];
551     tasks.push( function(callback) { dbRoutes.updateAicSite(req,res,callback); } );
552     async.series(tasks, function(err,result){
553         var msgArray = new Array();
554         if(err){
555             msgArray.push(err);
556             dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
557             return;
558         }
559         else {
560             msgArray.push('Successfully updated AIC_SITE table.');
561             dbRoutes.getAicSite(req,res,{code:'success', msg:msgArray},privilegeObj);
562             return;
563         }
564     });
565 });
566
567 // gamma - updateAicSwitch
568 router.post('/updateAicSwitch', csp.checkAuth, dbRoutes.checkDB, function(req,res){
569
570     var privilegeObj = req.session.loggedInAdmin;
571     var tasks = [];
572     tasks.push( function(callback) { dbRoutes.updateAicSwitch(req,res,callback); } );
573     async.series(tasks, function(err,result){
574         var msgArray = new Array();
575         if(err){
576             msgArray.push(err);
577             dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
578             return;
579         }
580         else {
581             msgArray.push('Successfully updated AIC_SWITCH table.');
582             dbRoutes.getAicSwitch(req,res,{code:'success', msg:msgArray},privilegeObj);
583             return;
584         }
585     });
586 });
587
588 // gamma - updateAicAvailZone
589 router.post('/updateAicAvailZone', csp.checkAuth, dbRoutes.checkDB, function(req,res){
590     var privilegeObj = req.session.loggedInAdmin;
591     var tasks = [];
592     tasks.push( function(callback) { dbRoutes.updateAicAvailZone(req,res,callback); } );
593     async.series(tasks, function(err,result){
594         var msgArray = new Array();
595         if(err){
596             msgArray.push(err);
597             dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
598             return;
599         }
600         else {
601             msgArray.push('Successfully updated AIC_AVAIL_ZONE_POOL table.');
602             dbRoutes.getAicAvailZone(req,res,{code:'success', msg:msgArray},privilegeObj);
603             return;
604         }
605     });
606 });
607
608 // gamma - updateVlanPool
609 router.post('/updateVlanPool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
610
611     var privilegeObj = req.session.loggedInAdmin;
612     var tasks = [];
613     tasks.push( function(callback) { dbRoutes.updateVlanPool(req,res,callback); } );
614     async.series(tasks, function(err,result){
615         var msgArray = new Array();
616         if(err){
617             msgArray.push(err);
618             dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
619             return;
620         }
621         else {
622             msgArray.push('Successfully updated VLAN_ID_POOL table.');
623             dbRoutes.getVlanPool(req,res,{code:'success', msg:msgArray},privilegeObj);
624             return;
625         }
626     });
627 });
628
629 // gamma - updateVpePool
630 router.post('/updateVpePool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
631     var privilegeObj = req.session.loggedInAdmin;
632     var tasks = [];
633     tasks.push( function(callback) { dbRoutes.updateVpePool(req,res,callback); } );
634     async.series(tasks, function(err,result){
635         var msgArray = new Array();
636         if(err){
637             msgArray.push(err);
638             dbRoutes.getVpePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
639             return;
640         }
641         else {
642             msgArray.push('Successfully updated VPE_POOL table.');
643             dbRoutes.getVpePool(req,res,{code:'success', msg:msgArray},privilegeObj);
644             return;
645         }
646     });
647 });
648
649 // gamma - updateVplspePool
650 router.post('/updateVplspePool', csp.checkAuth, dbRoutes.checkDB, function(req,res){
651
652     var privilegeObj = req.session.loggedInAdmin;
653     var tasks = [];
654     tasks.push( function(callback) { dbRoutes.updateVplspePool(req,res,callback); } );
655     async.series(tasks, function(err,result){
656         var msgArray = new Array();
657         if(err){
658             msgArray.push(err);
659             dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
660             return;
661         }
662         else {
663             msgArray.push('Successfully updated VPLSPE_POOL table.');
664             dbRoutes.getVplspePool(req,res,{code:'success', msg:msgArray},privilegeObj);
665             return;
666         }
667     });
668 });
669
670
671 // gamma - updateServiceHoming
672 router.post('/updateServiceHoming', csp.checkAuth, dbRoutes.checkDB, function(req,res){
673
674     var privilegeObj = req.session.loggedInAdmin;
675     var tasks = [];
676     tasks.push( function(callback) { dbRoutes.updateServiceHoming(req,res,callback); } );
677     async.series(tasks, function(err,result){
678         var msgArray = new Array();
679         if(err){
680             msgArray.push(err);
681             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
682             return;
683         }
684         else {
685             msgArray.push('Successfully updated SERVICE_HOMING table.');
686             dbRoutes.getServiceHoming(req,res,{code:'success', msg:msgArray},privilegeObj);
687             return;
688         }
689     });
690 });
691
692
693 router.post('/uploadVLAN', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res, next){
694
695         var msgArray = new Array();
696         var privilegeObj = req.session.loggedInAdmin;
697
698         if(req.file.originalname){
699         if (req.file.originalname.size == 0) {
700                         dbRoutes.getVlanPool(req,res,{code:'danger', msg:'There was an error uploading the file, please try again.'},privilegeObj);
701                         return;
702         }
703         fs.exists(req.file.path, function(exists) {
704             if(exists) {
705
706                                 var str = req.file.originalname;
707                                 
708                                 // check for valid filename format
709                                 var tagpos = str.search("_ctag_pool");
710                                 var csvpos = str.search(".csv");
711                                 if(tagpos != 13 || csvpos != 23){
712                                         var msgArray = new Array();
713                                         msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_ctag_pool.csv');
714                                         dbRoutes.getVlanPool(req,res,{code:'failure', msg:msgArray},privilegeObj);
715                                         return;
716                                 }
717                                 
718                                 try {
719                                         var csv = require('csv');
720
721                                         // the job of the parser is to convert a CSV file
722                                         // to a list of rows (array of rows)
723                                         var parser = csv.parse({
724                                         columns: function(line) {
725                                                 // By defining this callback, we get handed the
726                                                 // first line of the spreadsheet. Which we'll
727                                                 // ignore and effectively skip this line from processing
728                                         },
729                                         skip_empty_lines: true
730                                         });
731
732                                         var row = 0;
733                                         var f = new Array();
734                                         var transformer = csv.transform(function(data){
735                                                 // this will get row by row data, so for example,
736                                                 //logger.debug(data[0]+','+data[1]+','+data[2]);
737                                                 f[row] = new Array();
738                                                 for ( col=0; col<data.length; col++ )
739                                                 {
740                                                         f[row][col] = data[col];
741                                                 }
742                                                 row++;
743                                         });
744
745
746                                         // called when done with processing the CSV
747                     transformer.on("finish", function() {
748
749                         var funcArray = new Array();
750
751                         function createFunction(lrow,res)
752                         {
753                             return function(callback) { dbRoutes.addVLAN(lrow,res,callback); }
754                         }
755
756                         // loop for each row and create an array of callbacks for async.parallelLimit
757                         // had to create a function above 'createFunction' to get
758                         for (var x=0; x<f.length; x++)
759                         {
760                             funcArray.push( createFunction(f[x],res) );
761                         }
762
763                         // make db calls in parrallel
764                         //async.parallelLimit(funcArray, 5, function(err,result){
765                         async.series(funcArray, function(err,result){
766
767                             if ( err ) {
768                                 dbRoutes.getVlanPool(req,res, result,privilegeObj);
769                                 return;
770                             }
771                             else {
772                                 // result array has an entry in it, success entries are blank, figure out
773                                 // how many are not blank, aka errors.
774                                 var rowError = 0;
775                                 for(var i=0;i<result.length;i++){
776                                     if ( result[i].length > 0 )
777                                     {
778                                         rowError++;
779                                     }
780                                 }
781
782                                 var rowsProcessed = f.length - rowError;
783                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
784                                 if ( rowError > 0 )
785                                 {
786                                     result = {code:'failure', msg:result};
787                                 }
788                                 else
789                                 {
790                                     result = {code:'success', msg:result};
791                                 }
792                                 dbRoutes.getVlanPool(req,res,result,privilegeObj);
793                                 return;
794                             }
795                         });
796                     });
797
798
799                                 var stream = fs.createReadStream(req.file.path, "utf8");
800                                         stream.pipe(parser).pipe(transformer);
801
802
803                                 } catch(ex) {
804                                         console.error('error:'+ex);
805                                         msgArray = [];
806                                         msgArray.push('There was an error uploading the file. '+ex);
807                                         dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
808                                         return;
809                                 }
810
811             } else {
812                                 msgArray = [];
813                                 msgArray.push('There was an error uploading the file.');
814                                 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
815                                 return;
816             }
817         });
818         }
819         else {
820                 msgArray = [];
821                 msgArray.push('There was an error uploading the file.');
822                 dbRoutes.getVlanPool(req,res, {code:'danger', msg:msgArray}, privilegeObj);
823                 return;
824         }
825         
826 });
827
828
829 // POST
830 router.post('/uploadAicSite', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
831
832         var msgArray = new Array();
833         var privilegeObj = req.session.loggedInAdmin;
834
835         if(req.file.originalname){
836         if (req.file.originalname.size == 0) {
837                         dbRoutes.getAicSite(req,res, {code:'danger', msg:'There was an error uploading the file, please try again.'}, privilegeObj);
838                         return;
839         }
840         fs.exists(req.file.path, function(exists) {
841             if(exists) {
842
843                                 var str = req.file.originalname;
844                                 
845                                 // check for valid filename format
846                                 var tagpos = str.search("_site");
847                                 var csvpos = str.search(".csv");
848                                 if(tagpos != 13 || csvpos != 18){
849                                         msgArray.length = 0;
850                                         msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_site.csv');
851                                         dbRoutes.getAicSite(req,res,{code:'failure', msg:msgArray},privilegeObj);
852                                         return;
853                                 }
854
855                                 try {
856                                         var csv = require('csv');
857
858                                         // the job of the parser is to convert a CSV file
859                                         // to a list of rows (array of rows)
860                                         var parser = csv.parse({
861                                         columns: function(line) {
862                                                 // By defining this callback, we get handed the
863                                                 // first line of the spreadsheet. Which we'll
864                                                 // ignore and effectively skip this line from processing
865                                         },
866                                         skip_empty_lines: true
867                                         });
868
869                                         var row = 0;
870                     var f = new Array();
871                     var transformer = csv.transform(function(data){
872                         // this will get row by row data, so for example,
873                         //logger.debug(data[0]+','+data[1]+','+data[2]);
874
875                                                 // build an array of rows
876                         f[row] = new Array();
877                         for ( col=0; col<data.length; col++ )
878                         {
879                             f[row][col] = data[col];
880                         }
881                         row++;
882                     });
883
884                                         // called when done with processing the CSV
885                                         transformer.on("finish", function() {
886
887                                                 var funcArray = new Array();
888
889                                                 function createFunction(lrow,res)
890                                                 {
891                                                         return function(callback) { dbRoutes.addAicSite(lrow,res,callback); }
892                                                 }
893
894                                                 // loop for each row and create an array of callbacks for async.parallelLimit
895                                                 // had to create a function above 'createFunction' to get
896                                                 for (var x=0; x<f.length; x++)
897                                                 {
898                                                         funcArray.push( createFunction(f[x],res) );
899                                                 }
900
901                                                 // make db calls in parrallel
902                                                 async.parallelLimit(funcArray, 50, function(err,result){
903
904                                                         if ( err ) {
905                                                                 dbRoutes.getAicSite(req,res, result,privilegeObj);
906                                                                 return;
907                                                         }
908                                                         else {
909                                                                 // result array has an entry in it, success entries are blank, figure out
910                                                                 // how many are not blank, aka errors.
911                                                                 var rowError = 0;
912                                                                 for(var i=0;i<result.length;i++){
913                                                                         if ( result[i].length > 0 )
914                                                                         {
915                                                                                 rowError++;
916                                                                         }
917                                                                 }
918                                                                 
919                                                                 var rowsProcessed = f.length - rowError;
920                                                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
921                                                                 if ( rowError > 0 )
922                                                                 {
923                                                                         result = {code:'failure', msg:result};
924                                                                 }
925                                                                 else
926                                                                 {
927                                                                         result = {code:'success', msg:result};
928                                                                 }
929                                                                 dbRoutes.getAicSite(req,res,result,privilegeObj);
930                                                                 return;
931                                                         }
932                                                 });
933                                         });
934
935                                 var stream = fs.createReadStream(req.file.path, "utf8");
936                                         stream.pipe(parser).pipe(transformer);
937
938
939                                 } catch(ex) {
940                                         msgArray.length = 0;
941                                         msgArray.push('There was an error uploading the file. '+ex);
942                                         dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
943                                         return;
944                                 }
945
946             } else {
947                                 msgArray.length = 0;
948                                 msgArray.push('There was an error uploading the file.');
949                                 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
950                                 return;
951             }
952         });
953         }
954         else {
955                 msgArray.length = 0;
956                 msgArray.push('There was an error uploading the file.');
957                 dbRoutes.getAicSite(req,res,{code:'danger', msg:msgArray},privilegeObj);
958         }
959         
960 } );
961
962 // POST
963 router.post('/uploadAicSwitch', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
964
965     var msgArray = new Array();
966         var privilegeObj = req.session.loggedInAdmin;
967
968     if(req.file.originalname){
969         if (req.file.originalname.size == 0) {
970             dbRoutes.getAicSwitch(req,res,{code:'danger', msg:'There was an error uploading the file, please try again.'},privilegeObj);
971                         return;
972         }
973         fs.exists(req.file.path, function(exists) {
974
975             if(exists) {
976
977                 var str = req.file.orignalname;
978
979                 // check for valid filename format
980                 var tagpos = str.search("_switch");
981                 var csvpos = str.search(".csv");
982                 if(tagpos != 13 || csvpos != 20){
983                     msgArray.length = 0;
984                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_switch.csv');
985                     dbRoutes.getAicSwitch(req,res,{code:'failure', msg:msgArray},privilegeObj);
986                     return;
987                 }
988
989                 try {
990                     var csv = require('csv');
991
992                     // the job of the parser is to convert a CSV file
993                     // to a list of rows (array of rows)
994                     var parser = csv.parse({
995                         columns: function(line) {
996                             // By defining this callback, we get handed the
997                             // first line of the spreadsheet. Which we'll
998                             // ignore and effectively skip this line from processing
999                         },
1000                         skip_empty_lines: true
1001                     });
1002
1003                     var row = 0;
1004                     var f = new Array();
1005                     var transformer = csv.transform(function(data){
1006                         // this will get row by row data, so for example,
1007                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1008
1009                         // build an array of rows
1010                         f[row] = new Array();
1011                         for ( col=0; col<data.length; col++ )
1012                         {
1013                             f[row][col] = data[col];
1014                         }
1015                         row++;
1016                     });
1017
1018                     // called when done with processing the CSV
1019                     transformer.on("finish", function() {
1020
1021                         var funcArray = new Array();
1022
1023                         function createFunction(lrow,res)
1024                         {
1025                             return function(callback) { dbRoutes.addAicSwitch(lrow,res,callback); }
1026                         }
1027
1028                         // loop for each row and create an array of callbacks for async.parallelLimit
1029                         // had to create a function above 'createFunction' to get
1030                         for (var x=0; x<f.length; x++)
1031                         {
1032                             funcArray.push( createFunction(f[x],res) );
1033                         }
1034
1035                         // make db calls in parrallel
1036                         async.parallelLimit(funcArray, 50, function(err,result){
1037
1038                             if ( err ) {
1039                                 dbRoutes.getAicSwitch(req,res,result,privilegeObj);
1040                                                                 return;
1041                             }
1042                             else {
1043                                 // result array has an entry in it, success entries are blank, figure out
1044                                 // how many are not blank, aka errors.
1045                                 var rowError = 0;
1046                                 for(var i=0;i<result.length;i++){
1047                                     if ( result[i].length > 0 )
1048                                     {
1049                                         rowError++;
1050                                     }
1051                                 }
1052
1053                                 var rowsProcessed = f.length - rowError;
1054                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1055                                 if ( rowError > 0 )
1056                                 {
1057                                     result = {code:'failure', msg:result};
1058                                 }
1059                                 else
1060                                 {
1061                                     result = {code:'success', msg:result};
1062                                 }
1063                                 dbRoutes.getAicSwitch(req,res,result,privilegeObj);
1064                                                                 return;
1065                             }
1066                         });
1067                     });
1068
1069                     var stream = fs.createReadStream(req.file.path, "utf8");
1070                     stream.pipe(parser).pipe(transformer);
1071
1072
1073                 } catch(ex) {
1074                     msgArray.length = 0;
1075                     msgArray.push('There was an error uploading the file. '+ex);
1076                     dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
1077                                         return;
1078                 }
1079
1080             } else {
1081                 msgArray.length = 0;
1082                 msgArray.push('There was an error uploading the file.');
1083                 dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
1084                                 return;
1085             }
1086         });
1087     }
1088     else {
1089                 msgArray.length = 0;
1090         msgArray.push('There was an error uploading the file.');
1091         dbRoutes.getAicSwitch(req,res,{code:'danger', msg:msgArray},privilegeObj);
1092                 return;
1093     }
1094
1095 } );
1096
1097 // POST
1098 router.post('/uploadAicAvailZone', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1099
1100     var msgArray = new Array();
1101         var privilegeObj = req.session.loggedInAdmin;
1102
1103     if(req.file.originalname){
1104         if (req.file.originalname.size == 0) {
1105             dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1106                         return;
1107         }
1108         fs.exists(req.file.path, function(exists) {
1109
1110             if(exists) {
1111
1112                 var str = req.file.originalname;
1113
1114                 // check for valid filename format
1115                 var tagpos = str.search("_availabilityzone");
1116                 var csvpos = str.search(".csv");
1117                 if(tagpos != 13 || csvpos != 30){
1118                     msgArray.length = 0;
1119                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_availabilityzone.csv');
1120                     dbRoutes.getAicAvailZone(req,res,{code:'failure', msg:msgArray},privilegeObj);
1121                     return;
1122                 }
1123
1124                 try {
1125                     var csv = require('csv');
1126
1127                     // the job of the parser is to convert a CSV file
1128                     // to a list of rows (array of rows)
1129                     var parser = csv.parse({
1130                         columns: function(line) {
1131                             // By defining this callback, we get handed the
1132                             // first line of the spreadsheet. Which we'll
1133                             // ignore and effectively skip this line from processing
1134                         },
1135                         skip_empty_lines: true
1136                     });
1137
1138                     var row = 0;
1139                     var f = new Array();
1140                     var transformer = csv.transform(function(data){
1141                         // this will get row by row data, so for example,
1142                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1143
1144                         // build an array of rows
1145                         f[row] = new Array();
1146                         for ( col=0; col<data.length; col++ )
1147                         {
1148                             f[row][col] = data[col];
1149                         }
1150                         row++;
1151                     });
1152
1153                     // called when done with processing the CSV
1154                     transformer.on("finish", function() {
1155
1156                         var funcArray = new Array();
1157
1158                         function createFunction(lrow,res)
1159                                                 {
1160                             return function(callback) { dbRoutes.addAicAvailZone(lrow,res,callback); }
1161                         }
1162
1163                         // loop for each row and create an array of callbacks for async.parallelLimit
1164                         // had to create a function above 'createFunction' to get
1165                         for (var x=0; x<f.length; x++)
1166                         {
1167                             funcArray.push( createFunction(f[x],res) );
1168                         }
1169
1170                         // make db calls in parrallel
1171                         async.parallelLimit(funcArray, 50, function(err,result){
1172
1173                             if ( err ) {
1174                                 dbRoutes.getAicAvailZone(req,res,result,privilegeObj);
1175                                                                 return;
1176                             }
1177                             else {
1178                                 // result array has an entry in it, success entries are blank, figure out
1179                                 // how many are not blank, aka errors.
1180                                 var rowError = 0;
1181                                 for(var i=0;i<result.length;i++){
1182                                     if ( result[i].length > 0 )
1183                                     {
1184                                         rowError++;
1185                                     }
1186                                 }
1187
1188                                 var rowsProcessed = f.length - rowError;
1189                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1190                                 if ( rowError > 0 )
1191                                 {
1192                                     result = {code:'failure', msg:result};
1193                                 }
1194                                 else
1195                                 {
1196                                     result = {code:'success', msg:result};
1197                                 }
1198                                 dbRoutes.getAicAvailZone(req,res,result,privilegeObj);
1199                                                                 return;
1200                             }
1201                         });
1202                     });
1203
1204                     var stream = fs.createReadStream(req.file.path, "utf8");
1205                     stream.pipe(parser).pipe(transformer);
1206
1207
1208                 } catch(ex) {
1209                     msgArray.length = 0;
1210                     msgArray.push('There was an error uploading the file. '+ex);
1211                     dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
1212                                         return;
1213                 }
1214
1215             } else {
1216                 msgArray.length = 0;
1217                 msgArray.push('There was an error uploading the file.');
1218                 dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
1219                                 return;
1220             }
1221         });
1222  }
1223     else {
1224         msgArray.length = 0;
1225         msgArray.push('There was an error uploading the file.');
1226         dbRoutes.getAicAvailZone(req,res,{code:'danger', msg:msgArray},privilegeObj);
1227                 return;
1228     }
1229
1230 } );
1231
1232 // POST
1233 router.post('/uploadVpePool', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1234
1235     var msgArray = new Array();
1236         var privilegeObj = req.session.loggedInAdmin;
1237
1238     if(req.file.originalname){
1239         if (req.file.originalname.size == 0) {
1240             dbRoutes.getVpePool(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1241                         return;
1242         }
1243         fs.exists(req.file.path, function(exists) {
1244
1245             if(exists) {
1246
1247                 var str = req.file.originalname;
1248
1249                 // check for valid filename format
1250                 var tagpos = str.search("_vpe");
1251                 var csvpos = str.search(".csv");
1252                 if(tagpos != 13 || csvpos != 17){
1253                     msgArray.length = 0;
1254                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpe.csv');
1255                                         var resultObj = {code:'failure', msg:msgArray};
1256                     dbRoutes.getVpePool(req,res,resultObj,privilegeObj);
1257                                         return;
1258                 }
1259
1260                 try {
1261                     var csv = require('csv');
1262
1263                     // the job of the parser is to convert a CSV file
1264                     // to a list of rows (array of rows)
1265                     var parser = csv.parse({
1266                         columns: function(line) {
1267                             // By defining this callback, we get handed the
1268                             // first line of the spreadsheet. Which we'll
1269                             // ignore and effectively skip this line from processing
1270                         },
1271                         skip_empty_lines: true
1272                     });
1273
1274                     var row = 0;
1275                     var f = new Array();
1276                     var transformer = csv.transform(function(data){
1277                         // this will get row by row data, so for example,
1278                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1279
1280                         // build an array of rows
1281                         f[row] = new Array();
1282                         for ( col=0; col<data.length; col++ )
1283                         {
1284                             f[row][col] = data[col];
1285                         }
1286                         row++;
1287                     });
1288
1289                     // called when done with processing the CSV
1290                     transformer.on("finish", function() {
1291
1292                         var funcArray = new Array();
1293
1294                         function createFunction(lrow,res)
1295                         {
1296                             return function(callback) { dbRoutes.addVpePool(lrow,res,callback); }
1297                         }
1298
1299                         // loop for each row and create an array of callbacks for async.parallelLimit
1300                                                 // had to create a function above 'createFunction' to get
1301                         for (var x=0; x<f.length; x++)
1302                         {
1303                             funcArray.push( createFunction(f[x],res) );
1304                         }
1305
1306                         // make db calls in parrallel
1307                         async.parallelLimit(funcArray, 50, function(err,result){
1308
1309                             if ( err ) {
1310                                 dbRoutes.getVpePool(req,res,result,privilegeObj);
1311                                                                 return;
1312                             }
1313                             else {
1314                                 // result array has an entry in it, success entries are blank, figure out
1315                                 // how many are not blank, aka errors.
1316                                 var rowError = 0;
1317                                 for(var i=0;i<result.length;i++){
1318                                     if ( result[i].length > 0 )
1319                                     {
1320                                         rowError++;
1321                                     }
1322                                 }
1323
1324                                 var rowsProcessed = f.length - rowError;
1325                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1326                                 if ( rowError > 0 )
1327                                 {
1328                                     result = {code:'failure', msg:result};
1329                                 }
1330                                 else
1331                                 {
1332                                     result = {code:'success', msg:result};
1333                                 }
1334                                 dbRoutes.getVpePool(req,res,result,privilegeObj);
1335                                                                 return;
1336                             }
1337                         });
1338                     });
1339
1340                     var stream = fs.createReadStream(req.file.path, "utf8");
1341                     stream.pipe(parser).pipe(transformer);
1342
1343
1344                 } catch(ex) {
1345                     msgArray.length = 0;
1346                     msgArray.push('There was an error uploading the file. '+ex);
1347                     dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1348                                         return;
1349                 }
1350
1351             } else {
1352                 msgArray.length = 0;
1353                 msgArray.push('There was an error uploading the file.');
1354                 dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1355                                 return;
1356             }
1357         });
1358  }
1359     else {
1360         msgArray.length = 0;
1361         msgArray.push('There was an error uploading the file.');
1362         dbRoutes.getVpePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1363                 return;
1364     }
1365
1366 } );
1367
1368 // POST
1369 router.post('/uploadVplspePool', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res){
1370
1371     var msgArray = new Array();
1372         var privilegeObj = req.session.loggedInAdmin;
1373
1374     if(req.file.originalname){
1375         if (req.file.originalname.size == 0) {
1376             dbRoutes.getVplspePool(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1377                         return;
1378         }
1379         fs.exists(req.file.path, function(exists) {
1380
1381             if(exists) {
1382
1383                 var str = req.file.originalname;
1384
1385                 // check for valid filename format
1386                 var tagpos = str.search("_vpls");
1387                 var csvpos = str.search(".csv");
1388                 if(tagpos != 13 || csvpos != 18){
1389                     msgArray.length = 0;
1390                     msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpls.csv');
1391                     dbRoutes.getVplspePool(req,res,{code:'failure', msg:msgArray},privilegeObj);
1392                     return;
1393                 }
1394
1395                 try {
1396                     var csv = require('csv');
1397
1398                     // the job of the parser is to convert a CSV file
1399                     // to a list of rows (array of rows)
1400                     var parser = csv.parse({
1401                         columns: function(line) {
1402                             // By defining this callback, we get handed the
1403                             // first line of the spreadsheet. Which we'll
1404                             // ignore and effectively skip this line from processing
1405                         },
1406                         skip_empty_lines: true
1407                     });
1408
1409                     var row = 0;
1410                     var f = new Array();
1411                     var transformer = csv.transform(function(data){
1412                         // this will get row by row data, so for example,
1413                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1414
1415                         // build an array of rows
1416                         f[row] = new Array();
1417                         for ( col=0; col<data.length; col++ )
1418                         {
1419                             f[row][col] = data[col];
1420                         }
1421                         row++;
1422                     });
1423
1424                     // called when done with processing the CSV
1425                     transformer.on("finish", function() {
1426
1427                         var funcArray = new Array();
1428
1429                         function createFunction(lrow,res)
1430                         {
1431                             return function(callback) { dbRoutes.addVplspePool(lrow,res,callback); }
1432                         }
1433                                                 // loop for each row and create an array of callbacks for async.parallelLimit
1434                         // had to create a function above 'createFunction' to get
1435                         for (var x=0; x<f.length; x++)
1436                         {
1437                             funcArray.push( createFunction(f[x],res) );
1438                         }
1439
1440                         // make db calls in parrallel
1441                         async.parallelLimit(funcArray, 50, function(err,result){
1442
1443                             if ( err ) {
1444                                 dbRoutes.getVplspePool(req,res,result,privilegeObj);
1445                                                                 return;
1446                             }
1447                             else {
1448                                 // result array has an entry in it, success entries are blank, figure out
1449                                 // how many are not blank, aka errors.
1450                                 var rowError = 0;
1451                                 for(var i=0;i<result.length;i++){
1452                                     if ( result[i].length > 0 )
1453                                     {
1454                                         rowError++;
1455                                     }
1456                                 }
1457                                 var rowsProcessed = f.length - rowError;
1458                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1459                                 if ( rowError > 0 )
1460                                 {
1461                                     result = {code:'failure', msg:result};
1462                                 }
1463                                 else
1464                                 {
1465                                     result = {code:'success', msg:result};
1466                                 }
1467                                 dbRoutes.getVplspePool(req,res,result,privilegeObj);
1468                                                                 return;
1469                             }
1470                         });
1471                     });
1472
1473                     var stream = fs.createReadStream(req.file.path, "utf8");
1474                     stream.pipe(parser).pipe(transformer);
1475
1476
1477                 } catch(ex) {
1478                     msgArray.length = 0;
1479                     msgArray.push('There was an error uploading the file. '+ex);
1480                     dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1481                                         return;
1482                 }
1483
1484             } else {
1485                 msgArray.length = 0;
1486                 msgArray.push('There was an error uploading the file.');
1487                 dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1488                                 return;
1489             }
1490         });
1491  }
1492     else {
1493         msgArray.length = 0;
1494         msgArray.push('There was an error uploading the file.');
1495         dbRoutes.getVplspePool(req,res,{code:'danger', msg:msgArray},privilegeObj);
1496                 return;
1497     }
1498
1499 } );
1500
1501 // POST
1502 router.post('/uploadServiceHoming', csp.checkAuth, dbRoutes.checkDB, upload.single('filename'), function(req, res)
1503 {
1504     var msgArray = new Array();
1505     var privilegeObj = req.session.loggedInAdmin;
1506
1507     if(req.file.originalname)
1508         {
1509         if (req.file.originalname.size == 0) {
1510             dbRoutes.getServiceHoming(req,res,{code:'failure', msg:'There was an error uploading the file, please try again.'},privilegeObj);
1511             return;
1512         }
1513         fs.exists(req.file.path, function(exists) 
1514                 {
1515             if(exists) 
1516                         {
1517                 var str = req.file.originalname;
1518
1519                 // check for valid filename format
1520                 var csvpos = str.search(".csv");
1521                 if( (l_.startsWith(str,'aichoming_') != true)  || csvpos != 18)
1522                                 {
1523                         msgArray.length = 0;
1524                         msgArray.push('Not a valid filename, format must be aichoming_mmddYYYY.csv');
1525                         //msgArray.push('Not a valid filename, format must be pp_YYYYMMDDHH_vpls.csv');
1526                         dbRoutes.getServiceHoming(req,res,{code:'failure', msg:msgArray},privilegeObj);
1527                         return;
1528                 }
1529
1530                 try 
1531                                 {
1532                     var csv = require('csv');
1533
1534                     // the job of the parser is to convert a CSV file
1535                     // to a list of rows (array of rows)
1536                     var parser = csv.parse({
1537                         columns: function(line) {
1538                             // By defining this callback, we get handed the
1539                             // first line of the spreadsheet. Which we'll
1540                             // ignore and effectively skip this line from processing
1541                         },
1542                         skip_empty_lines: true
1543                     });
1544
1545                     var row = 0;
1546                     var f = new Array();
1547                                         var csvrows = new Array();
1548                     var transformer = csv.transform(function(data){
1549                         // this will get row by row data, so for example,
1550                         //logger.debug(data[0]+','+data[1]+','+data[2]);
1551
1552                         // build an array of rows
1553                         f[row] = new Array();
1554                         for ( col=0; col<data.length; col++ )
1555                         {
1556                             f[row][col] = data[col];
1557                         }
1558                         row++;
1559                     });
1560
1561                     // called when done with processing the CSV
1562                     transformer.on("finish", function() 
1563                                         {
1564                         var funcArray = new Array();
1565
1566                         function createFunction(lrow,res)
1567                         {
1568                             return function(callback) { dbRoutes.addServiceHoming(lrow,req,res,callback); }
1569                         }
1570                                                 funcArray.push(function(callback) {
1571                                                         dbRoutes.saveServiceHoming(req,res,callback);
1572                                                 });
1573                         // loop for each row and create an array of callbacks for async.parallelLimit
1574                         // had to create a function above 'createFunction' to get
1575                         for (var x=0; x<f.length; x++)
1576                         {
1577                                                         funcArray.push( createFunction(f[x],res) );
1578                                                 }
1579
1580                         // make db calls in series
1581                         async.series(funcArray, function(err,result)
1582                                                 {
1583                             if ( err ) 
1584                                                         {
1585                                 result = {code:'failure', msg:result};
1586                                 dbRoutes.getServiceHoming(req,res,result,privilegeObj);
1587                                 return;
1588                             }
1589                             else 
1590                                                         {       // result array has an entry in it, success entries are blank, figure out
1591                                 // how many are not blank, aka errors.
1592                                 var rowError = 0;
1593                                 for(var i=0;i<result.length;i++)
1594                                                                 {
1595                                     if ( result[i].length > 0 )
1596                                     {
1597                                         rowError++;
1598                                     }
1599                                 }
1600                                 var rowsProcessed = f.length - rowError;
1601                                 result.push(rowsProcessed + ' of ' + f.length + ' rows processed.');
1602                                 if ( rowError > 0 )
1603                                 {
1604                                     result = {code:'failure', msg:result};
1605                                 }
1606                                 else
1607                                 {
1608                                     result = {code:'success', msg:result};
1609                                 }
1610                                 dbRoutes.getServiceHoming(req,res,result,privilegeObj);
1611                                 return;
1612                             }
1613                         });
1614                     });
1615
1616                     var stream = fs.createReadStream(req.file.path, "utf8");
1617                     stream.pipe(parser).pipe(transformer);
1618
1619
1620                 } catch(ex) {
1621                     msgArray.length = 0;
1622                     msgArray.push('There was an error uploading the file. '+ex);
1623                     dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1624                     return;
1625                 }
1626
1627             } else {
1628                 msgArray.length = 0;
1629                 msgArray.push('There was an error uploading the file.');
1630                 dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1631                 return;
1632             }
1633         });
1634         }
1635     else 
1636         {
1637         msgArray.length = 0;
1638         msgArray.push('There was an error uploading the file.');
1639         dbRoutes.getServiceHoming(req,res,{code:'danger', msg:msgArray},privilegeObj);
1640         return;
1641     }
1642
1643 } );
1644 function removeNL(s) {
1645   /*
1646   ** Remove NewLine, CarriageReturn and Tab characters from a String
1647   **   s  string to be processed
1648   ** returns new string
1649   */
1650   r = "";
1651   for (i=0; i < s.length; i++)
1652   {
1653     if (s.charAt(i) != '\n' &&
1654         s.charAt(i) != '\r' &&
1655         s.charAt(i) != '\t')
1656     {
1657       r += s.charAt(i);
1658     }
1659   }
1660   return r;
1661 }
1662 function padLeft(nr, n, str){
1663     return Array(n-String(nr).length+1).join(str||'0')+nr;
1664 }
1665
1666
1667 module.exports = router;