1c3f1f293af926c65f8f7fbc06c497f4661e797f
[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 { NsUpdateComponent } from 'NsUpdateComponent';
33 import { RestService } from 'RestService';
34 import { forkJoin, Observable } from 'rxjs';
35 import { ScalingComponent } from 'ScalingComponent';
36 import { SharedService } from 'SharedService';
37 import { ShowInfoComponent } from 'ShowInfoComponent';
38 import { StartStopRebuildComponent } from 'StartStopRebuildComponent';
39 import { isNullOrUndefined } from 'util';
40 import { VmMigrationComponent } from 'VmMigrationComponent';
41 import { DF, VDU, VNFD } from 'VNFDModel';
42 /**
43  * Creating component
44  * @Component takes NSInstancesActionComponent.html as template url
45  */
46 @Component({
47   templateUrl: './NSInstancesActionComponent.html',
48   styleUrls: ['./NSInstancesActionComponent.scss'],
49   changeDetection: ChangeDetectionStrategy.OnPush
50 })
51 /** Exporting a class @exports NSInstancesActionComponent */
52 export class NSInstancesActionComponent {
53   /** To get the value from the nspackage via valuePrepareFunction default Property of ng-smarttable @public */
54   public value: NSDInstanceData;
55
56   /** Invoke service injectors @public */
57   public injector: Injector;
58
59   /** Instance of the modal service @public */
60   public restService: RestService;
61
62   /** Config Status Check @public */
63   public configStatus: string;
64
65   /** Operational Status Check @public */
66   public operationalStatus: string;
67
68   /** get Admin Details @public */
69   public getAdminDetails: {};
70
71   /** Scaling is accepted @public */
72   public isScalingPresent: boolean = false;
73
74   /** Check the loading results for loader status @public */
75   public isLoadingNSInstanceAction: boolean = false;
76
77   /** Give the message for the loading @public */
78   public message: string = 'PLEASEWAIT';
79
80   /** Assign the VNF Details @public */
81   public vnfDetails: VNFD[] = [];
82
83   /** Contains instance ID @public */
84   public instanceID: string;
85
86   /** Contains operational dashboard view @public */
87   public isShowOperationalDashboard: boolean = false;
88
89   /** Instance of the modal service @private */
90   private modalService: NgbModal;
91
92   /** Holds teh instance of AuthService class of type AuthService @private */
93   private router: Router;
94
95   /** Contains all methods related to shared @private */
96   private sharedService: SharedService;
97
98   /** Notifier service to popup notification @private */
99   private notifierService: NotifierService;
100
101   /** Contains tranlsate instance @private */
102   private translateService: TranslateService;
103
104   /** Detect changes for the User Input */
105   private cd: ChangeDetectorRef;
106
107   /** Set timeout @private */
108   private timeOut: number = 100;
109
110   constructor(injector: Injector) {
111     this.injector = injector;
112     this.modalService = this.injector.get(NgbModal);
113     this.restService = this.injector.get(RestService);
114     this.router = this.injector.get(Router);
115     this.sharedService = this.injector.get(SharedService);
116     this.notifierService = this.injector.get(NotifierService);
117     this.translateService = this.injector.get(TranslateService);
118     this.cd = this.injector.get(ChangeDetectorRef);
119   }
120
121   /**
122    * Lifecyle Hooks the trigger before component is instantiate
123    */
124   public ngOnInit(): void {
125     this.configStatus = this.value.ConfigStatus;
126     this.operationalStatus = this.value.OperationalStatus;
127     this.instanceID = this.value.identifier;
128     this.getAdminDetails = this.value.adminDetails;
129     this.isShowOperationalDashboard = !isNullOrUndefined(this.value.vcaStatus) ?
130       Object.keys(this.value.vcaStatus).length === 0 && typeof this.value.vcaStatus === 'object' : true;
131   }
132
133   /** Shows information using modalservice @public */
134   public infoNs(): void {
135     this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
136       id: this.instanceID,
137       page: 'ns-instance',
138       titleName: 'INSTANCEDETAILS'
139     };
140   }
141
142   /** Delete NS Instanace @public */
143   public deleteNSInstance(forceAction: boolean): void {
144     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
145     modalRef.componentInstance.params = { forceDeleteType: forceAction };
146     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
147       if (result) {
148         this.sharedService.callData();
149       }
150     }).catch();
151   }
152
153   /** History of operations for an Instanace @public */
154   public historyOfOperations(): void {
155     this.router.navigate(['/instances/ns/history-operations/', this.instanceID]).catch((): void => {
156       // Catch Navigation Error
157     });
158   }
159
160   /** NS Topology */
161   public nsTopology(): void {
162     this.router.navigate(['/instances/ns/', this.instanceID]).catch((): void => {
163       // Catch Navigation Error
164     });
165   }
166
167   /** Exec NS Primitive @public */
168   public execNSPrimitiveModal(): void {
169     this.modalService.open(NSPrimitiveComponent, { backdrop: 'static' }).componentInstance.params = {
170       memberIndex: this.value.memberIndex,
171       nsConfig: this.value.nsConfig,
172       name: this.value.NsdName
173     };
174   }
175
176   /** Redirect to Grafana Metrics @public */
177   public metrics(): void {
178     this.isLoadingNSInstanceAction = true;
179     this.restService.getResource(environment.NSDINSTANCES_URL + '/' + this.instanceID).subscribe((nsData: NSDDetails[]): void => {
180       nsData['vnfd-id'].forEach((vnfdID: string[]): void => {
181         this.restService.getResource(environment.VNFPACKAGES_URL + '/' + vnfdID)
182           .subscribe((vnfd: VNFD): void => {
183             vnfd.vdu.forEach((vduData: VDU): void => {
184               if (vduData['monitoring-parameter'] !== undefined && vduData['monitoring-parameter'].length > 0) {
185                 this.isLoadingNSInstanceAction = false;
186                 const location: string = environment.GRAFANA_URL + '/' + this.instanceID + '/osm-ns-metrics-metrics';
187                 window.open(location);
188               } else {
189                 this.isLoadingNSInstanceAction = false;
190                 this.notifierService.notify('error', this.translateService.instant('PAGE.NSMETRIC.METRICERROR'));
191               }
192             });
193             this.doChanges();
194           }, (error: ERRORDATA): void => {
195             this.restService.handleError(error, 'get');
196             this.isLoadingNSInstanceAction = false;
197           });
198       });
199     }, (error: ERRORDATA): void => {
200       this.restService.handleError(error, 'get');
201       this.isLoadingNSInstanceAction = false;
202     });
203   }
204
205   /**
206    * Do the manual scaling
207    * Here we are going to get a list of VNFD ID used in the instances
208    * and have this in array with URL created then pass to checkscaling method for forkjoin to get the data @public
209    */
210   public manualScaling(): void {
211     this.isLoadingNSInstanceAction = true;
212     const tempURL: Observable<{}>[] = [];
213     this.value.vnfID.forEach((id: string): void => {
214       const apiUrl: string = environment.VNFPACKAGESCONTENT_URL + '/' + id;
215       tempURL.push(this.restService.getResource(apiUrl));
216     });
217     this.checkScaling(tempURL);
218   }
219
220   /**
221    * Used to forkjoin to all the request to send parallely, get the data and check 'scaling-aspect' key is present @public
222    */
223   public checkScaling(URLS: Observable<{}>[]): void {
224     forkJoin(URLS).subscribe((data: VNFD[]): void => {
225       this.vnfDetails = data;
226       if (this.vnfDetails.length > 0) {
227         this.vnfDetails.forEach((vnfdData: VNFD): void => {
228           vnfdData.df.forEach((dfData: DF): void => {
229             if (!isNullOrUndefined(dfData['scaling-aspect']) && dfData['scaling-aspect'].length > 0) {
230               this.isScalingPresent = true;
231             }
232           });
233         });
234       }
235       this.isLoadingNSInstanceAction = false;
236       if (this.isScalingPresent) {
237         this.openScaling();
238       } else {
239         this.notifierService.notify('error', this.translateService.instant('SCALINGNOTFOUND'));
240       }
241       this.doChanges();
242     });
243   }
244
245   /** Open the scaling pop-up @public */
246   public openScaling(): void {
247     const modalRef: NgbModalRef = this.modalService.open(ScalingComponent, { backdrop: 'static' });
248     modalRef.componentInstance.params = {
249       id: this.instanceID,
250       vnfID: this.value.vnfID,
251       nsID: this.value['nsd-id'],
252       nsd: this.value.nsd,
253       data: this.vnfDetails
254     };
255     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
256       if (result) {
257         this.sharedService.callData();
258       }
259     }).catch();
260   }
261
262   /** To open VM Migration in NS Instances */
263   public openVmMigration(): void {
264     const modalRef: NgbModalRef = this.modalService.open(VmMigrationComponent, { backdrop: 'static' });
265     modalRef.componentInstance.params = {
266       id: this.instanceID
267     };
268     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
269       if (result) {
270         this.sharedService.callData();
271       }
272     }).catch();
273   }
274
275   /** To open the Ns Update pop-up */
276   public openNsUpdate(): void {
277     const modalRef: NgbModalRef = this.modalService.open(NsUpdateComponent, { backdrop: 'static' });
278     modalRef.componentInstance.params = {
279       id: this.instanceID
280     };
281     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
282       if (result) {
283         this.sharedService.callData();
284       }
285     }).catch();
286   }
287
288   /** To open the Start, Stop & Rebuild pop-up */
289   public openStart(actionType: string): void {
290     const modalRef: NgbModalRef = this.modalService.open(StartStopRebuildComponent, { backdrop: 'static' });
291     modalRef.componentInstance.params = {
292       id: this.instanceID
293     };
294     if (actionType === 'start') {
295       modalRef.componentInstance.instanceTitle = this.translateService.instant('START');
296     } else if (actionType === 'stop') {
297       modalRef.componentInstance.instanceTitle = this.translateService.instant('STOP');
298     } else {
299       modalRef.componentInstance.instanceTitle = this.translateService.instant('REBUILD');
300     }
301     modalRef.componentInstance.instanceType = actionType;
302     modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => {
303       if (result) {
304         this.sharedService.callData();
305       }
306     }).catch();
307   }
308
309   /**
310    * Check any changes in the child component @public
311    */
312   public doChanges(): void {
313     setTimeout((): void => {
314       this.cd.detectChanges();
315     }, this.timeOut);
316   }
317 }