blob: 2fd852559fca893aedd86fb43c67d9cf7d89b4a6 [file] [log] [blame]
Skip Wonnell2c977e22018-03-01 08:30:15 -06001/*
2============LICENSE_START==========================================
3===================================================================
4Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5===================================================================
6
7Unless otherwise specified, all software contained herein is licensed
8under the Apache License, Version 2.0 (the License);
9you may not use this software except in compliance with the License.
10You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14Unless required by applicable law or agreed to in writing, software
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19
Skip Wonnell2c977e22018-03-01 08:30:15 -060020============LICENSE_END============================================
21*/
22
23import { Component, OnInit } from '@angular/core';
24
25import { saveAs } from 'file-saver';
26import { Location } from '@angular/common';
27import { ActivatedRoute, Router } from '@angular/router';
28import { NotificationService } from '.././shared/services/notification.service';
29import { ParamShareService } from '.././shared/services/paramShare.service';
30import { MappingEditorService } from '.././shared/services/mapping-editor.service';
31import { NotificationsService } from 'angular2-notifications';
32import { HttpUtilService } from '.././shared/services/httpUtil/http-util.service';
33import 'rxjs/add/observable/interval';
34import { Observable } from 'rxjs/Observable';
35import { environment } from '../.././environments/environment';
36import { UtilityService } from '.././shared/services/utilityService/utility.service';
37import 'rxjs/add/operator/map';
38import * as XLSX from 'xlsx';
39import { NgProgress } from 'ngx-progressbar';
40
41
42let YAML = require('yamljs');
43
44type AOA = Array<Array<any>>;
45declare var $: any;
46
47@Component({ selector: 'test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] })
48export class TestComponent implements OnInit {
49 public displayParamObjects;
50 options = {
51 timeOut: 1000,
52 showProgressBar: true,
53 pauseOnHover: true,
54 clickToClose: true,
55 maxLength: 200
56 };
57 public action: any;
58 public vnfId: any;
59 public item: any = {};
60
61 public vnfType: any;
62 vnfcType: any;
63 protocol: any;
64 mode: any = 'NORMAL';
65 force: any = 'True';
66 ttl: any;
67 public formattedNameValuePairs = {};
68 public requestId = '';
69 public enableBrowse: boolean = true;
70 public enableSpinner: boolean = false;
71 host: any;
72 public refNameObj = {};
73
74 public artifactName;
75 public type;
76 public transactions = '';
77 public uploadFileName;
78
79 public payload = {};
80 public lastvmJson = {};
81 public vmPayload = [];
82 public subPayload = {};
83 public vmJson = {};
84 public vnfcJson = {};
85 public flag = 1;
86 public oldListName1 = '';
87 public actionIdentifiers = {};
88 public apiRequest = '';
89 public apiResponse = '';
90 public statusResponse;
91 public outputTimeStamp;
92 public status;
93 public statusReason;
94 public errorResponse;
95 public timer;
96 public subscribe;
97 public enableTestButton: boolean = false;
98 public enableAbort: boolean = false;
99 public showStatusResponseDiv: boolean = false;
100 public enablePollButton: boolean = true;
101 public pollCounter = 0;
102 public enableCounterDiv: boolean = false;
sj108sf1e61ce2018-03-13 20:29:58 +0530103 public enableDownload: boolean = false;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600104
105 constructor(private location: Location, private activeRoutes: ActivatedRoute, private notificationService: NotificationService, private nService: NotificationsService, private router: Router, private paramShareService: ParamShareService, private mappingEditorService: MappingEditorService, private httpUtil: HttpUtilService,
106 private utiltiy: UtilityService, private ngProgress: NgProgress) {
107
108 }
109
110 ngOnInit() {
111
112
113 }
114
Skip Wonnell2c977e22018-03-01 08:30:15 -0600115
116 public download() {
117 if (this.apiRequest) {
118 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request';
119 var theJSON = this.apiRequest;
120 if (fileName != null || fileName != '') {
121 var blob = new Blob([theJSON], {
122 type: 'text/json'
123 });
124 saveAs(blob, fileName);
125 }
126 }
127 else {
128 this.nService.error('Error', 'Please upload spreadsheet to download the request and response');
129 }
130
131 if (this.apiResponse) {
132 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response';
133 var theJSON = this.apiResponse;
134 if (fileName != null || fileName != '') {
135 var blob = new Blob([theJSON], {
136 type: 'text/json'
137 });
138 saveAs(blob, fileName);
139 }
140 }
141
142 }
143
144
145 public abortTest() {
146 //this.apiResponse = "";
147 this.enableBrowse = true;
148 this.enableTestButton = true;
149 this.enablePollButton = true;
sj108s90174642018-03-15 20:26:47 +0530150 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600151
152 }
153
154
Skip Wonnell2c977e22018-03-01 08:30:15 -0600155 excelBrowseOption() {
156 $('#excelInputFile').trigger('click');
157 }
158
159
160 upload(evt: any) {
161 /* wire up file reader */
162 $('#filesparam').trigger('click');
163 const target: DataTransfer = <DataTransfer>(evt.target);
sj108sf1e61ce2018-03-13 20:29:58 +0530164 this.pollCounter = 0;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600165 this.uploadFileName = evt.target.files[0].name;
166 var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1);
167
168 if (target.files.length != 1) {
169 throw new Error('Cannot upload multiple files on the entry');
170 }
171 if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') {
172 const reader = new FileReader();
173 reader.onload = (e: any) => {
174 /* read workbook */
175 const bstr = e.target.result;
176 const wb = XLSX.read(bstr, { type: 'binary' });
177 /* grab first sheet */
178 const wsname = wb.SheetNames[0];
179 const ws = wb.Sheets[wsname];
180
181 /* save data */
182 this.requestId = ''
183 this.enableTestButton = true;
184 this.enableAbort = true;
185 this.enablePollButton = true;
186
187 if (this.subscribe && this.subscribe != undefined) {
188 this.enableCounterDiv = false;
189 this.subscribe.unsubscribe();
190 }
191 this.apiRequest = '';
192 this.apiResponse = '';
193 this.showStatusResponseDiv = false;
194 this.errorResponse = '';
195 this.statusResponse = '';
sj108sb694d952018-03-21 21:25:39 +0530196 this.enableDownload = true;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600197 let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false })));
sj108sf1e61ce2018-03-13 20:29:58 +0530198
Skip Wonnell2c977e22018-03-01 08:30:15 -0600199
200
201 console.log('Array data ==' + arrData[0]);
202 this.vmPayload = [];
203 this.subPayload = {};
204 this.vmJson = {};
205 this.flag = 1;
206 this.payload = {};
207 this.oldListName1 = '';
208 this.actionIdentifiers = {};
sj108sf1e61ce2018-03-13 20:29:58 +0530209 // Refactor
210 this.payload = this.processUploadedFile(arrData);
211 this.uploadedFileResult();
sj108sb694d952018-03-21 21:25:39 +0530212 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600213
214 reader.readAsBinaryString(target.files[0]);
sj108sb694d952018-03-21 21:25:39 +0530215
sj108sf1e61ce2018-03-13 20:29:58 +0530216
Skip Wonnell2c977e22018-03-01 08:30:15 -0600217 }
218 else {
219 this.nService.error('Error', 'Incorrect spreadsheet uploaded');
220 this.flag = 1;
221 this.oldListName1 = '';
222 this.vmJson = {};
223 this.vnfcJson = {};
224 this.subPayload = {};
225 this.vmPayload = [];
226 this.payload = {};
227 this.action = '';
228 this.actionIdentifiers = {};
229 this.apiRequest = '';
230 this.apiResponse = '';
231 this.enableCounterDiv = false;
sj108sf1e61ce2018-03-13 20:29:58 +0530232 this.enableAbort = false;
233 this.enableTestButton = false;
234 this.enableDownload = false;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600235 }
236 }
237
sj108sb694d952018-03-21 21:25:39 +0530238 processUploadedFile(arrData) {
sj108sf1e61ce2018-03-13 20:29:58 +0530239 let tempPayload = {};
240 for (var i = 0; i < arrData.length; i++) {
241 var element = arrData[i];
242 if (element['TagName'] === 'action') {
243 this.action = element['Value'];
244 }
245 if (element['List Name'] === 'action-identifiers') {
246 this.vnfId = element['Value'];
247 var key = element['TagName'];
248 var value = element['Value'];
249 if (key && value) {
250 this.actionIdentifiers[key] = value;
251
252 }
253 }
sj108sb694d952018-03-21 21:25:39 +0530254
sj108sf1e61ce2018-03-13 20:29:58 +0530255 if (element['List Name'] === 'payload') {
256 var listName1 = element['List Name_1'];
257 var listName2 = element['List Name_2'];
258 var listName3 = element['List Name_3'];
259 var key = element['TagName'];
260 var value = element['Value'];
261 if (listName1) {
262 if (this.oldListName1 == '' || (listName1 === this.oldListName1)) {
263 this.constructTestPayload(listName2, listName3, key, value);
264 tempPayload[listName1] = this.subPayload;
265 }
266 else {
267 this.subPayload = {};
268 this.constructTestPayload(listName2, listName3, key, value);
269 tempPayload[listName1] = this.subPayload;
270 }
271 this.oldListName1 = listName1;
272 }
273 else {
274 tempPayload[key] = value;
275 }
276 }
277 }
278
279 return tempPayload;
280 }
281
sj108sb694d952018-03-21 21:25:39 +0530282 uploadedFileResult() {
sj108sf1e61ce2018-03-13 20:29:58 +0530283 if (this.action && this.actionIdentifiers['vnf-id']) {
284 this.nService.success('Success', 'SpreadSheet uploaded successfully');
285 }
286 else {
287 this.flag = 1;
288 this.oldListName1 = '';
289 this.vmJson = {};
290 this.vnfcJson = {};
291 this.subPayload = {};
292 this.vmPayload = [];
293 this.payload = {};
294 this.action = '';
295 this.actionIdentifiers = {};
296 this.apiRequest = '';
297 this.apiResponse = '';
298 this.enableCounterDiv = false;
299 this.enableAbort = false;
300 this.enableTestButton = false;
301 this.enableDownload = false;
302 this.nService.error("Error", "Please check the contents of the file uploaded")
303 }
304 }
305
306
307
Skip Wonnell2c977e22018-03-01 08:30:15 -0600308 constructTestPayload(listName2, listName3, key, value) {
309 if (listName2 == undefined && listName3 == undefined) {
310 this.subPayload[key] = value;
311 }
312 if (listName2) {
313
314 if (!listName3) {
315
316 //vmPayload.push(vmJson)
317 this.vmJson = {};
318 this.vnfcJson = {};
319 this.vmJson[key] = value;
320 this.flag = 0;
321 }
322 else {
323 this.vnfcJson[key] = value;
324 this.vmJson['vnfc'] = this.vnfcJson;
325 this.flag = 1;
326 }
327 if (this.vmJson) this.lastvmJson = this.vmJson;
328 if (this.flag == 0) {
329 this.vmPayload.push(this.lastvmJson);
330 if (this.vmPayload) this.subPayload['vm'] = this.vmPayload;
331 }
332 }
333 }
334
335 constructRequest() {
336 console.log('payload==' + JSON.stringify(this.payload));
337 let timeStamp = new Date().toISOString();
338 console.log('timestamp==' + timeStamp);
339 let reqId;
340 this.requestId = reqId = new Date().getTime().toString();
341 let data = {
342 'input': {
343 'common-header': {
344 'timestamp': timeStamp,
345 'api-ver': '2.00',
346 'originator-id': 'CDT',
347 'request-id': this.requestId,
348 'sub-request-id': this.requestId,
349 'flags': {
350 'mode': 'NORMAL',
351 'force': 'TRUE',
352 'ttl': 3600
353 }
354 },
355 'action': this.action,
356 'action-identifiers': this.actionIdentifiers,
357 'payload': JSON.stringify(this.payload)
358 }
359 };
360
361 return data;
362 }
363
364 testVnf() {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600365 this.enableBrowse = false;
366 this.enableTestButton = false;
367 this.enablePollButton = false;
368 this.timer = Observable.interval(10000);
369 this.subscribe = this.timer.subscribe((t) => this.pollTestStatus());
370 //console.log('full payload==' + JSON.stringify(this.apiRequest));
371 this.ngProgress.start();
372 this.apiRequest = JSON.stringify(this.constructRequest());
373 this.httpUtil.post(
374 {
375 url: environment.testVnf + this.getUrlEndPoint(this.action.toLowerCase()), data: this.apiRequest
376 })
377 .subscribe(resp => {
378 this.apiResponse = JSON.stringify(resp);
379 this.enableBrowse = true;
380 this.enableTestButton = true;
381 this.ngProgress.done();
382 },
383 error => {
384 this.nService.error('Error', 'Error in connecting to APPC Server');
385 this.enableBrowse = true;
386 this.enableTestButton = true;
387 this.enablePollButton = true;
388 this.enableCounterDiv = false;
sj108s90174642018-03-15 20:26:47 +0530389 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600390
391 });
392
393 setTimeout(() => {
394 this.ngProgress.done();
395 }, 3500);
396 }
397
398
399 pollTestStatus() {
400 if (this.requestId && this.actionIdentifiers['vnf-id']) {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600401 let timeStamp = new Date().toISOString();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600402 let reqId = new Date().getTime().toString();
403 let data = {
404 'input': {
405 'common-header': {
406 'timestamp': timeStamp,
407 'api-ver': '2.00',
408 'originator-id': 'CDT',
409 'request-id': reqId,
410 'sub-request-id': reqId,
411 'flags': {
412 'mode': 'NORMAL',
413 'force': 'TRUE',
414 'ttl': 3600
415 }
416 },
417 'action': 'ActionStatus',
418 'action-identifiers': this.actionIdentifiers,
419 'payload': '{"request-id":' + this.requestId + '}'
420 }
421 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600422 this.httpUtil.post(
423 {
424 url: environment.checkTestStatus, data: data
425
426 })
427 .subscribe(resp => {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600428 this.statusResponse = JSON.stringify(resp);
429 var status = ''
430 var statusReason = ''
431 this.enableCounterDiv = true;
432 this.pollCounter++;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600433 if (resp.output) var timeStamp = resp.output['common-header'].timestamp;
434 if (resp.output.payload) {
435 var payload = resp.output.payload.replace(/\\/g, "")
436 try {
437 payload = JSON.parse(payload)
438 status = payload['status'];
439 statusReason = payload['status-reason'];
440 }
441 catch (err) {
442 console.log("error" + err)
443 }
444 }
445 if (timeStamp && status && statusReason) {
446 this.showStatusResponseDiv = true;
447 this.outputTimeStamp = timeStamp;
448 this.status = status;
449 this.statusReason = statusReason;
sj108sf1e61ce2018-03-13 20:29:58 +0530450 if (status.toUpperCase() === 'SUCCESS' || status.toUpperCase() === 'SUCCESSFUL') {
sj108s90174642018-03-15 20:26:47 +0530451 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600452 this.enablePollButton = true;
453 }
454 if (status.toUpperCase() === 'FAILED') {
sj108s90174642018-03-15 20:26:47 +0530455 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600456 this.enablePollButton = true;
457 }
458 }
459 else {
460 this.showStatusResponseDiv = false;
sj108sb694d952018-03-21 21:25:39 +0530461 if (this.subscribe && this.subscribe != undefined) {
462 this.subscribe.unsubscribe();
463 this.enablePollButton = true;
464 }
465
Skip Wonnell2c977e22018-03-01 08:30:15 -0600466 }
467
Skip Wonnell2c977e22018-03-01 08:30:15 -0600468 },
469 error => {
470 this.statusResponse = null;
471 this.showStatusResponseDiv = false;
472 this.errorResponse = 'Error Connecting to APPC server';
473 this.enableCounterDiv = false;
sj108sb694d952018-03-21 21:25:39 +0530474 if (this.subscribe && this.subscribe != undefined) {
475 this.subscribe.unsubscribe();
476 this.enablePollButton = true;
477 }
Skip Wonnell2c977e22018-03-01 08:30:15 -0600478 });
Skip Wonnell2c977e22018-03-01 08:30:15 -0600479 }
480 else {
481 this.nService.error("Error", "Please enter vnf Id & request Id");
482 }
sj108sb694d952018-03-21 21:25:39 +0530483
Skip Wonnell2c977e22018-03-01 08:30:15 -0600484 }
485
486 getUrlEndPoint(action) {
487 switch (action) {
488 case 'configmodify':
489 return 'config-modify';
490 case 'configbackup':
491 return 'config-backup';
492 case 'configrestore':
493 return 'config-restore';
494 case 'healthcheck':
495 return 'health-check';
496 case 'quiescetraffic':
497 return 'quiesce-traffic';
498 case 'resumetraffic':
499 return 'resume-traffic';
500 case 'startapplication':
501 return 'start-application';
502 case 'stopapplication':
503 return 'stop-application';
504 case 'upgradebackout':
505 return 'upgrade-backout';
506 case 'upgradepostcheck':
507 return 'upgrade-post-check';
508 case 'upgradeprecheck':
509 return 'upgrade-pre-check';
510 case 'upgradesoftware':
511 return 'upgrade-software';
512 default:
513 return action.toLowerCase();
514 }
515
516 }
517
518}