blob: 4d96f8aa897e3a1c49d2783b0e57a93c5aa1b8f8 [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 */
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053021import { isNullOrUndefined } from 'util';
SANDHYA.JS017df362022-05-02 06:57:11 +053022import { HttpHeaders } from '@angular/common/http';
23import { Component, Injector, Input, OnInit } from '@angular/core';
24import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
25import { Router } from '@angular/router';
26import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
27import { TranslateService } from '@ngx-translate/core';
28import { NotifierService } from 'angular-notifier';
29import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
30import { environment } from 'environment';
31import { VerticalScaling } from 'NSInstanceModel';
32import { RestService } from 'RestService';
33import { SharedService } from 'SharedService';
SANDHYA.JS017df362022-05-02 06:57:11 +053034import { VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
35
36/**
37 * Creating component
38 * @Component takes VerticalScalingComponent.html as template url
39 */
40@Component({
41 selector: 'app-vertical-scaling',
42 templateUrl: './VerticalScalingComponent.html',
43 styleUrls: ['./VerticalScalingComponent.scss']
44})
45export class VerticalScalingComponent implements OnInit {
46 /** To inject services @public */
47 public injector: Injector;
48 /** Instance for active modal service @public */
49 public activeModal: NgbActiveModal;
50 /** Check the loading results @public */
51 public isLoadingResults: Boolean = false;
52 /** Give the message for the loading @public */
53 public message: string = 'PLEASEWAIT';
54 /** FormGroup instance added to the form @ html @public */
55 public scalingForm: FormGroup;
56 /** Items for the memberVNFIndex @public */
57 public memberTypes: {}[];
58 /** Contains MemberVNFIndex values @public */
59 public memberVnfIndex: {}[] = [];
60 /** Contains vnfInstanceId of the selected MemberVnfIndex @public */
61 public instanceId: string;
62 /** Items for vduId & countIndex @public */
63 public vdu: {}[];
64 /** Selected VNFInstanceId @public */
65 public selectedvnfId: string = '';
66 /** Array holds VNFR Data filtered with nsr ID @public */
67 public nsIdFilteredData: {}[] = [];
68 /** Form valid on submit trigger @public */
69 public submitted: boolean = false;
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053070 /** Contains vduId @public */
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +053071 public vduId: {};
72 /** Items for countIndex @public */
73 public countIndex: {}[];
SANDHYA.JS017df362022-05-02 06:57:11 +053074 /** Input contains component objects @private */
75 @Input() private params: URLPARAMS;
76 /** FormBuilder instance added to the formBuilder @private */
77 private formBuilder: FormBuilder;
78 /** Instance of the rest service @private */
79 private restService: RestService;
80 /** Controls the header form @private */
81 private headers: HttpHeaders;
82 /** Contains all methods related to shared @private */
83 private sharedService: SharedService;
84 /** Notifier service to popup notification @private */
85 private notifierService: NotifierService;
86 /** Contains tranlsate instance @private */
87 private translateService: TranslateService;
88 /** Holds the instance of AuthService class of type AuthService @private */
89 private router: Router;
90 constructor(injector: Injector) {
91 this.injector = injector;
92 this.restService = this.injector.get(RestService);
93 this.activeModal = this.injector.get(NgbActiveModal);
94 this.formBuilder = this.injector.get(FormBuilder);
95 this.sharedService = this.injector.get(SharedService);
96 this.notifierService = this.injector.get(NotifierService);
97 this.translateService = this.injector.get(TranslateService);
98 this.router = this.injector.get(Router);
99 }
100 /** convenience getter for easy access to form fields */
101 get f(): FormGroup['controls'] { return this.scalingForm.controls; }
102 /**
103 * Lifecyle Hooks the trigger before component is instantiate
104 */
105 public ngOnInit(): void {
106 this.initializeForm();
107 this.getMemberVnfIndex();
108 this.headers = new HttpHeaders({
109 'Content-Type': 'application/json',
110 Accept: 'application/json',
111 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
112 });
113 }
114 /** Initialize Scaling Forms @public */
115 public initializeForm(): void {
116 this.scalingForm = this.formBuilder.group({
117 memberVnfIndex: [null, [Validators.required]],
118 vduId: [null, [Validators.required]],
119 countIndex: [null, [Validators.required]],
120 virtualMemory: [null, [Validators.required]],
121 sizeOfStorage: [null, [Validators.required]],
122 numVirtualCpu: [null, [Validators.required]]
123 });
124 }
125
126 /** Getting MemberVnfIndex using VNFInstances API @public */
127 public getMemberVnfIndex(): void {
128 const vnfInstanceData: {}[] = [];
129 this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
130 vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
131 const vnfDataObj: {} =
132 {
133 VNFD: vnfData['vnfd-ref'],
134 VNFInstanceId: vnfData._id,
135 MemberIndex: vnfData['member-vnf-index-ref'],
136 NS: vnfData['nsr-id-ref'],
137 VNFID: vnfData['vnfd-id']
138 };
139 vnfInstanceData.push(vnfDataObj);
140 });
141 const nsId: string = 'NS';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530142 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530143 this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
144 this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
145 const memberIndex: string = 'MemberIndex';
146 const vnfinstanceID: string = 'VNFInstanceId';
147 const assignMemberIndex: {} = {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530148 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530149 id: resVNF[memberIndex],
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530150 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530151 vnfinstanceId: resVNF[vnfinstanceID]
152 };
153 this.memberVnfIndex.push(assignMemberIndex);
154 });
155 this.memberTypes = this.memberVnfIndex;
156 this.isLoadingResults = false;
157 }, (error: ERRORDATA): void => {
158 this.restService.handleError(error, 'get');
159 this.isLoadingResults = false;
160 });
161 }
162
163 /** Getting vdu-id & count-index from API */
164 public getVdu(id: string): void {
165 const vnfInstanceData: {}[] = [];
166 this.getFormControl('vduId').setValue(null);
167 this.getFormControl('countIndex').setValue(null);
168 if (!isNullOrUndefined(id)) {
169 this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
170 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
171 this.instanceId = id;
172 this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
173 const VDU: string = 'vdur';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530174 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530175 if (vnfInstanceDetail[VDU] !== undefined) {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530176 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530177 vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
178 const vnfInstanceDataObj: {} =
179 {
180 'count-index': vdu['count-index'],
181 VDU: vdu['vdu-id-ref']
182
183 };
184 vnfInstanceData.push(vnfInstanceDataObj);
185 });
186 this.vdu = vnfInstanceData;
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530187 const vduName: string = 'VDU';
188 this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
189 index === self.findIndex((t: {}): {} => (
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530190 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530191 t[vduName] === vdu[vduName]
192 ))
193 );
SANDHYA.JS017df362022-05-02 06:57:11 +0530194 }
195 }, (error: ERRORDATA): void => {
196 this.restService.handleError(error, 'get');
197 this.isLoadingResults = false;
198 });
199 }
200 }
201
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530202 /** Getting count-index by filtering id */
203 public getCountIndex(id: string): void {
204 const VDU: string = 'VDU';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530205 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530206 this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
207 }
208
SANDHYA.JS017df362022-05-02 06:57:11 +0530209 /** Vertical Scaling on submit */
210 public triggerVerticalScaling(): void {
211 this.submitted = true;
212 this.sharedService.cleanForm(this.scalingForm);
213 if (!this.scalingForm.invalid) {
214 const scalingPayload: VerticalScaling = {
215 lcmOperationType: 'verticalscale',
216 verticalScale: 'CHANGE_VNFFLAVOR',
217 nsInstanceId: this.params.id,
218 changeVnfFlavorData: {
219 vnfInstanceId: this.instanceId,
220 additionalParams: {
221 vduid: this.scalingForm.value.vduId,
222 vduCountIndex: this.scalingForm.value.countIndex,
223 virtualMemory: Number(this.scalingForm.value.virtualMemory),
224 sizeOfStorage: Number(this.scalingForm.value.sizeOfStorage),
225 numVirtualCpu: Number(this.scalingForm.value.numVirtualCpu)
226 }
227 }
228 };
229 this.verticalscaleInitialization(scalingPayload);
230 }
231 }
232
233 /** Initialize the vertical scaling operation @public */
234 public verticalscaleInitialization(scalingPayload: object): void {
235 this.isLoadingResults = true;
236 const apiURLHeader: APIURLHEADER = {
237 url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/verticalscale',
238 httpOptions: { headers: this.headers }
239 };
240 const modalData: MODALCLOSERESPONSEDATA = {
241 message: 'Done'
242 };
243 this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
244 this.activeModal.close(modalData);
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530245 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
246 // Catch Navigation Error
247 });
SANDHYA.JS017df362022-05-02 06:57:11 +0530248 }, (error: ERRORDATA): void => {
249 this.restService.handleError(error, 'post');
250 this.isLoadingResults = false;
251 });
252 }
253
254 /** Used to get the AbstractControl of controlName passed @private */
255 private getFormControl(controlName: string): AbstractControl {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530256 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS017df362022-05-02 06:57:11 +0530257 return this.scalingForm.controls[controlName];
258 }
259}