Bug 1383 Error when consulting NS instance's topology
[osm/NG-UI.git] / src / app / instances / vnf-instances / VNFInstancesComponent.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 NVF Instance Component
20  */
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { TranslateService } from '@ngx-translate/core';
23 import { ERRORDATA } from 'CommonModel';
24 import { DataService } from 'DataService';
25 import { environment } from 'environment';
26 import { LocalDataSource } from 'ng2-smart-table';
27 import { RestService } from 'RestService';
28 import { Subscription } from 'rxjs';
29 import { SharedService } from 'SharedService';
30 import { VNFInstanceData, VNFInstanceDetails } from 'VNFInstanceModel';
31 import { VNFInstancesActionComponent } from 'VNFInstancesActionComponent';
32 import { VNFLinkComponent } from 'VNFLinkComponent';
33
34 /**
35  * Creating component
36  * @Component takes VNFInstancesComponent.html as template url
37  */
38 @Component({
39     templateUrl: './VNFInstancesComponent.html',
40     styleUrls: ['./VNFInstancesComponent.scss']
41 })
42 /** Exporting a class @exports VNFInstancesComponent */
43 export class VNFInstancesComponent implements OnInit {
44     /** To inject services @public */
45     public injector: Injector;
46
47     /** smart table data service collections @public */
48     public dataSource: LocalDataSource = new LocalDataSource();
49
50     /** Instance component are stored in settings @public */
51     public settings: {} = {};
52
53     /** Contains objects for menu settings @public */
54     public columnList: {} = {};
55
56     /** vnf instance array @public */
57     public vnfInstanceData: {}[] = [];
58
59     /** selected rows array @public */
60     public selectedRows: string[] = [];
61
62     /** selected list array @public */
63     public selectList: string[] = [];
64
65     /** Check the loading results @public */
66     public isLoadingResults: boolean = true;
67
68     /** Give the message for the loading @public */
69     public message: string = 'PLEASEWAIT';
70
71     /** Class for empty and present data @public */
72     public checkDataClass: string;
73
74     /** Utilizes rest service for any CRUD operations @private */
75     private restService: RestService;
76
77     /** packages data service collections @private */
78     private dataService: DataService;
79
80     /** Contains all methods related to shared @private */
81     private sharedService: SharedService;
82
83     /** Contains tranlsate instance @private */
84     private translateService: TranslateService;
85
86     /** Instance of subscriptions @private */
87     private generateDataSub: Subscription;
88
89     constructor(injector: Injector) {
90         this.injector = injector;
91         this.restService = this.injector.get(RestService);
92         this.dataService = this.injector.get(DataService);
93         this.sharedService = this.injector.get(SharedService);
94         this.translateService = this.injector.get(TranslateService);
95     }
96
97     /**
98      * Lifecyle Hooks the trigger before component is instantiate
99      */
100     public ngOnInit(): void {
101         this.generateTableColumn();
102         this.generateTableSettings();
103         this.generateData();
104         this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
105     }
106
107     /** Generate smart table row title and filters @public  */
108     public generateTableSettings(): void {
109         this.settings = {
110             actions: { add: false, edit: false, delete: false, position: 'right' },
111             columns: this.columnList,
112             attr: this.sharedService.tableClassConfig(),
113             pager: this.sharedService.paginationPagerConfig(),
114             noDataMessage: this.translateService.instant('NODATAMSG')
115         };
116     }
117
118     /** Generate smart table row title and filters @public  */
119     public generateTableColumn(): void {
120         this.columnList = {
121             identifier: { title: this.translateService.instant('IDENTIFIER'), width: '25%', sortDirection: 'asc' },
122             VNFD: {
123                 title: this.translateService.instant('VNFD'), width: '20%', type: 'custom',
124                 valuePrepareFunction: (cell: VNFInstanceData, row: VNFInstanceData): VNFInstanceData => row,
125                 renderComponent: VNFLinkComponent
126             },
127             MemberIndex: { title: this.translateService.instant('MEMBERINDEX'), width: '15%' },
128             NS: { title: this.translateService.instant('NS'), width: '20%' },
129             CreatedAt: { title: this.translateService.instant('CREATEDAT'), width: '15%' },
130             Actions: {
131                 name: 'Action', width: '5%', filter: false, sort: false, type: 'custom',
132                 title: this.translateService.instant('ACTIONS'),
133                 valuePrepareFunction: (cell: VNFInstanceData, row: VNFInstanceData): VNFInstanceData => row,
134                 renderComponent: VNFInstancesActionComponent
135             }
136         };
137     }
138
139     /** generateData initiate the vnf-instance list */
140     public generateData(): void {
141         this.isLoadingResults = true;
142         this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]) => {
143             this.vnfInstanceData = [];
144             vnfInstancesData.forEach((vnfInstanceData: VNFInstanceDetails): void => {
145                 const vnfDataObj: {} =
146                 {
147                     VNFD: vnfInstanceData['vnfd-ref'],
148                     identifier: vnfInstanceData._id,
149                     MemberIndex: vnfInstanceData['member-vnf-index-ref'],
150                     NS: vnfInstanceData['nsr-id-ref'],
151                     VNFID: vnfInstanceData['vnfd-id'],
152                     CreatedAt: this.sharedService.convertEpochTime(Number(vnfInstanceData['created-time']))
153                 };
154                 this.vnfInstanceData.push(vnfDataObj);
155             });
156             if (this.vnfInstanceData.length > 0) {
157                 this.checkDataClass = 'dataTables_present';
158             } else {
159                 this.checkDataClass = 'dataTables_empty';
160             }
161             this.dataSource.load(this.vnfInstanceData).then((data: {}) => {
162                 this.isLoadingResults = false;
163             }).catch();
164         }, (error: ERRORDATA) => {
165             this.restService.handleError(error, 'get');
166             this.isLoadingResults = false;
167         });
168     }
169
170     /** smart table listing manipulation @public */
171     public onChange(perPageValue: number): void {
172         this.dataSource.setPaging(1, perPageValue, true);
173     }
174
175     /** smart table listing manipulation @public */
176     public onUserRowSelect(event: MessageEvent): void {
177         Object.assign(event.data, { page: 'vnf-instance' });
178         this.dataService.changeMessage(event.data);
179     }
180
181     /**
182      * Lifecyle hook which get trigger on component destruction
183      */
184     public ngOnDestroy(): void {
185         this.generateDataSub.unsubscribe();
186     }
187 }