2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Clone Package Model
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';
36 * @Component takes ClonePackageComponent.html as template url
40 selector: 'app-clone-package',
41 templateUrl: './ClonePackageComponent.html',
42 styleUrls: ['./ClonePackageComponent.scss']
44 /** Exporting a class @exports ClonePackageComponent */
45 export class ClonePackageComponent implements OnInit {
46 /** To inject services @public */
47 public injector: Injector;
49 /** Instance for active modal service @public */
50 public activeModal: NgbActiveModal;
52 /** Input contains component objects @public */
53 @Input() public params: URLPARAMS;
55 /** To handle loader status for API call @public */
56 public isLoadingResults: boolean = false;
58 /** Give the message for the loading @public */
59 public message: string = 'PLEASEWAIT';
61 /** Contains all methods related to shared @private */
62 private sharedService: SharedService;
64 /** Instance of the rest service @private */
65 private restService: RestService;
67 /** Notifier service to popup notification @private */
68 private notifierService: NotifierService;
70 /** Contains tranlsate instance @private */
71 private translateService: TranslateService;
73 /** Contains cloned package name instance @private */
74 private packageName: string = '';
76 /** Contains API end point for package creation @private */
77 private endPoint: string;
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);
88 * Lifecyle Hooks the trigger before component is instantiate
90 public ngOnInit(): void {
94 * Get package information based on type
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');
112 * Get HTTP header options
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'
126 * Get and modify package information based on type
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;
137 this.clonePackage(nsPackageContent);
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);
148 * Create clone package and upload as TAR.GZ file
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'
157 const modalData: MODALCLOSERESPONSEDATA = {
160 this.sharedService.targzFile({ packageType: this.params.page, id: this.params.id, descriptor: descriptorInfo })
161 .then((content: ArrayBuffer): void => {
162 const apiURLHeader: APIURLHEADER = {
164 httpOptions: { headers: apiHeader }
166 this.restService.postResource(apiURLHeader, content).subscribe((result: { id: string }): void => {
167 this.activeModal.close(modalData);
168 this.isLoadingResults = false;
169 this.notifierService.notify('success', this.translateService.instant('CLONESUCCESSFULLY'));
170 }, (error: ERRORDATA): void => {
171 this.isLoadingResults = false;
172 this.restService.handleError(error, 'post');
174 }).catch((): void => {
175 this.isLoadingResults = false;
176 this.notifierService.notify('error', this.translateService.instant('ERROR'));