Fix Bug 2148: VDU Id issue & Multivdu Count Index Error in VM Action
[osm/NG-UI.git] / src / app / utilities / vertical-scaling / VerticalScalingComponent.ts
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';
32 import { SharedService } from 'SharedService';
33 import { isNullOrUndefined } from 'util';
34 import { 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 })
45 export 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;
70      /** Contains vduId @public */
71     public vduId: {};
72     /** Items for countIndex @public */
73     public countIndex: {}[];
74     /** 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';
142             this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
143             this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
144                 const memberIndex: string = 'MemberIndex';
145                 const vnfinstanceID: string = 'VNFInstanceId';
146                 const assignMemberIndex: {} = {
147                     id: resVNF[memberIndex],
148                     vnfinstanceId: resVNF[vnfinstanceID]
149                 };
150                 this.memberVnfIndex.push(assignMemberIndex);
151             });
152             this.memberTypes = this.memberVnfIndex;
153             this.isLoadingResults = false;
154         }, (error: ERRORDATA): void => {
155             this.restService.handleError(error, 'get');
156             this.isLoadingResults = false;
157         });
158     }
159
160     /** Getting vdu-id & count-index from API */
161     public getVdu(id: string): void {
162         const vnfInstanceData: {}[] = [];
163         this.getFormControl('vduId').setValue(null);
164         this.getFormControl('countIndex').setValue(null);
165         if (!isNullOrUndefined(id)) {
166             this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
167                 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
168                     this.instanceId = id;
169                     this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
170                     const VDU: string = 'vdur';
171                     if (vnfInstanceDetail[VDU] !== undefined) {
172                         vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
173                             const vnfInstanceDataObj: {} =
174                             {
175                                 'count-index': vdu['count-index'],
176                                 VDU: vdu['vdu-id-ref']
177
178                             };
179                             vnfInstanceData.push(vnfInstanceDataObj);
180                         });
181                         this.vdu = vnfInstanceData;
182                         const vduName: string = 'VDU';
183                         this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
184                             index === self.findIndex((t: {}): {} => (
185                                 t[vduName] === vdu[vduName]
186                             ))
187                         );
188                     }
189                 }, (error: ERRORDATA): void => {
190                     this.restService.handleError(error, 'get');
191                     this.isLoadingResults = false;
192                 });
193         }
194     }
195
196     /** Getting count-index by filtering id  */
197     public getCountIndex(id: string): void {
198         const VDU: string = 'VDU';
199         this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
200     }
201
202     /** Vertical Scaling on submit */
203     public triggerVerticalScaling(): void {
204         this.submitted = true;
205         this.sharedService.cleanForm(this.scalingForm);
206         if (!this.scalingForm.invalid) {
207             const scalingPayload: VerticalScaling = {
208                 lcmOperationType: 'verticalscale',
209                 verticalScale: 'CHANGE_VNFFLAVOR',
210                 nsInstanceId: this.params.id,
211                 changeVnfFlavorData: {
212                     vnfInstanceId: this.instanceId,
213                     additionalParams: {
214                         vduid: this.scalingForm.value.vduId,
215                         vduCountIndex: this.scalingForm.value.countIndex,
216                         virtualMemory: Number(this.scalingForm.value.virtualMemory),
217                         sizeOfStorage: Number(this.scalingForm.value.sizeOfStorage),
218                         numVirtualCpu: Number(this.scalingForm.value.numVirtualCpu)
219                     }
220                 }
221             };
222             this.verticalscaleInitialization(scalingPayload);
223         }
224     }
225
226     /** Initialize the vertical scaling operation @public */
227     public verticalscaleInitialization(scalingPayload: object): void {
228         this.isLoadingResults = true;
229         const apiURLHeader: APIURLHEADER = {
230             url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/verticalscale',
231             httpOptions: { headers: this.headers }
232         };
233         const modalData: MODALCLOSERESPONSEDATA = {
234             message: 'Done'
235         };
236         this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
237             this.activeModal.close(modalData);
238             this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch();
239         }, (error: ERRORDATA): void => {
240             this.restService.handleError(error, 'post');
241             this.isLoadingResults = false;
242         });
243     }
244
245     /** Used to get the AbstractControl of controlName passed @private */
246     private getFormControl(controlName: string): AbstractControl {
247         return this.scalingForm.controls[controlName];
248     }
249 }