aboutsummaryrefslogtreecommitdiffstats
path: root/admportal/server/router/routes/vnf.js
blob: be004fe2db756edb4a5bbbd84b0e21bfc93e2d96 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Helper functions for processing a VNF worksheet

var helpers = require('./helpers.js');
var _ = require('lodash');
var csvtojson = require('csvtojson');
var async = require('async');
var uuid = require('node-uuid');   // generate a uuid with "uuid.v1()"
var path = require('path');
var fs = require("fs");
var moment = require("moment");

var vnf = module.exports;
var getParam = helpers.getParam;

var callback;
var indir;
var csvGeneral, csvZones, csvNetworks, csvVMs, csvVMnetworks, csvVMnetworkIPs, csvVMnetworkMACs, csvTagValues;
var rawJson={}
var finalJson={};  
var platform;
var req, res;
var preloadVersion;  // 1607, 1610, etc...

puts = helpers.puts;
putd = helpers.putd;

vnf.go = function(lreq,lres,cb,dir){
  puts("Processing VNF workbook");
  req = lreq;
  res = lres;
  callback = cb;
  if (dir!="") {
    platform="pc";
    indir=dir;
  } else {
    platform="portal";
    indir=process.cwd() + "/uploads/";
  }
  doGeneral();
}


// READ WORKSHEET: GENERAL

function doGeneral() {
  puts("Reading General worksheet");
  var csvFilename="General.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotGeneral);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotGeneral(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('General.csv file is missing from upload.');
    return;
  }
  csvGeneral = jsonObj;
  puts("\nRead this: ");
  putd(csvGeneral);
  puts("\n");
  doAvailZones();
}

// READ WORKSHEET: AVAILABILITY ZONES

function doAvailZones() {
  puts("Reading Availability-zones worksheet");
  var csvFilename="Availability-zones.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotAvailZones);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotAvailZones(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('Availability-zones.csv file is missing from upload.');
    return;
  }
  csvZones = jsonObj;
  csvZones = _.reject(csvZones, 'field2', 'Availability Zones');
  csvZones = _.reject(csvZones, 'field2', 'List the availability zones for this VNF');
  csvZones = _.reject(csvZones, 'field2', '');
  puts("\nRead this: ");
  putd(csvZones);
  puts("\n");
  doNetworks();
}

// READ WORKSHEET: NETWORKS

function doNetworks() {
  puts("Reading Networks worksheet");
  var csvFilename="Networks.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotNetworks);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotNetworks(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('Networks.csv file is missing from upload.');
    return;
  }
  csvNetworks = jsonObj;
  csvNetworks = _.reject(csvNetworks, 'field2', 'Networks');
  csvNetworks = _.reject(csvNetworks, 'field2', 'List the VNF networks. (VM-networks are on a different worksheet.)');
  csvNetworks = _.reject(csvNetworks, 'field2', 'network-role');
  csvNetworks = _.reject(csvNetworks, 'field2', '');
  puts("\nRead this: ");
  putd(csvNetworks);
  puts("\n");
  doVMs();
}

// READ WORKSHEET: VMs

function doVMs() {
  puts("Reading VMs worksheet");
  var csvFilename="VMs.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotVMs);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotVMs(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('VMs.csv file is missing from upload.');
    return;
  }
  csvVMs = jsonObj;
  csvVMs = _.reject(csvVMs, 'field2', 'VMs');
  csvVMs = _.reject(csvVMs, 'field2', 'List the VM types for this VNF');
  csvVMs = _.reject(csvVMs, 'field2', 'vm-type');
  csvVMs = _.reject(csvVMs, 'field2', '');
  puts("\nRead this: ");
  putd(csvVMs);
  puts("\n");
  doVMnetworks();
}

// READ WORKSHEET: VM-NETWORKS

