Fix Bug 2296: Newly Created Ns should come first in NG-UI
[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%' },
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: {
130                 title: this.translateService.instant('CREATEDAT'), width: '15%', sortDirection: 'desc',
131                 compareFunction: this.sharedService.compareFunction
132             },
133             Actions: {
134                 name: 'Action', width: '5%', filter: false, sort: false, type: 'custom',
135                 title: this.translateService.instant('ACTIONS'),
136                 valuePrepareFunction: (cell: VNFInstanceData, row: VNFInstanceData): VNFInstanceData => row,
137                 renderComponent: VNFInstancesActionComponent
138             }
139         };
140     }
141
142     /** generateData initiate the vnf-instance list */
143     public generateData(): void {
144         this.isLoadingResults = true;
145         this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]) => {
146             this.vnfInstanceData = [];
147             vnfInstancesData.forEach((vnfInstanceData: VNFInstanceDetails): void => {
148                 const vnfDataObj: {} =
149                 {
150                     VNFD: vnfInstanceData['vnfd-ref'],
151                     identifier: vnfInstanceData._id,
152                     MemberIndex: vnfInstanceData['member-vnf-index-ref'],
153                     NS: vnfInstanceData['nsr-id-ref'],
154                     VNFID: vnfInstanceData['vnfd-id'],
155                     CreatedAt: this.sharedService.convertEpochTime(Number(vnfInstanceData['created-time']))
156                 };
157                 this.vnfInstanceData.push(vnfDataObj);
158             });
159             if (this.vnfInstanceData.length > 0) {
160                 this.checkDataClass = 'dataTables_present';
161             } else {
162                 this.checkDataClass = 'dataTables_empty';
163             }
164             this.dataSource.load(this.vnfInstanceData).then((data: {}) => {
165                 this.isLoadingResults = false;
166             }).catch((): void => {
167                 // Catch Navigation Error
168             });
169         }, (error: ERRORDATA) => {
170             this.restService.handleError(error, 'get');
171             this.isLoadingResults = false;
172         });
173     }
174
175     /** smart table listing manipulation @public */
176     public onChange(perPageValue: number): void {
177         this.dataSource.setPaging(1, perPageValue, true);
178     }
179
180     /** smart table listing manipulation @public */
181     public onUserRowSelect(event: MessageEvent): void {
182         Object.assign(event.data, { page: 'vnf-instance' });
183         this.dataService.changeMessage(event.data);
184     }
185
186     /**
187      * Lifecyle hook which get trigger on component destruction
188      */
189     public ngOnDestroy(): void {
190         this.generateDataSub.unsubscribe();
191     }
192 }