1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
var express = require('express');
var router = express.Router();
var exec = require('child_process').exec;
var util = require('util');
var fs = require('fs.extra');
var dbRoutes = require('./dbRoutes');
var csp = require('./csp');
var multer = require('multer');
var bodyParser = require('body-parser');
var sax = require('sax'),strict=true,parser = sax.parser(strict);
var async = require('async');
var l_ = require('lodash');
var dateFormat = require('dateformat');
var properties = require(process.env.SDNC_CONFIG_DIR + '/admportal.json');
var vnf = require('./vnf');
var network = require('./network');
var moment = require('moment');
// pass host, username and password to ODL
// target host for ODL request
var username = properties.odlUser;
var password = properties.odlPasswd;
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
var host = properties.odlHost;
var port = properties.odlPort;
var header = {'Host': host, 'Authorization': auth, 'Content-Type': 'application/json'};
var options = {
host : host,
headers : header,
port : port,
rejectUnauthorized:false,
strictSSL: false
};
// multer 1.1
var unixTime = moment().unix();
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, process.cwd() + '/uploads/')
},
filename: function (req, file, cb) {
cb(null, unixTime + "." + file.originalname )
}
});
var upload = multer({
storage: storage,
fileFilter: function(req,file,cb) {
var type = file.mimetype;
if ( type.indexOf('ms-excel') == -1 ) {
return cb(null,false);
}
cb(null,true);
}
});
router.post('/uploadVnfCsv', csp.checkAuth, upload.array('filename'), function(req, res)
{
console.log('files:'+ JSON.stringify(req.files,null,4));
var tasks = []
var msgArray = new Array();
var privilegeObj = req.session.loggedInAdmin;
var privilegeObj = req.session.loggedInAdmin;
var tasks = [];
tasks.push ( function(callback) { vnf.go(req,res,callback,''); } );
tasks.push ( function(arg1,arg2,callback) { formatVnfInsertStatement(arg1,arg2,req,res,callback); } );
tasks.push( function(arg1, callback) { dbRoutes.addRow(arg1,req,res,callback); } );
async.waterfall(tasks, function(err,result)
{
if(err){
msgArray.push(err);
dbRoutes.getVnfData(req,res,{code:'failure', msg:msgArray},privilegeObj);
return;
}
else {
//logger.debug('Successfully uploaded ' + req.session.worksheetFilename);
msgArray.push('Successfully uploaded file.' );
dbRoutes.getVnfData(req,res,{code:'success', msg:msgArray},privilegeObj);
return;
}
});
});
router.post('/uploadNetworkCsv', csp.checkAuth, upload.array('filename'), function(req, res)
{
console.log('files:'+ JSON.stringify(req.files,null,4));
var tasks = []
var msgArray = new Array();
var privilegeObj = req.session.loggedInAdmin;
var privilegeObj = req.session.loggedInAdmin;
var tasks = [];
tasks.push ( function(callback) { network.go(req,res,callback,''); } );
tasks.push ( function(arg1,arg2,callback) { formatNetworkInsertStatement(arg1,arg2,req,res,callback); } );
tasks.push( function(arg1, callback) { dbRoutes.addRow(arg1,req,res,callback); } );
async.waterfall(tasks, function(err,result)
{
if(err){
msgArray.push(err);
dbRoutes.getVnfNetworkData(req,res,{code:'failure', msg:msgArray},privilegeObj);
return;
}
else {
//logger.debug('Successfully uploaded ' + req.session.worksheetFilename);
msgArray.push('Successfully uploaded file.' );
dbRoutes.getVnfNetworkData(req,res,{code:'success', msg:msgArray},privilegeObj);
return;
}
});
});
function formatVnfInsertStatement(content,filename,req,res,callback)
{
//var newstr = JSON.stringify(content).replace(/\\\"/g,'\\\\\\"');
//var ins_str = newstr.replace("\r\n ", "\\r\\n");
var newstr = JSON.stringify(content);
var enc_str = encodeURI(newstr);
var sql = "INSERT INTO PRE_LOAD_VNF_DATA "
+ "(filename,preload_data) VALUES ("
+ "'"+ filename + "',"
+ "'" + enc_str + "')";
callback(null,sql);
}
function formatNetworkInsertStatement(content,filename,req,res,callback)
{
var newstr = JSON.stringify(content);
var enc_str = encodeURI(newstr);
var sql = "INSERT INTO PRE_LOAD_VNF_NETWORK_DATA "
+ "(filename,preload_data) VALUES ("
+ "'"+ filename + "',"
+ "'" + enc_str + "')";
callback(null,sql);
}
module.exports = router;
|