| /* |
| Copyright 2020 TATA ELXSI |
| |
| Licensed under the Apache License, Version 2.0 (the 'License'); |
| you may not use this file 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. |
| |
| Author: I.Lloret <illoret@minsait.com> |
| */ |
| /** |
| * @file ShowVduConsoleComponent Component |
| */ |
| import { HttpHeaders } from '@angular/common/http'; |
| import { Component, Injector, Input, OnInit } from '@angular/core'; |
| import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| import { Router } from '@angular/router'; |
| import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| import { TranslateService } from '@ngx-translate/core'; |
| import { NotifierService } from 'angular-notifier'; |
| import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel'; |
| import { environment } from 'environment'; |
| import { ShowVduConsole, OperateVnfResult } from 'NSInstanceModel'; |
| import { RestService } from 'RestService'; |
| import { SharedService, isNullOrUndefined } from 'SharedService'; |
| import { DF, VNFD } from 'VNFDModel'; |
| import { InstanceData, VDUR, VNFInstanceDetails } from 'VNFInstanceModel'; |
| |
| /** |
| * Creating component |
| * @Component takes ShowVduConsoleComponent.html as template url |
| */ |
| @Component({ |
| selector: 'app-show-vdu-console', |
| templateUrl: './ShowVduConsoleComponent.html', |
| styleUrls: ['./ShowVduConsoleComponent.scss'] |
| }) |
| export class ShowVduConsoleComponent implements OnInit { |
| /** To inject services @public */ |
| public injector: Injector; |
| /** Instance for active modal service @public */ |
| public activeModal: NgbActiveModal; |
| /** Check the loading results @public */ |
| public isLoadingResults: Boolean = false; |
| /** Give the message for the loading @public */ |
| public message: string = 'PLEASEWAIT'; |
| /** FormGroup instance added to the form @ html @public */ |
| public consoleForm: FormGroup; |
| /** Items for the memberVNFIndex @public */ |
| public memberTypes: {}[]; |
| /** Contains MemberVNFIndex values @public */ |
| public memberVnfIndex: {}[] = []; |
| /** Contains vnfInstanceId of the selected MemberVnfIndex @public */ |
| public instanceId: string; |
| /** Items for vduId & countIndex @public */ |
| public vdu: {}[]; |
| /** Selected VNFInstanceId @public */ |
| public selectedvnfId: string = ''; |
| /** Check day1-2 operation @public */ |
| /** TODO - show remove this probably */ |
| public 'day1-2': boolean; |
| /** Array holds VNFR Data filtered with nsr ID @public */ |
| public nsIdFilteredData: {}[] = []; |
| /** Form valid on submit trigger @public */ |
| public submitted: boolean = false; |
| /** Contains vduId @public */ |
| public vduId: {}; |
| /** Items for countIndex @public */ |
| public countIndex: {}[]; |
| /** Input contains component objects @private */ |
| @Input() private params: URLPARAMS; |
| /** FormBuilder instance added to the formBuilder @private */ |
| private formBuilder: FormBuilder; |
| /** Instance of the rest service @private */ |
| private restService: RestService; |
| /** Notifier service to popup notification @private */ |
| private notifierService: NotifierService; |
| /** Contains tranlsate instance @private */ |
| private translateService: TranslateService; |
| /** Controls the header form @private */ |
| private headers: HttpHeaders; |
| private consoleRequestStartTime: number; |
| /** Contains all methods related to shared @private */ |
| private sharedService: SharedService; |
| /** Holds the instance of AuthService class of type AuthService @private */ |
| private router: Router; |
| constructor(injector: Injector) { |
| this.injector = injector; |
| this.restService = this.injector.get(RestService); |
| this.notifierService = this.injector.get(NotifierService); |
| this.translateService = this.injector.get(TranslateService); |
| this.activeModal = this.injector.get(NgbActiveModal); |
| this.formBuilder = this.injector.get(FormBuilder); |
| this.sharedService = this.injector.get(SharedService); |
| this.router = this.injector.get(Router); |
| } |
| /** convenience getter for easy access to form fields */ |
| get f(): FormGroup['controls'] { return this.consoleForm.controls; } |
| /** |
| * Lifecyle Hooks the trigger before component is instantiate |
| */ |
| public ngOnInit(): void { |
| this.initializeForm(); |
| this.getMemberVnfIndex(); |
| this.headers = new HttpHeaders({ |
| 'Content-Type': 'application/json', |
| Accept: 'application/json', |
| 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| }); |
| } |
| /** Initialize start, stop or rebuild Forms @public */ |
| public initializeForm(): void { |
| this.consoleForm = this.formBuilder.group({ |
| memberVnfIndex: [null, [Validators.required]], |
| vduId: [null, [Validators.required]], |
| countIndex: [null, [Validators.required]] |
| }); |
| } |
| |
| /** Getting MemberVnfIndex using VNFInstances API @public */ |
| public getMemberVnfIndex(): void { |
| this.isLoadingResults = true; |
| const vnfInstanceData: {}[] = []; |
| this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => { |
| vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => { |
| const vnfdRef: string = 'vnfd-ref'; |
| const memberIndex: string = 'member-vnf-index-ref'; |
| const nsrId: string = 'nsr-id-ref'; |
| const vnfId: string = 'vnfd-id'; |
| const vnfDataObj: {} = |
| { |
| // eslint-disable-next-line security/detect-object-injection |
| VNFD: vnfData[vnfdRef], |
| VNFInstanceId: vnfData._id, |
| // eslint-disable-next-line security/detect-object-injection |
| MemberIndex: vnfData[memberIndex], |
| // eslint-disable-next-line security/detect-object-injection |
| NS: vnfData[nsrId], |
| // eslint-disable-next-line security/detect-object-injection |
| VNFID: vnfData[vnfId] |
| }; |
| vnfInstanceData.push(vnfDataObj); |
| }); |
| const nsId: string = 'NS'; |
| // eslint-disable-next-line security/detect-object-injection |
| this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id); |
| this.nsIdFilteredData.forEach((resVNF: InstanceData): void => { |
| const assignMemberIndex: {} = { |
| id: resVNF.MemberIndex, |
| vnfinstanceId: resVNF.VNFInstanceId |
| }; |
| this.memberVnfIndex.push(assignMemberIndex); |
| }); |
| this.memberTypes = this.memberVnfIndex; |
| this.isLoadingResults = false; |
| }, (error: ERRORDATA): void => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| /** Getting vdu-id & count-index from VNFInstance API */ |
| public getVdu(id: string): void { |
| const vnfInstanceData: {}[] = []; |
| this.getFormControl('vduId').setValue(null); |
| this.getFormControl('countIndex').setValue(null); |
| if (!isNullOrUndefined(id)) { |
| this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id). |
| subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => { |
| this.instanceId = id; |
| this.selectedvnfId = vnfInstanceDetail['vnfd-ref']; |
| const VDU: string = 'vdur'; |
| // eslint-disable-next-line security/detect-object-injection |
| if (vnfInstanceDetail[VDU] !== undefined) { |
| // eslint-disable-next-line security/detect-object-injection |
| vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => { |
| const vnfInstanceDataObj: {} = |
| { |
| 'count-index': vdu['count-index'], |
| VDU: vdu['vdu-id-ref'] |
| }; |
| vnfInstanceData.push(vnfInstanceDataObj); |
| }); |
| this.vdu = vnfInstanceData; |
| const vduName: string = 'VDU'; |
| this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} => |
| index === self.findIndex((t: {}): {} => ( |
| // eslint-disable-next-line security/detect-object-injection |
| t[vduName] === vdu[vduName] |
| )) |
| ); |
| } |
| this.checkDay12Operation(this.selectedvnfId); |
| }, (error: ERRORDATA): void => { |
| this.restService.handleError(error, 'get'); |
| this.isLoadingResults = false; |
| }); |
| } |
| } |
| |
| /** Getting count-index by filtering id */ |
| public getCountIndex(id: string): void { |
| const VDU: string = 'VDU'; |
| // eslint-disable-next-line security/detect-object-injection |
| this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id); |
| } |
| |
| /** To check primitve actions from VNFR */ |
| public checkDay12Operation(id: string): void { |
| const apiUrl: string = environment.VNFPACKAGES_URL + '?id=' + id; |
| this.restService.getResource(apiUrl).subscribe((vnfdInfo: VNFD[]): void => { |
| const vnfInstances: VNFD = vnfdInfo[0]; |
| if (!isNullOrUndefined(vnfInstances.df)) { |
| vnfInstances.df.forEach((df: DF): void => { |
| if (df['lcm-operations-configuration'] !== undefined) { |
| if (df['lcm-operations-configuration']['operate-vnf-op-config']['day1-2'] !== undefined) { |
| this['day1-2'] = true; |
| } |
| } else { |
| this['day1-2'] = false; |
| } |
| }); |
| } |
| }, (error: ERRORDATA): void => { |
| this.isLoadingResults = false; |
| this.restService.handleError(error, 'get'); |
| }); |
| } |
| |
| /** Check and sends a request to get console */ |
| public prepareGetConsole(): void { |
| this.submitted = true; |
| this.sharedService.cleanForm(this.consoleForm); |
| if (this.consoleForm.invalid) { return; } // Proceed, onces form is valid |
| const prepareConsolePayload: ShowVduConsole = { |
| updateType: 'OPERATE_VNF', |
| operateVnfData: { |
| vnfInstanceId: this.instanceId, |
| changeStateTo: 'console', |
| additionalParam: { |
| 'run-day1': false, |
| vdu_id: this.consoleForm.value.vduId, |
| 'count-index': this.consoleForm.value.countIndex |
| } |
| } |
| }; |
| this.startGetConsole(prepareConsolePayload); |
| } |
| |
| /** Initialize get console operation @public */ |
| public startGetConsole(prepareConsolePayload: object): void { |
| this.message = 'LAUNCHINGCONSOLE'; |
| this.consoleRequestStartTime = Date.now(); |
| this.isLoadingResults = true; |
| const apiURLHeader: APIURLHEADER = { |
| url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/update', |
| httpOptions: { headers: this.headers } |
| }; |
| const modalData: MODALCLOSERESPONSEDATA = { |
| message: 'Done' |
| }; |
| this.restService.postResource(apiURLHeader, prepareConsolePayload).subscribe((result: OperateVnfResult): void => { |
| // Start waiting for the operation to complete |
| this.waitOperationCompleted(result.id); |
| }, (error: ERRORDATA): void => { |
| this.restService.handleError(error, 'post'); |
| this.isLoadingResults = false; |
| }); |
| } |
| |
| private waitOperationCompleted(operationId: string): void { |
| let elapsedTime = 0; |
| const interval = 3000; // 3 seconds interval between status checks |
| const maxWaitTime = 2 * 60 * 1000; // 2 minutes in milliseconds |
| |
| // Set interval to periodically check if the operation is completed |
| const checkInterval = setInterval(() => { |
| if (elapsedTime >= maxWaitTime) { |
| clearInterval(checkInterval); // Stop polling after 2 minutes |
| this.notifierService.notify('error', this.translateService.instant('OPERATIONFAILED')); |
| this.isLoadingResults = false; |
| return; |
| } |
| |
| this.getOperationStatus(operationId).subscribe((operationData: any) => { |
| if (operationData && operationData.operationState === 'COMPLETED') { |
| clearInterval(checkInterval); // Stop polling when operation is completed |
| elapsedTime = (Date.now() - this.consoleRequestStartTime); |
| this.openVmrcConsole(operationData.operationResultData.url); |
| // Close the modal window now |
| const modalData: MODALCLOSERESPONSEDATA = { |
| message: 'Done' |
| }; |
| this.activeModal.close(modalData); |
| this.router.navigate(['/instances/ns/']).catch((): void => { |
| // Catch Navigation Error |
| }); |
| } else if (operationData && operationData.operationState === 'FAILED') { |
| clearInterval(checkInterval); // Stop polling on error |
| this.notifierService.notify('error', this.translateService.instant('OPERATIONFAILED')); |
| this.isLoadingResults = false; |
| } |
| }, (error: ERRORDATA) => { |
| clearInterval(checkInterval); // Stop polling on error |
| this.restService.handleError(error, 'post'); |
| this.isLoadingResults = false; |
| }); |
| elapsedTime += interval; |
| }, interval); |
| } |
| |
| /** obtain the status of the getconsole operation */ |
| private getOperationStatus(operationId: string) { |
| const operationQueryUrl: string = environment.NSHISTORYOPERATIONS_URL + '/' + operationId; |
| // Make GET request to check operation status |
| return this.restService.getResource(operationQueryUrl); |
| } |
| |
| private openVmrcConsole(vmrcUrl) { |
| const link = document.createElement('a'); |
| link.href = vmrcUrl; |
| link.style.display = 'none'; |
| document.body.appendChild(link); |
| link.click(); |
| document.body.removeChild(link); |
| } |
| |
| /** Used to get the AbstractControl of controlName passed @private */ |
| private getFormControl(controlName: string): AbstractControl { |
| // eslint-disable-next-line security/detect-object-injection |
| return this.consoleForm.controls[controlName]; |
| } |
| } |