blob: 52b5d8a0101689df3910476ff291def1253c02dc [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===================================================================
Sandeep J77ebde82018-07-20 15:56:31 +05306Copyright (C) 2018 IBM.
7===================================================================
Skip Wonnell2c977e22018-03-01 08:30:15 -06008Unless otherwise specified, all software contained herein is licensed
9under the Apache License, Version 2.0 (the License);
10you may not use this software except in compliance with the License.
11You may obtain a copy of the License at
12
13 http://www.apache.org/licenses/LICENSE-2.0
14
15Unless required by applicable law or agreed to in writing, software
16distributed under the License is distributed on an "AS IS" BASIS,
17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18See the License for the specific language governing permissions and
19limitations under the License.
Skip Wonnell2c977e22018-03-01 08:30:15 -060020============LICENSE_END============================================
21*/
22
23import { Component, OnInit } from '@angular/core';
Sandeep J77ebde82018-07-20 15:56:31 +053024import { NgxSpinnerService } from 'ngx-spinner';
Skip Wonnell2c977e22018-03-01 08:30:15 -060025import { 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;
Sandeep J77ebde82018-07-20 15:56:31 +053094 public errorResponse;
Skip Wonnell2c977e22018-03-01 08:30:15 -060095 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;
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530104 private userId = localStorage['userId'];
Sandeep J77ebde82018-07-20 15:56:31 +0530105 constructor (
106 private location: Location,
107 private activeRoutes: ActivatedRoute,
108 private notificationService: NotificationService,
109 private nService: NotificationsService,
110 private router: Router,
111 private paramShareService: ParamShareService,
112 private mappingEditorService: MappingEditorService,
113 private httpUtil: HttpUtilService,
114 private utiltiy: UtilityService,
115 private ngProgress: NgProgress,
116 private spinner: NgxSpinnerService
117 ) {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600118
119 }
120
121 ngOnInit() {
122
123
124 }
125
Skip Wonnell2c977e22018-03-01 08:30:15 -0600126
127 public download() {
128 if (this.apiRequest) {
129 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request';
130 var theJSON = this.apiRequest;
131 if (fileName != null || fileName != '') {
132 var blob = new Blob([theJSON], {
133 type: 'text/json'
134 });
135 saveAs(blob, fileName);
136 }
137 }
138 else {
139 this.nService.error('Error', 'Please upload spreadsheet to download the request and response');
140 }
141
142 if (this.apiResponse) {
143 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response';
144 var theJSON = this.apiResponse;
145 if (fileName != null || fileName != '') {
146 var blob = new Blob([theJSON], {
147 type: 'text/json'
148 });
149 saveAs(blob, fileName);
150 }
151 }
152
153 }
154
155
156 public abortTest() {
157 //this.apiResponse = "";
158 this.enableBrowse = true;
159 this.enableTestButton = true;
160 this.enablePollButton = true;
sj108s90174642018-03-15 20:26:47 +0530161 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530162 this.nService.info("Information", "Test has been abandoned and polling stopped");
Skip Wonnell2c977e22018-03-01 08:30:15 -0600163 }
164
165
Skip Wonnell2c977e22018-03-01 08:30:15 -0600166 excelBrowseOption() {
167 $('#excelInputFile').trigger('click');
168 }
169
170
171 upload(evt: any) {
172 /* wire up file reader */
173 $('#filesparam').trigger('click');
174 const target: DataTransfer = <DataTransfer>(evt.target);
sj108sf1e61ce2018-03-13 20:29:58 +0530175 this.pollCounter = 0;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600176 this.uploadFileName = evt.target.files[0].name;
177 var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1);
178
179 if (target.files.length != 1) {
180 throw new Error('Cannot upload multiple files on the entry');
181 }
182 if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') {
Sandeep J77ebde82018-07-20 15:56:31 +0530183 this.spinner.show();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600184 const reader = new FileReader();
185 reader.onload = (e: any) => {
186 /* read workbook */
187 const bstr = e.target.result;
188 const wb = XLSX.read(bstr, { type: 'binary' });
189 /* grab first sheet */
190 const wsname = wb.SheetNames[0];
191 const ws = wb.Sheets[wsname];
192
193 /* save data */
194 this.requestId = ''
195 this.enableTestButton = true;
196 this.enableAbort = true;
197 this.enablePollButton = true;
198
199 if (this.subscribe && this.subscribe != undefined) {
200 this.enableCounterDiv = false;
201 this.subscribe.unsubscribe();
202 }
203 this.apiRequest = '';
204 this.apiResponse = '';
205 this.showStatusResponseDiv = false;
206 this.errorResponse = '';
207 this.statusResponse = '';
sj108sb694d952018-03-21 21:25:39 +0530208 this.enableDownload = true;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600209 let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false })));
sj108sf1e61ce2018-03-13 20:29:58 +0530210
Skip Wonnell2c977e22018-03-01 08:30:15 -0600211
212
Skip Wonnell2c977e22018-03-01 08:30:15 -0600213 this.vmPayload = [];
214 this.subPayload = {};
215 this.vmJson = {};
216 this.flag = 1;
217 this.payload = {};
218 this.oldListName1 = '';
219 this.actionIdentifiers = {};
sj108sf1e61ce2018-03-13 20:29:58 +0530220 // Refactor
221 this.payload = this.processUploadedFile(arrData);
222 this.uploadedFileResult();
sj108sb694d952018-03-21 21:25:39 +0530223 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600224
225 reader.readAsBinaryString(target.files[0]);
Sandeep J77ebde82018-07-20 15:56:31 +0530226 setTimeout(() => {
227 /** spinner ends after 2.5 seconds */
228 this.spinner.hide();
229 }, 2500);
sj108sf1e61ce2018-03-13 20:29:58 +0530230
Skip Wonnell2c977e22018-03-01 08:30:15 -0600231 }
232 else {
233 this.nService.error('Error', 'Incorrect spreadsheet uploaded');
IBM602-PC0F1E3C\Arundathi675e8b82018-07-16 14:30:26 +0530234 this.setValuesOnFileUploadFailure();
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 {
IBM602-PC0F1E3C\Arundathi675e8b82018-07-16 14:30:26 +0530287 this.setValuesOnFileUploadFailure();
sj108sf1e61ce2018-03-13 20:29:58 +0530288 this.nService.error("Error", "Please check the contents of the file uploaded")
289 }
290 }
291
292
293
Skip Wonnell2c977e22018-03-01 08:30:15 -0600294 constructTestPayload(listName2, listName3, key, value) {
295 if (listName2 == undefined && listName3 == undefined) {
296 this.subPayload[key] = value;
297 }
298 if (listName2) {
299
300 if (!listName3) {
301
302 //vmPayload.push(vmJson)
303 this.vmJson = {};
304 this.vnfcJson = {};
305 this.vmJson[key] = value;
306 this.flag = 0;
307 }
308 else {
309 this.vnfcJson[key] = value;
310 this.vmJson['vnfc'] = this.vnfcJson;
311 this.flag = 1;
312 }
313 if (this.vmJson) this.lastvmJson = this.vmJson;
314 if (this.flag == 0) {
315 this.vmPayload.push(this.lastvmJson);
316 if (this.vmPayload) this.subPayload['vm'] = this.vmPayload;
317 }
318 }
319 }
320
321 constructRequest() {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600322 let timeStamp = new Date().toISOString();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600323 let reqId;
324 this.requestId = reqId = new Date().getTime().toString();
325 let data = {
326 'input': {
327 'common-header': {
328 'timestamp': timeStamp,
329 'api-ver': '2.00',
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530330 'originator-id': this.userId,
Skip Wonnell2c977e22018-03-01 08:30:15 -0600331 'request-id': this.requestId,
332 'sub-request-id': this.requestId,
333 'flags': {
334 'mode': 'NORMAL',
335 'force': 'TRUE',
336 'ttl': 3600
337 }
338 },
339 'action': this.action,
340 'action-identifiers': this.actionIdentifiers,
341 'payload': JSON.stringify(this.payload)
342 }
343 };
344
345 return data;
346 }
347
348 testVnf() {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600349 this.enableBrowse = false;
350 this.enableTestButton = false;
351 this.enablePollButton = false;
352 this.timer = Observable.interval(10000);
353 this.subscribe = this.timer.subscribe((t) => this.pollTestStatus());
Skip Wonnell2c977e22018-03-01 08:30:15 -0600354 this.ngProgress.start();
355 this.apiRequest = JSON.stringify(this.constructRequest());
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530356
Skip Wonnell2c977e22018-03-01 08:30:15 -0600357 this.httpUtil.post(
358 {
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530359 url: environment.testVnf + "?urlAction=" + this.getUrlEndPoint(this.action.toLowerCase()),
360 data: this.apiRequest
Skip Wonnell2c977e22018-03-01 08:30:15 -0600361 })
362 .subscribe(resp => {
363 this.apiResponse = JSON.stringify(resp);
364 this.enableBrowse = true;
365 this.enableTestButton = true;
366 this.ngProgress.done();
367 },
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530368 error => {
369 this.nService.error('Error', 'Error in connecting to APPC Server');
370 this.enableBrowse = true;
371 this.enableTestButton = true;
372 this.enablePollButton = true;
373 this.enableCounterDiv = false;
374 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600375
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530376 });
Skip Wonnell2c977e22018-03-01 08:30:15 -0600377
378 setTimeout(() => {
379 this.ngProgress.done();
380 }, 3500);
381 }
382
383
384 pollTestStatus() {
385 if (this.requestId && this.actionIdentifiers['vnf-id']) {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600386 let timeStamp = new Date().toISOString();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600387 let reqId = new Date().getTime().toString();
388 let data = {
389 'input': {
390 'common-header': {
391 'timestamp': timeStamp,
392 'api-ver': '2.00',
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530393 'originator-id': this.userId,
Skip Wonnell2c977e22018-03-01 08:30:15 -0600394 'request-id': reqId,
395 'sub-request-id': reqId,
396 'flags': {
397 'mode': 'NORMAL',
398 'force': 'TRUE',
399 'ttl': 3600
400 }
401 },
402 'action': 'ActionStatus',
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530403 'action-identifiers': {
404 'vnf-id': this.actionIdentifiers['vnf-id']
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530405 },
Skip Wonnell2c977e22018-03-01 08:30:15 -0600406 'payload': '{"request-id":' + this.requestId + '}'
407 }
408 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600409 this.httpUtil.post(
410 {
411 url: environment.checkTestStatus, data: data
412
413 })
414 .subscribe(resp => {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600415 this.statusResponse = JSON.stringify(resp);
416 var status = ''
417 var statusReason = ''
418 this.enableCounterDiv = true;
419 this.pollCounter++;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600420 if (resp.output) var timeStamp = resp.output['common-header'].timestamp;
421 if (resp.output.payload) {
422 var payload = resp.output.payload.replace(/\\/g, "")
423 try {
424 payload = JSON.parse(payload)
425 status = payload['status'];
426 statusReason = payload['status-reason'];
427 }
428 catch (err) {
429 console.log("error" + err)
430 }
431 }
432 if (timeStamp && status && statusReason) {
433 this.showStatusResponseDiv = true;
434 this.outputTimeStamp = timeStamp;
435 this.status = status;
436 this.statusReason = statusReason;
sj108sf1e61ce2018-03-13 20:29:58 +0530437 if (status.toUpperCase() === 'SUCCESS' || status.toUpperCase() === 'SUCCESSFUL') {
sj108s90174642018-03-15 20:26:47 +0530438 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600439 this.enablePollButton = true;
440 }
441 if (status.toUpperCase() === 'FAILED') {
sj108s90174642018-03-15 20:26:47 +0530442 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600443 this.enablePollButton = true;
444 }
445 }
446 else {
447 this.showStatusResponseDiv = false;
sj108sb694d952018-03-21 21:25:39 +0530448 if (this.subscribe && this.subscribe != undefined) {
449 this.subscribe.unsubscribe();
450 this.enablePollButton = true;
451 }
452
Skip Wonnell2c977e22018-03-01 08:30:15 -0600453 }
454
Skip Wonnell2c977e22018-03-01 08:30:15 -0600455 },
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530456 error => {
457 this.statusResponse = null;
458 this.showStatusResponseDiv = false;
459 this.errorResponse = 'Error Connecting to APPC server';
460 this.enableCounterDiv = false;
461 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 else {
468 this.nService.error("Error", "Please enter vnf Id & request Id");
469 }
sj108sb694d952018-03-21 21:25:39 +0530470
Skip Wonnell2c977e22018-03-01 08:30:15 -0600471 }
472
473 getUrlEndPoint(action) {
474 switch (action) {
475 case 'configmodify':
476 return 'config-modify';
477 case 'configbackup':
478 return 'config-backup';
479 case 'configrestore':
480 return 'config-restore';
481 case 'healthcheck':
482 return 'health-check';
483 case 'quiescetraffic':
484 return 'quiesce-traffic';
485 case 'resumetraffic':
486 return 'resume-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';
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530499 case 'upgradebackup':
500 return 'upgrade-backup';
501 case 'attachvolume':
502 return 'attach-volume';
503 case 'detachvolume':
504 return 'detach-volume';
Skip Wonnell2c977e22018-03-01 08:30:15 -0600505 default:
506 return action.toLowerCase();
507 }
508
509 }
510
IBM602-PC0F1E3C\Arundathi675e8b82018-07-16 14:30:26 +0530511 setValuesOnFileUploadFailure() {
512 this.flag = 1;
513 this.oldListName1 = '';
514 this.vmJson = {};
515 this.vnfcJson = {};
516 this.subPayload = {};
517 this.vmPayload = [];
518 this.payload = {};
519 this.action = '';
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;
527 }
528
Skip Wonnell2c977e22018-03-01 08:30:15 -0600529}