Skip Wonnell | 2c977e2 | 2018-03-01 08:30:15 -0600 | [diff] [blame] | 1 | /* |
| 2 | ============LICENSE_START========================================== |
| 3 | =================================================================== |
| 4 | Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. |
| 5 | =================================================================== |
| 6 | |
| 7 | Unless otherwise specified, all software contained herein is licensed |
| 8 | under the Apache License, Version 2.0 (the License); |
| 9 | you may not use this software except in compliance with the License. |
| 10 | You may obtain a copy of the License at |
| 11 | |
| 12 | http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | |
| 14 | Unless required by applicable law or agreed to in writing, software |
| 15 | distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | See the License for the specific language governing permissions and |
| 18 | limitations under the License. |
| 19 | |
| 20 | ECOMP is a trademark and service mark of AT&T Intellectual Property. |
| 21 | ============LICENSE_END============================================ |
| 22 | */ |
| 23 | |
| 24 | import { Component, OnInit } from '@angular/core'; |
| 25 | |
| 26 | import { saveAs } from 'file-saver'; |
| 27 | import { Location } from '@angular/common'; |
| 28 | import { ActivatedRoute, Router } from '@angular/router'; |
| 29 | import { NotificationService } from '.././shared/services/notification.service'; |
| 30 | import { ParamShareService } from '.././shared/services/paramShare.service'; |
| 31 | import { MappingEditorService } from '.././shared/services/mapping-editor.service'; |
| 32 | import { NotificationsService } from 'angular2-notifications'; |
| 33 | import { HttpUtilService } from '.././shared/services/httpUtil/http-util.service'; |
| 34 | import 'rxjs/add/observable/interval'; |
| 35 | import { Observable } from 'rxjs/Observable'; |
| 36 | import { environment } from '../.././environments/environment'; |
| 37 | import { UtilityService } from '.././shared/services/utilityService/utility.service'; |
| 38 | import 'rxjs/add/operator/map'; |
| 39 | import * as XLSX from 'xlsx'; |
| 40 | import { NgProgress } from 'ngx-progressbar'; |
| 41 | |
| 42 | |
| 43 | let YAML = require('yamljs'); |
| 44 | |
| 45 | type AOA = Array<Array<any>>; |
| 46 | declare var $: any; |
| 47 | |
| 48 | @Component({ selector: 'test', templateUrl: './test.component.html', styleUrls: ['./test.component.css'] }) |
| 49 | export 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; |
| 104 | |
| 105 | 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, |
| 106 | private utiltiy: UtilityService, private ngProgress: NgProgress) { |
| 107 | |
| 108 | } |
| 109 | |
| 110 | ngOnInit() { |
| 111 | |
| 112 | |
| 113 | } |
| 114 | |
| 115 | prepareFileName(): any { |
| 116 | let fileNameObject: any = this.mappingEditorService.latestAction; |
| 117 | return fileNameObject; |
| 118 | } |
| 119 | |
| 120 | /*public download() { |
| 121 | let stringData: any; |
| 122 | stringData = JSON.stringify(this.paramShareService.getSessionParamData()); |
| 123 | let paramsKeyValueFromEditor: JSON; |
| 124 | paramsKeyValueFromEditor = JSON.parse(stringData); |
| 125 | let fileName = 'param_' + this.action + '_' + this.type + '_' + "0.0.1" + 'V'; |
| 126 | this.JSONToCSVConvertor([paramsKeyValueFromEditor], fileName, true); |
| 127 | |
| 128 | }*/ |
| 129 | |
| 130 | public download() { |
| 131 | if (this.apiRequest) { |
| 132 | var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_request'; |
| 133 | var theJSON = this.apiRequest; |
| 134 | if (fileName != null || fileName != '') { |
| 135 | var blob = new Blob([theJSON], { |
| 136 | type: 'text/json' |
| 137 | }); |
| 138 | saveAs(blob, fileName); |
| 139 | } |
| 140 | } |
| 141 | else { |
| 142 | this.nService.error('Error', 'Please upload spreadsheet to download the request and response'); |
| 143 | } |
| 144 | |
| 145 | if (this.apiResponse) { |
| 146 | var fileName = 'test_' + this.action + '_' + this.actionIdentifiers['vnf-id'] + '_response'; |
| 147 | var theJSON = this.apiResponse; |
| 148 | if (fileName != null || fileName != '') { |
| 149 | var blob = new Blob([theJSON], { |
| 150 | type: 'text/json' |
| 151 | }); |
| 152 | saveAs(blob, fileName); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | |
| 159 | public abortTest() { |
| 160 | //this.apiResponse = ""; |
| 161 | this.enableBrowse = true; |
| 162 | this.enableTestButton = true; |
| 163 | this.enablePollButton = true; |
| 164 | this.subscribe.unsubscribe(); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /*JSONToCSVConvertor(JSONData, fileName, ShowLabel) { |
| 170 | //If JSONData is not an object then JSON.parse will parse the JSON string in an Object |
| 171 | var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData; |
| 172 | |
| 173 | var CSV = ''; |
| 174 | |
| 175 | //This condition will generate the Label/Header |
| 176 | if (ShowLabel) { |
| 177 | var testRow = ""; |
| 178 | for (var index in arrData[0]) { |
| 179 | |
| 180 | CSV += arrData[0][index].name + '\t' + arrData[0][index].value + '\t' + arrData[0][index].source + '\r\n'; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (CSV == '') { |
| 185 | alert("Invalid data"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | //Initialize file format you want csv or xls |
| 190 | var uri = 'data:application/vnd.ms-excel,' + encodeURI(CSV); |
| 191 | |
| 192 | var link = document.createElement("a"); |
| 193 | link.href = uri; |
| 194 | |
| 195 | link.download = fileName + ".xls"; |
| 196 | |
| 197 | //this part will append the anchor tag and remove it after automatic click |
| 198 | document.body.appendChild(link); |
| 199 | link.click(); |
| 200 | document.body.removeChild(link); |
| 201 | }*/ |
| 202 | |
| 203 | excelBrowseOption() { |
| 204 | $('#excelInputFile').trigger('click'); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | upload(evt: any) { |
| 209 | /* wire up file reader */ |
| 210 | $('#filesparam').trigger('click'); |
| 211 | const target: DataTransfer = <DataTransfer>(evt.target); |
| 212 | |
| 213 | this.uploadFileName = evt.target.files[0].name; |
| 214 | var fileExtension = this.uploadFileName.substr(this.uploadFileName.lastIndexOf('.') + 1); |
| 215 | |
| 216 | if (target.files.length != 1) { |
| 217 | throw new Error('Cannot upload multiple files on the entry'); |
| 218 | } |
| 219 | if (fileExtension.toUpperCase() === 'XLS' || fileExtension.toUpperCase() === 'XLSX') { |
| 220 | const reader = new FileReader(); |
| 221 | reader.onload = (e: any) => { |
| 222 | /* read workbook */ |
| 223 | const bstr = e.target.result; |
| 224 | const wb = XLSX.read(bstr, { type: 'binary' }); |
| 225 | /* grab first sheet */ |
| 226 | const wsname = wb.SheetNames[0]; |
| 227 | const ws = wb.Sheets[wsname]; |
| 228 | |
| 229 | /* save data */ |
| 230 | this.requestId = '' |
| 231 | this.enableTestButton = true; |
| 232 | this.enableAbort = true; |
| 233 | this.enablePollButton = true; |
| 234 | |
| 235 | if (this.subscribe && this.subscribe != undefined) { |
| 236 | this.enableCounterDiv = false; |
| 237 | this.subscribe.unsubscribe(); |
| 238 | } |
| 239 | this.apiRequest = ''; |
| 240 | this.apiResponse = ''; |
| 241 | this.showStatusResponseDiv = false; |
| 242 | this.errorResponse = ''; |
| 243 | this.statusResponse = ''; |
| 244 | |
| 245 | let arrData = (<AOA>(XLSX.utils.sheet_to_json(ws, { blankrows: false }))); |
| 246 | this.nService.success('Success', 'SpreadSheet uploaded successfully'); |
| 247 | |
| 248 | |
| 249 | console.log('Array data ==' + arrData[0]); |
| 250 | this.vmPayload = []; |
| 251 | this.subPayload = {}; |
| 252 | this.vmJson = {}; |
| 253 | this.flag = 1; |
| 254 | this.payload = {}; |
| 255 | this.oldListName1 = ''; |
| 256 | this.actionIdentifiers = {}; |
| 257 | for (var i = 0; i < arrData.length; i++) { |
| 258 | var element = arrData[i]; |
| 259 | if (element['TagName'] === 'action') { |
| 260 | this.action = element['Value']; |
| 261 | } |
| 262 | if (element['List Name'] === 'action-identifiers') { |
| 263 | this.vnfId = element['Value']; |
| 264 | var key = element['TagName']; |
| 265 | var value = element['Value']; |
| 266 | if (key && value) { |
| 267 | this.actionIdentifiers[key] = value; |
| 268 | |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (element['List Name'] === 'payload') { |
| 273 | var listName1 = element['List Name_1']; |
| 274 | var listName2 = element['List Name_2']; |
| 275 | var listName3 = element['List Name_3']; |
| 276 | var key = element['TagName']; |
| 277 | var value = element['Value']; |
| 278 | if (listName1) { |
| 279 | if (this.oldListName1 == '' || (listName1 === this.oldListName1)) { |
| 280 | this.constructTestPayload(listName2, listName3, key, value); |
| 281 | this.payload[listName1] = this.subPayload; |
| 282 | } |
| 283 | else { |
| 284 | this.subPayload = {}; |
| 285 | this.constructTestPayload(listName2, listName3, key, value); |
| 286 | this.payload[listName1] = this.subPayload; |
| 287 | } |
| 288 | this.oldListName1 = listName1; |
| 289 | } |
| 290 | else { |
| 291 | this.payload[key] = value; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | //console.log("VM JSON===" + JSON.stringify(this.vmPayload)) |
| 297 | // console.log('VM payload===' + JSON.stringify(this.payload)); |
| 298 | }; |
| 299 | |
| 300 | reader.readAsBinaryString(target.files[0]); |
| 301 | } |
| 302 | else { |
| 303 | this.nService.error('Error', 'Incorrect spreadsheet uploaded'); |
| 304 | this.flag = 1; |
| 305 | this.oldListName1 = ''; |
| 306 | this.vmJson = {}; |
| 307 | this.vnfcJson = {}; |
| 308 | this.subPayload = {}; |
| 309 | this.vmPayload = []; |
| 310 | this.payload = {}; |
| 311 | this.action = ''; |
| 312 | this.actionIdentifiers = {}; |
| 313 | this.apiRequest = ''; |
| 314 | this.apiResponse = ''; |
| 315 | this.enableCounterDiv = false; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | constructTestPayload(listName2, listName3, key, value) { |
| 320 | if (listName2 == undefined && listName3 == undefined) { |
| 321 | this.subPayload[key] = value; |
| 322 | } |
| 323 | if (listName2) { |
| 324 | |
| 325 | if (!listName3) { |
| 326 | |
| 327 | //vmPayload.push(vmJson) |
| 328 | this.vmJson = {}; |
| 329 | this.vnfcJson = {}; |
| 330 | this.vmJson[key] = value; |
| 331 | this.flag = 0; |
| 332 | } |
| 333 | else { |
| 334 | this.vnfcJson[key] = value; |
| 335 | this.vmJson['vnfc'] = this.vnfcJson; |
| 336 | this.flag = 1; |
| 337 | } |
| 338 | if (this.vmJson) this.lastvmJson = this.vmJson; |
| 339 | if (this.flag == 0) { |
| 340 | this.vmPayload.push(this.lastvmJson); |
| 341 | if (this.vmPayload) this.subPayload['vm'] = this.vmPayload; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | constructRequest() { |
| 347 | console.log('payload==' + JSON.stringify(this.payload)); |
| 348 | let timeStamp = new Date().toISOString(); |
| 349 | console.log('timestamp==' + timeStamp); |
| 350 | let reqId; |
| 351 | this.requestId = reqId = new Date().getTime().toString(); |
| 352 | let data = { |
| 353 | 'input': { |
| 354 | 'common-header': { |
| 355 | 'timestamp': timeStamp, |
| 356 | 'api-ver': '2.00', |
| 357 | 'originator-id': 'CDT', |
| 358 | 'request-id': this.requestId, |
| 359 | 'sub-request-id': this.requestId, |
| 360 | 'flags': { |
| 361 | 'mode': 'NORMAL', |
| 362 | 'force': 'TRUE', |
| 363 | 'ttl': 3600 |
| 364 | } |
| 365 | }, |
| 366 | 'action': this.action, |
| 367 | 'action-identifiers': this.actionIdentifiers, |
| 368 | 'payload': JSON.stringify(this.payload) |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | return data; |
| 373 | } |
| 374 | |
| 375 | testVnf() { |
| 376 | //let payload = '{"request-parameters":{"vnf-host-ip-address":"' + this.host + '"},"configuration-parameters":"' + JSON.stringify(this.formattedNameValuePairs) + '"}"'; |
| 377 | //let payload = '{"request-parameters":{"host-ip-address:"' + this.host + '",port-number:"'+port+'"}}'; |
| 378 | |
| 379 | this.enableBrowse = false; |
| 380 | this.enableTestButton = false; |
| 381 | this.enablePollButton = false; |
| 382 | this.timer = Observable.interval(10000); |
| 383 | this.subscribe = this.timer.subscribe((t) => this.pollTestStatus()); |
| 384 | //console.log('full payload==' + JSON.stringify(this.apiRequest)); |
| 385 | this.ngProgress.start(); |
| 386 | this.apiRequest = JSON.stringify(this.constructRequest()); |
| 387 | this.httpUtil.post( |
| 388 | { |
| 389 | url: environment.testVnf + this.getUrlEndPoint(this.action.toLowerCase()), data: this.apiRequest |
| 390 | }) |
| 391 | .subscribe(resp => { |
| 392 | this.apiResponse = JSON.stringify(resp); |
| 393 | this.enableBrowse = true; |
| 394 | this.enableTestButton = true; |
| 395 | this.ngProgress.done(); |
| 396 | }, |
| 397 | error => { |
| 398 | this.nService.error('Error', 'Error in connecting to APPC Server'); |
| 399 | this.enableBrowse = true; |
| 400 | this.enableTestButton = true; |
| 401 | this.enablePollButton = true; |
| 402 | this.enableCounterDiv = false; |
| 403 | this.subscribe.unsubscribe(); |
| 404 | |
| 405 | }); |
| 406 | |
| 407 | setTimeout(() => { |
| 408 | this.ngProgress.done(); |
| 409 | }, 3500); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | pollTestStatus() { |
| 414 | if (this.requestId && this.actionIdentifiers['vnf-id']) { |
| 415 | // console.log("payload==" + JSON.stringify(this.payload)) |
| 416 | let timeStamp = new Date().toISOString(); |
| 417 | // console.log("timestamp==" + timeStamp) |
| 418 | let reqId = new Date().getTime().toString(); |
| 419 | let data = { |
| 420 | 'input': { |
| 421 | 'common-header': { |
| 422 | 'timestamp': timeStamp, |
| 423 | 'api-ver': '2.00', |
| 424 | 'originator-id': 'CDT', |
| 425 | 'request-id': reqId, |
| 426 | 'sub-request-id': reqId, |
| 427 | 'flags': { |
| 428 | 'mode': 'NORMAL', |
| 429 | 'force': 'TRUE', |
| 430 | 'ttl': 3600 |
| 431 | } |
| 432 | }, |
| 433 | 'action': 'ActionStatus', |
| 434 | 'action-identifiers': this.actionIdentifiers, |
| 435 | 'payload': '{"request-id":' + this.requestId + '}' |
| 436 | } |
| 437 | }; |
| 438 | //this.ngProgress.start(); |
| 439 | this.httpUtil.post( |
| 440 | { |
| 441 | url: environment.checkTestStatus, data: data |
| 442 | |
| 443 | }) |
| 444 | .subscribe(resp => { |
| 445 | // console.log('Response==' + JSON.stringify(resp)); |
| 446 | this.statusResponse = JSON.stringify(resp); |
| 447 | var status = '' |
| 448 | var statusReason = '' |
| 449 | this.enableCounterDiv = true; |
| 450 | this.pollCounter++; |
| 451 | //this.statusResponse=JSON.parse(this.statusResponse) |
| 452 | if (resp.output) var timeStamp = resp.output['common-header'].timestamp; |
| 453 | if (resp.output.payload) { |
| 454 | var payload = resp.output.payload.replace(/\\/g, "") |
| 455 | try { |
| 456 | payload = JSON.parse(payload) |
| 457 | status = payload['status']; |
| 458 | statusReason = payload['status-reason']; |
| 459 | } |
| 460 | catch (err) { |
| 461 | console.log("error" + err) |
| 462 | } |
| 463 | } |
| 464 | if (timeStamp && status && statusReason) { |
| 465 | this.showStatusResponseDiv = true; |
| 466 | this.outputTimeStamp = timeStamp; |
| 467 | this.status = status; |
| 468 | this.statusReason = statusReason; |
| 469 | if (status.toUpperCase() === 'SUCCESS') { |
| 470 | this.subscribe.unsubscribe(); |
| 471 | this.enablePollButton = true; |
| 472 | } |
| 473 | if (status.toUpperCase() === 'FAILED') { |
| 474 | this.subscribe.unsubscribe(); |
| 475 | this.enablePollButton = true; |
| 476 | } |
| 477 | } |
| 478 | else { |
| 479 | this.showStatusResponseDiv = false; |
| 480 | } |
| 481 | |
| 482 | // this.ngProgress.done(); |
| 483 | }, |
| 484 | error => { |
| 485 | this.statusResponse = null; |
| 486 | this.showStatusResponseDiv = false; |
| 487 | this.errorResponse = 'Error Connecting to APPC server'; |
| 488 | this.enableCounterDiv = false; |
| 489 | this.subscribe.unsubscribe(); |
| 490 | }); |
| 491 | |
| 492 | } |
| 493 | else { |
| 494 | this.nService.error("Error", "Please enter vnf Id & request Id"); |
| 495 | } |
| 496 | // setTimeout(() => { |
| 497 | // this.ngProgress.done(); |
| 498 | // }, 3500); |
| 499 | } |
| 500 | |
| 501 | getUrlEndPoint(action) { |
| 502 | switch (action) { |
| 503 | case 'configmodify': |
| 504 | return 'config-modify'; |
| 505 | case 'configbackup': |
| 506 | return 'config-backup'; |
| 507 | case 'configrestore': |
| 508 | return 'config-restore'; |
| 509 | case 'healthcheck': |
| 510 | return 'health-check'; |
| 511 | case 'quiescetraffic': |
| 512 | return 'quiesce-traffic'; |
| 513 | case 'resumetraffic': |
| 514 | return 'resume-traffic'; |
| 515 | case 'startapplication': |
| 516 | return 'start-application'; |
| 517 | case 'stopapplication': |
| 518 | return 'stop-application'; |
| 519 | case 'upgradebackout': |
| 520 | return 'upgrade-backout'; |
| 521 | case 'upgradepostcheck': |
| 522 | return 'upgrade-post-check'; |
| 523 | case 'upgradeprecheck': |
| 524 | return 'upgrade-pre-check'; |
| 525 | case 'upgradesoftware': |
| 526 | return 'upgrade-software'; |
| 527 | default: |
| 528 | return action.toLowerCase(); |
| 529 | } |
| 530 | |
| 531 | } |
| 532 | |
| 533 | } |