Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[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   // eslint-disable-next-line @typescript-eslint/no-magic-numbers
90   private timeOut: number = 1000;
91
92   constructor(injector: Injector) {
93     this.injector = injector;
94     this.restService = this.injector.get(RestService);
95     this.sharedService = this.injector.get(SharedService);
96     this.modalService = this.injector.get(NgbModal);
97     this.router = this.injector.get(Router);
98     this.notifierService = this.injector.get(NotifierService);
99     this.translateService = this.injector.get(TranslateService);
100     this.cd = this.injector.get(ChangeDetectorRef);
101   }
102
103   /**
104    * Lifecyle Hooks the trigger before component is instantiate
105    */
106   public ngOnInit(): void {
107     this.headers = new HttpHeaders({
108       Accept: 'application/zip, application/json',
109       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
110     });
111     this.vnfID = this.value.identifier;
112     this.vnfName = this.value.productName;
113   }
114
115   /** Delete VNF packages @public */
116   public deleteVNFPackage(): void {
117     // eslint-disable-next-line security/detect-non-literal-fs-filename
118     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
119     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
120       if (result) {
121         this.sharedService.callData();
122       }
123     }).catch((): void => {
124       // Catch Navigation Error
125   });
126   }
127
128   /** Set instance for NSD Edit @public */
129   public vnfdEdit(): void {
130     this.router.navigate(['/packages/vnf/edit/', this.vnfID]).then((nav: {}) => {
131       // Navigated Successfully
132     }, (error: Error) => {
133       // Navigation Error Handler
134     });
135   }
136
137   /** list out all the file content of a descriptors @public */
138   public showContent(): void {
139     // eslint-disable-next-line security/detect-non-literal-fs-filename
140     this.modalService.open(ShowContentComponent, { backdrop: 'static' }).componentInstance.params = { id: this.vnfID, page: 'vnfd' };
141   }
142
143   /** Download VNF Package @public */
144   public downloadVNFPackage(): void {
145     this.isLoadingDownloadResult = true;
146     const httpOptions: GETAPIURLHEADER = {
147       headers: this.headers,
148       responseType: 'blob'
149     };
150     this.restService.getResource(environment.VNFPACKAGES_URL + '/' + this.vnfID + '/package_content', httpOptions)
151       .subscribe((response: Blob) => {
152         this.isLoadingDownloadResult = false;
153         this.changeDetactionforDownload();
154         const binaryData: Blob[] = [];
155         binaryData.push(response);
156         this.sharedService.downloadFiles(this.vnfName, binaryData, response.type);
157       }, (error: ERRORDATA) => {
158         this.isLoadingDownloadResult = false;
159         this.notifierService.notify('error', this.translateService.instant('ERROR'));
160         this.changeDetactionforDownload();
161         if (typeof error.error === 'object') {
162           error.error.text().then((data: string): void => {
163             error.error = JSON.parse(data);
164             this.restService.handleError(error, 'getBlob');
165           });
166         }
167       });
168   }
169
170   /** Compose VNF Packages @public */
171   public composeVNFPackages(): void {
172     this.router.navigate(['/packages/vnf/compose/', this.vnfID]).catch((): void => {
173       // Catch Navigation Error
174   });
175   }
176
177   /** Change the detaction @public */
178   public changeDetactionforDownload(): void {
179     setTimeout(() => {
180       this.cd.detectChanges();
181     }, this.timeOut);
182   }
183
184   /** Clone NS Packages @public */
185   public cloneVNFPackage(): void {
186     // eslint-disable-next-line security/detect-non-literal-fs-filename
187     const cloneModal: NgbModalRef = this.modalService.open(ClonePackageComponent, { backdrop: 'static' });
188     cloneModal.componentInstance.params = { id: this.vnfID, page: 'vnfd', name: this.vnfName };
189     cloneModal.result.then((result: MODALCLOSERESPONSEDATA) => {
190       if (result) {
191         this.sharedService.callData();
192       }
193     }).catch((): void => {
194       // Catch Navigation Error
195   });
196   }
197 }