Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / sdn-controller / sdn-controller-details / SDNControllerDetailsComponent.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 SDN Controller details Component.
20  */
21 import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23 import { TranslateService } from '@ngx-translate/core';
24 import { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA, SDN_TYPES } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { NewSDNControllerComponent } from 'NewSDNControllerComponent';
28 import { LocalDataSource } from 'ng2-smart-table';
29 import { RestService } from 'RestService';
30 import { Subscription } from 'rxjs';
31 import { SDNControllerActionComponent } from 'SDNControllerActionComponent';
32 import { SDNControllerList, SDNControllerModel } from 'SDNControllerModel';
33 import { SharedService } from 'SharedService';
34
35 /**
36  * Creating component
37  * @Component takes SDNControllerDetailsComponent.html as template url
38  */
39 @Component({
40     templateUrl: './SDNControllerDetailsComponent.html',
41     styleUrls: ['./SDNControllerDetailsComponent.scss']
42 })
43 /** Exporting a class @exports SDNControllerDetailsComponent */
44 export class SDNControllerDetailsComponent implements OnInit, OnDestroy {
45     /** Injector to invoke other services @public */
46     public injector: Injector;
47
48     /** Selected list array @public */
49     public selectList: object[] = [];
50
51     /** Instance component are stored in settings @public */
52     public settings: {} = {};
53
54     /** Contains objects for menu settings @public */
55     public columnList: {} = {};
56
57     /** Data of smarttable populate through LocalDataSource @public */
58     public dataSource: LocalDataSource = new LocalDataSource();
59
60     /** Check the loading results @public */
61     public isLoadingResults: boolean = true;
62
63     /** Give the message for the loading @public */
64     public message: string = 'PLEASEWAIT';
65
66     /** Class for empty and present data @public */
67     public checkDataClass: string;
68
69     /** operational State init data @public */
70     public operationalStateFirstStep: string = CONFIGCONSTANT.sdnOperationalStateFirstStep;
71
72     /** operational State running data @public */
73     public operationalStateSecondStep: string = CONFIGCONSTANT.sdnOperationalStateStateSecondStep;
74
75     /** operational State failed data @public */
76     public operationalStateThirdStep: string = CONFIGCONSTANT.sdnOperationalStateThirdStep;
77
78     /** Instance of the rest service @private */
79     private restService: RestService;
80
81     /** Contains all methods related to shared @private */
82     private sharedService: SharedService;
83
84     /** dataService to pass the data from one component to another @private */
85     private dataService: DataService;
86
87     /** Contains tranlsate instance @private */
88     private translateService: TranslateService;
89
90     /** Formation of appropriate Data for LocalDatasource @private */
91     private sdnData: {}[] = [];
92
93     /** Instance of the modal service @private */
94     private modalService: NgbModal;
95
96     /** Instance of subscriptions @private */
97     private generateDataSub: Subscription;
98
99     constructor(injector: Injector) {
100         this.injector = injector;
101         this.restService = this.injector.get(RestService);
102         this.translateService = this.injector.get(TranslateService);
103         this.sharedService = this.injector.get(SharedService);
104         this.dataService = this.injector.get(DataService);
105         this.modalService = this.injector.get(NgbModal);
106     }
107
108     /**
109      * Lifecyle Hooks the trigger before component is instantiate
110      */
111     public ngOnInit(): void {
112         this.generateTableColumn();
113         this.generateTableSettings();
114         this.generateData();
115         this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
116     }
117
118     /** Generate smart table row title and filters @public  */
119     public generateTableSettings(): void {
120         this.settings = {
121             columns: this.columnList,
122             actions: { add: false, edit: false, delete: false, position: 'right' },
123             attr: this.sharedService.tableClassConfig(),
124             pager: this.sharedService.paginationPagerConfig(),
125             noDataMessage: this.translateService.instant('NODATAMSG')
126         };
127     }
128
129     /** Generate smart table row title and filters @public  */
130     public generateTableColumn(): void {
131         this.columnList = {
132             name: { title: this.translateService.instant('NAME'), width: '15%', sortDirection: 'asc' },
133             identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
134             type: {
135                 title: this.translateService.instant('TYPE'), width: '15%',
136                 filter: {
137                     type: 'list',
138                     config: {
139                         selectText: 'Select',
140                         list: SDN_TYPES
141                     }
142                 }
143             },
144             operationalState: {
145                 title: this.translateService.instant('OPERATIONALSTATUS'), width: '15%', type: 'html',
146                 filter: {
147                     type: 'list',
148                     config: {
149                         selectText: 'Select',
150                         list: [
151                             { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep },
152                             { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep },
153                             { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }
154                         ]
155                     }
156                 },
157                 valuePrepareFunction: (cell: SDNControllerList, row: SDNControllerList): string => {
158                     if (row.operationalState === this.operationalStateFirstStep) {
159                         return `<span class="icon-label" title="${row.operationalState}">
160                         <i class="fas fa-clock text-warning"></i>
161                         </span>`;
162                     } else if (row.operationalState === this.operationalStateSecondStep) {
163                         return `<span class="icon-label" title="${row.operationalState}">
164                         <i class="fas fa-check-circle text-success"></i>
165                         </span>`;
166                     } else if (row.operationalState === this.operationalStateThirdStep) {
167                         return `<span class="icon-label" title="${row.operationalState}">
168                         <i class="fas fa-times-circle text-danger"></i>
169                         </span>`;
170                     } else {
171                         return `<span>${row.operationalState}</span>`;
172                     }
173                 }
174             },
175             url: { title: this.translateService.instant('URL'), width: '30%' },
176             Actions: {
177                 name: 'Action', width: '5%', filter: false, sort: false, type: 'custom',
178                 title: this.translateService.instant('ACTIONS'),
179                 valuePrepareFunction: (cell: SDNControllerList, row: SDNControllerList): SDNControllerList => row,
180                 renderComponent: SDNControllerActionComponent
181             }
182         };
183     }
184
185     /** smart table listing manipulation @public */
186     public onChange(perPageValue: number): void {
187         this.dataSource.setPaging(1, perPageValue, true);
188     }
189
190     /** smart table listing manipulation @public */
191     public onUserRowSelect(event: MessageEvent): void {
192         Object.assign(event.data, { page: 'sdn-controller' });
193         this.dataService.changeMessage(event.data);
194     }
195
196     /** Generate generateSDNData object from loop and return for the datasource @public */
197     public generateSDNList(sdn: SDNControllerModel): SDNControllerList {
198         return {
199             name: sdn.name,
200             identifier: sdn._id,
201             type: sdn.type,
202             operationalState: sdn._admin.operationalState,
203             url: sdn.url
204         };
205     }
206
207     /** Compose new SDN Controller @public */
208     public composeSDN(): void {
209         // eslint-disable-next-line security/detect-non-literal-fs-filename
210         const modalRef: NgbModalRef = this.modalService.open(NewSDNControllerComponent, { backdrop: 'static' });
211         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
212             if (result) {
213                 this.sharedService.callData();
214             }
215         }).catch((): void => {
216             // Catch Navigation Error
217         });
218     }
219
220     /**
221      * Lifecyle hook which get trigger on component destruction
222      */
223     public ngOnDestroy(): void {
224         this.generateDataSub.unsubscribe();
225     }
226
227     /** Fetching the data from server to Load in the smarttable @protected */
228     protected generateData(): void {
229         this.isLoadingResults = true;
230         this.sdnData = [];
231         this.restService.getResource(environment.SDNCONTROLLER_URL).subscribe((sdnDetails: {}[]) => {
232             sdnDetails.forEach((res: SDNControllerModel) => {
233                 const sdnDataObj: SDNControllerList = this.generateSDNList(res);
234                 this.sdnData.push(sdnDataObj);
235             });
236             if (this.sdnData.length > 0) {
237                 this.checkDataClass = 'dataTables_present';
238             } else {
239                 this.checkDataClass = 'dataTables_empty';
240             }
241             this.dataSource.load(this.sdnData).then((data: {}) => {
242                 this.isLoadingResults = false;
243             }).catch((): void => {
244                 // Catch Navigation Error
245             });
246         }, (error: ERRORDATA) => {
247             this.restService.handleError(error, 'get');
248             this.isLoadingResults = false;
249         });
250     }
251 }