Feature-9518: Scale-in/Scale-out commands triggered from the UI
[osm/NG-UI.git] / src / app / utilities / ns-instances-action / NSInstancesActionComponent.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: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file NS InstancesAction Component
20  */
21 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Injector } from '@angular/core';
22 import { Router } from '@angular/router';
23 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
27 import { DeleteComponent } from 'DeleteComponent';
28 import { environment } from 'environment';
29 import { NSDDetails } from 'NSDModel';
30 import { NSDInstanceData } from 'NSInstanceModel';
31 import { NSPrimitiveComponent } from 'NSPrimitiveComponent';
32 import { RestService } from 'RestService';
33 import { forkJoin, Observable } from 'rxjs';
34 import { ScalingComponent } from 'ScalingComponent';
35 import { SharedService } from 'SharedService';
36 import { ShowInfoComponent } from 'ShowInfoComponent';
37 import { isNullOrUndefined } from 'util';
38 import { DF, VDU, VNFD } from 'VNFDModel';
39 /**
40  * Creating component
41  * @Component takes NSInstancesActionComponent.html as template url
42  */
43 @Component({
44   templateUrl: './NSInstancesActionComponent.html',
45   styleUrls: ['./NSInstancesActionComponent.scss'],
46   changeDetection: ChangeDetectionStrategy.OnPush
47 })
48 /** Exporting a class @exports NSInstancesActionComponent */
49 export class NSInstancesActionComponent {
50   /** To get the value from the nspackage via valuePrepareFunction default Property of ng-smarttable @public */
51   public value: NSDInstanceData;
52
53   /** Invoke service injectors @public */
54   public injector: Injector;
55
56   /** Instance of the modal service @public */
57   public restService: RestService;
58
59   /** Config Status Check @public */
60   public configStatus: string;
61
62   /** Operational Status Check @public */
63   public operationalStatus: string;
64
65   /** get Admin Details @public */
66   public getAdminDetails: {};
67
68   /** Scaling is accepted @public */
69   public isScalingPresent: boolean = false;
70
71   /** Check the loading results for loader status @public */
72   public isLoadingNSInstanceAction: boolean = false;
73
74   /** Give the message for the loading @public */
75   public message: string = 'PLEASEWAIT';
76
77   /** Assign the VNF Details @public */
78   public vnfDetails: VNFD[] = [];
79
80   /** Instance of the modal service @private */
81   private modalService: NgbModal;
82
83   /** Holds teh instance of AuthService class of type AuthService @private */
84   private router: Router;
85
86   /** Contains instance ID @private */
87   private instanceID: string;
88
89   /** Contains all methods related to shared @private */
90   private sharedService: SharedService;
91
92   /** Notifier service to popup notification @private */
93   private notifierService: NotifierService;
94
95   /** Contains tranlsate instance @private */
96   private translateService: TranslateService;
97
98   /** Detect changes for the User Input */
99   private cd: ChangeDetectorRef;
100
101   /** Set timeout @private */
102   private timeOut: number = 100;
103
104   constructor(injector: Injector) {
105     this.injector = injector;
106     this.modalService = this.injector.get(NgbModal);
107     this.restService = this.injector.get(RestService);
108     this.router = this.injector.get(Router);
109     this.sharedService = this.injector.get(SharedService);
110     this.notifierService = this.injector.get(NotifierService);
111     this.translateService = this.injector.get(TranslateService);
112     this.cd = this.injector.get(ChangeDetectorRef);
113   }
114
115   /**
116    * Lifecyle Hooks the trigger before component is instantiate
117    */
118   public ngOnInit(): void {
119     this.configStatus = this.value.ConfigStatus;
120     this.operationalStatus = this.value.OperationalStatus;
121     this.instanceID = this.value.identifier;
122     this.getAdminDetails = this.value.adminDetails;
123   }
124
125   /** Shows information using modalservice @public */
126   public infoNs(): void {
127     this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
128       id: this.instanceID,
129       page: 'ns-instance',
130       titleName: 'INSTANCEDETAILS'
131     };
132   }
133
134   /** Delete NS Instanace @public */
135   public deleteNSInstance(forceAction: boolean): void {
136     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
137     modalRef.componentInstance.params = { forceDeleteType: forceAction };
138     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
139       if (result) {
140         this.sharedService.callData();
141       }
142     }).catch();
143   }
144
145   /** History of operations for an Instanace @public */
146   public historyOfOperations(): void {
147     this.router.navigate(['/instances/ns/history-operations/', this.instanceID]).catch((): void => {
148       // Catch Navigation Error
149     });
150   }
151
152   /** NS Topology */
153   public nsTopology(): void {
154     this.router.navigate(['/instances/ns/', this.instanceID]).catch((): void => {
155       // Catch Navigation Error
156     });
157   }
158
159   /** Exec NS Primitive @public */
160   public execNSPrimitiveModal(): void {
161     this.modalService.open(NSPrimitiveComponent, { backdrop: 'static' }).componentInstance.params = {
162       memberIndex: this.value.memberIndex,
163       nsConfig: this.value.nsConfig,
164       name: this.value.NsdName
165     };
166   }
167
168   /** Redirect to Grafana Metrics @public */
169   public metrics(): void {
170     this.isLoadingNSInstanceAction = true;
171     this.restService.getResource(environment.NSDINSTANCES_URL + '/' + this.instanceID).subscribe((nsData: NSDDetails[]): void => {
172       nsData['vnfd-id'].forEach((vnfdID: string[]): void => {
173         this.restService.getResource(environment.VNFPACKAGES_URL + '/' + vnfdID)
174           .subscribe((vnfd: VNFD): void => {
175             vnfd.vdu.forEach((vduData: VDU): void => {
176               if (vduData['monitoring-parameter'] !== undefined && vduData['monitoring-parameter'].length > 0) {
177                 this.isLoadingNSInstanceAction = false;
178                 const location: string = environment.GRAFANA_URL + '/' + this.instanceID + '/osm-ns-metrics-metrics';
179                 window.open(location);
180               } else {
181                 this.isLoadingNSInstanceAction = false;
182                 this.notifierService.notify('error', this.translateService.instant('GRAFANA.METRICSERROR'));
183               }
184             });
185             this.doChanges();
186           }, (error: ERRORDATA): void => {
187             this.restService.handleError(error, 'get');
188             this.isLoadingNSInstanceAction = false;
189           });
190       });
191     }, (error: ERRORDATA): void => {
192       this.restService.handleError(error, 'get');
193       this.isLoadingNSInstanceAction = false;
194     });
195   }
196
197   /**
198    * Do the manual scaling
199    * Here we are going to get a list of VNFD ID used in the instances
200    * and have this in array with URL created then pass to checkscaling method for forkjoin to get the data @public
201    */
202   public manualScaling(): void {
203     this.isLoadingNSInstanceAction = true;
204     const tempURL: Observable<{}>[] = [];
205     this.value.vnfID.forEach((id: string): void => {
206       const apiUrl: string = environment.VNFPACKAGESCONTENT_URL + '/' + id;
207       tempURL.push(this.restService.getResource(apiUrl));
208     });
209     this.checkScaling(tempURL);
210   }
211
212   /**
213    * Used to forkjoin to all the request to send parallely, get the data and check 'scaling-aspect' key is present @public
214    */
215   public checkScaling(URLS: Observable<{}>[]): void {
216     forkJoin(URLS).subscribe((data: VNFD[]): void => {
217       this.vnfDetails = data;
218       if (this.vnfDetails.length > 0) {
219         this.vnfDetails.forEach((vnfdData: VNFD): void => {
220           vnfdData.df.forEach((dfData: DF): void => {
221             if (!isNullOrUndefined(dfData['scaling-aspect']) && dfData['scaling-aspect'].length > 0) {
222               this.isScalingPresent = true;
223             }
224           });
225         });
226       }
227       this.isLoadingNSInstanceAction = false;
228       if (this.isScalingPresent) {
229         this.openScaling();
230       } else {
231         this.notifierService.notify('error', this.translateService.instant('SCALINGNOTFOUND'));
232       }
233       this.doChanges();
234     });
235   }
236
237   /** Open the scaling pop-up @public */
238   public openScaling(): void {
239     const modalRef: NgbModalRef = this.modalService.open(ScalingComponent, { backdrop: 'static' });
240     modalRef.componentInstance.params = {
241       id: this.instanceID,
242       vnfID: this.value.vnfID,
243       nsID: this.value['nsd-id'],
244       nsd: this.value.nsd,
245       data: this.vnfDetails
246     };
247     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
248       if (result) {
249         this.sharedService.callData();
250       }
251     }).catch();
252   }
253
254   /**
255    * Check any changes in the child component @public
256    */
257   public doChanges(): void {
258     setTimeout((): void => {
259       this.cd.detectChanges();
260     }, this.timeOut);
261   }
262 }