Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | var express = require('express'); |
| 2 | var router = express.Router(); |
| 3 | var exec = require('child_process').exec; |
| 4 | var util = require('util'); |
| 5 | var fs = require('fs.extra'); |
| 6 | var dbRoutes = require('./dbRoutes'); |
| 7 | var csp = require('./csp'); |
| 8 | var multer = require('multer'); |
| 9 | var bodyParser = require('body-parser'); |
| 10 | var sax = require('sax'),strict=true,parser = sax.parser(strict); |
| 11 | var async = require('async'); |
| 12 | var l_ = require('lodash'); |
| 13 | var dateFormat = require('dateformat'); |
| 14 | var properties = require(process.env.SDNC_CONFIG_DIR + '/admportal.json'); |
| 15 | var vnf = require('./vnf'); |
| 16 | var network = require('./network'); |
| 17 | var moment = require('moment'); |
| 18 | |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 19 | // pass host, username and password to ODL |
| 20 | // target host for ODL request |
| 21 | var username = properties.odlUser; |
| 22 | var password = properties.odlPasswd; |
| 23 | var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64'); |
| 24 | var host = properties.odlHost; |
| 25 | var port = properties.odlPort; |
| 26 | |
| 27 | var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'application/json'}; |
| 28 | var options = { |
| 29 | host : host, |
| 30 | headers : header, |
| 31 | port : port, |
| 32 | rejectUnauthorized:false, |
| 33 | strictSSL: false |
| 34 | }; |
| 35 | |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 36 | // multer |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 37 | var unixTime = moment().unix(); |
| 38 | var storage = multer.diskStorage({ |
| 39 | destination: function (req, file, cb) { |
| 40 | cb(null, process.cwd() + '/uploads/') |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 41 | return; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 42 | }, |
| 43 | filename: function (req, file, cb) { |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 44 | console.log('filename'); |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 45 | cb(null, unixTime + "." + file.originalname ) |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 46 | return; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 47 | } |
| 48 | }); |
| 49 | |
| 50 | var 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) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 58 | return; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 59 | } |
| 60 | }); |
| 61 | |
Al Rotundo | 2f8e4d0 | 2017-03-24 13:54:52 +0000 | [diff] [blame] | 62 | router.post('/uploadVnfCsv', csp.checkAuth, upload.array('filename'), function(req, res) |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 63 | { |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 64 | var msgArray = new Array(); |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 65 | 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) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 70 | tasks.push( function(arg1, callback) { dbRoutes.addRow(arg1,req,res,callback); } ); |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 71 | async.waterfall(tasks, function(err,result) |
| 72 | { |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 73 | 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) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 83 | }); |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 84 | }); |
| 85 | |
Al Rotundo | 2f8e4d0 | 2017-03-24 13:54:52 +0000 | [diff] [blame] | 86 | router.post('/uploadNetworkCsv', csp.checkAuth, upload.array('filename'), function(req, res) |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 87 | { |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 88 | console.log('uploadNetworkCsv'); |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 89 | |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 90 | var msgArray = new Array(); |
| 91 | var privilegeObj = req.session.loggedInAdmin; |
| 92 | var tasks = []; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 93 | |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 94 | 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) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 109 | }); |
| 110 | |
| 111 | |
| 112 | function formatVnfInsertStatement(content,filename,req,res,callback) |
| 113 | { |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 114 | var newstr = JSON.stringify(content); |
| 115 | var enc_str = encodeURI(newstr); |
| 116 | var sql = "INSERT INTO PRE_LOAD_VNF_DATA " |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 117 | + "(filename,preload_data) VALUES (" |
| 118 | + "'"+ filename + "'," |
| 119 | + "'" + enc_str + "')"; |
| 120 | |
| 121 | callback(null,sql); |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 122 | return; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | function formatNetworkInsertStatement(content,filename,req,res,callback) |
| 126 | { |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 127 | var newstr = JSON.stringify(content); |
| 128 | var enc_str = encodeURI(newstr); |
| 129 | var sql = "INSERT INTO PRE_LOAD_VNF_NETWORK_DATA " |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 130 | + "(filename,preload_data) VALUES (" |
| 131 | + "'"+ filename + "'," |
| 132 | + "'" + enc_str + "')"; |
| 133 | |
| 134 | callback(null,sql); |
Rotundo, Al (ar3165) | 18dcbec | 2019-07-31 14:46:56 +0000 | [diff] [blame^] | 135 | return; |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 138 | module.exports = router; |