function doVMnetworks() {
  puts("Reading VM-networks worksheet");
  var csvFilename="VM-networks.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotVMnetworks);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotVMnetworks(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('VM-networks.csv file is missing from upload.');
    return;
  }
  csvVMnetworks = jsonObj;
  csvVMnetworks = _.reject(csvVMnetworks, 'field2', 'VM-networks');
  csvVMnetworks = _.reject(csvVMnetworks, 'field2', 'List the VM-networks for each VM type');
  csvVMnetworks = _.reject(csvVMnetworks, 'field2', 'vm-type');
  csvVMnetworks = _.reject(csvVMnetworks, 'field2', '');
  puts("\nRead this: ");
  putd(csvVMnetworks);
  puts("\n");
  doVMnetworkIPs();
}

// READ WORKSHEET: VM-NETWORK-IPS

function doVMnetworkIPs() {
  puts("Reading VM-network-IPs worksheet");
  var csvFilename="VM-network-IPs.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotVMnetworkIPs);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotVMnetworkIPs(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('VM-network-IPs.csv file is missing from upload.');
    return;
  }
  csvVMnetworkIPs = jsonObj;
  csvVMnetworkIPs = _.reject(csvVMnetworkIPs, 'field2', 'VM-network-IPs');
  csvVMnetworkIPs = _.reject(csvVMnetworkIPs, 'field2', 'List the IPs assigned to each VM-network');
  csvVMnetworkIPs = _.reject(csvVMnetworkIPs, 'field2', 'vm-type');
  csvVMnetworkIPs = _.reject(csvVMnetworkIPs, 'field2', '');
  puts("\nRead this: ");
  putd(csvVMnetworkIPs);
  puts("\n");
  doVMnetworkMACs();
}

// READ WORKSHEET: VM-NETWORK-MACS

function doVMnetworkMACs() {
  puts("Reading VM-network-MACs worksheet");
  var csvFilename="VM-network-MACs.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotVMnetworkMACs);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotVMnetworkMACs(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('VM-network-MACs.csv file is missing from upload.');
    return;
  }
  csvVMnetworkMACs = jsonObj;
  csvVMnetworkMACs = _.reject(csvVMnetworkMACs, 'field2', 'VM-network-MACs');
  csvVMnetworkMACs = _.reject(csvVMnetworkMACs, 'field2', 'List the MACs assigned to each VM-network');
  csvVMnetworkMACs = _.reject(csvVMnetworkMACs, 'field2', 'vm-type');
  csvVMnetworkMACs = _.reject(csvVMnetworkMACs, 'field2', '');
  puts("\nRead this: ");
  putd(csvVMnetworkMACs);
  puts("\n");
  doTagValues();
}

// READ WORKSHEET: TAG-VALUES

function doTagValues() {
  puts("Reading Tag-values worksheet");
  var csvFilename="Tag-values.csv";
  var newFileName = helpers.getFileName(req, csvFilename);
  if ( newFileName != null ) {
    helpers.readCsv(indir, newFileName, gotTagValues);
  }
  else {
    callback(csvFilename + ' file is missing from upload.');
  }
}

function gotTagValues(err, jsonObj) {
  if (err) {
    puts("\nError!");
    putd(err);
    callback('Tag-values.csv file is missing from upload.');
    return;
  }
  csvTagValues = jsonObj;
  csvTagValues = _.reject(csvTagValues, 'field2', 'Tag-values');
  csvTagValues = _.reject(csvTagValues, 'field2', 'Extra data to be passed into the HEAT template for this VNF');
  csvTagValues = _.reject(csvTagValues, 'field2', 'vnf-parameter-name');
  csvTagValues = _.reject(csvTagValues, 'field2', 'vnf-parameter-value');
  csvTagValues = _.reject(csvTagValues, 'field2', '');
  puts("\nRead this: ");
  putd(csvTagValues);
  puts("\n");
  doneReadingFiles();
}




function doneReadingFiles() {
  puts("\n");
  puts("DONE READING FILES!");
  puts("\n");
  processJson();
}


