| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 TATA ELXSI |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | Author: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file VerticalScaling Component |
| 20 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| 23 | import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { Router } from '@angular/router'; |
| 25 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 26 | import { TranslateService } from '@ngx-translate/core'; |
| 27 | import { NotifierService } from 'angular-notifier'; |
| 28 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel'; |
| 29 | import { environment } from 'environment'; |
| 30 | import { VerticalScaling } from 'NSInstanceModel'; |
| 31 | import { RestService } from 'RestService'; |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame^] | 32 | import { SharedService, isNullOrUndefined } from 'SharedService'; |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 33 | import { InstanceData, VDUR, VNFInstanceDetails } from 'VNFInstanceModel'; |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Creating component |
| 37 | * @Component takes VerticalScalingComponent.html as template url |
| 38 | */ |
| 39 | @Component({ |
| 40 | selector: 'app-vertical-scaling', |
| 41 | templateUrl: './VerticalScalingComponent.html', |
| 42 | styleUrls: ['./VerticalScalingComponent.scss'] |
| 43 | }) |
| 44 | export class VerticalScalingComponent implements OnInit { |
| 45 | /** To inject services @public */ |
| 46 | public injector: Injector; |
| 47 | /** Instance for active modal service @public */ |
| 48 | public activeModal: NgbActiveModal; |
| 49 | /** Check the loading results @public */ |
| 50 | public isLoadingResults: Boolean = false; |
| 51 | /** Give the message for the loading @public */ |
| 52 | public message: string = 'PLEASEWAIT'; |
| 53 | /** FormGroup instance added to the form @ html @public */ |
| 54 | public scalingForm: FormGroup; |
| 55 | /** Items for the memberVNFIndex @public */ |
| 56 | public memberTypes: {}[]; |
| 57 | /** Contains MemberVNFIndex values @public */ |
| 58 | public memberVnfIndex: {}[] = []; |
| 59 | /** Contains vnfInstanceId of the selected MemberVnfIndex @public */ |
| 60 | public instanceId: string; |
| 61 | /** Items for vduId & countIndex @public */ |
| 62 | public vdu: {}[]; |
| 63 | /** Selected VNFInstanceId @public */ |
| 64 | public selectedvnfId: string = ''; |
| 65 | /** Array holds VNFR Data filtered with nsr ID @public */ |
| 66 | public nsIdFilteredData: {}[] = []; |
| 67 | /** Form valid on submit trigger @public */ |
| 68 | public submitted: boolean = false; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 69 | /** Contains vduId @public */ |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 70 | public vduId: {}; |
| 71 | /** Items for countIndex @public */ |
| 72 | public countIndex: {}[]; |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 73 | /** Input contains component objects @private */ |
| 74 | @Input() private params: URLPARAMS; |
| 75 | /** FormBuilder instance added to the formBuilder @private */ |
| 76 | private formBuilder: FormBuilder; |
| 77 | /** Instance of the rest service @private */ |
| 78 | private restService: RestService; |
| 79 | /** Controls the header form @private */ |
| 80 | private headers: HttpHeaders; |
| 81 | /** Contains all methods related to shared @private */ |
| 82 | private sharedService: SharedService; |
| 83 | /** Notifier service to popup notification @private */ |
| 84 | private notifierService: NotifierService; |
| 85 | /** Contains tranlsate instance @private */ |
| 86 | private translateService: TranslateService; |
| 87 | /** Holds the instance of AuthService class of type AuthService @private */ |
| 88 | private router: Router; |
| 89 | constructor(injector: Injector) { |
| 90 | this.injector = injector; |
| 91 | this.restService = this.injector.get(RestService); |
| 92 | this.activeModal = this.injector.get(NgbActiveModal); |
| 93 | this.formBuilder = this.injector.get(FormBuilder); |
| 94 | this.sharedService = this.injector.get(SharedService); |
| 95 | this.notifierService = this.injector.get(NotifierService); |
| 96 | this.translateService = this.injector.get(TranslateService); |
| 97 | this.router = this.injector.get(Router); |
| 98 | } |
| 99 | /** convenience getter for easy access to form fields */ |
| 100 | get f(): FormGroup['controls'] { return this.scalingForm.controls; } |
| 101 | /** |
| 102 | * Lifecyle Hooks the trigger before component is instantiate |
| 103 | */ |
| 104 | public ngOnInit(): void { |
| 105 | this.initializeForm(); |
| 106 | this.getMemberVnfIndex(); |
| 107 | this.headers = new HttpHeaders({ |
| 108 | 'Content-Type': 'application/json', |
| 109 | Accept: 'application/json', |
| 110 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 111 | }); |
| 112 | } |
| 113 | /** Initialize Scaling Forms @public */ |
| 114 | public initializeForm(): void { |
| 115 | this.scalingForm = this.formBuilder.group({ |
| 116 | memberVnfIndex: [null, [Validators.required]], |
| 117 | vduId: [null, [Validators.required]], |
| 118 | countIndex: [null, [Validators.required]], |
| 119 | virtualMemory: [null, [Validators.required]], |
| 120 | sizeOfStorage: [null, [Validators.required]], |
| 121 | numVirtualCpu: [null, [Validators.required]] |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | /** Getting MemberVnfIndex using VNFInstances API @public */ |
| 126 | public getMemberVnfIndex(): void { |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 127 | this.isLoadingResults = true; |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 128 | const vnfInstanceData: {}[] = []; |
| 129 | this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => { |
| 130 | vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => { |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 131 | const vnfdRef: string = 'vnfd-ref'; |
| 132 | const memberIndex: string = 'member-vnf-index-ref'; |
| 133 | const nsrId: string = 'nsr-id-ref'; |
| 134 | const vnfId: string = 'vnfd-id'; |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 135 | const vnfDataObj: {} = |
| 136 | { |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 137 | // eslint-disable-next-line security/detect-object-injection |
| 138 | VNFD: vnfData[vnfdRef], |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 139 | VNFInstanceId: vnfData._id, |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 140 | // eslint-disable-next-line security/detect-object-injection |
| 141 | MemberIndex: vnfData[memberIndex], |
| 142 | // eslint-disable-next-line security/detect-object-injection |
| 143 | NS: vnfData[nsrId], |
| 144 | // eslint-disable-next-line security/detect-object-injection |
| 145 | VNFID: vnfData[vnfId] |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 146 | }; |
| 147 | vnfInstanceData.push(vnfDataObj); |
| 148 | }); |
| 149 | const nsId: string = 'NS'; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 150 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 151 | this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id); |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 152 | this.nsIdFilteredData.forEach((resVNF: InstanceData): void => { |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 153 | const assignMemberIndex: {} = { |
| SANDHYA.JS | c7e6462 | 2023-10-12 11:05:48 +0530 | [diff] [blame] | 154 | id: resVNF.MemberIndex, |
| 155 | vnfinstanceId: resVNF.VNFInstanceId |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 156 | }; |
| 157 | this.memberVnfIndex.push(assignMemberIndex); |
| 158 | }); |
| 159 | this.memberTypes = this.memberVnfIndex; |
| 160 | this.isLoadingResults = false; |
| 161 | }, (error: ERRORDATA): void => { |
| 162 | this.restService.handleError(error, 'get'); |
| 163 | this.isLoadingResults = false; |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | /** Getting vdu-id & count-index from API */ |
| 168 | public getVdu(id: string): void { |
| 169 | const vnfInstanceData: {}[] = []; |
| 170 | this.getFormControl('vduId').setValue(null); |
| 171 | this.getFormControl('countIndex').setValue(null); |
| 172 | if (!isNullOrUndefined(id)) { |
| 173 | this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id). |
| 174 | subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => { |
| 175 | this.instanceId = id; |
| 176 | this.selectedvnfId = vnfInstanceDetail['vnfd-ref']; |
| 177 | const VDU: string = 'vdur'; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 178 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 179 | if (vnfInstanceDetail[VDU] !== undefined) { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 180 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 181 | vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => { |
| 182 | const vnfInstanceDataObj: {} = |
| 183 | { |
| 184 | 'count-index': vdu['count-index'], |
| 185 | VDU: vdu['vdu-id-ref'] |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 186 | }; |
| 187 | vnfInstanceData.push(vnfInstanceDataObj); |
| 188 | }); |
| 189 | this.vdu = vnfInstanceData; |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 190 | const vduName: string = 'VDU'; |
| 191 | this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} => |
| 192 | index === self.findIndex((t: {}): {} => ( |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 193 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 194 | t[vduName] === vdu[vduName] |
| 195 | )) |
| 196 | ); |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 197 | } |
| 198 | }, (error: ERRORDATA): void => { |
| 199 | this.restService.handleError(error, 'get'); |
| 200 | this.isLoadingResults = false; |
| 201 | }); |
| 202 | } |
| 203 | } |
| 204 | |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 205 | /** Getting count-index by filtering id */ |
| 206 | public getCountIndex(id: string): void { |
| 207 | const VDU: string = 'VDU'; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 208 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 209 | this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id); |
| 210 | } |
| 211 | |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 212 | /** Vertical Scaling on submit */ |
| 213 | public triggerVerticalScaling(): void { |
| 214 | this.submitted = true; |
| 215 | this.sharedService.cleanForm(this.scalingForm); |
| 216 | if (!this.scalingForm.invalid) { |
| 217 | const scalingPayload: VerticalScaling = { |
| 218 | lcmOperationType: 'verticalscale', |
| 219 | verticalScale: 'CHANGE_VNFFLAVOR', |
| 220 | nsInstanceId: this.params.id, |
| 221 | changeVnfFlavorData: { |
| 222 | vnfInstanceId: this.instanceId, |
| 223 | additionalParams: { |
| 224 | vduid: this.scalingForm.value.vduId, |
| 225 | vduCountIndex: this.scalingForm.value.countIndex, |
| 226 | virtualMemory: Number(this.scalingForm.value.virtualMemory), |
| 227 | sizeOfStorage: Number(this.scalingForm.value.sizeOfStorage), |
| 228 | numVirtualCpu: Number(this.scalingForm.value.numVirtualCpu) |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | this.verticalscaleInitialization(scalingPayload); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** Initialize the vertical scaling operation @public */ |
| 237 | public verticalscaleInitialization(scalingPayload: object): void { |
| 238 | this.isLoadingResults = true; |
| 239 | const apiURLHeader: APIURLHEADER = { |
| 240 | url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/verticalscale', |
| 241 | httpOptions: { headers: this.headers } |
| 242 | }; |
| 243 | const modalData: MODALCLOSERESPONSEDATA = { |
| 244 | message: 'Done' |
| 245 | }; |
| 246 | this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => { |
| 247 | this.activeModal.close(modalData); |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 248 | this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => { |
| 249 | // Catch Navigation Error |
| 250 | }); |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 251 | }, (error: ERRORDATA): void => { |
| 252 | this.restService.handleError(error, 'post'); |
| 253 | this.isLoadingResults = false; |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | /** Used to get the AbstractControl of controlName passed @private */ |
| 258 | private getFormControl(controlName: string): AbstractControl { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 259 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | 017df36 | 2022-05-02 06:57:11 +0530 | [diff] [blame] | 260 | return this.scalingForm.controls[controlName]; |
| 261 | } |
| 262 | } |