The NS/VNF package content will shown
[osm/NG-UI.git] / src / app / packages / show-content / ShowContentComponent.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 Show content modal
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input, OnInit } from '@angular/core';
23 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24 import { ERRORDATA, GETAPIURLHEADER, URLPARAMS } from 'CommonModel';
25 import { environment } from 'environment';
26 import * as jsyaml from 'js-yaml';
27 import { RestService } from 'RestService';
28
29 /** Shows json data in the information modal */
30 const defaults: {} = { 'text/json': '' };
31
32 /**
33  * Creating component
34  * @Component takes ShowContentComponent.html as template url
35  */
36 @Component({
37   selector: 'app-show-content',
38   templateUrl: './ShowContentComponent.html',
39   styleUrls: ['./ShowContentComponent.scss']
40 })
41 /** Exporting a class @exports ShowContentComponent */
42 export class ShowContentComponent implements OnInit {
43   /** To inject services @public */
44   public injector: Injector;
45
46   /** Check the loading results @public */
47   public isLoadingResults: boolean = false;
48
49   /** Instance for active modal service @public */
50   public activeModal: NgbActiveModal;
51
52   /** Contains files information @public */
53   public contents: {}[];
54
55   /** Input contains component objects @public */
56   @Input() public params: URLPARAMS;
57
58   /** Instance of the rest service @private */
59   private restService: RestService;
60
61   /** Controls the header form @private */
62   private headers: HttpHeaders;
63
64   constructor(injector: Injector) {
65     this.injector = injector;
66     this.restService = this.injector.get(RestService);
67     this.activeModal = this.injector.get(NgbActiveModal);
68   }
69
70   /**
71    * Lifecyle Hooks the trigger before component is instantiate @public
72    */
73   public ngOnInit(): void {
74     this.headers = new HttpHeaders({
75       'Content-Type': 'application/json',
76       Accept: 'text/plain',
77       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
78     });
79     if (this.params.page === 'nsd') {
80       this.getContent(environment.NSDESCRIPTORS_URL + '/' + this.params.id + '/artifacts');
81     } else if (this.params.page === 'vnfd') {
82       this.getContent(environment.VNFPACKAGES_URL + '/' + this.params.id + '/artifacts');
83     }
84   }
85   /** Get the NSD Content List @public */
86   public getContent(URL: string): void {
87     this.isLoadingResults = true;
88     const httpOptions: GETAPIURLHEADER = {
89       headers: this.headers,
90       responseType: 'text'
91     };
92     this.restService.getResource(URL, httpOptions).subscribe((content: {}[]): void => {
93       const getJson: string[] = jsyaml.load(content.toString(), { json: true });
94       this.contents = getJson;
95       this.isLoadingResults = false;
96     }, (error: ERRORDATA): void => {
97       this.restService.handleError(error, 'get');
98       this.isLoadingResults = false;
99     });
100   }
101 }