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 url: { title: this.translateService.instant('URL'), width: '30%' },
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
185 /** smart table listing manipulation @public */
186 public onChange(perPageValue: number): void {
187 this.dataSource.setPaging(1, perPageValue, true);
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);
196 /** Generate generateSDNData object from loop and return for the datasource @public */
197 public generateSDNList(sdn: SDNControllerModel): SDNControllerList {
202 operationalState: sdn._admin.operationalState,
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) => {
213 this.sharedService.callData();
215 }).catch((): void => {
216 // Catch Navigation Error
221 * Lifecyle hook which get trigger on component destruction
223 public ngOnDestroy(): void {
224 this.generateDataSub.unsubscribe();
227 /** Fetching the data from server to Load in the smarttable @protected */
228 protected generateData(): void {
229 this.isLoadingResults = true;
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);
236 if (this.sdnData.length > 0) {
237 this.checkDataClass = 'dataTables_present';
239 this.checkDataClass = 'dataTables_empty';
241 this.dataSource.load(this.sdnData).then((data: {}) => {
242 this.isLoadingResults = false;
243 }).catch((): void => {
244 // Catch Navigation Error
246 }, (error: ERRORDATA) => {
247 this.restService.handleError(error, 'get');
248 this.isLoadingResults = false;