blob: 522c6daa3fdd5001c4f738a5b8bbca3a90c23a12 [file] [log] [blame]
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -05001var express = require('express');
2var router = express.Router();
3var exec = require('child_process').exec;
4var util = require('util');
5var fs = require('fs.extra');
6var dbRoutes = require('./dbRoutes');
7var csp = require('./csp');
8var multer = require('multer');
9var bodyParser = require('body-parser');
10var sax = require('sax'),strict=true,parser = sax.parser(strict);
11var async = require('async');
12var l_ = require('lodash');
13var dateFormat = require('dateformat');
14var properties = require(process.env.SDNC_CONFIG_DIR + '/admportal.json');
15var vnf = require('./vnf');
16var network = require('./network');
17var moment = require('moment');
18
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050019// pass host, username and password to ODL
20// target host for ODL request
21var username = properties.odlUser;
22var password = properties.odlPasswd;
23var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
24var host = properties.odlHost;
25var port = properties.odlPort;
26
27var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'application/json'};
28var options = {
29 host : host,
30 headers : header,
31 port : port,
32 rejectUnauthorized:false,
33 strictSSL: false
34};
35
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000036// multer
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050037var unixTime = moment().unix();
38var storage = multer.diskStorage({
39 destination: function (req, file, cb) {
40 cb(null, process.cwd() + '/uploads/')
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000041 return;
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050042 },
43 filename: function (req, file, cb) {
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000044console.log('filename');
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050045 cb(null, unixTime + "." + file.originalname )
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000046 return;
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050047 }
48});
49
50var upload = multer({
51 storage: storage,
52 fileFilter: function(req,file,cb) {
53 var type = file.mimetype;
54 if ( type.indexOf('ms-excel') == -1 ) {
55 return cb(null,false);
56 }
57 cb(null,true);
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000058 return;
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050059 }
60});
61
Al Rotundo2f8e4d02017-03-24 13:54:52 +000062router.post('/uploadVnfCsv', csp.checkAuth, upload.array('filename'), function(req, res)
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050063{
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000064 var msgArray = new Array();
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050065 var privilegeObj = req.session.loggedInAdmin;
66 var tasks = [];
67
68 tasks.push ( function(callback) { vnf.go(req,res,callback,''); } );
69 tasks.push ( function(arg1,arg2,callback) { formatVnfInsertStatement(arg1,arg2,req,res,callback); } );
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000070 tasks.push( function(arg1, callback) { dbRoutes.addRow(arg1,req,res,callback); } );
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050071 async.waterfall(tasks, function(err,result)
72 {
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000073 if(err){
74 msgArray.push(err);
75 dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj);
76 return;
77 }
78 else {
79 msgArray.push('Successfully uploaded file.' );
80 dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj);
81 return;
82 }
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050083 });
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050084});
85
Al Rotundo2f8e4d02017-03-24 13:54:52 +000086router.post('/uploadNetworkCsv', csp.checkAuth, upload.array('filename'), function(req, res)
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050087{
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000088 console.log('uploadNetworkCsv');
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050089
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000090 var msgArray = new Array();
91 var privilegeObj = req.session.loggedInAdmin;
92 var tasks = [];
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -050093
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +000094 tasks.push ( function(callback) { network.go(req,res,callback,''); } );
95 tasks.push ( function(arg1,arg2,callback) { formatNetworkInsertStatement(arg1,arg2,req,res,callback); } );
96 tasks.push( function(arg1, callback) { dbRoutes.addRow(arg1,req,res,callback); } );
97 async.waterfall(tasks, function(err,result)
98 {
99 if(err){
100 console.log('ERROR:' + err);
101 msgArray.push(err);
102 dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj);
103 }
104 else {
105 msgArray.push('Successfully uploaded file.' );
106 dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj);
107 }
108 });
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -0500109});
110
111
112function formatVnfInsertStatement(content,filename,req,res,callback)
113{
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +0000114 var newstr = JSON.stringify(content);
115 var enc_str = encodeURI(newstr);
116 var sql = "INSERT INTO PRE_LOAD_VNF_DATA "
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -0500117 + "(filename,preload_data) VALUES ("
118 + "'"+ filename + "',"
119 + "'" + enc_str + "')";
120
121 callback(null,sql);
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +0000122 return;
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -0500123}
124
125function formatNetworkInsertStatement(content,filename,req,res,callback)
126{
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +0000127 var newstr = JSON.stringify(content);
128 var enc_str = encodeURI(newstr);
129 var sql = "INSERT INTO PRE_LOAD_VNF_NETWORK_DATA "
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -0500130 + "(filename,preload_data) VALUES ("
131 + "'"+ filename + "',"
132 + "'" + enc_str + "')";
133
134 callback(null,sql);
Rotundo, Al (ar3165)18dcbec2019-07-31 14:46:56 +0000135 return;
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -0500136}
137
Timoney, Daniel (dt5972)324ee362017-02-15 10:37:53 -0500138module.exports = router;