2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
5 ===================================================================
6 Copyright (C) 2018 IBM.
7 ===================================================================
8 Unless otherwise specified, all software contained herein is licensed
9 under the Apache License, Version 2.0 (the License);
10 you may not use this software except in compliance with the License.
11 You may obtain a copy of the License at
13 http://www.apache.org/licenses/LICENSE-2.0
15 Unless required by applicable law or agreed to in writing, software
16 distributed under the License is distributed on an "AS IS" BASIS,
17 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 See the License for the specific language governing permissions and
19 limitations under the License.
20 ============LICENSE_END============================================
23 import { Component, OnInit } from '@angular/core';
24 import { NgxSpinnerService } from 'ngx-spinner';
25 import { saveAs } from 'file-saver';
26 import { Location } from '@angular/common';
27 import { ActivatedRoute, Router } from '@angular/router';
28 import { ParamShareService } from '.././shared/services/paramShare.service';
29 import { MappingEditorService } from '.././shared/services/mapping-editor.service';
30 import { NotificationsService } from 'angular2-notifications';
31 import { HttpUtilService } from '.././shared/services/httpUtil/http-util.service';
32 import 'rxjs/add/observable/interval';
33 import { Observable } from 'rxjs/Observable';
34 import { environment } from '../.././environments/environment';
35 import { UtilityService } from '.././shared/services/utilityService/utility.service';
36 import 'rxjs/add/operator/map';
37 import * as XLSX from 'xlsx';
38 import { NgProgress } from 'ngx-progressbar';
41 let YAML = require('yamljs');
43 type AOA = Array<Array<any>>;
46 @Component({ selector: 'test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] })
47 export class TestComponent implements OnInit {
48 public displayParamObjects;
51 showProgressBar: true,
58 public item: any = {};
66 public formattedNameValuePairs = {};
67 public requestId = '';
68 public enableBrowse: boolean = true;
69 public enableSpinner: boolean = false;
71 public refNameObj = {};
75 public transactions = '';
76 public uploadFileName;
79 public lastvmJson = {};
80 public vmPayload = [];
81 public subPayload = {};
85 public oldListName1 = '';
86 public actionIdentifiers = {};
87 public apiRequest = '';
88 public apiResponse = '';
89 public statusResponse;
90 public outputTimeStamp;
96 public enableTestButton: boolean = false;
97 public enableAbort: boolean = false;
98 public showStatusResponseDiv: boolean = false;
99 public enablePollButton: boolean = true;
100 public pollCounter = 0;
101 public enableCounterDiv: boolean = false;
102 public enableDownload: boolean = false;
103 private userId = localStorage['userId'];
105 private location: Location,
106 private activeRoutes: ActivatedRoute,
107 private nService: NotificationsService,
108 private router: Router,
109 private paramShareService: ParamShareService,
110 private mappingEditorService: MappingEditorService,
111 private httpUtil: HttpUtilService,
112 private utiltiy: UtilityService,
113 private ngProgress: NgProgress,
114 private spinner: NgxSpinnerService
126 if (this.apiRequest) {
127 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request';
128 var theJSON = this.apiRequest;
129 if (fileName != null || fileName != '') {
130 var blob = new Blob([theJSON], {
133 saveAs(blob, fileName);
137 this.nService.error('Error', 'Please upload spreadsheet to download the request and response');
140 if (this.apiResponse) {
141 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response';
142 var theJSON = this.apiResponse;
143 if (fileName != null || fileName != '') {
144 var blob = new Blob([theJSON], {
147 saveAs(blob, fileName);
155 //this.apiResponse = "";
156 this.enableBrowse = true;
157 this.enableTestButton = true;
158 this.enablePollButton = true;
159 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
160 this.nService.info("Information", "Test has been abandoned and polling stopped");
164 excelBrowseOption() {
165 $('#excelInputFile').trigger('click');
170 /* wire up file reader */
171 $('#filesparam').trigger('click');
172 const target: DataTransfer = <DataTransfer>(evt.target);
173 this.pollCounter = 0;
174 this.uploadFileName = evt.target.files[0].name;
175 var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1);
177 if (target.files.length != 1) {
178 throw new Error('Cannot upload multiple files on the entry');
180 if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') {
182 const reader = new FileReader();
183 reader.onload = (e: any) => {
185 const bstr = e.target.result;
186 const wb = XLSX.read(bstr, { type: 'binary' });
187 /* grab first sheet */
188 const wsname = wb.SheetNames[0];
189 const ws = wb.Sheets[wsname];
193 this.enableTestButton = true;
194 this.enableAbort = true;
195 this.enablePollButton = true;
197 if (this.subscribe && this.subscribe != undefined) {
198 this.enableCounterDiv = false;
199 this.subscribe.unsubscribe();
201 this.apiRequest = '';
202 this.apiResponse = '';
203 this.showStatusResponseDiv = false;
204 this.errorResponse = '';
205 this.statusResponse = '';
206 this.enableDownload = true;
207 let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false })));
212 this.subPayload = {};
216 this.oldListName1 = '';
217 this.actionIdentifiers = {};
219 this.payload = this.processUploadedFile(arrData);
220 this.uploadedFileResult();
223 reader.readAsBinaryString(target.files[0]);
225 /** spinner ends after 2.5 seconds */
231 this.nService.error('Error', 'Incorrect spreadsheet uploaded');
232 this.setValuesOnFileUploadFailure();
236 processUploadedFile(arrData) {
237 let tempPayload = {};
238 for (var i = 0; i < arrData.length; i++) {
239 var element = arrData[i];
240 if (element['TagName'] === 'action') {
241 this.action = element['Value'];
243 if (element['List Name'] === 'action-identifiers') {
244 this.vnfId = element['Value'];
245 var key = element['TagName'];
246 var value = element['Value'];
248 this.actionIdentifiers[key] = value;
253 if (element['List Name'] === 'payload') {
254 var listName1 = element['List Name_1'];
255 var listName2 = element['List Name_2'];
256 var listName3 = element['List Name_3'];
257 var key = element['TagName'];
258 var value = element['Value'];
260 if (this.oldListName1 == '' || (listName1 === this.oldListName1)) {
261 this.constructTestPayload(listName2, listName3, key, value);
262 tempPayload[listName1] = this.subPayload;
265 this.subPayload = {};
266 this.constructTestPayload(listName2, listName3, key, value);
267 tempPayload[listName1] = this.subPayload;
269 this.oldListName1 = listName1;
272 tempPayload[key] = value;
280 uploadedFileResult() {
281 if (this.action && this.actionIdentifiers['vnf-id']) {
282 this.nService.success('Success', 'SpreadSheet uploaded successfully');
285 this.setValuesOnFileUploadFailure();
286 this.nService.error("Error", "Please check the contents of the file uploaded")
292 constructTestPayload(listName2, listName3, key, value) {
293 if (listName2 == undefined && listName3 == undefined) {
294 this.subPayload[key] = value;
300 //vmPayload.push(vmJson)
303 this.vmJson[key] = value;
307 this.vnfcJson[key] = value;
308 this.vmJson['vnfc'] = this.vnfcJson;
311 if (this.vmJson) this.lastvmJson = this.vmJson;
312 if (this.flag == 0) {
313 this.vmPayload.push(this.lastvmJson);
314 if (this.vmPayload) this.subPayload['vm'] = this.vmPayload;
320 let timeStamp = new Date().toISOString();
322 this.requestId = reqId = new Date().getTime().toString();
326 'timestamp': timeStamp,
328 'originator-id': this.userId,
329 'request-id': this.requestId,
330 'sub-request-id': this.requestId,
337 'action': this.action,
338 'action-identifiers': this.actionIdentifiers,
339 'payload': JSON.stringify(this.payload)
347 this.enableBrowse = false;
348 this.enableTestButton = false;
349 this.enablePollButton = false;
350 this.timer = Observable.interval(10000);
351 this.subscribe = this.timer.subscribe((t) => this.pollTestStatus());
352 this.ngProgress.start();
353 this.apiRequest = JSON.stringify(this.constructRequest());
357 url: environment.testVnf + "?urlAction=" + this.getUrlEndPoint(this.action.toLowerCase()),
358 data: this.apiRequest
361 this.apiResponse = JSON.stringify(resp);
362 this.enableBrowse = true;
363 this.enableTestButton = true;
364 this.ngProgress.done();
367 this.nService.error('Error', 'Error in connecting to APPC Server');
368 this.enableBrowse = true;
369 this.enableTestButton = true;
370 this.enablePollButton = true;
371 this.enableCounterDiv = false;
372 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
377 this.ngProgress.done();
383 if (this.requestId && this.actionIdentifiers['vnf-id']) {
384 let timeStamp = new Date().toISOString();
385 let reqId = new Date().getTime().toString();
389 'timestamp': timeStamp,
391 'originator-id': this.userId,
393 'sub-request-id': reqId,
400 'action': 'ActionStatus',
401 'action-identifiers': {
402 'vnf-id': this.actionIdentifiers['vnf-id']
404 'payload': '{"request-id":' + this.requestId + '}'
409 url: environment.checkTestStatus, data: data
413 this.statusResponse = JSON.stringify(resp);
415 var statusReason = ''
416 this.enableCounterDiv = true;
418 if (resp.output) var timeStamp = resp.output['common-header'].timestamp;
419 if (resp.output.payload) {
420 var payload = resp.output.payload.replace(/\\/g, "")
422 payload = JSON.parse(payload)
423 status = payload['status'];
424 statusReason = payload['status-reason'];
427 console.log("error" + err)
430 if (timeStamp && status && statusReason) {
431 this.showStatusResponseDiv = true;
432 this.outputTimeStamp = timeStamp;
433 this.status = status;
434 this.statusReason = statusReason;
435 if (status.toUpperCase() === 'SUCCESS' || status.toUpperCase() === 'SUCCESSFUL') {
436 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
437 this.enablePollButton = true;
439 if (status.toUpperCase() === 'FAILED') {
440 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
441 this.enablePollButton = true;
445 this.showStatusResponseDiv = false;
446 if (this.subscribe && this.subscribe != undefined) {
447 this.subscribe.unsubscribe();
448 this.enablePollButton = true;
455 this.statusResponse = null;
456 this.showStatusResponseDiv = false;
457 this.errorResponse = 'Error Connecting to APPC server';
458 this.enableCounterDiv = false;
459 if (this.subscribe && this.subscribe != undefined) {
460 this.subscribe.unsubscribe();
461 this.enablePollButton = true;
466 this.nService.error("Error", "Please enter vnf Id & request Id");
471 getUrlEndPoint(action) {
474 return 'config-modify';
476 return 'config-backup';
477 case 'configrestore':
478 return 'config-restore';
480 return 'health-check';
481 case 'quiescetraffic':
482 return 'quiesce-traffic';
483 case 'resumetraffic':
484 return 'resume-traffic';
485 case 'distributetraffic':
486 return 'distribute-traffic';
487 case 'startapplication':
488 return 'start-application';
489 case 'stopapplication':
490 return 'stop-application';
491 case 'upgradebackout':
492 return 'upgrade-backout';
493 case 'upgradepostcheck':
494 return 'upgrade-post-check';
495 case 'upgradeprecheck':
496 return 'upgrade-pre-check';
497 case 'upgradesoftware':
498 return 'upgrade-software';
499 case 'upgradebackup':
500 return 'upgrade-backup';
502 return 'attach-volume';
504 return 'detach-volume';
506 return action.toLowerCase();
511 setValuesOnFileUploadFailure() {
513 this.oldListName1 = '';
516 this.subPayload = {};
520 this.actionIdentifiers = {};
521 this.apiRequest = '';
522 this.apiResponse = '';
523 this.enableCounterDiv = false;
524 this.enableAbort = false;
525 this.enableTestButton = false;
526 this.enableDownload = false;