blob: f3f6a944f2815645f65b86f3c7b8769085f3eb6a [file] [log] [blame]
/*
============LICENSE_START==========================================
===================================================================
Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
===================================================================
Copyright (C) 2018 IBM.
===================================================================
Unless otherwise specified, all software contained herein is licensed
under the Apache License, Version 2.0 (the License);
you may not use this software except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END============================================
*/
import { Component, OnInit } from '@angular/core';
import { NgxSpinnerService } from 'ngx-spinner';
import { saveAs } from 'file-saver';
import { Location } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { ParamShareService } from '.././shared/services/paramShare.service';
import { MappingEditorService } from '.././shared/services/mapping-editor.service';
import { NotificationsService } from 'angular2-notifications';
import { HttpUtilService } from '.././shared/services/httpUtil/http-util.service';
import 'rxjs/add/observable/interval';
import { Observable } from 'rxjs/Observable';
import { environment } from '../.././environments/environment';
import { UtilityService } from '.././shared/services/utilityService/utility.service';
import 'rxjs/add/operator/map';
import * as XLSX from 'xlsx';
import { NgProgress } from 'ngx-progressbar';
let YAML = require('yamljs');
type AOA = Array<Array<any>>;
declare var $: any;
@Component({ selector: 'test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] })
export class TestComponent implements OnInit {
public displayParamObjects;
options = {
timeOut: 1000,
showProgressBar: true,
pauseOnHover: true,
clickToClose: true,
maxLength: 200
};
public action: any;
public vnfId: any;
public item: any = {};
public vnfType: any;
vnfcType: any;
protocol: any;
mode: any = 'NORMAL';
force: any = 'True';
ttl: any;
public formattedNameValuePairs = {};
public requestId = '';
public enableBrowse: boolean = true;
public enableSpinner: boolean = false;
host: any;
public refNameObj = {};
public artifactName;
public type;
public transactions = '';
public uploadFileName;
public payload = {};
public lastvmJson = {};
public vmPayload = [];
public subPayload = {};
public vmJson = {};
public vnfcJson = {};
public flag = 1;
public oldListName1 = '';
public actionIdentifiers = {};
public apiRequest = '';
public apiResponse = '';
public statusResponse;
public outputTimeStamp;
public status;
public statusReason;
public errorResponse;
public timer;
public subscribe;
public enableTestButton: boolean = false;
public enableAbort: boolean = false;
public showStatusResponseDiv: boolean = false;
public enablePollButton: boolean = true;
public pollCounter = 0;
public enableCounterDiv: boolean = false;
public enableDownload: boolean = false;
private userId = localStorage['userId'];
constructor (
private location: Location,
private activeRoutes: ActivatedRoute,
private nService: NotificationsService,
private router: Router,
private paramShareService: ParamShareService,
private mappingEditorService: MappingEditorService,
private httpUtil: HttpUtilService,
private utiltiy: UtilityService,
private ngProgress: NgProgress,
private spinner: NgxSpinnerService
) {
}
ngOnInit() {
}
public download() {
if (this.apiRequest) {
var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request';
var theJSON = this.apiRequest;
if (fileName != null || fileName != '') {
var blob = new Blob([theJSON], {
type: 'text/json'
});
saveAs(blob, fileName);
}
}
else {
this.nService.error('Error', 'Please upload spreadsheet to download the request and response');
}
if (this.apiResponse) {
var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response';
var theJSON = this.apiResponse;
if (fileName != null || fileName != '') {
var blob = new Blob([theJSON], {
type: 'text/json'
});
saveAs(blob, fileName);
}
}
}
public abortTest() {
//this.apiResponse = "";
this.enableBrowse = true;
this.enableTestButton = true;
this.enablePollButton = true;
if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
this.nService.info("Information", "Test has been abandoned and polling stopped");
}
excelBrowseOption() {
$('#excelInputFile').trigger('click');
}
upload(evt: any) {
/* wire up file reader */
$('#filesparam').trigger('click');
const target: DataTransfer = <DataTransfer>(evt.target);
this.pollCounter = 0;
this.uploadFileName = evt.target.files[0].name;
var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1);
if (target.files.length != 1) {
throw new Error('Cannot upload multiple files on the entry');
}
if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') {
this.spinner.show();
const reader = new FileReader();
reader.onload = (e: any) => {
/* read workbook */
const bstr = e.target.result;
const wb = XLSX.read(bstr, { type: 'binary' });
/* grab first sheet */
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
/* save data */
this.requestId = ''
this.enableTestButton = true;
this.enableAbort = true;
this.enablePollButton = true;
if (this.subscribe && this.subscribe != undefined) {
this.enableCounterDiv = false;
this.subscribe.unsubscribe();
}
this.apiRequest = '';
this.apiResponse = '';
this.showStatusResponseDiv = false;
this.errorResponse = '';
this.statusResponse = '';
this.enableDownload = true;
let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false })));
this.vmPayload = [];
this.subPayload = {};
this.vmJson = {};
this.flag = 1;
this.payload = {};
this.oldListName1 = '';
this.actionIdentifiers = {};
// Refactor
this.payload = this.processUploadedFile(arrData);
this.uploadedFileResult();
};
reader.readAsBinaryString(target.files[0]);
setTimeout(() => {
/** spinner ends after 2.5 seconds */
this.spinner.hide();
}, 2500);
}
else {
this.nService.error('Error', 'Incorrect spreadsheet uploaded');
this.setValuesOnFileUploadFailure();
}
}
processUploadedFile(arrData) {
let tempPayload = {};
for (var i = 0; i < arrData.length; i++) {
var element = arrData[i];
if (element['TagName'] === 'action') {
this.action = element['Value'];
}
if (element['List Name'] === 'action-identifiers') {
this.vnfId = element['Value'];
var key = element['TagName'];
var value = element['Value'];
if (key && value) {
this.actionIdentifiers[key] = value;
}
}
if (element['List Name'] === 'payload') {
var listName1 = element['List Name_1'];
var listName2 = element['List Name_2'];
var listName3 = element['List Name_3'];
var key = element['TagName'];
var value = element['Value'];
if (listName1) {
if (this.oldListName1 == '' || (listName1 === this.oldListName1)) {
this.constructTestPayload(listName2, listName3, key, value);
tempPayload[listName1] = this.subPayload;
}
else {
this.subPayload = {};
this.constructTestPayload(listName2, listName3, key, value);
tempPayload[listName1] = this.subPayload;
}
this.oldListName1 = listName1;
}
else {
tempPayload[key] = value;
}
}
}
return tempPayload;
}
uploadedFileResult() {
if (this.action && this.actionIdentifiers['vnf-id']) {
this.nService.success('Success', 'SpreadSheet uploaded successfully');
}
else {
this.setValuesOnFileUploadFailure();
this.nService.error("Error", "Please check the contents of the file uploaded")
}
}
constructTestPayload(listName2, listName3, key, value) {
if (listName2 == undefined && listName3 == undefined) {
this.subPayload[key] = value;
}
if (listName2) {
if (!listName3) {
//vmPayload.push(vmJson)
this.vmJson = {};
this.vnfcJson = {};
this.vmJson[key] = value;
this.flag = 0;
}
else {
this.vnfcJson[key] = value;
this.vmJson['vnfc'] = this.vnfcJson;
this.flag = 1;
}
if (this.vmJson) this.lastvmJson = this.vmJson;
if (this.flag == 0) {
this.vmPayload.push(this.lastvmJson);
if (this.vmPayload) this.subPayload['vm'] = this.vmPayload;
}
}
}
constructRequest() {
let timeStamp = new Date().toISOString();
let reqId;
this.requestId = reqId = new Date().getTime().toString();
let data = {
'input': {
'common-header': {
'timestamp': timeStamp,
'api-ver': '2.00',
'originator-id': this.userId,
'request-id': this.requestId,
'sub-request-id': this.requestId,
'flags': {
'mode': 'NORMAL',
'force': 'TRUE',
'ttl': 3600
}
},
'action': this.action,
'action-identifiers': this.actionIdentifiers,
'payload': JSON.stringify(this.payload)
}
};
return data;
}
testVnf() {
this.enableBrowse = false;
this.enableTestButton = false;
this.enablePollButton = false;
this.timer = Observable.interval(10000);
this.subscribe = this.timer.subscribe((t) => this.pollTestStatus());
this.ngProgress.start();
this.apiRequest = JSON.stringify(this.constructRequest());
this.httpUtil.post(
{
url: environment.testVnf + "?urlAction=" + this.getUrlEndPoint(this.action.toLowerCase()),
data: this.apiRequest
})
.subscribe(resp => {
this.apiResponse = JSON.stringify(resp);
this.enableBrowse = true;
this.enableTestButton = true;
this.ngProgress.done();
},
error => {
this.nService.error('Error', 'Error in connecting to APPC Server');
this.enableBrowse = true;
this.enableTestButton = true;
this.enablePollButton = true;
this.enableCounterDiv = false;
if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
});
setTimeout(() => {
this.ngProgress.done();
}, 3500);
}
pollTestStatus() {
if (this.requestId && this.actionIdentifiers['vnf-id']) {
let timeStamp = new Date().toISOString();
let reqId = new Date().getTime().toString();
let data = {
'input': {
'common-header': {
'timestamp': timeStamp,
'api-ver': '2.00',
'originator-id': this.userId,
'request-id': reqId,
'sub-request-id': reqId,
'flags': {
'mode': 'NORMAL',
'force': 'TRUE',
'ttl': 3600
}
},
'action': 'ActionStatus',
'action-identifiers': {
'vnf-id': this.actionIdentifiers['vnf-id']
},
'payload': '{"request-id":' + this.requestId + '}'
}
};
this.httpUtil.post(
{
url: environment.checkTestStatus, data: data
})
.subscribe(resp => {
this.statusResponse = JSON.stringify(resp);
var status = ''
var statusReason = ''
this.enableCounterDiv = true;
this.pollCounter++;
if (resp.output) var timeStamp = resp.output['common-header'].timestamp;
if (resp.output.payload) {
var payload = resp.output.payload.replace(/\\/g, "")
try {
payload = JSON.parse(payload)
status = payload['status'];
statusReason = payload['status-reason'];
}
catch (err) {
console.log("error" + err)
}
}
if (timeStamp && status && statusReason) {
this.showStatusResponseDiv = true;
this.outputTimeStamp = timeStamp;
this.status = status;
this.statusReason = statusReason;
if (status.toUpperCase() === 'SUCCESS' || status.toUpperCase() === 'SUCCESSFUL') {
if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
this.enablePollButton = true;
}
if (status.toUpperCase() === 'FAILED') {
if (this.subscribe && this.subscribe != undefined) this.subscribe.unsubscribe();
this.enablePollButton = true;
}
}
else {
this.showStatusResponseDiv = false;
if (this.subscribe && this.subscribe != undefined) {
this.subscribe.unsubscribe();
this.enablePollButton = true;
}
}
},
error => {
this.statusResponse = null;
this.showStatusResponseDiv = false;
this.errorResponse = 'Error Connecting to APPC server';
this.enableCounterDiv = false;
if (this.subscribe && this.subscribe != undefined) {
this.subscribe.unsubscribe();
this.enablePollButton = true;
}
});
}
else {
this.nService.error("Error", "Please enter vnf Id & request Id");
}
}
getUrlEndPoint(action) {
switch (action) {
case 'configmodify':
return 'config-modify';
case 'configbackup':
return 'config-backup';
case 'configrestore':
return 'config-restore';
case 'healthcheck':
return 'health-check';
case 'quiescetraffic':
return 'quiesce-traffic';
case 'resumetraffic':
return 'resume-traffic';
case 'distributetraffic':
return 'distribute-traffic';
case 'startapplication':
return 'start-application';
case 'stopapplication':
return 'stop-application';
case 'upgradebackout':
return 'upgrade-backout';
case 'upgradepostcheck':
return 'upgrade-post-check';
case 'upgradeprecheck':
return 'upgrade-pre-check';
case 'upgradesoftware':
return 'upgrade-software';
case 'upgradebackup':
return 'upgrade-backup';
case 'attachvolume':
return 'attach-volume';
case 'detachvolume':
return 'detach-volume';
default:
return action.toLowerCase();
}
}
setValuesOnFileUploadFailure() {
this.flag = 1;
this.oldListName1 = '';
this.vmJson = {};
this.vnfcJson = {};
this.subPayload = {};
this.vmPayload = [];
this.payload = {};
this.action = '';
this.actionIdentifiers = {};
this.apiRequest = '';
this.apiResponse = '';
this.enableCounterDiv = false;
this.enableAbort = false;
this.enableTestButton = false;
this.enableDownload = false;
}
}