4d96f8aa897e3a1c49d2783b0e57a93c5aa1b8f8
[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 { isNullOrUndefined } from 'util';
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 { TranslateService } from '@ngx-translate/core';
28 import { NotifierService } from 'angular-notifier';
29 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
30 import { environment } from 'environment';
31 import { VerticalScaling } from 'NSInstanceModel';
32 import { RestService } from 'RestService';
33 import { SharedService } from 'SharedService';
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             // eslint-disable-next-line security/detect-object-injection
143             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: {} = {
148                     // eslint-disable-next-line security/detect-object-injection
149                     id: resVNF[memberIndex],
150                     // eslint-disable-next-line security/detect-object-injection
151                     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';
174                     // eslint-disable-next-line security/detect-object-injection
175                     if (vnfInstanceDetail[VDU] !== undefined) {
176                         // eslint-disable-next-line security/detect-object-injection
177                         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;
187                         const vduName: string = 'VDU';
188                         this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
189                             index === self.findIndex((t: {}): {} => (
190                                 // eslint-disable-next-line security/detect-object-injection
191                                 t[vduName] === vdu[vduName]
192                             ))
193                         );
194                     }
195                 }, (error: ERRORDATA): void => {
196                     this.restService.handleError(error, 'get');
197                     this.isLoadingResults = false;
198                 });
199         }
200     }
201
202     /** Getting count-index by filtering id  */
203     public getCountIndex(id: string): void {
204         const VDU: string = 'VDU';
205         // eslint-disable-next-line security/detect-object-injection
206         this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
207     }
208
209     /** 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);
245             this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
246                 // Catch Navigation Error
247             });
248         }, (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 {
256         // eslint-disable-next-line security/detect-object-injection
257         return this.scalingForm.controls[controlName];
258     }
259 }