NG-UI Bug 1136 - Unable to execute NS level primitive
[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 { SharedService } from 'SharedService';
34 import { ShowInfoComponent } from 'ShowInfoComponent';
35 /**
36  * Creating component
37  * @Component takes NSInstancesActionComponent.html as template url
38  */
39 @Component({
40   templateUrl: './NSInstancesActionComponent.html',
41   styleUrls: ['./NSInstancesActionComponent.scss'],
42   changeDetection: ChangeDetectionStrategy.OnPush
43 })
44 /** Exporting a class @exports NSInstancesActionComponent */
45 export class NSInstancesActionComponent {
46   /** To get the value from the nspackage via valuePrepareFunction default Property of ng-smarttable @public */
47   public value: NSDInstanceData;
48
49   /** Invoke service injectors @public */
50   public injector: Injector;
51
52   /** Instance of the modal service @public */
53   public restService: RestService;
54
55   /** Config Status Check @public */
56   public configStatus: string;
57
58   /** Operational Status Check @public */
59   public operationalStatus: string;
60
61   /** Check the loading results for loader status @public */
62   public isLoadingMetricsResult: boolean = false;
63
64   /** Give the message for the loading @public */
65   public message: string = 'PLEASEWAIT';
66
67   /** Instance of the modal service @private */
68   private modalService: NgbModal;
69
70   /** Holds teh instance of AuthService class of type AuthService @private */
71   private router: Router;
72
73   /** Contains instance ID @private */
74   private instanceID: string;
75
76   /** Contains all methods related to shared @private */
77   private sharedService: SharedService;
78
79   /** Notifier service to popup notification @private */
80   private notifierService: NotifierService;
81
82   /** Contains tranlsate instance @private */
83   private translateService: TranslateService;
84
85   /** Detect changes for the User Input */
86   private cd: ChangeDetectorRef;
87
88   /** Set timeout @private */
89   private timeOut: number = 1000;
90
91   constructor(injector: Injector) {
92     this.injector = injector;
93     this.modalService = this.injector.get(NgbModal);
94     this.restService = this.injector.get(RestService);
95     this.router = this.injector.get(Router);
96     this.sharedService = this.injector.get(SharedService);
97     this.notifierService = this.injector.get(NotifierService);
98     this.translateService = this.injector.get(TranslateService);
99     this.cd = this.injector.get(ChangeDetectorRef);
100   }
101
102   /**
103    * Lifecyle Hooks the trigger before component is instantiate
104    */
105   public ngOnInit(): void {
106     this.configStatus = this.value.ConfigStatus;
107     this.operationalStatus = this.value.OperationalStatus;
108     this.instanceID = this.value.identifier;
109   }
110
111   /** Shows information using modalservice @public */
112   public infoNs(): void {
113     this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
114       id: this.instanceID,
115       page: 'ns-instance',
116       titleName: 'INSTANCEDETAILS'
117     };
118   }
119
120   /** Delete NS Instanace @public */
121   public deleteNSInstance(forceAction: boolean): void {
122     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
123     modalRef.componentInstance.params = { forceDeleteType: forceAction };
124     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
125       if (result) {
126         this.sharedService.callData();
127       }
128     }).catch();
129   }
130
131   /** History of operations for an Instanace @public */
132   public historyOfOperations(): void {
133     this.router.navigate(['/instances/ns/history-operations/', this.instanceID]).catch(() => {
134       // Catch Navigation Error
135     });
136   }
137
138   /** NS Topology */
139   public nsTopology(): void {
140     this.router.navigate(['/instances/ns/', this.instanceID]).catch(() => {
141       // Catch Navigation Error
142     });
143   }
144
145   /** Exec NS Primitive @public */
146   public execNSPrimitiveModal(): void {
147     this.modalService.open(NSPrimitiveComponent).componentInstance.params = {
148       memberIndex: this.value.memberIndex,
149       nsConfig: this.value.nsConfig,
150       name: this.value.NsdName
151     };
152   }
153
154   /** Redirect to Grafana Metrics @public */
155   public metrics(): void {
156     this.isLoadingMetricsResult = true;
157     this.restService.getResource(environment.NSDINSTANCES_URL + '/' + this.instanceID).subscribe((nsData: NSDDetails[]) => {
158       nsData['vnfd-id'].forEach((vnfdID: string[]) => {
159         this.restService.getResource(environment.VNFPACKAGES_URL + '/' + vnfdID)
160           .subscribe((vnfd: {}[]) => {
161             if (vnfd['monitoring-param'] !== undefined && vnfd['monitoring-param'].length > 0) {
162               this.isLoadingMetricsResult = false;
163               const location: string = environment.GRAFANA_URL + '/' + this.instanceID + '/osm-ns-metrics-metrics';
164               window.open(location);
165             } else {
166               this.isLoadingMetricsResult = false;
167               this.notifierService.notify('error', this.translateService.instant('GRAFANA.METRICSERROR'));
168             }
169             setTimeout(() => {
170               this.cd.detectChanges();
171             }, this.timeOut);
172           }, (error: ERRORDATA) => {
173             this.restService.handleError(error, 'get');
174             this.isLoadingMetricsResult = false;
175           });
176       });
177     }, (error: ERRORDATA) => {
178       this.restService.handleError(error, 'get');
179       this.isLoadingMetricsResult = false;
180     });
181   }
182 }