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