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