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 @private */
110 public nsRunningInstance: string[] = [];
112 /** Utilizes rest service for any CRUD operations @private */
113 private restService: RestService;
115 /** Utilizes auth service for any auth operations @private */
116 private authService: AuthenticationService;
118 /** Used to subscribe vnfdPackage @private */
119 private vnfdPackageCountSub: Subscription;
121 /** Used to subscribe nsdPackage @private */
122 private nsdPackageCountSub: Subscription;
124 /** Used to subscribe nsInstance @private */
125 private nsInstanceCountSub: Subscription;
127 /** Used to subscribe vnfInstance @private */
128 private vnfInstanceCountSub: Subscription;
130 /** Used to subscribe vimAccount @private */
131 private vimAccountCountSub: Subscription;
133 /** Used to subscribe sdnController @private */
134 private sdnControllerCountSub: Subscription;
136 /** No of Hours of NS Success Instances @private */
137 private noOfHours: number[] = [];
139 /** collects charts objects @private */
140 private charts: object = [];
142 /** Contains all methods related to projects @private */
143 private projectService: ProjectService;
145 /** Contains all methods related to shared @private */
146 private sharedService: SharedService;
148 /** Contains NS Instance Details */
149 private nsInstancesDataArr: {}[];
151 /** Container created time array @private */
152 private createdTimes: string[] = [];
154 /** Contains slice limit const @private */
155 private sliceLimit: number = 10;
157 /** Contians hour converter @private */
158 private hourConverter: number = 3600;
160 /** Contians color code for chart @private */
161 private chartColorPink: string = '#e4397c';
163 /** Contians color code for chart @private */
164 private chartColorPurple: string = '#605ca8';
166 /** Contians color code for chart @private */
167 private chartColorCyan: string = '#00c0ef';
169 /** Contians color code for chart @private */
170 private chartColorBlue: string = '#054C8C';
172 /** Contians color code for chart @private */
173 private chartColorYellow: string = '#ffce56';
175 constructor(injector: Injector) {
176 this.injector = injector;
177 this.restService = this.injector.get(RestService);
178 this.authService = this.injector.get(AuthenticationService);
179 this.projectService = this.injector.get(ProjectService);
180 this.sharedService = this.injector.get(SharedService);
181 this.translateService = this.injector.get(TranslateService);
185 * Lifecyle Hooks the trigger before component is instantiate
187 public ngOnInit(): void {
188 this.username$ = this.authService.username;
189 this.isAdmin = (localStorage.getItem('isAdmin') === 'true') ? true : false;
190 this.selectedProject = this.authService.ProjectName;
191 this.checkAdminPrivilege();
192 this.getUserAccessedProjects();
193 this.getAllProjects();
194 this.getVnfdPackageCount();
195 this.getNsdPackageCount();
196 this.getNsInstanceCount();
197 this.getVnfInstanceCount();
198 this.getVimAccountCount();
199 this.getSDNControllerCount();
202 /** Get all the projects @public */
203 public getUserAccessedProjects(): void {
204 this.projectService.getUserProjects().subscribe((projects: UserDetail) => {
205 const projectList: {}[] = projects.project_role_mappings;
206 this.projectList = projectList.filter(
207 (thing: ProjectRoleMappings, i: number, arr: []) => arr
208 .findIndex((t: ProjectRoleMappings) => t.project_name === thing.project_name) === i
210 }, (error: Error) => {
211 // TODO: Handle failure
215 /** Fetching all the Project in dashboard @public */
216 public getAllProjects(): void {
217 this.isProjectsLoadingResults = true;
218 this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectDetails[]) => {
219 this.allProjectList = [];
220 projectsData.forEach((projectData: ProjectDetails) => {
221 const projectDataObj: ProjectData = this.generateProjectData(projectData);
222 this.allProjectList.push(projectDataObj);
224 this.isProjectsLoadingResults = false;
225 }, (error: ERRORDATA) => {
226 this.restService.handleError(error, 'get');
227 this.isProjectsLoadingResults = false;
231 /** Generate Projects object from loop and return for the datasource @public */
232 public generateProjectData(projectData: ProjectDetails): ProjectData {
234 projectName: projectData.name,
235 modificationDate: this.sharedService.convertEpochTime(projectData._admin.modified),
236 creationDate: this.sharedService.convertEpochTime(projectData._admin.created),
238 project: projectData._id
242 /** Function to check admin privilege @public */
243 public checkAdminPrivilege(): void {
245 this.projectService.getCurrentProjectDetails().subscribe((projectDetails: {}) => {
246 this.currentProjectDetails = projectDetails;
247 }, (error: Error) => {
248 // TODO: Handle failure
253 /** Get VNFD Package details @public */
254 public getVnfdPackageCount(): void {
255 this.vnfdPackageCountSub = this.restService.getResource(environment.VNFPACKAGESCONTENT_URL)
256 .subscribe((vnfdPackageData: VNFDDetails[]) => {
257 this.vnfdPackageCount = vnfdPackageData.length;
258 }, (error: ERRORDATA) => {
259 this.restService.handleError(error, 'get');
263 /** Get NSD Package details @public */
264 public getNsdPackageCount(): void {
265 this.nsdPackageCountSub = this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL)
266 .subscribe((nsdPackageData: NSDDetails[]) => {
267 this.nsdPackageCount = nsdPackageData.length;
268 }, (error: ERRORDATA) => {
269 this.restService.handleError(error, 'get');
273 /** Get NS Instance details @public */
274 public getNsInstanceCount(): void {
275 this.isCanvasLoadingResults = true;
276 this.nsInstanceCountSub = this.restService.getResource(environment.NSDINSTANCES_URL)
277 .subscribe((nsInstancesData: NSInstanceDetails[]) => {
278 this.nsInstancesDataArr = nsInstancesData;
279 this.nsInstanceCount = nsInstancesData.length;
280 this.nsInstanceChart();
281 this.isCanvasLoadingResults = false;
282 }, (error: ERRORDATA) => {
283 this.restService.handleError(error, 'get');
284 this.isCanvasLoadingResults = false;
288 /** Get NS Instance chart details @public */
289 public nsInstanceChart(): void {
290 this.nsInstancesDataArr.forEach((nsdInstanceData: NSDDetails) => {
291 const operationalStatus: string = nsdInstanceData['operational-status'];
292 const configStatus: string = nsdInstanceData['config-status'];
293 if (operationalStatus === 'failed' || configStatus === 'failed') {
294 this.nsFailedInstances.push(nsdInstanceData);
295 } else if (operationalStatus === 'running' && configStatus === 'configured') {
296 this.nsRunningInstance.push(nsdInstanceData.name);
297 this.createdTimes.push(((nsdInstanceData._admin.created).toString()).slice(0, this.sliceLimit));
300 const now: Date = new Date();
301 const currentTime: number = Number((now.getTime().toString().slice(0, this.sliceLimit)));
302 this.createdTimes.forEach((createdTime: string) => {
303 this.noOfHours.push((Math.round((currentTime - Number(createdTime)) / this.hourConverter)));
308 /** Prepare and sketch NS instance chart */
309 public drawNsChart(): void {
310 this.charts = new Chart('canvas', {
313 labels: this.nsRunningInstance,
315 data: this.noOfHours,
316 label: this.translateService.instant('NOOFHOURS'),
317 borderColor: [this.chartColorPurple, this.chartColorPink, this.chartColorCyan,
318 this.chartColorBlue, this.chartColorYellow],
320 backgroundColor: [this.chartColorPurple, this.chartColorPink, this.chartColorCyan,
321 this.chartColorBlue, this.chartColorYellow]
325 legend: { display: false },
330 // tslint:disable-next-line: no-any
331 callback: (label: any, index: number, labels: string): string => {
332 const length: number = 20;
333 const ending: string = '...';
334 if (label.length > length) {
335 return label.substring(0, length - ending.length) + ending;
343 labelString: this.translateService.instant('INSTANCES')
353 labelString: this.translateService.instant('NOOFHOURS')
361 /** Get VNFD instance details @public */
362 public getVnfInstanceCount(): void {
363 this.vnfInstanceCountSub = this.restService.getResource(environment.NSDINSTANCES_URL)
364 .subscribe((vnfInstanceData: VNFInstanceDetails[]) => {
365 this.vnfInstanceCount = vnfInstanceData.length;
366 }, (error: ERRORDATA) => {
367 this.restService.handleError(error, 'get');
371 /** Get VIM account details @public */
372 public getVimAccountCount(): void {
373 this.vimAccountCountSub = this.restService.getResource(environment.VIMACCOUNTS_URL)
374 .subscribe((vimAccountData: VimAccountDetails[]) => {
375 this.vimAccountCount = vimAccountData.length;
376 }, (error: ERRORDATA) => {
377 this.restService.handleError(error, 'get');
381 /** Get SDN Controller Count @public */
382 public getSDNControllerCount(): void {
383 this.sdnControllerCountSub = this.restService.getResource(environment.SDNCONTROLLER_URL)
384 .subscribe((sdnControllerData: SDNControllerModel[]) => {
385 this.sdnControllerCount = sdnControllerData.length;
386 }, (error: ERRORDATA) => {
387 this.restService.handleError(error, 'get');
392 * Lifecyle Hooks the trigger before component is deleted
394 public ngOnDestroy(): void {
395 this.vnfdPackageCountSub.unsubscribe();
396 this.nsdPackageCountSub.unsubscribe();
397 this.nsInstanceCountSub.unsubscribe();
398 this.vnfInstanceCountSub.unsubscribe();
399 this.vimAccountCountSub.unsubscribe();
400 this.sdnControllerCountSub.unsubscribe();