| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +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 Vm Migration Component |
| 20 | */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 21 | import { isNullOrUndefined } from 'util'; |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 22 | import { HttpHeaders } from '@angular/common/http'; |
| 23 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| 24 | import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 25 | import { Router } from '@angular/router'; |
| 26 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel'; |
| 28 | import { environment } from 'environment'; |
| 29 | import { VMMIGRATION } from 'NSInstanceModel'; |
| 30 | import { RestService } from 'RestService'; |
| 31 | import { SharedService } from 'SharedService'; |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 32 | import { VDUR, VNFInstanceDetails } from 'VNFInstanceModel'; |
| 33 | |
| 34 | /** |
| 35 | * Creating component |
| 36 | * @Component takes VmMigrationComponent.html as template url |
| 37 | */ |
| 38 | @Component({ |
| 39 | selector: 'app-vm-migration', |
| 40 | templateUrl: './VmMigrationComponent.html', |
| 41 | styleUrls: ['./VmMigrationComponent.scss'] |
| 42 | }) |
| 43 | export class VmMigrationComponent implements OnInit { |
| 44 | /** To inject services @public */ |
| 45 | public injector: Injector; |
| 46 | /** Instance for active modal service @public */ |
| 47 | public activeModal: NgbActiveModal; |
| 48 | /** Check the loading results @public */ |
| 49 | public isLoadingResults: Boolean = false; |
| 50 | /** Give the message for the loading @public */ |
| 51 | public message: string = 'PLEASEWAIT'; |
| 52 | /** FormGroup instance added to the form @ html @public */ |
| 53 | public migrationForm: FormGroup; |
| 54 | /** Items for the vdu-Id and count-index @public */ |
| 55 | public vdu: {}[]; |
| 56 | /** Selected VNFInstanceId @public */ |
| 57 | public selectedvnfId: string = ''; |
| 58 | /** Array holds VNFR Data filtered with nsr ID @public */ |
| 59 | public nsIdFilteredData: {}[] = []; |
| 60 | /** Items for the member types @public */ |
| 61 | public memberTypes: {}[]; |
| 62 | /** Contains MemberVNFIndex values @public */ |
| 63 | public memberVnfIndex: {}[] = []; |
| 64 | /** Contains vnfInstanceId of the selected MemberVnfIndex @public */ |
| 65 | public instanceId: string; |
| 66 | /** Form valid on submit trigger @public */ |
| 67 | public submitted: boolean = false; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 68 | /** Contains vduId @public */ |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 69 | public vduId: {}; |
| 70 | /** Items for countIndex @public */ |
| 71 | public countIndex: {}[]; |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 72 | /** Input contains component objects @private */ |
| 73 | @Input() private params: URLPARAMS; |
| 74 | /** FormBuilder instance added to the formBuilder @private */ |
| 75 | private formBuilder: FormBuilder; |
| 76 | /** Instance of the rest service @private */ |
| 77 | private restService: RestService; |
| 78 | /** Controls the header form @private */ |
| 79 | private headers: HttpHeaders; |
| 80 | /** Contains all methods related to shared @private */ |
| 81 | private sharedService: SharedService; |
| 82 | /** Holds the instance of AuthService class of type AuthService @private */ |
| 83 | private router: Router; |
| 84 | constructor(injector: Injector) { |
| 85 | this.injector = injector; |
| 86 | this.restService = this.injector.get(RestService); |
| 87 | this.activeModal = this.injector.get(NgbActiveModal); |
| 88 | this.formBuilder = this.injector.get(FormBuilder); |
| 89 | this.sharedService = this.injector.get(SharedService); |
| 90 | this.router = this.injector.get(Router); |
| 91 | } |
| 92 | /** convenience getter for easy access to form fields */ |
| 93 | get f(): FormGroup['controls'] { return this.migrationForm.controls; } |
| 94 | /** |
| 95 | * Lifecyle Hooks the trigger before component is instantiate |
| 96 | */ |
| 97 | public ngOnInit(): void { |
| 98 | this.initializeForm(); |
| 99 | this.getMemberVnfIndex(); |
| 100 | this.headers = new HttpHeaders({ |
| 101 | 'Content-Type': 'application/json', |
| 102 | Accept: 'application/json', |
| 103 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 104 | }); |
| 105 | } |
| 106 | /** Initialize Migration Forms @public */ |
| 107 | public initializeForm(): void { |
| 108 | this.migrationForm = this.formBuilder.group({ |
| 109 | memberVnfIndex: [null, [Validators.required]], |
| 110 | vduId: [null], |
| 111 | countIndex: [null], |
| 112 | migrateToHost: [null] |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | /** Getting MemberVnfIndex using NSDescriptor API @public */ |
| 117 | public getMemberVnfIndex(): void { |
| 118 | const vnfInstanceData: {}[] = []; |
| 119 | this.isLoadingResults = true; |
| 120 | this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => { |
| 121 | vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => { |
| 122 | const vnfDataObj: {} = |
| 123 | { |
| 124 | VNFD: vnfData['vnfd-ref'], |
| 125 | VNFInstanceId: vnfData._id, |
| 126 | MemberIndex: vnfData['member-vnf-index-ref'], |
| 127 | NS: vnfData['nsr-id-ref'] |
| 128 | }; |
| 129 | vnfInstanceData.push(vnfDataObj); |
| 130 | }); |
| 131 | const nsId: string = 'NS'; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 132 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 133 | this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id); |
| 134 | this.nsIdFilteredData.forEach((resVNF: {}[]): void => { |
| 135 | const memberIndex: string = 'MemberIndex'; |
| 136 | const vnfinstanceID: string = 'VNFInstanceId'; |
| 137 | const assignMemberIndex: {} = { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 138 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 139 | id: resVNF[memberIndex], |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 140 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 141 | vnfinstanceId: resVNF[vnfinstanceID] |
| 142 | }; |
| 143 | this.memberVnfIndex.push(assignMemberIndex); |
| 144 | }); |
| 145 | this.memberTypes = this.memberVnfIndex; |
| 146 | this.isLoadingResults = false; |
| 147 | }, (error: ERRORDATA): void => { |
| 148 | this.restService.handleError(error, 'get'); |
| 149 | this.isLoadingResults = false; |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | /** Getting vdu-id & count-index from VNFInstance API */ |
| 154 | public getVdu(id: string): void { |
| 155 | this.vdu = []; |
| 156 | const vnfInstanceData: {}[] = []; |
| 157 | this.instanceId = id; |
| 158 | this.getFormControl('vduId').setValue(null); |
| 159 | this.getFormControl('countIndex').setValue(null); |
| 160 | if (!isNullOrUndefined(id)) { |
| 161 | this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id). |
| 162 | subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => { |
| 163 | this.selectedvnfId = vnfInstanceDetail['vnfd-ref']; |
| 164 | const VDU: string = 'vdur'; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 165 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 166 | if (vnfInstanceDetail[VDU] !== undefined) { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 167 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 168 | vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => { |
| 169 | const vnfInstanceDataObj: {} = |
| 170 | { |
| 171 | 'count-index': vdu['count-index'], |
| 172 | VDU: vdu['vdu-id-ref'] |
| 173 | |
| 174 | }; |
| 175 | vnfInstanceData.push(vnfInstanceDataObj); |
| 176 | }); |
| 177 | this.vdu = vnfInstanceData; |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 178 | const vduName: string = 'VDU'; |
| 179 | this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} => |
| 180 | index === self.findIndex((t: {}): {} => ( |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 181 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 182 | t[vduName] === vdu[vduName] |
| 183 | )) |
| 184 | ); |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 185 | } |
| 186 | }, (error: ERRORDATA): void => { |
| 187 | this.restService.handleError(error, 'get'); |
| 188 | this.isLoadingResults = false; |
| 189 | }); |
| 190 | } |
| 191 | } |
| 192 | |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 193 | /** Getting count-index by filtering id */ |
| 194 | public getCountIndex(id: string): void { |
| 195 | const VDU: string = 'VDU'; |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 196 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | bc5d33e | 2022-08-25 08:19:13 +0530 | [diff] [blame] | 197 | this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id); |
| 198 | } |
| 199 | |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 200 | /** Trigger VM Migration on submit */ |
| 201 | public triggerMigration(): void { |
| 202 | this.submitted = true; |
| 203 | this.sharedService.cleanForm(this.migrationForm); |
| 204 | if (this.migrationForm.invalid) { return; } // Proceed, onces form is valid |
| 205 | const migrationPayload: VMMIGRATION = { |
| 206 | lcmOperationType: 'migrate', |
| 207 | vnfInstanceId: this.instanceId |
| 208 | }; |
| 209 | if (!isNullOrUndefined(this.migrationForm.value.migrateToHost)) { |
| 210 | migrationPayload.migrateToHost = this.migrationForm.value.migrateToHost; |
| 211 | } |
| 212 | if (!isNullOrUndefined(this.migrationForm.value.vduId) && !isNullOrUndefined(this.migrationForm.value.countIndex)) { |
| 213 | migrationPayload.vdu = { |
| 214 | vduId: this.migrationForm.value.vduId, |
| 215 | vduCountIndex: this.migrationForm.value.countIndex |
| 216 | }; |
| 217 | } else if (!isNullOrUndefined(this.migrationForm.value.vduId)) { |
| 218 | migrationPayload.vdu = { |
| 219 | vduId: this.migrationForm.value.vduId |
| 220 | }; |
| 221 | } else if (!isNullOrUndefined(this.migrationForm.value.countIndex)) { |
| 222 | migrationPayload.vdu = { |
| 223 | vduCountIndex: this.migrationForm.value.countIndex |
| 224 | }; |
| 225 | } |
| 226 | this.migrationInitialization(migrationPayload); |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** Initialize the Vm Migration operation @public */ |
| 230 | public migrationInitialization(migrationPayload: object): void { |
| 231 | this.isLoadingResults = true; |
| 232 | const apiURLHeader: APIURLHEADER = { |
| 233 | url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/migrate', |
| 234 | httpOptions: { headers: this.headers } |
| 235 | }; |
| 236 | const modalData: MODALCLOSERESPONSEDATA = { |
| 237 | message: 'Done' |
| 238 | }; |
| 239 | this.restService.postResource(apiURLHeader, migrationPayload).subscribe((result: {}): void => { |
| 240 | this.activeModal.close(modalData); |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 241 | this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => { |
| 242 | // Catch Navigation Error |
| 243 | }); |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 244 | }, (error: ERRORDATA): void => { |
| 245 | this.restService.handleError(error, 'post'); |
| 246 | this.isLoadingResults = false; |
| 247 | }); |
| 248 | } |
| 249 | |
| 250 | /** Used to get the AbstractControl of controlName passed @private */ |
| 251 | private getFormControl(controlName: string): AbstractControl { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 252 | // eslint-disable-next-line security/detect-object-injection |
| SANDHYA.JS | fced3d4 | 2022-04-28 20:28:17 +0530 | [diff] [blame] | 253 | return this.migrationForm.controls[controlName]; |
| 254 | } |
| 255 | } |