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: SANDHYA JS (sandhya.j@tataelxsi.co.in)
19 * @file NS Config Template details Component.
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
26 import { ComposePackages } from 'ComposePackages';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import { LocalDataSource } from 'ng2-smart-table';
30 import { NSConfigTemplateActionComponent } from 'NSCONFIGTEMPLATEACTION';
31 import { NSCONFIG, NSConfigData } from 'NSCONFIGTEMPLATEMODEL';
32 import { RestService } from 'RestService';
33 import { Subscription } from 'rxjs';
34 import { SharedService } from 'SharedService';
35 import { VNFData } from 'VNFDModel';
39 * @Component takes NSConfigTemplateComponent.html as template url
42 selector: 'app-ns-config-template',
43 templateUrl: './NSConfigTemplateComponent.html',
44 styleUrls: ['./NSConfigTemplateComponent.scss']
46 /** Exporting a class @exports NSConfigTemplateComponent */
47 export class NSConfigTemplateComponent implements OnInit {
48 /** To inject services @public */
49 public injector: Injector;
51 /** Data of smarttable populate through LocalDataSource @public */
52 public dataSource: LocalDataSource = new LocalDataSource();
54 /** handle translate @public */
55 public translateService: TranslateService;
57 /** Columns list of the smart table @public */
58 public columnLists: object = {};
60 /** Settings for smarttable to populate the table with columns @public */
61 public settings: object = {};
63 /** Check the loading results @public */
64 public isLoadingResults: boolean = true;
66 /** Give the message for the loading @public */
67 public message: string = 'PLEASEWAIT';
69 /** Class for empty and present data @public */
70 public checkDataClass: string;
72 /** Instance of the rest service @private */
73 private restService: RestService;
75 /** dataService to pass the data from one component to another @private */
76 private dataService: DataService;
78 /** Formation of appropriate Data for LocalDatasource @private */
79 private nsConfigData: NSConfigData[] = [];
81 /** Contains all methods related to shared @private */
82 private sharedService: SharedService;
84 /** Controls the header form @private */
85 private headers: HttpHeaders;
87 /** Instance of the modal service @private */
88 private modalService: NgbModal;
90 /** Instance of subscriptions @private */
91 private generateDataSub: Subscription;
93 constructor(injector: Injector) {
94 this.injector = injector;
95 this.restService = this.injector.get(RestService);
96 this.dataService = this.injector.get(DataService);
97 this.sharedService = this.injector.get(SharedService);
98 this.translateService = this.injector.get(TranslateService);
99 this.modalService = this.injector.get(NgbModal);
103 * Lifecyle Hooks the trigger before component is instantiate
105 public ngOnInit(): void {
106 this.generateColumns();
107 this.generateSettings();
109 this.headers = new HttpHeaders({
110 'Content-Type': 'application/gzip',
111 Accept: 'application/json',
112 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
114 this.generateDataSub = this.sharedService.dataEvent.subscribe((): void => { this.generateData(); });
117 /** smart table Header Colums @public */
118 public generateColumns(): void {
120 name: { title: this.translateService.instant('NAME'), width: '25%' },
121 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '25%' },
122 nsdId: { title: this.translateService.instant('PAGE.INSTANCEINSTANTIATE.NSID'), width: '25%' },
123 created: { title: this.translateService.instant('Created'), width: '15%' },
125 name: 'Action', width: '15%', filter: false, sort: false, type: 'custom',
126 title: this.translateService.instant('ACTIONS'),
127 valuePrepareFunction: (cell: VNFData, row: VNFData): VNFData => row, renderComponent: NSConfigTemplateActionComponent
132 /** smart table Data Settings @public */
133 public generateSettings(): void {
136 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
140 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>',
143 columns: this.columnLists,
150 attr: this.sharedService.tableClassConfig(),
151 pager: this.sharedService.paginationPagerConfig(),
152 noDataMessage: this.translateService.instant('NODATAMSG')
156 /** smart table listing manipulation @public */
157 public onChange(perPageValue: number): void {
158 this.dataSource.setPaging(1, perPageValue, true);
161 /** OnUserRowSelect Function @public */
162 public onUserRowSelect(event: MessageEvent): void {
163 Object.assign(event.data, { page: 'ns-config-template' });
164 this.dataService.changeMessage(event.data);
168 /** Generate nsData object from loop and return for the datasource @public */
169 public generatetemplateData(nsData: NSCONFIG): NSConfigData {
172 identifier: nsData._id,
173 operationalState: nsData._admin.operationalState,
174 created: this.sharedService.convertEpochTime(Number(nsData._admin.created)),
175 modified: this.sharedService.convertEpochTime(Number(nsData._admin.modified)),
179 /** Handle compose new ns package method @public */
180 public composeTemplate(): void {
181 // eslint-disable-next-line security/detect-non-literal-fs-filename
182 const modalRef: NgbModalRef = this.modalService.open(ComposePackages, { backdrop: 'static' });
183 modalRef.componentInstance.params = { page: 'ns-config-template' };
184 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
186 this.sharedService.callData();
188 }).catch((): void => {
189 // Catch Navigation Error
194 * Lifecyle hook which get trigger on component destruction
196 public ngOnDestroy(): void {
197 this.generateDataSub.unsubscribe();
200 /** Fetching the data from server to Load in the smarttable @protected */
201 protected generateData(): void {
202 this.isLoadingResults = true;
203 this.restService.getResource(environment.NSCONFIGTEMPLATE_URL).subscribe((nsdConfigData: NSCONFIG[]): void => {
204 this.nsConfigData = [];
205 nsdConfigData.forEach((nsddata: NSCONFIG): void => {
206 const vnfDataObj: NSConfigData = this.generatetemplateData(nsddata);
207 this.nsConfigData.push(vnfDataObj);
209 if (this.nsConfigData.length > 0) {
210 this.checkDataClass = 'dataTables_present';
212 this.checkDataClass = 'dataTables_empty';
214 this.dataSource.load(this.nsConfigData).then((data: boolean): void => {
215 this.isLoadingResults = false;
216 }).catch((): void => {
217 this.isLoadingResults = false;
219 }, (error: ERRORDATA): void => {
220 this.restService.handleError(error, 'get');
221 this.isLoadingResults = false;