Feature 10911-Vertical scaling of VM instances from OSM
[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     /** Input contains component objects @private */
71     @Input() private params: URLPARAMS;
72     /** FormBuilder instance added to the formBuilder @private */
73     private formBuilder: FormBuilder;
74     /** Instance of the rest service @private */
75     private restService: RestService;
76     /** Controls the header form @private */
77     private headers: HttpHeaders;
78     /** Contains all methods related to shared @private */
79     private sharedService: SharedService;
80     /** Notifier service to popup notification @private */
81     private notifierService: NotifierService;
82     /** Contains tranlsate instance @private */
83     private translateService: TranslateService;
84     /** Holds the instance of AuthService class of type AuthService @private */
85     private router: Router;
86     constructor(injector: Injector) {
87         this.injector = injector;
88         this.restService = this.injector.get(RestService);
89         this.activeModal = this.injector.get(NgbActiveModal);
90         this.formBuilder = this.injector.get(FormBuilder);
91         this.sharedService = this.injector.get(SharedService);
92         this.notifierService = this.injector.get(NotifierService);
93         this.translateService = this.injector.get(TranslateService);
94         this.router = this.injector.get(Router);
95     }
96     /** convenience getter for easy access to form fields */
97     get f(): FormGroup['controls'] { return this.scalingForm.controls; }
98     /**
99      * Lifecyle Hooks the trigger before component is instantiate
100      */
101     public ngOnInit(): void {
102         this.initializeForm();
103         this.getMemberVnfIndex();
104         this.headers = new HttpHeaders({
105             'Content-Type': 'application/json',
106             Accept: 'application/json',
107             'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
108         });
109     }
110     /** Initialize Scaling Forms @public */
111     public initializeForm(): void {
112         this.scalingForm = this.formBuilder.group({
113             memberVnfIndex: [null, [Validators.required]],
114             vduId: [null, [Validators.required]],
115             countIndex: [null, [Validators.required]],
116             virtualMemory: [null, [Validators.required]],
117             sizeOfStorage: [null, [Validators.required]],
118             numVirtualCpu: [null, [Validators.required]]
119         });
120     }
121
122     /** Getting MemberVnfIndex using VNFInstances API @public */
123     public getMemberVnfIndex(): void {
124         const vnfInstanceData: {}[] = [];
125         this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
126             vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
127                 const vnfDataObj: {} =
128                 {
129                     VNFD: vnfData['vnfd-ref'],
130                     VNFInstanceId: vnfData._id,
131                     MemberIndex: vnfData['member-vnf-index-ref'],
132                     NS: vnfData['nsr-id-ref'],
133                     VNFID: vnfData['vnfd-id']
134                 };
135                 vnfInstanceData.push(vnfDataObj);
136             });
137             const nsId: string = 'NS';
138             this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
139             this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
140                 const memberIndex: string = 'MemberIndex';
141                 const vnfinstanceID: string = 'VNFInstanceId';
142                 const assignMemberIndex: {} = {
143                     id: resVNF[memberIndex],
144                     vnfinstanceId: resVNF[vnfinstanceID]
145                 };
146                 this.memberVnfIndex.push(assignMemberIndex);
147             });
148             this.memberTypes = this.memberVnfIndex;
149             this.isLoadingResults = false;
150         }, (error: ERRORDATA): void => {
151             this.restService.handleError(error, 'get');
152             this.isLoadingResults = false;
153         });
154     }
155
156     /** Getting vdu-id & count-index from API */
157     public getVdu(id: string): void {
158         const vnfInstanceData: {}[] = [];
159         this.getFormControl('vduId').setValue(null);
160         this.getFormControl('countIndex').setValue(null);
161         if (!isNullOrUndefined(id)) {
162             this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
163                 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
164                     this.instanceId = id;
165                     this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
166                     const VDU: string = 'vdur';
167                     if (vnfInstanceDetail[VDU] !== undefined) {
168                         vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
169                             const vnfInstanceDataObj: {} =
170                             {
171                                 'count-index': vdu['count-index'],
172                                 VDU: vdu['vdu-id-ref']
173
174                             };
175                             vnfInstanceData.push(vnfInstanceDataObj);
176                         });
177                         this.vdu = vnfInstanceData;
178                     }
179                 }, (error: ERRORDATA): void => {
180                     this.restService.handleError(error, 'get');
181                     this.isLoadingResults = false;
182                 });
183         }
184     }
185
186     /** Vertical Scaling on submit */
187     public triggerVerticalScaling(): void {
188         this.submitted = true;
189         this.sharedService.cleanForm(this.scalingForm);
190         if (!this.scalingForm.invalid) {
191             const scalingPayload: VerticalScaling = {
192                 lcmOperationType: 'verticalscale',
193                 verticalScale: 'CHANGE_VNFFLAVOR',
194                 nsInstanceId: this.params.id,
195                 changeVnfFlavorData: {
196                     vnfInstanceId: this.instanceId,
197                     additionalParams: {
198                         vduid: this.scalingForm.value.vduId,
199                         vduCountIndex: this.scalingForm.value.countIndex,
200                         virtualMemory: Number(this.scalingForm.value.virtualMemory),
201                         sizeOfStorage: Number(this.scalingForm.value.sizeOfStorage),
202                         numVirtualCpu: Number(this.scalingForm.value.numVirtualCpu)
203                     }
204                 }
205             };
206             this.verticalscaleInitialization(scalingPayload);
207         }
208     }
209
210     /** Initialize the vertical scaling operation @public */
211     public verticalscaleInitialization(scalingPayload: object): void {
212         this.isLoadingResults = true;
213         const apiURLHeader: APIURLHEADER = {
214             url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/verticalscale',
215             httpOptions: { headers: this.headers }
216         };
217         const modalData: MODALCLOSERESPONSEDATA = {
218             message: 'Done'
219         };
220         this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
221             this.activeModal.close(modalData);
222             this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch();
223         }, (error: ERRORDATA): void => {
224             this.restService.handleError(error, 'post');
225             this.isLoadingResults = false;
226         });
227     }
228
229     /** Used to get the AbstractControl of controlName passed @private */
230     private getFormControl(controlName: string): AbstractControl {
231         return this.scalingForm.controls[controlName];
232     }
233 }