blob: 411b0572d3568442d271c595be25ddd90612969b [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
20ECOMP is a trademark and service mark of AT&T Intellectual Property.
21============LICENSE_END============================================
22*/
23
24import { Component, OnInit } from '@angular/core';
25
26import { saveAs } from 'file-saver';
27import { Location } from '@angular/common';
28import { ActivatedRoute, Router } from '@angular/router';
29import { NotificationService } from '.././shared/services/notification.service';
30import { ParamShareService } from '.././shared/services/paramShare.service';
31import { MappingEditorService } from '.././shared/services/mapping-editor.service';
32import { NotificationsService } from 'angular2-notifications';
33import { HttpUtilService } from '.././shared/services/httpUtil/http-util.service';
34import 'rxjs/add/observable/interval';
35import { Observable } from 'rxjs/Observable';
36import { environment } from '../.././environments/environment';
37import { UtilityService } from '.././shared/services/utilityService/utility.service';
38import 'rxjs/add/operator/map';
39import * as XLSX from 'xlsx';
40import { NgProgress } from 'ngx-progressbar';
41
42
43let YAML = require('yamljs');
44
45type AOA = Array<Array<any>>;
46declare var $: any;
47
48@Component({ selector: 'test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] })
49export class TestComponent implements OnInit {
50 public displayParamObjects;
51 options = {
52 timeOut: 1000,
53 showProgressBar: true,
54 pauseOnHover: true,
55 clickToClose: true,
56 maxLength: 200
57 };
58 public action: any;
59 public vnfId: any;
60 public item: any = {};
61
62 public vnfType: any;
63 vnfcType: any;
64 protocol: any;
65 mode: any = 'NORMAL';
66 force: any = 'True';
67 ttl: any;
68 public formattedNameValuePairs = {};
69 public requestId = '';
70 public enableBrowse: boolean = true;
71 public enableSpinner: boolean = false;
72 host: any;
73 public refNameObj = {};
74
75 public artifactName;
76 public type;
77 public transactions = '';
78 public uploadFileName;
79
80 public payload = {};
81 public lastvmJson = {};
82 public vmPayload = [];
83 public subPayload = {};
84 public vmJson = {};
85 public vnfcJson = {};
86 public flag = 1;
87 public oldListName1 = '';
88 public actionIdentifiers = {};
89 public apiRequest = '';
90 public apiResponse = '';
91 public statusResponse;
92 public outputTimeStamp;
93 public status;
94 public statusReason;
95 public errorResponse;
96 public timer;
97 public subscribe;
98 public enableTestButton: boolean = false;
99 public enableAbort: boolean = false;
100 public showStatusResponseDiv: boolean = false;
101 public enablePollButton: boolean = true;
102 public pollCounter = 0;
103 public enableCounterDiv: boolean = false;
sj108sf1e61ce2018-03-13 20:29:58 +0530104 public enableDownload: boolean = false;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600105
106 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,
107 private utiltiy: UtilityService, private ngProgress: NgProgress) {
108
109 }
110
111 ngOnInit() {
112
113
114 }
115
Skip Wonnell2c977e22018-03-01 08:30:15 -0600116
117 public download() {
118 if (this.apiRequest) {
119 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request';
120 var theJSON = this.apiRequest;
121 if (fileName != null || fileName != '') {
122 var blob = new Blob([theJSON], {
123 type: 'text/json'
124 });
125 saveAs(blob, fileName);
126 }
127 }
128 else {
129 this.nService.error('Error', 'Please upload spreadsheet to download the request and response');
130 }
131
132 if (this.apiResponse) {
133 var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response';
134 var theJSON = this.apiResponse;
135 if (fileName != null || fileName != '') {
136 var blob = new Blob([theJSON], {
137 type: 'text/json'
138 });
139 saveAs(blob, fileName);
140 }
141 }
142
143 }
144
145
146 public abortTest() {
147 //this.apiResponse = "";
148 this.enableBrowse = true;
149 this.enableTestButton = true;
150 this.enablePollButton = true;
151 this.subscribe.unsubscribe();
152
153 }
154
155
Skip Wonnell2c977e22018-03-01 08:30:15 -0600156 excelBrowseOption() {
157 $('#excelInputFile').trigger('click');
158 }
159
160
161 upload(evt: any) {
162 /* wire up file reader */
163 $('#filesparam').trigger('click');
164 const target: DataTransfer = <DataTransfer>(evt.target);
sj108sf1e61ce2018-03-13 20:29:58 +0530165 this.pollCounter = 0;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600166 this.uploadFileName = evt.target.files[0].name;
167 var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1);
168
169 if (target.files.length != 1) {
170 throw new Error('Cannot upload multiple files on the entry');
171 }
172 if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') {
173 const reader = new FileReader();
174 reader.onload = (e: any) => {
175 /* read workbook */
176 const bstr = e.target.result;
177 const wb = XLSX.read(bstr, { type: 'binary' });
178 /* grab first sheet */
179 const wsname = wb.SheetNames[0];
180 const ws = wb.Sheets[wsname];
181
182 /* save data */
183 this.requestId = ''
184 this.enableTestButton = true;
185 this.enableAbort = true;
186 this.enablePollButton = true;
187
188 if (this.subscribe && this.subscribe != undefined) {
189 this.enableCounterDiv = false;
190 this.subscribe.unsubscribe();
191 }
192 this.apiRequest = '';
193 this.apiResponse = '';
194 this.showStatusResponseDiv = false;
195 this.errorResponse = '';
196 this.statusResponse = '';
sj108sf1e61ce2018-03-13 20:29:58 +0530197 this.enableDownload=true;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600198 let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false })));
sj108sf1e61ce2018-03-13 20:29:58 +0530199
Skip Wonnell2c977e22018-03-01 08:30:15 -0600200
201
202 console.log('Array data ==' + arrData[0]);
203 this.vmPayload = [];
204 this.subPayload = {};
205 this.vmJson = {};
206 this.flag = 1;
207 this.payload = {};
208 this.oldListName1 = '';
209 this.actionIdentifiers = {};
sj108sf1e61ce2018-03-13 20:29:58 +0530210 // Refactor
211 this.payload = this.processUploadedFile(arrData);
212 this.uploadedFileResult();
213 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600214
215 reader.readAsBinaryString(target.files[0]);
sj108sf1e61ce2018-03-13 20:29:58 +0530216
217
Skip Wonnell2c977e22018-03-01 08:30:15 -0600218 }
219 else {
220 this.nService.error('Error', 'Incorrect spreadsheet uploaded');
221 this.flag = 1;
222 this.oldListName1 = '';
223 this.vmJson = {};
224 this.vnfcJson = {};
225 this.subPayload = {};
226 this.vmPayload = [];
227 this.payload = {};
228 this.action = '';
229 this.actionIdentifiers = {};
230 this.apiRequest = '';
231 this.apiResponse = '';
232 this.enableCounterDiv = false;
sj108sf1e61ce2018-03-13 20:29:58 +0530233 this.enableAbort = false;
234 this.enableTestButton = false;
235 this.enableDownload = false;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600236 }
237 }
238
sj108sf1e61ce2018-03-13 20:29:58 +0530239processUploadedFile(arrData) {
240 let tempPayload = {};
241 for (var i = 0; i < arrData.length; i++) {
242 var element = arrData[i];
243 if (element['TagName'] === 'action') {
244 this.action = element['Value'];
245 }
246 if (element['List Name'] === 'action-identifiers') {
247 this.vnfId = element['Value'];
248 var key = element['TagName'];
249 var value = element['Value'];
250 if (key && value) {
251 this.actionIdentifiers[key] = value;
252
253 }
254 }
255
256 if (element['List Name'] === 'payload') {
257 var listName1 = element['List Name_1'];
258 var listName2 = element['List Name_2'];
259 var listName3 = element['List Name_3'];
260 var key = element['TagName'];
261 var value = element['Value'];
262 if (listName1) {
263 if (this.oldListName1 == '' || (listName1 === this.oldListName1)) {
264 this.constructTestPayload(listName2, listName3, key, value);
265 tempPayload[listName1] = this.subPayload;
266 }
267 else {
268 this.subPayload = {};
269 this.constructTestPayload(listName2, listName3, key, value);
270 tempPayload[listName1] = this.subPayload;
271 }
272 this.oldListName1 = listName1;
273 }
274 else {
275 tempPayload[key] = value;
276 }
277 }
278 }
279
280 return tempPayload;
281 }
282
283 uploadedFileResult() {
284 if (this.action && this.actionIdentifiers['vnf-id']) {
285 this.nService.success('Success', 'SpreadSheet uploaded successfully');
286 }
287 else {
288 this.flag = 1;
289 this.oldListName1 = '';
290 this.vmJson = {};
291 this.vnfcJson = {};
292 this.subPayload = {};
293 this.vmPayload = [];
294 this.payload = {};
295 this.action = '';
296 this.actionIdentifiers = {};
297 this.apiRequest = '';
298 this.apiResponse = '';
299 this.enableCounterDiv = false;
300 this.enableAbort = false;
301 this.enableTestButton = false;
302 this.enableDownload = false;
303 this.nService.error("Error", "Please check the contents of the file uploaded")
304 }
305 }
306
307
308
Skip Wonnell2c977e22018-03-01 08:30:15 -0600309 constructTestPayload(listName2, listName3, key, value) {
310 if (listName2 == undefined && listName3 == undefined) {
311 this.subPayload[key] = value;
312 }
313 if (listName2) {
314
315 if (!listName3) {
316
317 //vmPayload.push(vmJson)
318 this.vmJson = {};
319 this.vnfcJson = {};
320 this.vmJson[key] = value;
321 this.flag = 0;
322 }
323 else {
324 this.vnfcJson[key] = value;
325 this.vmJson['vnfc'] = this.vnfcJson;
326 this.flag = 1;
327 }
328 if (this.vmJson) this.lastvmJson = this.vmJson;
329 if (this.flag == 0) {
330 this.vmPayload.push(this.lastvmJson);
331 if (this.vmPayload) this.subPayload['vm'] = this.vmPayload;
332 }
333 }
334 }
335
336 constructRequest() {
337 console.log('payload==' + JSON.stringify(this.payload));
338 let timeStamp = new Date().toISOString();
339 console.log('timestamp==' + timeStamp);
340 let reqId;
341 this.requestId = reqId = new Date().getTime().toString();
342 let data = {
343 'input': {
344 'common-header': {
345 'timestamp': timeStamp,
346 'api-ver': '2.00',
347 'originator-id': 'CDT',
348 'request-id': this.requestId,
349 'sub-request-id': this.requestId,
350 'flags': {
351 'mode': 'NORMAL',
352 'force': 'TRUE',
353 'ttl': 3600
354 }
355 },
356 'action': this.action,
357 'action-identifiers': this.actionIdentifiers,
358 'payload': JSON.stringify(this.payload)
359 }
360 };
361
362 return data;
363 }
364
365 testVnf() {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600366 this.enableBrowse = false;
367 this.enableTestButton = false;
368 this.enablePollButton = false;
369 this.timer = Observable.interval(10000);
370 this.subscribe = this.timer.subscribe((t) => this.pollTestStatus());
371 //console.log('full payload==' + JSON.stringify(this.apiRequest));
372 this.ngProgress.start();
373 this.apiRequest = JSON.stringify(this.constructRequest());
374 this.httpUtil.post(
375 {
376 url: environment.testVnf + this.getUrlEndPoint(this.action.toLowerCase()), data: this.apiRequest
377 })
378 .subscribe(resp => {
379 this.apiResponse = JSON.stringify(resp);
380 this.enableBrowse = true;
381 this.enableTestButton = true;
382 this.ngProgress.done();
383 },
384 error => {
385 this.nService.error('Error', 'Error in connecting to APPC Server');
386 this.enableBrowse = true;
387 this.enableTestButton = true;
388 this.enablePollButton = true;
389 this.enableCounterDiv = false;
390 this.subscribe.unsubscribe();
391
392 });
393
394 setTimeout(() => {
395 this.ngProgress.done();
396 }, 3500);
397 }
398
399
400 pollTestStatus() {
401 if (this.requestId && this.actionIdentifiers['vnf-id']) {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600402 let timeStamp = new Date().toISOString();
Skip Wonnell2c977e22018-03-01 08:30:15 -0600403 let reqId = new Date().getTime().toString();
404 let data = {
405 'input': {
406 'common-header': {
407 'timestamp': timeStamp,
408 'api-ver': '2.00',
409 'originator-id': 'CDT',
410 'request-id': reqId,
411 'sub-request-id': reqId,
412 'flags': {
413 'mode': 'NORMAL',
414 'force': 'TRUE',
415 'ttl': 3600
416 }
417 },
418 'action': 'ActionStatus',
419 'action-identifiers': this.actionIdentifiers,
420 'payload': '{"request-id":' + this.requestId + '}'
421 }
422 };
Skip Wonnell2c977e22018-03-01 08:30:15 -0600423 this.httpUtil.post(
424 {
425 url: environment.checkTestStatus, data: data
426
427 })
428 .subscribe(resp => {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600429 this.statusResponse = JSON.stringify(resp);
430 var status = ''
431 var statusReason = ''
432 this.enableCounterDiv = true;
433 this.pollCounter++;
Skip Wonnell2c977e22018-03-01 08:30:15 -0600434 if (resp.output) var timeStamp = resp.output['common-header'].timestamp;
435 if (resp.output.payload) {
436 var payload = resp.output.payload.replace(/\\/g, "")
437 try {
438 payload = JSON.parse(payload)
439 status = payload['status'];
440 statusReason = payload['status-reason'];
441 }
442 catch (err) {
443 console.log("error" + err)
444 }
445 }
446 if (timeStamp && status && statusReason) {
447 this.showStatusResponseDiv = true;
448 this.outputTimeStamp = timeStamp;
449 this.status = status;
450 this.statusReason = statusReason;
sj108sf1e61ce2018-03-13 20:29:58 +0530451 if (status.toUpperCase() === 'SUCCESS' || status.toUpperCase() === 'SUCCESSFUL') {
Skip Wonnell2c977e22018-03-01 08:30:15 -0600452 this.subscribe.unsubscribe();
453 this.enablePollButton = true;
454 }
455 if (status.toUpperCase() === 'FAILED') {
456 this.subscribe.unsubscribe();
457 this.enablePollButton = true;
458 }
459 }
460 else {
461 this.showStatusResponseDiv = false;
462 }
463
sj108sf1e61ce2018-03-13 20:29:58 +0530464
Skip Wonnell2c977e22018-03-01 08:30:15 -0600465 },
466 error => {
467 this.statusResponse = null;
468 this.showStatusResponseDiv = false;
469 this.errorResponse = 'Error Connecting to APPC server';
470 this.enableCounterDiv = false;
471 this.subscribe.unsubscribe();
472 });
473
474 }
475 else {
476 this.nService.error("Error", "Please enter vnf Id & request Id");
477 }
sj108sf1e61ce2018-03-13 20:29:58 +0530478
Skip Wonnell2c977e22018-03-01 08:30:15 -0600479 }
480
481 getUrlEndPoint(action) {
482 switch (action) {
483 case 'configmodify':
484 return 'config-modify';
485 case 'configbackup':
486 return 'config-backup';
487 case 'configrestore':
488 return 'config-restore';
489 case 'healthcheck':
490 return 'health-check';
491 case 'quiescetraffic':
492 return 'quiesce-traffic';
493 case 'resumetraffic':
494 return 'resume-traffic';
495 case 'startapplication':
496 return 'start-application';
497 case 'stopapplication':
498 return 'stop-application';
499 case 'upgradebackout':
500 return 'upgrade-backout';
501 case 'upgradepostcheck':
502 return 'upgrade-post-check';
503 case 'upgradeprecheck':
504 return 'upgrade-pre-check';
505 case 'upgradesoftware':
506 return 'upgrade-software';
507 default:
508 return action.toLowerCase();
509 }
510
511 }
512
513}