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