Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[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 { isNullOrUndefined } from 'util';
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { ActivatedRoute } from '@angular/router';
24 import { Router } from '@angular/router';
25 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
26 import { TranslateService } from '@ngx-translate/core';
27 import { CONFIGCONSTANT, ERRORDATA } from 'CommonModel';
28 import { DataService } from 'DataService';
29 import { environment } from 'environment';
30 import * as HttpStatus from 'http-status-codes';
31 import { LocalDataSource } from 'ng2-smart-table';
32 import { NSDInstanceData } from 'NSInstanceModel';
33 import { RestService } from 'RestService';
34 import { Subscription } from 'rxjs';
35 import { SharedService } from 'SharedService';
36 import { ShowInfoComponent } from 'ShowInfoComponent';
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     this.paramsID = this.activatedRoute.snapshot.paramMap.get('id');
134     this.paramsType = this.activatedRoute.snapshot.paramMap.get('type');
135     if (this.paramsType === 'ns') {
136       this.historyURL = environment.NSHISTORYOPERATIONS_URL + '/?nsInstanceId=' + this.paramsID;
137       this.page = 'ns-history-operation';
138       this.titleName = 'INSTANCEDETAILS';
139     } else if (this.paramsType === 'netslice') {
140       this.historyURL = environment.NSTHISTORYOPERATIONS_URL + '/?netsliceInstanceId=' + this.paramsID;
141       this.page = 'nst-history-operation';
142       this.titleName = 'INSTANCEDETAILS';
143     }
144     this.generateTableColumn();
145     this.generateTableSettings();
146     this.generateData();
147     this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
148   }
149
150   /** Generate smart table row title and filters @public  */
151   public generateTableSettings(): void {
152     this.settings = {
153       columns: this.columnList,
154       actions: {
155         add: false, edit: false, delete: false, position: 'right',
156         custom: [{
157           name: 'showInformation', title: '<i class="fas fa-info" title=" ' + this.translateService.instant('INFO') + ' "></i>'}]
158       },
159       attr: this.sharedService.tableClassConfig(),
160       pager: this.sharedService.paginationPagerConfig(),
161       noDataMessage: this.translateService.instant('NODATAMSG')
162     };
163   }
164
165   /** Generate smart table row title and filters @public  */
166   public generateTableColumn(): void {
167     this.columnList = {
168       id: { title: this.translateService.instant('ID'), width: '30%' },
169       type: { title: this.translateService.instant('TYPE'), width: '20%' },
170       state: {
171         type: 'html', title: this.translateService.instant('OPERATIONSTATE'), width: '15%',
172         filter: {
173           type: 'list',
174           config: {
175             selectText: 'Select',
176             list: [
177               { value: this.historyStateFirstStep, title: this.historyStateFirstStep },
178               { value: this.historyStateSecondStep, title: this.historyStateSecondStep },
179               { value: this.historyStateThirdStep, title: this.historyStateThirdStep }
180             ]
181           }
182         },
183         valuePrepareFunction: (cell: NSDInstanceData, row: NSDInstanceData): string => {
184           if (row.state === this.historyStateFirstStep) {
185             return `<span class="icon-label" title="${row.state}">
186                         <i class="fas fa-clock text-warning"></i>
187                         </span>`;
188           } else if (row.state === this.historyStateSecondStep) {
189             return `<span class="icon-label" title="${row.state}">
190                         <i class="fas fa-check-circle text-success"></i>
191                         </span>`;
192           } else if (row.state === this.historyStateThirdStep) {
193             return `<span class="icon-label" title="${row.state}">
194                         <i class="fas fa-times-circle text-danger"></i>
195                         </span>`;
196           } else {
197             return `<span>${row.state}</span>`;
198           }
199         }
200       },
201       startTime: { title: this.translateService.instant('STARTTIME'), width: '15%' },
202       statusEnteredTime: { title: this.translateService.instant('STATUSENTEREDTIME'), width: '15%' }
203     };
204   }
205
206   /** smart table listing manipulation @public */
207   public onUserRowSelect(event: MessageEvent): void {
208     this.dataService.changeMessage(event.data);
209   }
210   /** smart table listing manipulation @public */
211   public onChange(perPageValue: number): void {
212     this.dataSource.setPaging(1, perPageValue, true);
213   }
214   /** show information methods modal with ns history info */
215   public showInformation(event: MessageEvent): void {
216     // eslint-disable-next-line security/detect-non-literal-fs-filename
217     this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
218       id: event.data.id,
219       page: this.page,
220       titleName: this.titleName
221     };
222   }
223
224   /**
225    * Lifecyle hook which get trigger on component destruction
226    */
227   public ngOnDestroy(): void {
228     this.generateDataSub.unsubscribe();
229   }
230
231   /** generateData initiate the ns-instance list @private */
232   private generateData(): void {
233     this.isLoadingResults = true;
234     this.restService.getResource(this.historyURL).subscribe((nsdInstancesData: {}[]): void => {
235       this.nsAndnstInstanceData = [];
236       nsdInstancesData.forEach((nsdAndnstInstanceData: NSDInstanceData): void => {
237         let scaleType: string = '';
238         if (!isNullOrUndefined(nsdAndnstInstanceData.operationParams.scaleVnfData)) {
239           scaleType = ' (' + nsdAndnstInstanceData.operationParams.scaleVnfData.scaleVnfType + ')';
240         }
241         const nsAndnstDataObj: {} = {
242           id: nsdAndnstInstanceData.id,
243           type: nsdAndnstInstanceData.lcmOperationType + scaleType,
244           state: nsdAndnstInstanceData.operationState,
245           startTime: this.sharedService.convertEpochTime(nsdAndnstInstanceData.startTime),
246           statusEnteredTime: this.sharedService.convertEpochTime(nsdAndnstInstanceData.statusEnteredTime)
247         };
248         this.nsAndnstInstanceData.push(nsAndnstDataObj);
249       });
250
251       if (this.nsAndnstInstanceData.length > 0) {
252         this.checkDataClass = 'dataTables_present';
253       } else {
254         this.checkDataClass = 'dataTables_empty';
255       }
256       this.dataSource.load(this.nsAndnstInstanceData).then((data: {}): void => {
257         //empty block
258       }).catch((): void => {
259         // Catch Navigation Error
260     });
261       this.isLoadingResults = false;
262     }, (error: ERRORDATA): void => {
263       this.isLoadingResults = false;
264       if (error.error.status === HttpStatus.NOT_FOUND || error.error.status === HttpStatus.UNAUTHORIZED) {
265         this.router.navigateByUrl('404', { skipLocationChange: true }).catch((): void => {
266           // Catch Navigation Error
267       });
268       } else {
269         this.restService.handleError(error, 'get');
270       }
271     });
272   }
273 }