// PROCESS THE CSV FILES INTO OBJECTS TO BE ASSEMBLED INTO FINAL OUTPUT
function processJson() {
  processGeneral();
  processAvailZones();
  processNetworks();
  processVMnetworks();
  processVMnetips();
  processVMnetmacs();
  processVMs();
  processTagValues();
  assembleJson();
}

// ASSEMBLE AND OUTPUT RESULTS

function assembleJson() {
  puts("\n");
  puts("Using raw JSON and assembling final ouptut JSON.");
  puts("\n");

  vnfTopoID = { "service-type": "SDN-MOBILITY",
	        "vnf-name": rawJson['vf-module-name'],
		"vnf-type": rawJson['vf-module-model-name'], 
		"generic-vnf-name": rawJson['generic-vnf-name'], 
		"generic-vnf-type": rawJson['generic-vnf-type'] };

  vnfZones = rawJson['availability-zones'];

  vnfNetworks = rawJson['networks'];

  vnfVMs = rawJson['vms'];

  vnfParams = rawJson['tag-values'];

  vnfAssignments = { "availability-zones": vnfZones,
	             "vnf-networks": vnfNetworks,
		     "vnf-vms": vnfVMs};

  vnfTopo = { "vnf-topology-identifier": vnfTopoID,
              "vnf-assignments": vnfAssignments,
              "vnf-parameters": vnfParams };

  vnfInput = {'vnf-topology-information': vnfTopo}; 

  finalJson = {"input": vnfInput};

  outputJson();
}

function outputJson() {
  puts("\n");
  puts("\n");
  puts(JSON.stringify(finalJson,null,2));
  puts("\n");
  puts("\n");
  var unixTime, fullpath_filename, filename;
  unixTime = moment().unix();
  if (platform=='portal') {
    fullpath_filename = process.cwd() + "/uploads/" + unixTime + ".vnf_worksheet.json";
    filename = unixTime + ".vnf_worksheet.json.";
  } else {
    fullpath_filename = "./output.json."+unixTime;
    filename = "output.json." + unixTime;
  }
  helpers.writeOutput(req, fullpath_filename, JSON.stringify(finalJson,null,2), callback);
  callback(null,  finalJson, filename);
}


// Gather functions that actually process data after it is all read

function processGeneral() {

  preloadVersion = getParam(csvGeneral, 'field2', 'preload-version', 'field3');
  rawJson['preload-version'] = preloadVersion;
  puts("Preload version: " + preloadVersion);

  if ( (preloadVersion!='1607') && (preloadVersion!='1610') ) {
    puts("\nError - incorrect version of preload worksheet.");
    callback('Error - incorrect version of preload worksheet.');
  }

  rawJson['vf-module-name'] = getParam(csvGeneral, 'field2', 'vf-module-name', 'field3');
  // rawJson['vf-module-type'] = getParam(csvGeneral, 'field2', 'vf-module-type', 'field3');
  try {
    rawJson['vf-module-model-name'] = getParam(csvGeneral, 'field2', 'vf-module-model-name', 'field3');
  } catch (e) {
    puts("\n\n");
    puts("ERROR ERROR ERROR ERROR ERROR\n");
    puts("Failed to find data field 'vf-module-model-name'. Maybe this preload worksheet is older?")
    puts("If on the 'general' tab there is a field called 'vf-module-type' please rename it to 'vf-module-model-name'");
    puts("\n\n");
    process.exit();
  }
  rawJson['generic-vnf-name'] = getParam(csvGeneral, 'field2', 'vnf-name', 'field3');
  rawJson['generic-vnf-type'] = getParam(csvGeneral, 'field2', 'vnf-type', 'field3');
  rawJson['request-id'] = uuid.v1();
  rawJson['source'] = "ADMINPORTAL";
  rawJson['request-action'] = "PreloadVNFRequest";
  rawJson['svc-request-id'] = uuid.v1();
  rawJson['svc-action'] = "reserve";
  puts('rawJson:');
  putd(rawJson);
  puts("\n");
}

