2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: SANDHYA JS (sandhya.j@tataelxsi.co.in)
19 * @file VerticalScaling Component
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';
32 import { SharedService, isNullOrUndefined } from 'SharedService';
33 import { InstanceData, VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
37 * @Component takes VerticalScalingComponent.html as template url
40 selector: 'app-vertical-scaling',
41 templateUrl: './VerticalScalingComponent.html',
42 styleUrls: ['./VerticalScalingComponent.scss']
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 */
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;
69 /** Contains vduId @public */
71 /** Items for countIndex @public */
72 public countIndex: {}[];
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);
99 /** convenience getter for easy access to form fields */
100 get f(): FormGroup['controls'] { return this.scalingForm.controls; }
102 * Lifecyle Hooks the trigger before component is instantiate
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'
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]]
125 /** Getting MemberVnfIndex using VNFInstances API @public */
126 public getMemberVnfIndex(): void {
127 this.isLoadingResults = true;
128 const vnfInstanceData: {}[] = [];
129 this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
130 vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
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';
135 const vnfDataObj: {} =
137 // eslint-disable-next-line security/detect-object-injection
138 VNFD: vnfData[vnfdRef],
139 VNFInstanceId: vnfData._id,
140 // eslint-disable-next-line security/detect-object-injection
141 MemberIndex: vnfData[memberIndex],
142 // eslint-disable-next-line security/detect-object-injection
144 // eslint-disable-next-line security/detect-object-injection
145 VNFID: vnfData[vnfId]
147 vnfInstanceData.push(vnfDataObj);
149 const nsId: string = 'NS';
150 // eslint-disable-next-line security/detect-object-injection
151 this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
152 this.nsIdFilteredData.forEach((resVNF: InstanceData): void => {
153 const assignMemberIndex: {} = {
154 id: resVNF.MemberIndex,
155 vnfinstanceId: resVNF.VNFInstanceId
157 this.memberVnfIndex.push(assignMemberIndex);
159 this.memberTypes = this.memberVnfIndex;
160 this.isLoadingResults = false;
161 }, (error: ERRORDATA): void => {
162 this.restService.handleError(error, 'get');
163 this.isLoadingResults = false;
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';
178 // eslint-disable-next-line security/detect-object-injection
179 if (vnfInstanceDetail[VDU] !== undefined) {
180 // eslint-disable-next-line security/detect-object-injection
181 vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
182 const vnfInstanceDataObj: {} =
184 'count-index': vdu['count-index'],
185 VDU: vdu['vdu-id-ref']
187 vnfInstanceData.push(vnfInstanceDataObj);
189 this.vdu = vnfInstanceData;
190 const vduName: string = 'VDU';
191 this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
192 index === self.findIndex((t: {}): {} => (
193 // eslint-disable-next-line security/detect-object-injection
194 t[vduName] === vdu[vduName]
198 }, (error: ERRORDATA): void => {
199 this.restService.handleError(error, 'get');
200 this.isLoadingResults = false;
205 /** Getting count-index by filtering id */
206 public getCountIndex(id: string): void {
207 const VDU: string = 'VDU';
208 // eslint-disable-next-line security/detect-object-injection
209 this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
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,
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)
232 this.verticalscaleInitialization(scalingPayload);
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 }
243 const modalData: MODALCLOSERESPONSEDATA = {
246 this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
247 this.activeModal.close(modalData);
248 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
249 // Catch Navigation Error
251 }, (error: ERRORDATA): void => {
252 this.restService.handleError(error, 'post');
253 this.isLoadingResults = false;
257 /** Used to get the AbstractControl of controlName passed @private */
258 private getFormControl(controlName: string): AbstractControl {
259 // eslint-disable-next-line security/detect-object-injection
260 return this.scalingForm.controls[controlName];