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 Dashboard Component
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { TranslateService } from '@ngx-translate/core';
23 import { AuthenticationService } from 'AuthenticationService';
24 import { Chart } from 'chart.js';
25 import { ERRORDATA } from 'CommonModel';
26 import { environment } from 'environment';
27 import { NSDDetails } from 'NSDModel';
28 import { NSInstanceDetails } from 'NSInstanceModel';
29 import { ProjectData, ProjectDetails } from 'ProjectModel';
30 import { ProjectService } from 'ProjectService';
31 import { RestService } from 'RestService';
32 import { Observable, Subscription } from 'rxjs';
33 import { SDNControllerModel } from 'SDNControllerModel';
34 import { SharedService } from 'SharedService';
35 import { ProjectRoleMappings, UserDetail } from 'UserModel';
36 import { VimAccountDetails } from 'VimAccountModel';
37 import { VNFDDetails } from 'VNFDModel';
38 import { VNFInstanceDetails } from 'VNFInstanceModel';
42 * @Component takes DashboardComponent.html as template url
45 styleUrls: ['./DashboardComponent.scss'],
46 templateUrl: './DashboardComponent.html'
50 * This file created during the angular project creation
53 /** Exporting a class @exports DashboardComponent */
54 export class DashboardComponent implements OnInit {
55 /** Invoke service injectors @public */
56 public injector: Injector;
58 /** handle translate @public */
59 public translateService: TranslateService;
61 /** Observable holds logined value @public */
62 public username$: Observable<string>;
64 /** Variables holds admin is logged or not @public */
65 public isAdmin: boolean;
67 /** List of NS failed Instances @public */
68 public nsFailedInstances: {}[] = [];
70 /** Setting up count for vnfdPackages @public */
71 public vnfdPackageCount: number;
73 /** Setting up count for nsdPackage @public */
74 public nsdPackageCount: number;
76 /** Setting up count for nsInstance @public */
77 public nsInstanceCount: number;
79 /** Setting up count for vnfInstance @public */
80 public vnfInstanceCount: number;
82 /** Setting up count for vimAccount @public */
83 public vimAccountCount: number;
85 /** Setting up count for sdnController @public */
86 public sdnControllerCount: number;
88 /** Variables holds current project details @public */
89 public currentProjectDetails: {};
91 /** Array holds all the projects @public */
92 public projectList: {}[] = [];
94 /** Array holds all the projects @public */
95 public allProjectList: {}[] = [];
97 /** Variables holds the selected project @public */
98 public selectedProject: Observable<string>;
100 /** Check the Instances loading results @public */
101 public isCanvasLoadingResults: boolean = true;
103 /** Check the Projects loading results @public */
104 public isProjectsLoadingResults: boolean = true;
106 /** Give the message for the loading @public */
107 public message: string = 'PLEASEWAIT';
109 /** List of NS Success Instances @public */
110 public nsRunningInstance: {}[] = [];
112 /** List of color for Instances @private */
113 private backgroundColor: string[] = [];
115 /** Utilizes rest service for any CRUD operations @private */
116 private restService: RestService;
118 /** Utilizes auth service for any auth operations @private */
119 private authService: AuthenticationService;
121 /** Used to subscribe vnfdPackage @private */
122 private vnfdPackageCountSub: Subscription;
124 /** Used to subscribe nsdPackage @private */
125 private nsdPackageCountSub: Subscription;
127 /** Used to subscribe nsInstance @private */
128 private nsInstanceCountSub: Subscription;
130 /** Used to subscribe vnfInstance @private */
131 private vnfInstanceCountSub: Subscription;
133 /** Used to subscribe vimAccount @private */
134 private vimAccountCountSub: Subscription;
136 /** Used to subscribe sdnController @private */
137 private sdnControllerCountSub: Subscription;
139 /** No of Hours of NS Success Instances @private */
140 private noOfHours: number[] = [];
142 /** collects charts objects @private */
143 private charts: object = [];
145 /** Contains all methods related to projects @private */
146 private projectService: ProjectService;
148 /** Contains all methods related to shared @private */
149 private sharedService: SharedService;
151 /** Contains NS Instance Details */
152 private nsInstancesDataArr: {}[];
154 /** Container created time array @private */
155 private createdTimes: string[] = [];
157 /** Contains slice limit const @private */
158 private sliceLimit: number = 10;
160 /** Contians hour converter @private */
161 private hourConverter: number = 3600;
163 constructor(injector: Injector) {
164 this.injector = injector;
165 this.restService = this.injector.get(RestService);
166 this.authService = this.injector.get(AuthenticationService);
167 this.projectService = this.injector.get(ProjectService);
168 this.sharedService = this.injector.get(SharedService);
169 this.translateService = this.injector.get(TranslateService);
173 * Lifecyle Hooks the trigger before component is instantiate
175 public ngOnInit(): void {
176 this.username$ = this.authService.username;
177 this.isAdmin = (localStorage.getItem('isAdmin') === 'true') ? true : false;
178 this.selectedProject = this.authService.ProjectName;
179 this.checkAdminPrivilege();
180 this.getUserAccessedProjects();
181 this.getAllProjects();
182 this.getVnfdPackageCount();
183 this.getNsdPackageCount();
184 this.getNsInstanceCount();
185 this.getVnfInstanceCount();
186 this.getVimAccountCount();
187 this.getSDNControllerCount();
190 /** Get all the projects @public */
191 public getUserAccessedProjects(): void {
192 this.projectService.getUserProjects().subscribe((projects: UserDetail) => {
193 const projectList: {}[] = projects.project_role_mappings;
194 this.projectList = projectList.filter(
195 (thing: ProjectRoleMappings, i: number, arr: []) => arr
196 .findIndex((t: ProjectRoleMappings) => t.project_name === thing.project_name) === i
198 }, (error: Error) => {
199 // TODO: Handle failure
203 /** Fetching all the Project in dashboard @public */
204 public getAllProjects(): void {
205 this.isProjectsLoadingResults = true;
206 this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectDetails[]) => {
207 this.allProjectList = [];
208 projectsData.forEach((projectData: ProjectDetails) => {
209 const projectDataObj: ProjectData = this.generateProjectData(projectData);
210 this.allProjectList.push(projectDataObj);
212 this.isProjectsLoadingResults = false;
213 }, (error: ERRORDATA) => {
214 this.restService.handleError(error, 'get');
215 this.isProjectsLoadingResults = false;
219 /** Generate Projects object from loop and return for the datasource @public */
220 public generateProjectData(projectData: ProjectDetails): ProjectData {
222 projectName: projectData.name,
223 modificationDate: this.sharedService.convertEpochTime(projectData._admin.modified),
224 creationDate: this.sharedService.convertEpochTime(projectData._admin.created),
226 project: projectData._id
230 /** Function to check admin privilege @public */
231 public checkAdminPrivilege(): void {
233 this.projectService.getCurrentProjectDetails().subscribe((projectDetails: {}) => {
234 this.currentProjectDetails = projectDetails;
235 }, (error: Error) => {
236 // TODO: Handle failure
241 /** Get VNFD Package details @public */
242 public getVnfdPackageCount(): void {
243 this.vnfdPackageCountSub = this.restService.getResource(environment.VNFPACKAGESCONTENT_URL)
244 .subscribe((vnfdPackageData: VNFDDetails[]) => {
245 this.vnfdPackageCount = vnfdPackageData.length;
246 }, (error: ERRORDATA) => {
247 this.restService.handleError(error, 'get');
251 /** Get NSD Package details @public */
252 public getNsdPackageCount(): void {
253 this.nsdPackageCountSub = this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL)
254 .subscribe((nsdPackageData: NSDDetails[]) => {
255 this.nsdPackageCount = nsdPackageData.length;
256 }, (error: ERRORDATA) => {
257 this.restService.handleError(error, 'get');
261 /** Get NS Instance details @public */
262 public getNsInstanceCount(): void {
263 this.isCanvasLoadingResults = true;
264 this.nsInstanceCountSub = this.restService.getResource(environment.NSDINSTANCES_URL)
265 .subscribe((nsInstancesData: NSInstanceDetails[]) => {
266 this.nsInstancesDataArr = nsInstancesData;
267 this.nsInstanceCount = nsInstancesData.length;
268 this.nsInstanceChart();
269 this.isCanvasLoadingResults = false;
270 }, (error: ERRORDATA) => {
271 this.restService.handleError(error, 'get');
272 this.isCanvasLoadingResults = false;
276 /** Get NS Instance chart details @public */
277 public nsInstanceChart(): void {
278 this.nsInstancesDataArr.forEach((nsdInstanceData: NSDDetails) => {
279 const operationalStatus: string = nsdInstanceData['operational-status'];
280 const configStatus: string = nsdInstanceData['config-status'];
281 if (operationalStatus === 'failed' || configStatus === 'failed') {
282 this.nsFailedInstances.push(nsdInstanceData);
283 } else if (operationalStatus === 'running' && configStatus === 'configured') {
284 this.nsRunningInstance.push({ name: nsdInstanceData.name, id: nsdInstanceData.id });
285 this.backgroundColor.push(this.sharedService.generateColor());
286 this.createdTimes.push(((nsdInstanceData._admin.created).toString()).slice(0, this.sliceLimit));
289 const now: Date = new Date();
290 const currentTime: number = Number((now.getTime().toString().slice(0, this.sliceLimit)));
291 this.createdTimes.forEach((createdTime: string) => {
292 this.noOfHours.push((Math.round((currentTime - Number(createdTime)) / this.hourConverter)));
297 /** Prepare and sketch NS instance chart */
298 public drawNsChart(): void {
299 this.charts = new Chart('canvas', {
302 labels: this.nsRunningInstance,
304 data: this.noOfHours,
305 label: this.translateService.instant('NOOFHOURS'),
306 borderColor: this.backgroundColor,
308 backgroundColor: this.backgroundColor
313 onHover(evt: Event, item: {}): void {
314 const el: HTMLElement = document.getElementById('canvas');
315 el.style.cursor = item[0] ? 'pointer' : 'default';
318 onClick(evt: Event, item: {}): void {
319 if (item[0] !== undefined) {
320 const location: string = '/instances/ns/' + item[0]._chart.data.labels[item[0]._index].id;
321 window.open(location);
324 legend: { display: false },
329 // tslint:disable-next-line: no-any
330 callback: (label: any, index: number, labels: string): string => {
331 const length: number = 20;
332 const ending: string = '...';
333 if (label.name.length > length) {
334 return label.name.substring(0, length - ending.length) + ending;
342 labelString: this.translateService.instant('INSTANCES')
352 labelString: this.translateService.instant('NOOFHOURS')
360 /** Get VNFD instance details @public */
361 public getVnfInstanceCount(): void {
362 this.vnfInstanceCountSub = this.restService.getResource(environment.NSDINSTANCES_URL)
363 .subscribe((vnfInstanceData: VNFInstanceDetails[]) => {
364 this.vnfInstanceCount = vnfInstanceData.length;
365 }, (error: ERRORDATA) => {
366 this.restService.handleError(error, 'get');
370 /** Get VIM account details @public */
371 public getVimAccountCount(): void {
372 this.vimAccountCountSub = this.restService.getResource(environment.VIMACCOUNTS_URL)
373 .subscribe((vimAccountData: VimAccountDetails[]) => {
374 this.vimAccountCount = vimAccountData.length;
375 }, (error: ERRORDATA) => {
376 this.restService.handleError(error, 'get');
380 /** Get SDN Controller Count @public */
381 public getSDNControllerCount(): void {
382 this.sdnControllerCountSub = this.restService.getResource(environment.SDNCONTROLLER_URL)
383 .subscribe((sdnControllerData: SDNControllerModel[]) => {
384 this.sdnControllerCount = sdnControllerData.length;
385 }, (error: ERRORDATA) => {
386 this.restService.handleError(error, 'get');
391 * Lifecyle Hooks the trigger before component is deleted
393 public ngOnDestroy(): void {
394 this.vnfdPackageCountSub.unsubscribe();
395 this.nsdPackageCountSub.unsubscribe();
396 this.nsInstanceCountSub.unsubscribe();
397 this.vnfInstanceCountSub.unsubscribe();
398 this.vimAccountCountSub.unsubscribe();
399 this.sdnControllerCountSub.unsubscribe();