blob: 36ec2fcaada46463639a7b988b6223b1e22f1b90 [file] [log] [blame]
SANDHYA.JS017df362022-05-02 06:57:11 +05301/*
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 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, Injector, Input, OnInit } from '@angular/core';
23import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { Router } from '@angular/router';
25import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26import { TranslateService } from '@ngx-translate/core';
27import { NotifierService } from 'angular-notifier';
28import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
29import { environment } from 'environment';
30import { VerticalScaling } from 'NSInstanceModel';
31import { RestService } from 'RestService';
SANDHYA.JSc84f1122024-06-04 21:50:03 +053032import { SharedService, isNullOrUndefined } from 'SharedService';
SANDHYA.JSc7e64622023-10-12 11:05:48 +053033import { InstanceData, VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
SANDHYA.JS017df362022-05-02 06:57:11 +053034
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})
44export 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.JS0a34dfa2023-04-25 23:59:41 +053069 /** Contains vduId @public */
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +053070 public vduId: {};
71 /** Items for countIndex @public */
72 public countIndex: {}[];
SANDHYA.JS017df362022-05-02 06:57:11 +053073 /** 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.JSc7e64622023-10-12 11:05:48 +0530127 this.isLoadingResults = true;
SANDHYA.JS017df362022-05-02 06:57:11 +0530128 const vnfInstanceData: {}[] = [];
129 this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
130 vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530131 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.JS017df362022-05-02 06:57:11 +0530135 const vnfDataObj: {} =
136 {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530137 // eslint-disable-next-line security/detect-object-injection
138 VNFD: vnfData[vnfdRef],
SANDHYA.JS017df362022-05-02 06:57:11 +0530139 VNFInstanceId: vnfData._id,
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530140 // 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.JS017df362022-05-02 06:57:11 +0530146 };
147 vnfInstanceData.push(vnfDataObj);
148 });
149 const nsId: string = 'NS';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530150 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530151 this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530152 this.nsIdFilteredData.forEach((resVNF: InstanceData): void => {
SANDHYA.JS017df362022-05-02 06:57:11 +0530153 const assignMemberIndex: {} = {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530154 id: resVNF.MemberIndex,
155 vnfinstanceId: resVNF.VNFInstanceId
SANDHYA.JS017df362022-05-02 06:57:11 +0530156 };
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.JS0a34dfa2023-04-25 23:59:41 +0530178 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530179 if (vnfInstanceDetail[VDU] !== undefined) {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530180 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530181 vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
182 const vnfInstanceDataObj: {} =
183 {
184 'count-index': vdu['count-index'],
185 VDU: vdu['vdu-id-ref']
SANDHYA.JS017df362022-05-02 06:57:11 +0530186 };
187 vnfInstanceData.push(vnfInstanceDataObj);
188 });
189 this.vdu = vnfInstanceData;
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530190 const vduName: string = 'VDU';
191 this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
192 index === self.findIndex((t: {}): {} => (
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530193 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530194 t[vduName] === vdu[vduName]
195 ))
196 );
SANDHYA.JS017df362022-05-02 06:57:11 +0530197 }
198 }, (error: ERRORDATA): void => {
199 this.restService.handleError(error, 'get');
200 this.isLoadingResults = false;
201 });
202 }
203 }
204
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530205 /** Getting count-index by filtering id */
206 public getCountIndex(id: string): void {
207 const VDU: string = 'VDU';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530208 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530209 this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
210 }
211
SANDHYA.JS017df362022-05-02 06:57:11 +0530212 /** 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.JS0a34dfa2023-04-25 23:59:41 +0530248 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
249 // Catch Navigation Error
250 });
SANDHYA.JS017df362022-05-02 06:57:11 +0530251 }, (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.JS0a34dfa2023-04-25 23:59:41 +0530259 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530260 return this.scalingForm.controls[controlName];
261 }
262}