Bug 1383 Error when consulting NS instance's topology
[osm/NG-UI.git] / src / app / utilities / clone-package / ClonePackageComponent.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 Clone Package  Model
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 { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { APIURLHEADER, ERRORDATA, GETAPIURLHEADER, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
27 import { environment } from 'environment';
28 import * as jsyaml from 'js-yaml';
29 import { NSDATACREATION, NSDDetails } from 'NSDModel';
30 import { RestService } from 'RestService';
31 import { SharedService } from 'SharedService';
32 import { VNFDATA } from 'VNFDModel';
33
34 /**
35  * Creating component
36  * @Component takes ClonePackageComponent.html as template url
37  */
38
39 @Component({
40   selector: 'app-clone-package',
41   templateUrl: './ClonePackageComponent.html',
42   styleUrls: ['./ClonePackageComponent.scss']
43 })
44 /** Exporting a class @exports ClonePackageComponent */
45 export class ClonePackageComponent implements OnInit {
46   /** To inject services @public */
47   public injector: Injector;
48
49   /** Instance for active modal service @public */
50   public activeModal: NgbActiveModal;
51
52   /** Input contains component objects @public */
53   @Input() public params: URLPARAMS;
54
55   /** To handle loader status for API call @public */
56   public isLoadingResults: boolean = false;
57
58   /** Give the message for the loading @public */
59   public message: string = 'PLEASEWAIT';
60
61   /** Contains all methods related to shared @private */
62   private sharedService: SharedService;
63
64   /** Instance of the rest service @private */
65   private restService: RestService;
66
67   /** Notifier service to popup notification @private */
68   private notifierService: NotifierService;
69
70   /** Contains tranlsate instance @private */
71   private translateService: TranslateService;
72
73   /** Contains cloned package name instance @private */
74   private packageName: string = '';
75
76   /** Contains API end point for package creation @private */
77   private endPoint: string;
78
79   constructor(injector: Injector) {
80     this.injector = injector;
81     this.restService = this.injector.get(RestService);
82     this.sharedService = this.injector.get(SharedService);
83     this.activeModal = this.injector.get(NgbActiveModal);
84     this.notifierService = this.injector.get(NotifierService);
85     this.translateService = this.injector.get(TranslateService);
86   }
87   /**
88    * Lifecyle Hooks the trigger before component is instantiate
89    */
90   public ngOnInit(): void {
91     // Empty Block
92   }
93   /**
94    * Get package information based on type
95    */
96   public clonePackageInfo(): void {
97     let apiUrl: string = '';
98     const httpOptions: GETAPIURLHEADER = this.getHttpoptions();
99     apiUrl = this.params.page === 'nsd' ? apiUrl = environment.NSDESCRIPTORS_URL + '/' + this.params.id + '/nsd' :
100       apiUrl = environment.VNFPACKAGES_URL + '/' + this.params.id + '/vnfd';
101     this.isLoadingResults = true;
102     this.restService.getResource(apiUrl, httpOptions)
103       .subscribe((nsData: NSDDetails[]): void => {
104         this.modifyContent(nsData);
105       }, (error: ERRORDATA): void => {
106         this.isLoadingResults = false;
107         error.error = typeof error.error === 'string' ? jsyaml.load(error.error) : error.error;
108         this.restService.handleError(error, 'get');
109       });
110   }
111   /**
112    * Get HTTP header options
113    */
114   private getHttpoptions(): GETAPIURLHEADER {
115     const apiHeaders: HttpHeaders = new HttpHeaders({
116       'Content-Type': 'application/json',
117       Accept: 'text/plain',
118       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
119     });
120     return {
121       headers: apiHeaders,
122       responseType: 'text'
123     };
124   }
125   /**
126    * Get and modify package information based on type
127    */
128   private modifyContent(packageInfo: NSDDetails[]): void {
129     if (this.params.page === 'nsd') {
130       const nsPackageContent: NSDATACREATION = jsyaml.load(packageInfo.toString());
131       this.packageName = 'clone_' + nsPackageContent.nsd.nsd[0].name;
132       this.endPoint = environment.NSDESCRIPTORSCONTENT_URL;
133       nsPackageContent.nsd.nsd.forEach((nsd: NSDDetails): void => {
134         nsd.id = 'clone_' + nsd.id;
135         nsd.name = 'clone_' + nsd.name;
136       });
137       this.clonePackage(nsPackageContent);
138     } else {
139       const vnfPackageContent: VNFDATA = jsyaml.load(packageInfo.toString());
140       this.packageName = 'clone_' + vnfPackageContent.vnfd['product-name'];
141       this.endPoint = environment.VNFPACKAGESCONTENT_URL;
142       vnfPackageContent.vnfd.id = 'clone_' + vnfPackageContent.vnfd.id;
143       vnfPackageContent.vnfd['product-name'] = 'clone_' + vnfPackageContent.vnfd['product-name'];
144       this.clonePackage(vnfPackageContent);
145     }
146   }
147   /**
148    * Create clone package and upload as TAR.GZ file
149    */
150   private clonePackage(packageContent: NSDATACREATION | VNFDATA): void {
151     const descriptorInfo: string = jsyaml.dump(packageContent);
152     const apiHeader: HttpHeaders = new HttpHeaders({
153       'Content-Type': 'application/gzip',
154       Accept: 'application/json',
155       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
156     });
157     const modalData: MODALCLOSERESPONSEDATA = {
158       message: 'Done'
159     };
160     this.sharedService.targzFile({ packageType: this.params.page, id: this.params.id, descriptor: descriptorInfo })
161       .then((content: ArrayBuffer): void => {
162         const apiURLHeader: APIURLHEADER = {
163           url: this.endPoint,
164           httpOptions: { headers: apiHeader }
165         };
166         // tslint:disable-next-line: completed-docs
167         this.restService.postResource(apiURLHeader, content).subscribe((result: { id: string }): void => {
168           this.activeModal.close(modalData);
169           this.isLoadingResults = false;
170           this.notifierService.notify('success', this.translateService.instant('CLONESUCCESSFULLY'));
171         }, (error: ERRORDATA): void => {
172           this.isLoadingResults = false;
173           this.restService.handleError(error, 'post');
174         });
175       }).catch((): void => {
176         this.isLoadingResults = false;
177         this.notifierService.notify('error', this.translateService.instant('ERROR'));
178       });
179   }
180 }