Bug 1383 Error when consulting NS instance's topology
[osm/NG-UI.git] / src / app / utilities / vnf-packages-action / VNFPackagesActionComponent.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 VNF-packagesAction Component
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { ChangeDetectorRef, Component, Injector } from '@angular/core';
23 import { Router } from '@angular/router';
24 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { ClonePackageComponent } from 'ClonePackage';
28 import { ERRORDATA, GETAPIURLHEADER, MODALCLOSERESPONSEDATA } from 'CommonModel';
29 import { DeleteComponent } from 'DeleteComponent';
30 import { environment } from 'environment';
31 import { RestService } from 'RestService';
32 import { SharedService } from 'SharedService';
33 import { ShowContentComponent } from 'ShowContent';
34 import { VNFData } from 'VNFDModel';
35
36 /**
37  * Creating component
38  * @Component takes VNFPackagesActionComponent.html as template url
39  */
40 @Component({
41   templateUrl: './VNFPackagesActionComponent.html',
42   styleUrls: ['./VNFPackagesActionComponent.scss']
43 })
44 /** Exporting a class @exports VNFPackagesActionComponent */
45 export class VNFPackagesActionComponent {
46   /** To get the value from the vnfpackage via valuePrepareFunction default Property of ng-smarttable @public */
47   public value: VNFData;
48
49   /** To inject services @public */
50   public injector: Injector;
51
52   /** Check the loading results for loader status @public */
53   public isLoadingDownloadResult: boolean = false;
54
55   /** Give the message for the loading @public */
56   public message: string = 'PLEASEWAIT';
57
58   /** Instance of the rest service @private */
59   private restService: RestService;
60
61   /** Holds teh instance of AuthService class of type AuthService @private */
62   private router: Router;
63
64   /** Instance of the modal service @private */
65   private modalService: NgbModal;
66
67   /** Variables holds NS ID @private */
68   private vnfID: string;
69
70   /** Variables holds NS name @private */
71   private vnfName: string;
72
73   /** Controls the header form @private */
74   private headers: HttpHeaders;
75
76   /** Contains all methods related to shared @private */
77   private sharedService: SharedService;
78
79   /** Notifier service to popup notification @private */
80   private notifierService: NotifierService;
81
82   /** Contains tranlsate instance @private */
83   private translateService: TranslateService;
84
85   /** Detect changes for the User Input */
86   private cd: ChangeDetectorRef;
87
88   /** Set timeout @private */
89   private timeOut: number = 1000;
90
91   constructor(injector: Injector) {
92     this.injector = injector;
93     this.restService = this.injector.get(RestService);
94     this.sharedService = this.injector.get(SharedService);
95     this.modalService = this.injector.get(NgbModal);
96     this.router = this.injector.get(Router);
97     this.notifierService = this.injector.get(NotifierService);
98     this.translateService = this.injector.get(TranslateService);
99     this.cd = this.injector.get(ChangeDetectorRef);
100   }
101
102   /**
103    * Lifecyle Hooks the trigger before component is instantiate
104    */
105   public ngOnInit(): void {
106     this.headers = new HttpHeaders({
107       Accept: 'application/zip, application/json',
108       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
109     });
110     this.vnfID = this.value.identifier;
111     this.vnfName = this.value.productName;
112   }
113
114   /** Delete VNF packages @public */
115   public deleteVNFPackage(): void {
116     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
117     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
118       if (result) {
119         this.sharedService.callData();
120       }
121     }).catch();
122   }
123
124   /** Set instance for NSD Edit @public */
125   public vnfdEdit(): void {
126     this.router.navigate(['/packages/vnf/edit/', this.vnfID]).then((nav: {}) => {
127       // Navigated Successfully
128     }, (error: Error) => {
129       // Navigation Error Handler
130     });
131   }
132
133   /** list out all the file content of a descriptors @public */
134   public showContent(): void {
135     this.modalService.open(ShowContentComponent, { backdrop: 'static' }).componentInstance.params = { id: this.vnfID, page: 'vnfd' };
136   }
137
138   /** Download VNF Package @public */
139   public downloadVNFPackage(): void {
140     this.isLoadingDownloadResult = true;
141     const httpOptions: GETAPIURLHEADER = {
142       headers: this.headers,
143       responseType: 'blob'
144     };
145     this.restService.getResource(environment.VNFPACKAGES_URL + '/' + this.vnfID + '/package_content', httpOptions)
146       .subscribe((response: Blob) => {
147         this.isLoadingDownloadResult = false;
148         this.changeDetactionforDownload();
149         const binaryData: Blob[] = [];
150         binaryData.push(response);
151         this.sharedService.downloadFiles(this.vnfName, binaryData, response.type);
152       }, (error: ERRORDATA) => {
153         this.isLoadingDownloadResult = false;
154         this.notifierService.notify('error', this.translateService.instant('ERROR'));
155         this.changeDetactionforDownload();
156         if (typeof error.error === 'object') {
157           error.error.text().then((data: string): void => {
158             error.error = JSON.parse(data);
159             this.restService.handleError(error, 'getBlob');
160           });
161         }
162       });
163   }
164
165   /** Compose VNF Packages @public */
166   public composeVNFPackages(): void {
167     this.router.navigate(['/packages/vnf/compose/', this.vnfID]).catch();
168   }
169
170   /** Change the detaction @public */
171   public changeDetactionforDownload(): void {
172     setTimeout(() => {
173       this.cd.detectChanges();
174     }, this.timeOut);
175   }
176
177   /** Clone NS Packages @public */
178   public cloneVNFPackage(): void {
179     const cloneModal: NgbModalRef = this.modalService.open(ClonePackageComponent, { backdrop: 'static' });
180     cloneModal.componentInstance.params = { id: this.vnfID, page: 'vnfd', name: this.vnfName };
181     cloneModal.result.then((result: MODALCLOSERESPONSEDATA) => {
182       if (result) {
183         this.sharedService.callData();
184       }
185     }).catch();
186   }
187 }