/* ============LICENSE_START========================================== =================================================================== Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. =================================================================== 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. ECOMP is a trademark and service mark of AT&T Intellectual Property. ============LICENSE_END============================================ */ import { Component, OnInit } from '@angular/core'; import { saveAs } from 'file-saver'; import { Location } from '@angular/common'; import { ActivatedRoute, Router } from '@angular/router'; import { NotificationService } from '.././shared/services/notification.service'; 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>; 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; 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, private utiltiy: UtilityService, private ngProgress: NgProgress) { } ngOnInit() { } prepareFileName(): any { let fileNameObject: any = this.mappingEditorService.latestAction; return fileNameObject; } /*public download() { let stringData: any; stringData = JSON.stringify(this.paramShareService.getSessionParamData()); let paramsKeyValueFromEditor: JSON; paramsKeyValueFromEditor = JSON.parse(stringData); let fileName = 'param_' + this.action + '_' + this.type + '_' + "0.0.1" + 'V'; this.JSONToCSVConvertor([paramsKeyValueFromEditor], fileName, true); }*/ 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; this.subscribe.unsubscribe(); } /*JSONToCSVConvertor(JSONData, fileName, ShowLabel) { //If JSONData is not an object then JSON.parse will parse the JSON string in an Object var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData; var CSV = ''; //This condition will generate the Label/Header if (ShowLabel) { var testRow = ""; for (var index in arrData[0]) { CSV += arrData[0][index].name + '\t' + arrData[0][index].value + '\t' + arrData[0][index].source + '\r\n'; } } if (CSV == '') { alert("Invalid data"); return; } //Initialize file format you want csv or xls var uri = 'data:application/vnd.ms-excel,' + encodeURI(CSV); var link = document.createElement("a"); link.href = uri; link.download = fileName + ".xls"; //this part will append the anchor tag and remove it after automatic click document.body.appendChild(link); link.click(); document.body.removeChild(link); }*/ excelBrowseOption() { $('#excelInputFile').trigger('click'); } upload(evt: any) { /* wire up file reader */ $('#filesparam').trigger('click'); const target: DataTransfer = (evt.target); 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') { 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 = ''; let arrData = ((XLSX.utils.sheet_to_json(ws, { blankrows: false }))); this.nService.success('Success', 'SpreadSheet uploaded successfully'); console.log('Array data ==' + arrData[0]); this.vmPayload = []; this.subPayload = {}; this.vmJson = {}; this.flag = 1; this.payload = {}; this.oldListName1 = ''; this.actionIdentifiers = {}; 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); this.payload[listName1] = this.subPayload; } else { this.subPayload = {}; this.constructTestPayload(listName2, listName3, key, value); this.payload[listName1] = this.subPayload; } this.oldListName1 = listName1; } else { this.payload[key] = value; } } } //console.log("VM JSON===" + JSON.stringify(this.vmPayload)) // console.log('VM payload===' + JSON.stringify(this.payload)); }; reader.readAsBinaryString(target.files[0]); } else { this.nService.error('Error', 'Incorrect spreadsheet uploaded'); 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; } } 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() { console.log('payload==' + JSON.stringify(this.payload)); let timeStamp = new Date().toISOString(); console.log('timestamp==' + timeStamp); let reqId; this.requestId = reqId = new Date().getTime().toString(); let data = { 'input': { 'common-header': { 'timestamp': timeStamp, 'api-ver': '2.00', 'originator-id': 'CDT', '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() { //let payload = '{"request-parameters":{"vnf-host-ip-address":"' + this.host + '"},"configuration-parameters":"' + JSON.stringify(this.formattedNameValuePairs) + '"}"'; //let payload = '{"request-parameters":{"host-ip-address:"' + this.host + '",port-number:"'+port+'"}}'; this.enableBrowse = false; this.enableTestButton = false; this.enablePollButton = false; this.timer = Observable.interval(10000); this.subscribe = this.timer.subscribe((t) => this.pollTestStatus()); //console.log('full payload==' + JSON.stringify(this.apiRequest)); this.ngProgress.start(); this.apiRequest = JSON.stringify(this.constructRequest()); this.httpUtil.post( { url: environment.testVnf + 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; this.subscribe.unsubscribe(); }); setTimeout(() => { this.ngProgress.done(); }, 3500); } pollTestStatus() { if (this.requestId && this.actionIdentifiers['vnf-id']) { // console.log("payload==" + JSON.stringify(this.payload)) let timeStamp = new Date().toISOString(); // console.log("timestamp==" + timeStamp) let reqId = new Date().getTime().toString(); let data = { 'input': { 'common-header': { 'timestamp': timeStamp, 'api-ver': '2.00', 'originator-id': 'CDT', 'request-id': reqId, 'sub-request-id': reqId, 'flags': { 'mode': 'NORMAL', 'force': 'TRUE', 'ttl': 3600 } }, 'action': 'ActionStatus', 'action-identifiers': this.actionIdentifiers, 'payload': '{"request-id":' + this.requestId + '}' } }; //this.ngProgress.start(); this.httpUtil.post( { url: environment.checkTestStatus, data: data }) .subscribe(resp => { // console.log('Response==' + JSON.stringify(resp)); this.statusResponse = JSON.stringify(resp); var status = '' var statusReason = '' this.enableCounterDiv = true; this.pollCounter++; //this.statusResponse=JSON.parse(this.statusResponse) 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') { this.subscribe.unsubscribe(); this.enablePollButton = true; } if (status.toUpperCase() === 'FAILED') { this.subscribe.unsubscribe(); this.enablePollButton = true; } } else { this.showStatusResponseDiv = false; } // this.ngProgress.done(); }, error => { this.statusResponse = null; this.showStatusResponseDiv = false; this.errorResponse = 'Error Connecting to APPC server'; this.enableCounterDiv = false; this.subscribe.unsubscribe(); }); } else { this.nService.error("Error", "Please enter vnf Id & request Id"); } // setTimeout(() => { // this.ngProgress.done(); // }, 3500); } 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 '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'; default: return action.toLowerCase(); } } }