Bug 1383 Error when consulting NS instance's topology
[osm/NG-UI.git] / src / app / utilities / ns-packages-action / NsPackagesActionComponent.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 NS 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 { InstantiateNsComponent } from 'InstantiateNs';
32 import { NSData } from 'NSDModel';
33 import { RestService } from 'RestService';
34 import { SharedService } from 'SharedService';
35 import { ShowContentComponent } from 'ShowContent';
36
37 /**
38  * Creating component
39  * @Component takes NsPackagesActionComponent.html as template url
40  */
41 @Component({
42   selector: 'app-ns-packages-action',
43   templateUrl: './NsPackagesActionComponent.html',
44   styleUrls: ['./NsPackagesActionComponent.scss']
45 })
46
47 /** Exporting a class @exports NsPackagesActionComponent */
48 export class NsPackagesActionComponent {
49   /** To get the value from the nspackage via valuePrepareFunction default Property of ng-smarttable @public */
50   public value: NSData;
51
52   /** To inject services @public */
53   public injector: Injector;
54
55   /** Check the loading results for loader status @public */
56   public isLoadingDownloadResult: boolean = false;
57
58   /** Give the message for the loading @public */
59   public message: string = 'PLEASEWAIT';
60
61   /** Instance of the rest service @private */
62   private restService: RestService;
63
64   /** Holds teh instance of AuthService class of type AuthService @private */
65   private router: Router;
66
67   /** Instance of the modal service @private */
68   private modalService: NgbModal;
69
70   /** Variables holds NS ID @private */
71   private nsdID: string;
72
73   /** Variables holds NS name @private */
74   private nsdName: string;
75
76   /** Controls the header form @private */
77   private headers: HttpHeaders;
78
79   /** Contains all methods related to shared @private */
80   private sharedService: SharedService;
81
82   /** Notifier service to popup notification @private */
83   private notifierService: NotifierService;
84
85   /** Contains tranlsate instance @private */
86   private translateService: TranslateService;
87
88   /** Detect changes for the User Input */
89   private cd: ChangeDetectorRef;
90
91   /** Set timeout @private */
92   private timeOut: number = 1000;
93
94   constructor(injector: Injector) {
95     this.injector = injector;
96     this.restService = this.injector.get(RestService);
97     this.sharedService = this.injector.get(SharedService);
98     this.modalService = this.injector.get(NgbModal);
99     this.router = this.injector.get(Router);
100     this.notifierService = this.injector.get(NotifierService);
101     this.translateService = this.injector.get(TranslateService);
102     this.cd = this.injector.get(ChangeDetectorRef);
103   }
104
105   /** Lifecyle Hooks the trigger before component is instantiate @public */
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.nsdID = this.value.identifier;
112     this.nsdName = this.value.name;
113   }
114
115   /** Instantiate NS using modalservice @public */
116   public instantiateNS(): void {
117     this.modalService.open(InstantiateNsComponent, { backdrop: 'static' });
118   }
119
120   /** Delete NS Package @public */
121   public deleteNSPackage(): void {
122     const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
123     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
124       if (result) {
125         this.sharedService.callData();
126       }
127     }).catch();
128   }
129
130   /** Set instance for NSD Edit @public */
131   public nsdEdit(): void {
132     this.router.navigate(['/packages/ns/edit/', this.nsdID]).catch(() => {
133       // Catch Navigation Error
134     });
135   }
136
137   /** list out all the file content of a descriptors @public */
138   public showContent(): void {
139     this.modalService.open(ShowContentComponent, { backdrop: 'static' }).componentInstance.params = { id: this.nsdID, page: 'nsd' };
140   }
141
142   /** Download NS Package @public */
143   public downloadNSPackage(): void {
144     this.isLoadingDownloadResult = true;
145     const httpOptions: GETAPIURLHEADER = {
146       headers: this.headers,
147       responseType: 'blob'
148     };
149     this.restService.getResource(environment.NSDESCRIPTORS_URL + '/' + this.nsdID + '/nsd_content', httpOptions)
150       .subscribe((response: Blob) => {
151         const binaryData: Blob[] = [];
152         binaryData.push(response);
153         this.sharedService.downloadFiles(this.nsdName, binaryData, response.type);
154         this.isLoadingDownloadResult = false;
155         this.changeDetactionforDownload();
156       }, (error: ERRORDATA) => {
157         this.isLoadingDownloadResult = false;
158         this.notifierService.notify('error', this.translateService.instant('ERROR'));
159         this.changeDetactionforDownload();
160         if (typeof error.error === 'object') {
161           error.error.text().then((data: string): void => {
162             error.error = JSON.parse(data);
163             this.restService.handleError(error, 'getBlob');
164           });
165         }
166       });
167   }
168
169   /** Compose NS Packages @public */
170   public composeNSPackages(): void {
171     this.router.navigate(['/packages/ns/compose/', this.nsdID]).catch(() => {
172       // Catch Navigation Error
173     });
174   }
175
176   /** Change the detaction @public */
177    public changeDetactionforDownload(): void {
178     setTimeout(() => {
179       this.cd.detectChanges();
180     }, this.timeOut);
181   }
182
183   /** Clone NS Packages @public */
184   public cloneNSPackage(): void {
185     const cloneModal: NgbModalRef = this.modalService.open(ClonePackageComponent, { backdrop: 'static' });
186     cloneModal.componentInstance.params = { id: this.nsdID, page: 'nsd', name: this.nsdName };
187     cloneModal.result.then((result: MODALCLOSERESPONSEDATA) => {
188       if (result) {
189         this.sharedService.callData();
190       }
191     }).catch();
192   }
193 }