blob: 3797c8c0d9f42588dfffd456c07b5c7136c508d5 [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.
Skip Wonnell2c977e22018-03-01 08:30:15 -060019============LICENSE_END============================================
20*/
21
22import { Component, OnInit } from '@angular/core';
23
24import { saveAs } from 'file-saver';
25import { Location } from '@angular/common';
26import { ActivatedRoute, Router } from '@angular/router';
27import { NotificationService } from '.././shared/services/notification.service';
28import { ParamShareService } from '.././shared/services/paramShare.service';
29import { MappingEditorService } from '.././shared/services/mapping-editor.service';
30import { NotificationsService } from 'angular2-notifications';
31import { HttpUtilService } from '.././shared/services/httpUtil/http-util.service';
32import 'rxjs/add/observable/interval';
33import { Observable } from 'rxjs/Observable';
34import { environment } from '../.././environments/environment';
35import { UtilityService } from '.././shared/services/utilityService/utility.service';
36import 'rxjs/add/operator/map';
37import * as XLSX from 'xlsx';
38import { NgProgress } from 'ngx-progressbar';
39
40
41let YAML = require('yamljs');
42
43type AOA = Array<Array<any>>;
44declare var $: any;
45
46@Component({ selector: 'test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] })
47export class TestComponent implements OnInit {
48 public displayParamObjects;
49 options = {
50 timeOut: 1000,
51 showProgressBar: true,
52 pauseOnHover: true,
53 clickToClose: true,
54 maxLength: 200
55 };
56 public action: any;
57 public vnfId: any;
58 public item: any = {};
59
60 public vnfType: any;
61 vnfcType: any;
62 protocol: any;
63 mode: any = 'NORMAL';
64 force: any = 'True';
65 ttl: any;
66 public formattedNameValuePairs = {};
67 public requestId = '';
68 public enableBrowse: boolean = true;
69 public enableSpinner: boolean = false;
70 host: any;
71 public refNameObj = {};
72
73 public artifactName;
74 public type;
75 public transactions = '';
76 public uploadFileName;
77
78 public payload = {};
79 public lastvmJson = {};
80 public vmPayload = [];
81 public subPayload = {};
82 public vmJson = {};
83 public vnfcJson = {};
84 public flag = 1;
85 public oldListName1 = '';
86 public actionIdentifiers = {};
87 public apiRequest = '';
88 public apiResponse = '';
89 public statusResponse;
90 public outputTimeStamp;
91 public status;
92 public statusReason;
93 public errorResponse;
94 public timer;
95 public subscribe;
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;
sj108sf1e61ce2018-03-13 20:29:58 +0530102 public enableDownload: boolean = false;
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530103 private userId = localStorage['userId'];
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530104 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,
Skip Wonnell2c977e22018-03-01 08:30:15 -0600105 private utiltiy: UtilityService, private ngProgress: NgProgress) {
106
107 }
108
109 ngOnInit() {
110
111
112 }
113
Skip Wonnell2c977e22018-03-01 08:30:15 -0600114
115 public download() {
116 if (this.apiRequest) {
117 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request';
118 var theJSON = this.apiRequest;
119 if (fileName != null || fileName != '') {
120 var blob = new Blob([theJSON], {
121 type: 'text/json'
122 });
123 saveAs(blob, fileName);
124 }
125 }
126 else {
127 this.nService.error('Error', 'Please upload spreadsheet to download the request and response');
128 }
129
130 if (this.apiResponse) {
131 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response';
132 var theJSON = this.apiResponse;
133 if (fileName != null || fileName != '') {
134 var blob = new Blob([theJSON], {
135 type: 'text/json'
136 });
137 saveAs(blob, fileName);
138 }
139 }
140
141 }
142
143
144 public abortTest() {
145 //this.apiResponse = "";
146 this.enableBrowse = true;
147 this.enableTestButton = true;
148 this.enablePollButton = true;
sj108s90174642018-03-15 20:26:47 +0530149 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530150 this.nService.info("Information", "Test has been abandoned and polling stopped");
Skip Wonnell2c977e22018-03-01 08:30:15 -0600151 }
152
153
Skip Wonnell2c977e22018-03-01 08:30:15 -0600154 excelBrowseOption() {
155 $('#excelInputFile').trigger('click');
156 }
157
158
159 upload(evt: any) {
160 /* wire up file reader */
161 $('#filesparam').trigger('click');
162 const target: DataTransfer = <DataTransfer>(evt.target);
sj108sf1e61ce2018-03-13 20:29:58 +0530163 this.pollCounter = 0;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600164 this.uploadFileName = evt.target.files[0].name;
165 var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1);
166
167 if (target.files.length != 1) {
168 throw new Error('Cannot upload multiple files on the entry');
169 }
170 if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') {
171 const reader = new FileReader();
172 reader.onload = (e: any) => {
173 /* read workbook */
174 const bstr = e.target.result;
175 const wb = XLSX.read(bstr, { type: 'binary' });
176 /* grab first sheet */
177 const wsname = wb.SheetNames[0];
178 const ws = wb.Sheets[wsname];
179
180 /* save data */
181 this.requestId = ''
182 this.enableTestButton = true;
183 this.enableAbort = true;
184 this.enablePollButton = true;
185
186 if (this.subscribe && this.subscribe != undefined) {
187 this.enableCounterDiv = false;
188 this.subscribe.unsubscribe();
189 }
190 this.apiRequest = '';
191 this.apiResponse = '';
192 this.showStatusResponseDiv = false;
193 this.errorResponse = '';
194 this.statusResponse = '';
sj108sb694d952018-03-21 21:25:39 +0530195 this.enableDownload = true;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600196 let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false })));
sj108sf1e61ce2018-03-13 20:29:58 +0530197
Skip Wonnell2c977e22018-03-01 08:30:15 -0600198
199
Skip Wonnell2c977e22018-03-01 08:30:15 -0600200 this.vmPayload = [];
201 this.subPayload = {};
202 this.vmJson = {};
203 this.flag = 1;
204 this.payload = {};
205 this.oldListName1 = '';
206 this.actionIdentifiers = {};
sj108sf1e61ce2018-03-13 20:29:58 +0530207 // Refactor
208 this.payload = this.processUploadedFile(arrData);
209 this.uploadedFileResult();
sj108sb694d952018-03-21 21:25:39 +0530210 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600211
212 reader.readAsBinaryString(target.files[0]);
sj108sb694d952018-03-21 21:25:39 +0530213
sj108sf1e61ce2018-03-13 20:29:58 +0530214
Skip Wonnell2c977e22018-03-01 08:30:15 -0600215 }
216 else {
217 this.nService.error('Error', 'Incorrect spreadsheet uploaded');
218 this.flag = 1;
219 this.oldListName1 = '';
220 this.vmJson = {};
221 this.vnfcJson = {};
222 this.subPayload = {};
223 this.vmPayload = [];
224 this.payload = {};
225 this.action = '';
226 this.actionIdentifiers = {};
227 this.apiRequest = '';
228 this.apiResponse = '';
229 this.enableCounterDiv = false;
sj108sf1e61ce2018-03-13 20:29:58 +0530230 this.enableAbort = false;
231 this.enableTestButton = false;
232 this.enableDownload = false;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600233 }
234 }
235
sj108sb694d952018-03-21 21:25:39 +0530236 processUploadedFile(arrData) {
sj108sf1e61ce2018-03-13 20:29:58 +0530237 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'];
242 }
243 if (element['List Name'] === 'action-identifiers') {
244 this.vnfId = element['Value'];
245 var key = element['TagName'];
246 var value = element['Value'];
247 if (key && value) {
248 this.actionIdentifiers[key] = value;
249
250 }
251 }
sj108sb694d952018-03-21 21:25:39 +0530252
sj108sf1e61ce2018-03-13 20:29:58 +0530253 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'];
259 if (listName1) {
260 if (this.oldListName1 == '' || (listName1 === this.oldListName1)) {
261 this.constructTestPayload(listName2, listName3, key, value);
262 tempPayload[listName1] = this.subPayload;
263 }
264 else {
265 this.subPayload = {};
266 this.constructTestPayload(listName2, listName3, key, value);
267 tempPayload[listName1] = this.subPayload;
268 }
269 this.oldListName1 = listName1;
270 }
271 else {
272 tempPayload[key] = value;
273 }
274 }
275 }
276
277 return tempPayload;
278 }
279
sj108sb694d952018-03-21 21:25:39 +0530280 uploadedFileResult() {
sj108sf1e61ce2018-03-13 20:29:58 +0530281 if (this.action && this.actionIdentifiers['vnf-id']) {
282 this.nService.success('Success', 'SpreadSheet uploaded successfully');
283 }
284 else {
285 this.flag = 1;
286 this.oldListName1 = '';
287 this.vmJson = {};
288 this.vnfcJson = {};
289 this.subPayload = {};
290 this.vmPayload = [];
291 this.payload = {};
292 this.action = '';
293 this.actionIdentifiers = {};
294 this.apiRequest = '';
295 this.apiResponse = '';
296 this.enableCounterDiv = false;
297 this.enableAbort = false;
298 this.enableTestButton = false;
299 this.enableDownload = false;
300 this.nService.error("Error", "Please check the contents of the file uploaded")
301 }
302 }
303
304
305
Skip Wonnell2c977e22018-03-01 08:30:15 -0600306 constructTestPayload(listName2, listName3, key, value) {
307 if (listName2 == undefined && listName3 == undefined) {
308 this.subPayload[key] = value;
309 }
310 if (listName2) {
311
312 if (!listName3) {
313
314 //vmPayload.push(vmJson)
315 this.vmJson = {};
316 this.vnfcJson = {};
317 this.vmJson[key] = value;
318 this.flag = 0;
319 }
320 else {
321 this.vnfcJson[key] = value;
322 this.vmJson['vnfc'] = this.vnfcJson;
323 this.flag = 1;
324 }
325 if (this.vmJson) this.lastvmJson = this.vmJson;
326 if (this.flag == 0) {
327 this.vmPayload.push(this.lastvmJson);
328 if (this.vmPayload) this.subPayload['vm'] = this.vmPayload;
329 }
330 }
331 }
332
333 constructRequest() {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600334 let timeStamp = new Date().toISOString();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600335 let reqId;
336 this.requestId = reqId = new Date().getTime().toString();
337 let data = {
338 'input': {
339 'common-header': {
340 'timestamp': timeStamp,
341 'api-ver': '2.00',
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530342 'originator-id': this.userId,
Skip Wonnell2c977e22018-03-01 08:30:15 -0600343 'request-id': this.requestId,
344 'sub-request-id': this.requestId,
345 'flags': {
346 'mode': 'NORMAL',
347 'force': 'TRUE',
348 'ttl': 3600
349 }
350 },
351 'action': this.action,
352 'action-identifiers': this.actionIdentifiers,
353 'payload': JSON.stringify(this.payload)
354 }
355 };
356
357 return data;
358 }
359
360 testVnf() {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600361 this.enableBrowse = false;
362 this.enableTestButton = false;
363 this.enablePollButton = false;
364 this.timer = Observable.interval(10000);
365 this.subscribe = this.timer.subscribe((t) => this.pollTestStatus());
Skip Wonnell2c977e22018-03-01 08:30:15 -0600366 this.ngProgress.start();
367 this.apiRequest = JSON.stringify(this.constructRequest());
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530368
Skip Wonnell2c977e22018-03-01 08:30:15 -0600369 this.httpUtil.post(
370 {
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530371 url: environment.testVnf + "?urlAction=" + this.getUrlEndPoint(this.action.toLowerCase()),
372 data: this.apiRequest
Skip Wonnell2c977e22018-03-01 08:30:15 -0600373 })
374 .subscribe(resp => {
375 this.apiResponse = JSON.stringify(resp);
376 this.enableBrowse = true;
377 this.enableTestButton = true;
378 this.ngProgress.done();
379 },
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530380 error => {
381 this.nService.error('Error', 'Error in connecting to APPC Server');
382 this.enableBrowse = true;
383 this.enableTestButton = true;
384 this.enablePollButton = true;
385 this.enableCounterDiv = false;
386 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600387
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530388 });
Skip Wonnell2c977e22018-03-01 08:30:15 -0600389
390 setTimeout(() => {
391 this.ngProgress.done();
392 }, 3500);
393 }
394
395
396 pollTestStatus() {
397 if (this.requestId && this.actionIdentifiers['vnf-id']) {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600398 let timeStamp = new Date().toISOString();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600399 let reqId = new Date().getTime().toString();
400 let data = {
401 'input': {
402 'common-header': {
403 'timestamp': timeStamp,
404 'api-ver': '2.00',
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530405 'originator-id': this.userId,
Skip Wonnell2c977e22018-03-01 08:30:15 -0600406 'request-id': reqId,
407 'sub-request-id': reqId,
408 'flags': {
409 'mode': 'NORMAL',
410 'force': 'TRUE',
411 'ttl': 3600
412 }
413 },
414 'action': 'ActionStatus',
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530415 'action-identifiers': {
416 'vnf-id': this.actionIdentifiers['vnf-id']
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530417 },
Skip Wonnell2c977e22018-03-01 08:30:15 -0600418 'payload': '{"request-id":' + this.requestId + '}'
419 }
420 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600421 this.httpUtil.post(
422 {
423 url: environment.checkTestStatus, data: data
424
425 })
426 .subscribe(resp => {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600427 this.statusResponse = JSON.stringify(resp);
428 var status = ''
429 var statusReason = ''
430 this.enableCounterDiv = true;
431 this.pollCounter++;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600432 if (resp.output) var timeStamp = resp.output['common-header'].timestamp;
433 if (resp.output.payload) {
434 var payload = resp.output.payload.replace(/\\/g, "")
435 try {
436 payload = JSON.parse(payload)
437 status = payload['status'];
438 statusReason = payload['status-reason'];
439 }
440 catch (err) {
441 console.log("error" + err)
442 }
443 }
444 if (timeStamp && status && statusReason) {
445 this.showStatusResponseDiv = true;
446 this.outputTimeStamp = timeStamp;
447 this.status = status;
448 this.statusReason = statusReason;
sj108sf1e61ce2018-03-13 20:29:58 +0530449 if (status.toUpperCase() === 'SUCCESS' || status.toUpperCase() === 'SUCCESSFUL') {
sj108s90174642018-03-15 20:26:47 +0530450 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600451 this.enablePollButton = true;
452 }
453 if (status.toUpperCase() === 'FAILED') {
sj108s90174642018-03-15 20:26:47 +0530454 if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600455 this.enablePollButton = true;
456 }
457 }
458 else {
459 this.showStatusResponseDiv = false;
sj108sb694d952018-03-21 21:25:39 +0530460 if (this.subscribe && this.subscribe != undefined) {
461 this.subscribe.unsubscribe();
462 this.enablePollButton = true;
463 }
464
Skip Wonnell2c977e22018-03-01 08:30:15 -0600465 }
466
Skip Wonnell2c977e22018-03-01 08:30:15 -0600467 },
Kumar, Amaresh (ak583p)6c7ccba2018-05-09 22:29:45 +0530468 error => {
469 this.statusResponse = null;
470 this.showStatusResponseDiv = false;
471 this.errorResponse = 'Error Connecting to APPC server';
472 this.enableCounterDiv = false;
473 if (this.subscribe && this.subscribe != undefined) {
474 this.subscribe.unsubscribe();
475 this.enablePollButton = true;
476 }
477 });
Skip Wonnell2c977e22018-03-01 08:30:15 -0600478 }
479 else {
480 this.nService.error("Error", "Please enter vnf Id & request Id");
481 }
sj108sb694d952018-03-21 21:25:39 +0530482
Skip Wonnell2c977e22018-03-01 08:30:15 -0600483 }
484
485 getUrlEndPoint(action) {
486 switch (action) {
487 case 'configmodify':
488 return 'config-modify';
489 case 'configbackup':
490 return 'config-backup';
491 case 'configrestore':
492 return 'config-restore';
493 case 'healthcheck':
494 return 'health-check';
495 case 'quiescetraffic':
496 return 'quiesce-traffic';
497 case 'resumetraffic':
498 return 'resume-traffic';
499 case 'startapplication':
500 return 'start-application';
501 case 'stopapplication':
502 return 'stop-application';
503 case 'upgradebackout':
504 return 'upgrade-backout';
505 case 'upgradepostcheck':
506 return 'upgrade-post-check';
507 case 'upgradeprecheck':
508 return 'upgrade-pre-check';
509 case 'upgradesoftware':
510 return 'upgrade-software';
Mohamed Asgar Samiulla(ma926a)d7dffc22018-04-02 19:00:49 +0530511 case 'upgradebackup':
512 return 'upgrade-backup';
513 case 'attachvolume':
514 return 'attach-volume';
515 case 'detachvolume':
516 return 'detach-volume';
Skip Wonnell2c977e22018-03-01 08:30:15 -0600517 default:
518 return action.toLowerCase();
519 }
520
521 }
522
523}