blob: 2feef7c0c567f24f59325d0a65a54ad43e6c6640 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 Dashboard Component
20 */
21import { Component, Injector, OnInit } from '@angular/core';
22import { TranslateService } from '@ngx-translate/core';
SANDHYA.JS4a7a5422021-05-15 15:35:22 +053023import { NotifierService } from 'angular-notifier';
kumaran.m3b4814a2020-05-01 19:48:54 +053024import { AuthenticationService } from 'AuthenticationService';
Barath Kumar Re53dbb62021-06-08 11:06:38 +053025import { Chart } from 'chart.js';
SANDHYA.JS4a7a5422021-05-15 15:35:22 +053026import 'chartjs-plugin-labels';
27import { ERRORDATA, TYPESECTION, VIM_TYPES } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053028import { environment } from 'environment';
29import { NSDDetails } from 'NSDModel';
30import { NSInstanceDetails } from 'NSInstanceModel';
31import { ProjectData, ProjectDetails } from 'ProjectModel';
32import { ProjectService } from 'ProjectService';
33import { RestService } from 'RestService';
34import { Observable, Subscription } from 'rxjs';
35import { SDNControllerModel } from 'SDNControllerModel';
36import { SharedService } from 'SharedService';
37import { ProjectRoleMappings, UserDetail } from 'UserModel';
SANDHYA.JS4a7a5422021-05-15 15:35:22 +053038import { isNullOrUndefined } from 'util';
kumaran.m3b4814a2020-05-01 19:48:54 +053039import { VimAccountDetails } from 'VimAccountModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053040import { VNFInstanceDetails } from 'VNFInstanceModel';
41
42/**
43 * Creating component
44 * @Component takes DashboardComponent.html as template url
45 */
46@Component({
47 styleUrls: ['./DashboardComponent.scss'],
48 templateUrl: './DashboardComponent.html'
49})
50
51/**
52 * This file created during the angular project creation
53 */
54
55/** Exporting a class @exports DashboardComponent */
56export class DashboardComponent implements OnInit {
57 /** Invoke service injectors @public */
58 public injector: Injector;
59
60 /** handle translate @public */
61 public translateService: TranslateService;
62
63 /** Observable holds logined value @public */
64 public username$: Observable<string>;
65
66 /** Variables holds admin is logged or not @public */
67 public isAdmin: boolean;
68
69 /** List of NS failed Instances @public */
70 public nsFailedInstances: {}[] = [];
71
72 /** Setting up count for vnfdPackages @public */
73 public vnfdPackageCount: number;
74
75 /** Setting up count for nsdPackage @public */
76 public nsdPackageCount: number;
77
78 /** Setting up count for nsInstance @public */
79 public nsInstanceCount: number;
80
81 /** Setting up count for vnfInstance @public */
82 public vnfInstanceCount: number;
83
84 /** Setting up count for vimAccount @public */
85 public vimAccountCount: number;
86
87 /** Setting up count for sdnController @public */
88 public sdnControllerCount: number;
89
90 /** Variables holds current project details @public */
91 public currentProjectDetails: {};
92
93 /** Array holds all the projects @public */
94 public projectList: {}[] = [];
95
96 /** Array holds all the projects @public */
97 public allProjectList: {}[] = [];
98
99 /** Variables holds the selected project @public */
100 public selectedProject: Observable<string>;
101
102 /** Check the Instances loading results @public */
103 public isCanvasLoadingResults: boolean = true;
104
105 /** Check the Projects loading results @public */
106 public isProjectsLoadingResults: boolean = true;
107
108 /** Give the message for the loading @public */
109 public message: string = 'PLEASEWAIT';
110
Barath Kumar R208bef22020-07-07 12:28:04 +0530111 /** List of NS Success Instances @public */
Barath Kumar R5abb2742020-11-22 20:15:10 +0530112 public nsRunningInstance: string[] = [];
Barath Kumar R208bef22020-07-07 12:28:04 +0530113
SANDHYA.JS4a7a5422021-05-15 15:35:22 +0530114 /** Contains VIM Account details @public */
115 public vimData: VimAccountDetails[] = [];
116
117 /** Contains Selected VIM Details @public */
118 public selectedVIMDetails: VimAccountDetails = null;
119
120 /** List of VIM_TYPES @public */
121 public vimTypes: TYPESECTION[] = VIM_TYPES;
122
123 /** Array holds Vim data filtered with selected vimtype */
124 public vimList: VimAccountDetails[] = [];
125
Barath Kumar R208bef22020-07-07 12:28:04 +0530126 /** List of color for Instances @private */
127 private backgroundColor: string[] = [];
kumaran.m3b4814a2020-05-01 19:48:54 +0530128
129 /** Utilizes rest service for any CRUD operations @private */
130 private restService: RestService;
131
132 /** Utilizes auth service for any auth operations @private */
133 private authService: AuthenticationService;
134
135 /** Used to subscribe vnfdPackage @private */
136 private vnfdPackageCountSub: Subscription;
137
138 /** Used to subscribe nsdPackage @private */
139 private nsdPackageCountSub: Subscription;
140
141 /** Used to subscribe nsInstance @private */
142 private nsInstanceCountSub: Subscription;
143
144 /** Used to subscribe vnfInstance @private */
145 private vnfInstanceCountSub: Subscription;
146
147 /** Used to subscribe vimAccount @private */
148 private vimAccountCountSub: Subscription;
149
150 /** Used to subscribe sdnController @private */
151 private sdnControllerCountSub: Subscription;
152
153 /** No of Hours of NS Success Instances @private */
154 private noOfHours: number[] = [];
155
156 /** collects charts objects @private */
157 private charts: object = [];
158
159 /** Contains all methods related to projects @private */
160 private projectService: ProjectService;
161
162 /** Contains all methods related to shared @private */
163 private sharedService: SharedService;
164
165 /** Contains NS Instance Details */
166 private nsInstancesDataArr: {}[];
167
168 /** Container created time array @private */
169 private createdTimes: string[] = [];
170
171 /** Contains slice limit const @private */
172 private sliceLimit: number = 10;
173
174 /** Contians hour converter @private */
175 private hourConverter: number = 3600;
176
SANDHYA.JS4a7a5422021-05-15 15:35:22 +0530177 /** Notifier service to popup notification @private */
178 private notifierService: NotifierService;
179
kumaran.m3b4814a2020-05-01 19:48:54 +0530180 constructor(injector: Injector) {
181 this.injector = injector;
182 this.restService = this.injector.get(RestService);
183 this.authService = this.injector.get(AuthenticationService);
184 this.projectService = this.injector.get(ProjectService);
185 this.sharedService = this.injector.get(SharedService);
186 this.translateService = this.injector.get(TranslateService);
SANDHYA.JS4a7a5422021-05-15 15:35:22 +0530187 this.notifierService = this.injector.get(NotifierService);
kumaran.m3b4814a2020-05-01 19:48:54 +0530188 }
189
190 /**
191 * Lifecyle Hooks the trigger before component is instantiate
192 */
193 public ngOnInit(): void {
194 this.username$ = this.authService.username;
195 this.isAdmin = (localStorage.getItem('isAdmin') === 'true') ? true : false;
196 this.selectedProject = this.authService.ProjectName;
197 this.checkAdminPrivilege();
198 this.getUserAccessedProjects();
199 this.getAllProjects();
200 this.getVnfdPackageCount();
201 this.getNsdPackageCount();
202 this.getNsInstanceCount();
203 this.getVnfInstanceCount();
204 this.getVimAccountCount();
205 this.getSDNControllerCount();
206 }
207
208 /** Get all the projects @public */
209 public getUserAccessedProjects(): void {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530210 this.projectService.getUserProjects().subscribe((projects: UserDetail): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530211 const projectList: {}[] = projects.project_role_mappings;
212 this.projectList = projectList.filter(
Barath Kumar R063a3f12020-12-29 16:35:09 +0530213 (thing: ProjectRoleMappings, i: number, arr: []): boolean => arr
214 .findIndex((t: ProjectRoleMappings): boolean => t.project_name === thing.project_name) === i
kumaran.m3b4814a2020-05-01 19:48:54 +0530215 );
Barath Kumar R063a3f12020-12-29 16:35:09 +0530216 }, (error: Error): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530217 // TODO: Handle failure
218 });
219 }
220
221 /** Fetching all the Project in dashboard @public */
222 public getAllProjects(): void {
223 this.isProjectsLoadingResults = true;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530224 this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectDetails[]): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530225 this.allProjectList = [];
Barath Kumar R063a3f12020-12-29 16:35:09 +0530226 projectsData.forEach((projectData: ProjectDetails): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530227 const projectDataObj: ProjectData = this.generateProjectData(projectData);
228 this.allProjectList.push(projectDataObj);
229 });
230 this.isProjectsLoadingResults = false;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530231 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530232 this.restService.handleError(error, 'get');
233 this.isProjectsLoadingResults = false;
234 });
235 }
236
237 /** Generate Projects object from loop and return for the datasource @public */
238 public generateProjectData(projectData: ProjectDetails): ProjectData {
239 return {
240 projectName: projectData.name,
241 modificationDate: this.sharedService.convertEpochTime(projectData._admin.modified),
242 creationDate: this.sharedService.convertEpochTime(projectData._admin.created),
243 id: projectData._id,
244 project: projectData._id
245 };
246 }
247
248 /** Function to check admin privilege @public */
249 public checkAdminPrivilege(): void {
250 if (!this.isAdmin) {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530251 this.projectService.getCurrentProjectDetails().subscribe((projectDetails: {}): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530252 this.currentProjectDetails = projectDetails;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530253 }, (error: Error): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530254 // TODO: Handle failure
255 });
256 }
257 }
258
259 /** Get VNFD Package details @public */
260 public getVnfdPackageCount(): void {
261 this.vnfdPackageCountSub = this.restService.getResource(environment.VNFPACKAGESCONTENT_URL)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530262 .subscribe((vnfdPackageData: []): void => {
Barath Kumar R208bef22020-07-07 12:28:04 +0530263 this.vnfdPackageCount = vnfdPackageData.length;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530264 }, (error: ERRORDATA): void => {
Barath Kumar R208bef22020-07-07 12:28:04 +0530265 this.restService.handleError(error, 'get');
266 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530267 }
268
269 /** Get NSD Package details @public */
270 public getNsdPackageCount(): void {
271 this.nsdPackageCountSub = this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530272 .subscribe((nsdPackageData: NSDDetails[]): void => {
Barath Kumar R208bef22020-07-07 12:28:04 +0530273 this.nsdPackageCount = nsdPackageData.length;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530274 }, (error: ERRORDATA): void => {
Barath Kumar R208bef22020-07-07 12:28:04 +0530275 this.restService.handleError(error, 'get');
276 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530277 }
278
279 /** Get NS Instance details @public */
280 public getNsInstanceCount(): void {
281 this.isCanvasLoadingResults = true;
282 this.nsInstanceCountSub = this.restService.getResource(environment.NSDINSTANCES_URL)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530283 .subscribe((nsInstancesData: NSInstanceDetails[]): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530284 this.nsInstancesDataArr = nsInstancesData;
285 this.nsInstanceCount = nsInstancesData.length;
286 this.nsInstanceChart();
287 this.isCanvasLoadingResults = false;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530288 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530289 this.restService.handleError(error, 'get');
290 this.isCanvasLoadingResults = false;
291 });
292 }
293
294 /** Get NS Instance chart details @public */
295 public nsInstanceChart(): void {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530296 this.nsInstancesDataArr.forEach((nsdInstanceData: NSDDetails): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530297 const operationalStatus: string = nsdInstanceData['operational-status'];
298 const configStatus: string = nsdInstanceData['config-status'];
299 if (operationalStatus === 'failed' || configStatus === 'failed') {
300 this.nsFailedInstances.push(nsdInstanceData);
301 } else if (operationalStatus === 'running' && configStatus === 'configured') {
Barath Kumar R5abb2742020-11-22 20:15:10 +0530302 this.nsRunningInstance.push(nsdInstanceData.name);
Barath Kumar R208bef22020-07-07 12:28:04 +0530303 this.backgroundColor.push(this.sharedService.generateColor());
kumaran.m3b4814a2020-05-01 19:48:54 +0530304 this.createdTimes.push(((nsdInstanceData._admin.created).toString()).slice(0, this.sliceLimit));
305 }
306 });
307 const now: Date = new Date();
308 const currentTime: number = Number((now.getTime().toString().slice(0, this.sliceLimit)));
Barath Kumar R063a3f12020-12-29 16:35:09 +0530309 this.createdTimes.forEach((createdTime: string): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530310 this.noOfHours.push((Math.round((currentTime - Number(createdTime)) / this.hourConverter)));
311 });
312 this.drawNsChart();
313 }
314
315 /** Prepare and sketch NS instance chart */
316 public drawNsChart(): void {
317 this.charts = new Chart('canvas', {
318 type: 'bar',
319 data: {
320 labels: this.nsRunningInstance,
321 datasets: [{
322 data: this.noOfHours,
323 label: this.translateService.instant('NOOFHOURS'),
Barath Kumar R208bef22020-07-07 12:28:04 +0530324 borderColor: this.backgroundColor,
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530325 fill: false,
Barath Kumar R208bef22020-07-07 12:28:04 +0530326 backgroundColor: this.backgroundColor
kumaran.m3b4814a2020-05-01 19:48:54 +0530327 }]
328 },
329 options: {
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530330 hover: {
331 onHover(evt: Event, item: {}): void {
332 const el: HTMLElement = document.getElementById('canvas');
333 el.style.cursor = item[0] ? 'pointer' : 'default';
334 }
Barath Kumar R208bef22020-07-07 12:28:04 +0530335 },
SANDHYA.JS4a7a5422021-05-15 15:35:22 +0530336 plugins: {
337 labels: {
338 // render 'label', 'value', 'percentage', 'image' or custom function, default is 'percentage'
339 render: 'value'
340 }
341 },
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530342 legend: { display: false },
kumaran.m3b4814a2020-05-01 19:48:54 +0530343 scales: {
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530344 xAxes: [{
kumaran.m3b4814a2020-05-01 19:48:54 +0530345 display: true,
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530346 scaleLabel: {
347 display: true,
348 labelString: this.translateService.instant('INSTANCES')
349 }
350 }],
351 yAxes: [{
kumaran.m3b4814a2020-05-01 19:48:54 +0530352 ticks: {
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530353 beginAtZero: true
kumaran.m3b4814a2020-05-01 19:48:54 +0530354 },
355 display: true,
Barath Kumar Re53dbb62021-06-08 11:06:38 +0530356 scaleLabel: {
357 display: true,
358 labelString: this.translateService.instant('NOOFHOURS')
359 }
360 }]
kumaran.m3b4814a2020-05-01 19:48:54 +0530361 }
362 }
363 });
364 }
365
366 /** Get VNFD instance details @public */
367 public getVnfInstanceCount(): void {
368 this.vnfInstanceCountSub = this.restService.getResource(environment.NSDINSTANCES_URL)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530369 .subscribe((vnfInstanceData: VNFInstanceDetails[]): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530370 this.vnfInstanceCount = vnfInstanceData.length;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530371 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530372 this.restService.handleError(error, 'get');
373 });
374 }
375
376 /** Get VIM account details @public */
377 public getVimAccountCount(): void {
378 this.vimAccountCountSub = this.restService.getResource(environment.VIMACCOUNTS_URL)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530379 .subscribe((vimAccountData: VimAccountDetails[]): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530380 this.vimAccountCount = vimAccountData.length;
SANDHYA.JS4a7a5422021-05-15 15:35:22 +0530381 this.vimData = vimAccountData;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530382 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530383 this.restService.handleError(error, 'get');
384 });
385 }
386
387 /** Get SDN Controller Count @public */
388 public getSDNControllerCount(): void {
389 this.sdnControllerCountSub = this.restService.getResource(environment.SDNCONTROLLER_URL)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530390 .subscribe((sdnControllerData: SDNControllerModel[]): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530391 this.sdnControllerCount = sdnControllerData.length;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530392 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530393 this.restService.handleError(error, 'get');
394 });
395 }
396
SANDHYA.JS4a7a5422021-05-15 15:35:22 +0530397 /** Get Vim data filtered by the selected Vim Type @public */
398 public getSelectedVimTypeList(selectedVIMType: string): void {
399 this.vimList = this.vimData.filter((vimData: VimAccountDetails): boolean =>
400 vimData.vim_type === selectedVIMType);
401
402 }
403
404 /** Get Selected VIM details @public */
405 public getSelectedVIMDetails(vimDetails: VimAccountDetails): void {
406 if (!isNullOrUndefined(vimDetails.resources)) {
407 this.selectedVIMDetails = vimDetails;
408 } else {
409 this.notifierService.notify('error', this.translateService.instant('RESOURCESNOTFOUND'));
410 }
411 }
412
kumaran.m3b4814a2020-05-01 19:48:54 +0530413 /**
414 * Lifecyle Hooks the trigger before component is deleted
415 */
416 public ngOnDestroy(): void {
417 this.vnfdPackageCountSub.unsubscribe();
418 this.nsdPackageCountSub.unsubscribe();
419 this.nsInstanceCountSub.unsubscribe();
420 this.vnfInstanceCountSub.unsubscribe();
421 this.vimAccountCountSub.unsubscribe();
422 this.sdnControllerCountSub.unsubscribe();
423 }
424}