Initial Commit - NG UI
[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             ip: { title: this.translateService.instant('IP'), width: '15%' },
176             port: { title: this.translateService.instant('PORT'), width: '15%' },
177             Actions: {
178                 name: 'Action', width: '5%', filter: false, sort: false, type: 'custom',
179                 title: this.translateService.instant('ACTIONS'),
180                 valuePrepareFunction: (cell: SDNControllerList, row: SDNControllerList): SDNControllerList => row,
181                 renderComponent: SDNControllerActionComponent
182             }
183         };
184     }
185
186     /** smart table listing manipulation @public */
187     public onChange(perPageValue: number): void {
188         this.dataSource.setPaging(1, perPageValue, true);
189     }
190
191     /** smart table listing manipulation @public */
192     public onUserRowSelect(event: MessageEvent): void {
193         Object.assign(event.data, { page: 'sdn-controller' });
194         this.dataService.changeMessage(event.data);
195     }
196
197     /** Generate generateSDNData object from loop and return for the datasource @public */
198     public generateSDNList(sdn: SDNControllerModel): SDNControllerList {
199         return {
200             name: sdn.name,
201             identifier: sdn._id,
202             type: sdn.type,
203             operationalState: sdn._admin.operationalState,
204             ip: sdn.ip,
205             port: sdn.port
206         };
207     }
208
209     /** Compose new SDN Controller @public */
210     public composeSDN(): void {
211         const modalRef: NgbModalRef = this.modalService.open(NewSDNControllerComponent, { backdrop: 'static' });
212         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
213             if (result) {
214                 this.sharedService.callData();
215             }
216         }).catch();
217     }
218
219     /**
220      * Lifecyle hook which get trigger on component destruction
221      */
222     public ngOnDestroy(): void {
223         this.generateDataSub.unsubscribe();
224     }
225
226     /** Fetching the data from server to Load in the smarttable @protected */
227     protected generateData(): void {
228         this.isLoadingResults = true;
229         this.sdnData = [];
230         this.restService.getResource(environment.SDNCONTROLLER_URL).subscribe((sdnDetails: {}[]) => {
231             sdnDetails.forEach((res: SDNControllerModel) => {
232                 const sdnDataObj: SDNControllerList = this.generateSDNList(res);
233                 this.sdnData.push(sdnDataObj);
234             });
235             if (this.sdnData.length > 0) {
236                 this.checkDataClass = 'dataTables_present';
237             } else {
238                 this.checkDataClass = 'dataTables_empty';
239             }
240             this.dataSource.load(this.sdnData).then((data: {}) => {
241                 this.isLoadingResults = false;
242             }).catch();
243         }, (error: ERRORDATA) => {
244             this.restService.handleError(error, 'get');
245             this.isLoadingResults = false;
246         });
247     }
248
249 }