2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file SDN Controller details Component.
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';
37 * @Component takes SDNControllerDetailsComponent.html as template url
40 templateUrl: './SDNControllerDetailsComponent.html',
41 styleUrls: ['./SDNControllerDetailsComponent.scss']
43 /** Exporting a class @exports SDNControllerDetailsComponent */
44 export class SDNControllerDetailsComponent implements OnInit, OnDestroy {
45 /** Injector to invoke other services @public */
46 public injector: Injector;
48 /** Selected list array @public */
49 public selectList: object[] = [];
51 /** Instance component are stored in settings @public */
52 public settings: {} = {};
54 /** Contains objects for menu settings @public */
55 public columnList: {} = {};
57 /** Data of smarttable populate through LocalDataSource @public */
58 public dataSource: LocalDataSource = new LocalDataSource();
60 /** Check the loading results @public */
61 public isLoadingResults: boolean = true;
63 /** Give the message for the loading @public */
64 public message: string = 'PLEASEWAIT';
66 /** Class for empty and present data @public */
67 public checkDataClass: string;
69 /** operational State init data @public */
70 public operationalStateFirstStep: string = CONFIGCONSTANT.sdnOperationalStateFirstStep;
72 /** operational State running data @public */
73 public operationalStateSecondStep: string = CONFIGCONSTANT.sdnOperationalStateStateSecondStep;
75 /** operational State failed data @public */
76 public operationalStateThirdStep: string = CONFIGCONSTANT.sdnOperationalStateThirdStep;
78 /** Instance of the rest service @private */
79 private restService: RestService;
81 /** Contains all methods related to shared @private */
82 private sharedService: SharedService;
84 /** dataService to pass the data from one component to another @private */
85 private dataService: DataService;
87 /** Contains tranlsate instance @private */
88 private translateService: TranslateService;
90 /** Formation of appropriate Data for LocalDatasource @private */
91 private sdnData: {}[] = [];
93 /** Instance of the modal service @private */
94 private modalService: NgbModal;
96 /** Instance of subscriptions @private */
97 private generateDataSub: Subscription;
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);
109 * Lifecyle Hooks the trigger before component is instantiate
111 public ngOnInit(): void {
112 this.generateTableColumn();
113 this.generateTableSettings();
115 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
118 /** Generate smart table row title and filters @public */
119 public generateTableSettings(): void {
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')
129 /** Generate smart table row title and filters @public */
130 public generateTableColumn(): void {
132 name: { title: this.translateService.instant('NAME'), width: '15%', sortDirection: 'asc' },
133 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
135 title: this.translateService.instant('TYPE'), width: '15%',
139 selectText: 'Select',
145 title: this.translateService.instant('OPERATIONALSTATUS'), width: '15%', type: 'html',
149 selectText: 'Select',
151 { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep },
152 { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep },
153 { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }
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>
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>
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>
171 return `<span>${row.operationalState}</span>`;
175 ip: { title: this.translateService.instant('IP'), width: '15%' },
176 port: { title: this.translateService.instant('PORT'), width: '15%' },
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
186 /** smart table listing manipulation @public */
187 public onChange(perPageValue: number): void {
188 this.dataSource.setPaging(1, perPageValue, true);
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);
197 /** Generate generateSDNData object from loop and return for the datasource @public */
198 public generateSDNList(sdn: SDNControllerModel): SDNControllerList {
203 operationalState: sdn._admin.operationalState,
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) => {
214 this.sharedService.callData();
220 * Lifecyle hook which get trigger on component destruction
222 public ngOnDestroy(): void {
223 this.generateDataSub.unsubscribe();
226 /** Fetching the data from server to Load in the smarttable @protected */
227 protected generateData(): void {
228 this.isLoadingResults = true;
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);
235 if (this.sdnData.length > 0) {
236 this.checkDataClass = 'dataTables_present';
238 this.checkDataClass = 'dataTables_empty';
240 this.dataSource.load(this.sdnData).then((data: {}) => {
241 this.isLoadingResults = false;
243 }, (error: ERRORDATA) => {
244 this.restService.handleError(error, 'get');
245 this.isLoadingResults = false;