function processAvailZones() {
  var newZones = _.map(csvZones, function(x) { return {'availability-zone': x['field2']}; } );
  rawJson['availability-zones'] = newZones;
  puts("Availability zones read:");
  putd(rawJson['availability-zones']);
  puts("\n");
}

function processNetworks() {
  var newNetworks = [];
  csvNetworks.forEach( function(network) {
    var netJson = {};
    netJson["network-role"] = network.field2;
    netJson["network-name"] = network.field3;
    netJson["network-id"] = network.field4;
    netJson["contrail-network-fqdn"] = network.field5;
    netJson["subnet-name"] = network.field6;
    netJson["subnet-id"] = network.field7;
    netJson["ipv6-subnet-name"] = network.field8;
    netJson["ipv6-subnet-id"] = network.field9;
    newNetworks.push(netJson);
    }
  );
  puts("networks:");
  putd(newNetworks);
  rawJson["networks"] = newNetworks;
}

function processVMs() {
  var newVMs = [];
  csvVMs.forEach( function(vm) {
    var vmJson = {};
    vmJson["vm-type"] = vm.field2;
    vmJson["vm-name"] = vm.field3;
    newVMs.push(vmJson);
    }
  );

  puts("VMs:");
  putd(newVMs);

  // OK, now for each type, get count and then build vm-names array of the names
  var vnfvms=[]
  vmTypes = _.uniq(_.pluck(newVMs,'vm-type'));
  vmTypes.forEach( function(vmType) {
    puts(vmType);
    var vmJson={};
    var vmThisType;
    var vmCount;
    var vmNames=[];
    var tmpNames;
    vmThisType=_.select(newVMs, 'vm-type', vmType);
    vmCount=vmThisType.length;
    vmJson["vm-type"] = vmType;
    vmJson["vm-count"] = vmCount;
    tmpNames = _.pluck(vmThisType,'vm-name');
    vmJson["vm-names"] = _.map(tmpNames, function(nam) { return {"vm-name": nam}; } );
    netroles = _.select( rawJson["vm-networks"], "vm-type", vmType );
    newnetroles=[]
    netroles.forEach( function(netrole) {
      tmpNetDetails = {};
      tmpNetDetails["network-role"] = netrole["network-role"];
      tmpNetDetails["use-dhcp"] = netrole["use-dhcp"];

      var tmpipsThisVmType=[];
      tmpipsThisVmType = _.select( rawJson["vm-net-ips"], "vm-type", vmType);
      var tmpips=[];
      tmpips = _.select( tmpipsThisVmType, "network-role", netrole["network-role"]);
      tmpipsJson = _.map(tmpips, function(ip) { return {"ip-address": ip["ip-address"]} } );
      tmpipsJson = _.reject(tmpipsJson, function(o) { return (o["ip-address"]==undefined); } );
      tmpNetDetails["network-ips"] = tmpipsJson;

      var tmpipsv6ThisVmType=[];
      tmpipsv6ThisVmType = _.select( rawJson["vm-net-ips"], "vm-type", vmType);
      var tmpipsv6=[];
      tmpipsv6 = _.select( tmpipsv6ThisVmType, "network-role", netrole["network-role"]);
      tmpipsv6Json = _.map(tmpipsv6, function(ip) { return {"ip-address-ipv6": ip["ipv6-address"]} } );
      tmpipsv6Json = _.reject(tmpipsv6Json, function(o) { return (o["ip-address-ipv6"]==undefined); } );
      tmpNetDetails["network-ips-v6"] = tmpipsv6Json;

      var tmpirpThisVmType=[];
      tmpirpThisVmType = _.select( rawJson["vm-net-ips"], "vm-type", vmType);
      var tmpirp=[];
      tmpirp = _.select( tmpirpThisVmType, "network-role", netrole["network-role"]);
      tmpirpJson = _.map(tmpirp, function(irp) { return {"interface-route-prefix-cidr": irp["interface-route-prefix"]} } );
      tmpirpJson = _.reject(tmpirpJson, function(o) { return (o["interface-route-prefix-cidr"]==undefined); } );
      tmpNetDetails["interface-route-prefixes"] = tmpirpJson;

      var tmpmacsThisVmType=[];
      tmpmacsThisVmType = _.select( rawJson["vm-net-macs"], "vm-type", vmType);
      var tmpmacs=[];
      tmpmacs = _.select( tmpmacsThisVmType, "network-role", netrole["network-role"]);
      tmpmacsJson = _.map(tmpmacs, function(mac) { return {"mac-address": mac["mac-address"]} } );
      tmpNetDetails["network-macs"] = tmpmacsJson;

      var fip='';
      fip = netrole["floating-ip"];
      fip = _.trim(fip);
      if (fip != '') {
        tmpNetDetails["floating-ip"] = netrole["floating-ip"];
      }

      var fipv6='';
      fipv6 = netrole["floating-ip-v6"];
      fipv6 = _.trim(fipv6);
      if (fipv6 != '') {
        tmpNetDetails["floating-ip-v6"] = netrole["floating-ip-v6"];
      }

      newnetroles.push(tmpNetDetails);
      }
    );
    vmJson["vm-networks"] = newnetroles;
    putd(vmJson);
    vnfvms.push(vmJson);
    }
  );
  rawJson["vms"] = vnfvms;
}

