Feature-9518: Scale-in/Scale-out commands triggered from the UI
[osm/NG-UI.git] / src / app / instances / ns-history-operations / HistoryOperationsComponent.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 History Of Operations Component
20  */
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { ActivatedRoute } from '@angular/router';
23 import { Router } from '@angular/router';
24 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { CONFIGCONSTANT, ERRORDATA } from 'CommonModel';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import * as HttpStatus from 'http-status-codes';
30 import { LocalDataSource } from 'ng2-smart-table';
31 import { NSDInstanceData } from 'NSInstanceModel';
32 import { RestService } from 'RestService';
33 import { Subscription } from 'rxjs';
34 import { SharedService } from 'SharedService';
35 import { ShowInfoComponent } from 'ShowInfoComponent';
36 import { isNullOrUndefined } from 'util';
37
38 /**
39  * Creating component
40  * @Component takes HistoryOperationsComponent.html as template url
41  */
42 @Component({
43   templateUrl: './HistoryOperationsComponent.html',
44   styleUrls: ['./HistoryOperationsComponent.scss']
45 })
46 /** Exporting a class @exports HistoryOperationsComponent */
47 export class HistoryOperationsComponent implements OnInit {
48   /** Injector to invoke other services @public */
49   public injector: Injector;
50
51   /** NS Instance array @public */
52   public nsAndnstInstanceData: object[] = [];
53
54   /** Datasource instance @public */
55   public dataSource: LocalDataSource = new LocalDataSource();
56
57   /** Instance component are stored in settings @public */
58   public settings: {} = {};
59
60   /** Contains objects for smart table title and filter settings @public */
61   public columnList: {} = {};
62
63   /** Variable handles the page name @public */
64   public page: string;
65
66   /** Variable handles the title name @public */
67   public titleName: string;
68
69   /** Check the loading results @public */
70   public isLoadingResults: boolean = true;
71
72   /** Give the message for the loading @public */
73   public message: string = 'PLEASEWAIT';
74
75   /** Class for empty and present data @public */
76   public checkDataClass: string;
77
78   /** History State init data @public */
79   public historyStateFirstStep: string = CONFIGCONSTANT.historyStateFirstStep;
80
81   /** History State running data @public */
82   public historyStateSecondStep: string = CONFIGCONSTANT.historyStateSecondStep;
83
84   /** History State failed data @public */
85   public historyStateThirdStep: string = CONFIGCONSTANT.historyStateThirdStep;
86
87   /** dataService to pass the data from one component to another @private */
88   private dataService: DataService;
89
90   /** Utilizes rest service for any CRUD operations @private */
91   private restService: RestService;
92
93   /** Contains all methods related to shared @private */
94   private sharedService: SharedService;
95
96   /** Holds teh instance of AuthService class of type AuthService @private */
97   private activatedRoute: ActivatedRoute;
98
99   /** Instance of the modal service @private */
100   private modalService: NgbModal;
101
102   /** variables contains paramsID @private */
103   private paramsID: string;
104
105   /** variables contains paramsID @private */
106   private paramsType: string;
107
108   /** variables conatins URL of the History operations @public */
109   private historyURL: string;
110
111   /** Contains tranlsate instance @private */
112   private translateService: TranslateService;
113
114   /** Instance of subscriptions @private */
115   private generateDataSub: Subscription;
116
117   /** Service holds the router information @private */
118   private router: Router;
119
120   constructor(injector: Injector) {
121     this.injector = injector;
122     this.restService = this.injector.get(RestService);
123     this.dataService = this.injector.get(DataService);
124     this.sharedService = this.injector.get(SharedService);
125     this.activatedRoute = this.injector.get(ActivatedRoute);
126     this.modalService = this.injector.get(NgbModal);
127     this.translateService = this.injector.get(TranslateService);
128     this.router = this.injector.get(Router);
129   }
130
131   /** Lifecyle Hooks the trigger before component is instantiate @public */
132   public ngOnInit(): void {
133     // tslint:disable-next-line:no-backbone-get-set-outside-model
134     this.paramsID = this.activatedRoute.snapshot.paramMap.get('id');
135     // tslint:disable-next-line:no-backbone-get-set-outside-model
136     this.paramsType = this.activatedRoute.snapshot.paramMap.get('type');
137     if (this.paramsType === 'ns') {
138       this.historyURL = environment.NSHISTORYOPERATIONS_URL + '/?nsInstanceId=' + this.paramsID;
139       this.page = 'ns-history-operation';
140       this.titleName = 'INSTANCEDETAILS';
141     } else if (this.paramsType === 'netslice') {
142       this.historyURL = environment.NSTHISTORYOPERATIONS_URL + '/?netsliceInstanceId=' + this.paramsID;
143       this.page = 'nst-history-operation';
144       this.titleName = 'INSTANCEDETAILS';
145     }
146     this.generateTableColumn();
147     this.generateTableSettings();
148     this.generateData();
149     this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
150   }
151
152   /** Generate smart table row title and filters @public  */
153   public generateTableSettings(): void {
154     this.settings = {
155       columns: this.columnList,
156       actions: {
157         add: false, edit: false, delete: false, position: 'right',
158         custom: [{
159           name: 'showInformation', title: '<i class="fas fa-info" title=" ' + this.translateService.instant('INFO') + ' "></i>'}]
160       },
161       attr: this.sharedService.tableClassConfig(),
162       pager: this.sharedService.paginationPagerConfig(),
163       noDataMessage: this.translateService.instant('NODATAMSG')
164     };
165   }
166
167   /** Generate smart table row title and filters @public  */
168   public generateTableColumn(): void {
169     this.columnList = {
170       id: { title: this.translateService.instant('ID'), width: '30%' },
171       type: { title: this.translateService.instant('TYPE'), width: '20%' },
172       state: {
173         type: 'html', title: this.translateService.instant('OPERATIONSTATE'), width: '15%',
174         filter: {
175           type: 'list',
176           config: {
177             selectText: 'Select',
178             list: [
179               { value: this.historyStateFirstStep, title: this.historyStateFirstStep },
180               { value: this.historyStateSecondStep, title: this.historyStateSecondStep },
181               { value: this.historyStateThirdStep, title: this.historyStateThirdStep }
182             ]
183           }
184         },
185         valuePrepareFunction: (cell: NSDInstanceData, row: NSDInstanceData): string => {
186           if (row.state === this.historyStateFirstStep) {
187             return `<span class="icon-label" title="${row.state}">
188                         <i class="fas fa-clock text-warning"></i>
189                         </span>`;
190           } else if (row.state === this.historyStateSecondStep) {
191             return `<span class="icon-label" title="${row.state}">
192                         <i class="fas fa-check-circle text-success"></i>
193                         </span>`;
194           } else if (row.state === this.historyStateThirdStep) {
195             return `<span class="icon-label" title="${row.state}">
196                         <i class="fas fa-times-circle text-danger"></i>
197                         </span>`;
198           } else {
199             return `<span>${row.state}</span>`;
200           }
201         }
202       },
203       startTime: { title: this.translateService.instant('STARTTIME'), width: '15%' },
204       statusEnteredTime: { title: this.translateService.instant('STATUSENTEREDTIME'), width: '15%' }
205     };
206   }
207
208   /** smart table listing manipulation @public */
209   public onUserRowSelect(event: MessageEvent): void {
210     this.dataService.changeMessage(event.data);
211   }
212   /** smart table listing manipulation @public */
213   public onChange(perPageValue: number): void {
214     this.dataSource.setPaging(1, perPageValue, true);
215   }
216   /** show information methods modal with ns history info */
217   public showInformation(event: MessageEvent): void {
218     this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
219       id: event.data.id,
220       page: this.page,
221       titleName: this.titleName
222     };
223   }
224
225   /**
226    * Lifecyle hook which get trigger on component destruction
227    */
228   public ngOnDestroy(): void {
229     this.generateDataSub.unsubscribe();
230   }
231
232   /** generateData initiate the ns-instance list @private */
233   private generateData(): void {
234     this.isLoadingResults = true;
235     this.restService.getResource(this.historyURL).subscribe((nsdInstancesData: {}[]): void => {
236       this.nsAndnstInstanceData = [];
237       nsdInstancesData.forEach((nsdAndnstInstanceData: NSDInstanceData): void => {
238         let scaleType: string = '';
239         if (!isNullOrUndefined(nsdAndnstInstanceData.operationParams.scaleVnfData)) {
240           scaleType = ' (' + nsdAndnstInstanceData.operationParams.scaleVnfData.scaleVnfType + ')';
241         }
242         const nsAndnstDataObj: {} = {
243           id: nsdAndnstInstanceData.id,
244           type: nsdAndnstInstanceData.lcmOperationType + scaleType,
245           state: nsdAndnstInstanceData.operationState,
246           startTime: this.sharedService.convertEpochTime(nsdAndnstInstanceData.startTime),
247           statusEnteredTime: this.sharedService.convertEpochTime(nsdAndnstInstanceData.statusEnteredTime)
248         };
249         this.nsAndnstInstanceData.push(nsAndnstDataObj);
250       });
251
252       if (this.nsAndnstInstanceData.length > 0) {
253         this.checkDataClass = 'dataTables_present';
254       } else {
255         this.checkDataClass = 'dataTables_empty';
256       }
257       this.dataSource.load(this.nsAndnstInstanceData).then((data: {}): void => {
258         //empty block
259       }).catch();
260       this.isLoadingResults = false;
261     }, (error: ERRORDATA): void => {
262       this.isLoadingResults = false;
263       if (error.error.status === HttpStatus.NOT_FOUND || error.error.status === HttpStatus.UNAUTHORIZED) {
264         this.router.navigateByUrl('404', { skipLocationChange: true }).catch();
265       } else {
266         this.restService.handleError(error, 'get');
267       }
268     });
269   }
270 }