New VIM Design with the config implemented.
[osm/NG-UI.git] / src / app / vim-accounts / info-vim / InfoVimComponent.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 Info VIM Page
20  */
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { ActivatedRoute, Router } from '@angular/router';
23 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
24 import { ERRORDATA } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import * as HttpStatus from 'http-status-codes';
28 import { RestService } from 'RestService';
29 import { isNullOrUndefined } from 'util';
30 import { CONFIG, VimAccountDetails, VIMData } from 'VimAccountModel';
31
32 /**
33  * Creating component
34  * @Component InfoVimComponent.html as template url
35  */
36 @Component({
37   selector: 'app-info-vim',
38   templateUrl: './InfoVimComponent.html',
39   styleUrls: ['./InfoVimComponent.scss']
40 })
41 /** Exporting a class @exports InfoVimComponent */
42 export class InfoVimComponent implements OnInit {
43   /** To inject services @public */
44   public injector: Injector;
45
46   /** vimAccountDetails to populate in InfoVIM Page @private */
47   public vimAccountDetails: VimAccountDetails;
48
49   /** Information Top Left @public */
50   public configParams: {} = {};
51
52   /** Showing more details of collapase */
53   public isCollapsed: boolean = true;
54
55   /** Contains vim details @public */
56   public vimDetails: {}[];
57
58   /** Check the Projects loading results @public */
59   public isLoadingResults: boolean = false;
60
61   /** Give the message for the loading @public */
62   public message: string = 'PLEASEWAIT';
63
64   /** variables contains paramsID @private */
65   private paramsID: string;
66
67   /** Instance of the rest service @private */
68   private restService: RestService;
69
70   /** Holds the instance of router class @private */
71   private router: Router;
72
73   /** dataService to pass the data from one component to another @private */
74   private dataService: DataService;
75
76   /** vimId to populate in InfoVIM Page @private */
77   private vimId: string;
78
79   /** Holds teh instance of AuthService class of type AuthService @private */
80   private activatedRoute: ActivatedRoute;
81
82   /** Utilizes modal service for any modal operations @private */
83   private modalService: NgbModal;
84
85   constructor(injector: Injector) {
86     this.injector = injector;
87     this.restService = this.injector.get(RestService);
88     this.dataService = this.injector.get(DataService);
89     this.activatedRoute = this.injector.get(ActivatedRoute);
90     this.modalService = this.injector.get(NgbModal);
91     this.router = this.injector.get(Router);
92   }
93
94   /**
95    * Lifecyle Hooks the trigger before component is instantiate
96    */
97   public ngOnInit(): void {
98     // tslint:disable-next-line:no-backbone-get-set-outside-model
99     this.paramsID = this.activatedRoute.snapshot.paramMap.get('id');
100     this.dataService.currentMessage.subscribe((data: VIMData) => {
101       this.vimId = data.identifier;
102     });
103     this.generateData();
104   }
105
106   /** Routing to VIM Account Details Page @public */
107   public onVimAccountBack(): void {
108     this.router.navigate(['vim/details']).catch(() => {
109       // Error Cached
110     });
111   }
112
113   /** Generate Data function @public */
114   public generateData(): void {
115     this.isLoadingResults = true;
116     this.restService.getResource(environment.VIMACCOUNTS_URL + '/' + this.paramsID)
117       .subscribe((vimAccountsData: VimAccountDetails) => {
118         this.showDetails(vimAccountsData);
119         if (vimAccountsData.config.location !== undefined) {
120           const locationArr: string[] = vimAccountsData.config.location.split(',');
121           if (Array.isArray(locationArr)) {
122             vimAccountsData.config.location = locationArr[0];
123           }
124         }
125         Object.keys(vimAccountsData.config).forEach((key: string) => {
126           if (Array.isArray(vimAccountsData.config[key]) || typeof vimAccountsData.config[key] === 'object') {
127             vimAccountsData.config[key] = JSON.stringify(vimAccountsData.config[key]);
128           }
129           const keyArr: string[] = key.split('_');
130           if (keyArr.length > 1 ) {
131             vimAccountsData.config[key.split('_').join(' ')] = vimAccountsData.config[key];
132             delete vimAccountsData.config[key];
133           }
134         });
135         this.configParams = vimAccountsData.config;
136         this.isLoadingResults = false;
137       }, (error: ERRORDATA) => {
138         this.isLoadingResults = false;
139         if (error.error.status === HttpStatus.NOT_FOUND || error.error.status === HttpStatus.UNAUTHORIZED) {
140           this.router.navigateByUrl('404', { skipLocationChange: true }).catch();
141         } else {
142           this.restService.handleError(error, 'get');
143         }
144       });
145   }
146
147   /** show general vim detailed information @public */
148   public showDetails(vimAccountsData: VimAccountDetails): void {
149     this.vimDetails = [
150       {
151         title: 'PAGE.VIMDETAILS.NAME',
152         value: vimAccountsData.name
153       },
154       {
155         title: 'PAGE.VIMDETAILS.VIMUSERNAME',
156         value: vimAccountsData.vim_user
157       },
158       {
159         title: 'PAGE.VIMDETAILS.VIMURL',
160         value: vimAccountsData.vim_url
161       },
162       {
163         title: 'PAGE.VIMDETAILS.VIMTYPE',
164         value: vimAccountsData.vim_type
165       },
166       {
167         title: 'PAGE.VIMDETAILS.TENANTNAME',
168         value: vimAccountsData.vim_tenant_name
169       },
170       {
171         title: 'PAGE.VIMDETAILS.DESCRIPTION',
172         value: vimAccountsData.description
173       },
174       {
175         title: 'PAGE.VIMDETAILS.SCHEMATYPE',
176         value: vimAccountsData.schema_type
177       },
178       {
179         title: 'PAGE.VIMDETAILS.SCHEMAVERSION',
180         value: vimAccountsData.schema_version
181       }
182     ];
183   }
184 }