function processVMnetworks() {
  // For each VM type, for each Network role, get details like use-dhcp
  var newVMnetworks = [];
  csvVMnetworks.forEach( function(vm) {
    var newvmJson = {};
    newvmJson["vm-type"] = vm.field2;
    newvmJson["network-role"] = vm.field3;
    newvmJson["use-dhcp"] = vm.field4;
    newvmJson["floating-ip"] = vm.field5;
    newvmJson["floating-ip-v6"] = vm.field6;
    newVMnetworks.push(newvmJson);
    }
  );
  rawJson["vm-networks"] = newVMnetworks;
  puts("rawJson for vm-networks...");
  putd( rawJson["vm-networks"] );
}


function processVMnetips() {
  // For each VM type, for each network role, get the set of network IPs
  puts("Processing VM-net-ips");
  var newVMnetips = [];
  csvVMnetworkIPs.forEach( function(vm) {
    var newvmnetipsJson = {};
    newvmnetipsJson["vm-type"] = vm.field2;
    newvmnetipsJson["network-role"] = vm.field3;
    if (_.trim(vm.field4)!="") { 
      newvmnetipsJson["ip-address"] = vm.field4;
    }
    if (_.trim(vm.field5)!="") { 
    newvmnetipsJson["ipv6-address"] = vm.field5;
    }
    if (_.trim(vm.field6)!="") { 
    newvmnetipsJson["interface-route-prefix"] = vm.field6;
    }
    newVMnetips.push(newvmnetipsJson);
    }
  );
  rawJson["vm-net-ips"] = newVMnetips;
  puts("rawJson for vm-net-ips");
  putd(rawJson["vm-net-ips"]);
}

function processVMnetmacs() {
  // For each VM type, for each network role, get the set of MACs 
  puts("Processing VM-net-macs");
  var newVMnetmacs = [];
  csvVMnetworkMACs.forEach( function(vm) {
    var newvmnetmacsJson = {};
    newvmnetmacsJson["vm-type"] = vm.field2;
    newvmnetmacsJson["network-role"] = vm.field3;
    newvmnetmacsJson["mac-address"] = vm.field4;
    newVMnetmacs.push(newvmnetmacsJson);
    }
  );
  rawJson["vm-net-macs"] = newVMnetmacs;
  puts("rawJson for vm-net-macs");
  putd(rawJson["vm-net-macs"]);
}

function processTagValues() {
  var newTagValues = _.map(csvTagValues, function(x) { return {'vnf-parameter-name': x['field2'], 
	  'vnf-parameter-value': x['field3']}; } );
  rawJson['tag-values'] = newTagValues;
  puts("Tag-values read:");
  putd(rawJson['tag-values']);
  puts("\n");
}