Angular upgrade
[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, isNullOrUndefined } from 'SharedService';
33 import { InstanceData, VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
34
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 })
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 */
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;
69     /** Contains vduId @public */
70     public vduId: {};
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);
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 {
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: {} =
136                 {
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
143                     NS: vnfData[nsrId],
144                     // eslint-disable-next-line security/detect-object-injection
145                     VNFID: vnfData[vnfId]
146                 };
147                 vnfInstanceData.push(vnfDataObj);
148             });
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
156                 };
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';
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: {} =
183                             {
184                                 'count-index': vdu['count-index'],
185                                 VDU: vdu['vdu-id-ref']
186                             };
187                             vnfInstanceData.push(vnfInstanceDataObj);
188                         });
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]
195                             ))
196                         );
197                     }
198                 }, (error: ERRORDATA): void => {
199                     this.restService.handleError(error, 'get');
200                     this.isLoadingResults = false;
201                 });
202         }
203     }
204
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);
210     }
211
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,
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);
248             this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
249                 // Catch Navigation Error
250             });
251         }, (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 {
259         // eslint-disable-next-line security/detect-object-injection
260         return this.scalingForm.controls[controlName];
261     }
262 }