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 { NSDDetails } from 'NSDModel';
30 import { RestService } from 'RestService';
31 import { SharedService } from 'SharedService';
35 * @Component takes ClonePackageComponent.html as template url
39 selector: 'app-clone-package',
40 templateUrl: './ClonePackageComponent.html',
41 styleUrls: ['./ClonePackageComponent.scss']
43 /** Exporting a class @exports ClonePackageComponent */
44 export class ClonePackageComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
48 /** Instance for active modal service @public */
49 public activeModal: NgbActiveModal;
51 /** Input contains component objects @public */
52 @Input() public params: URLPARAMS;
54 /** To handle loader status for API call @public */
55 public isLoadingResults: boolean = false;
57 /** Give the message for the loading @public */
58 public message: string = 'PLEASEWAIT';
60 /** Contains all methods related to shared @private */
61 private sharedService: SharedService;
63 /** Instance of the rest service @private */
64 private restService: RestService;
66 /** Notifier service to popup notification @private */
67 private notifierService: NotifierService;
69 /** Contains tranlsate instance @private */
70 private translateService: TranslateService;
72 /** Contains cloned package name instance @private */
73 private packageName: string = '';
75 /** Contains API end point for package creation @private */
76 private endPoint: string;
78 constructor(injector: Injector) {
79 this.injector = injector;
80 this.restService = this.injector.get(RestService);
81 this.sharedService = this.injector.get(SharedService);
82 this.activeModal = this.injector.get(NgbActiveModal);
83 this.notifierService = this.injector.get(NotifierService);
84 this.translateService = this.injector.get(TranslateService);
87 * Lifecyle Hooks the trigger before component is instantiate
89 public ngOnInit(): void {
93 * Get package information based on type
95 public clonePackageInfo(): void {
96 let apiUrl: string = '';
97 const httpOptions: GETAPIURLHEADER = this.getHttpoptions();
98 apiUrl = this.params.page === 'nsd' ? apiUrl = environment.NSDESCRIPTORS_URL + '/' + this.params.id + '/nsd' :
99 apiUrl = environment.VNFPACKAGES_URL + '/' + this.params.id + '/vnfd';
100 this.isLoadingResults = true;
101 this.restService.getResource(apiUrl, httpOptions)
102 .subscribe((nsData: NSDDetails[]) => {
103 this.modifyContent(nsData);
104 }, (error: ERRORDATA) => {
105 this.isLoadingResults = false;
106 error.error = typeof error.error === 'string' ? jsyaml.load(error.error) : error.error;
107 this.restService.handleError(error, 'get');
111 * Get HTTP header options
113 private getHttpoptions(): GETAPIURLHEADER {
114 const apiHeaders: HttpHeaders = new HttpHeaders({
115 'Content-Type': 'application/json',
116 Accept: 'text/plain',
117 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
125 * Get and modify package information based on type
127 private modifyContent(packageInfo: NSDDetails[]): void {
128 const packageContent: string = jsyaml.load(packageInfo.toString());
129 if (this.params.page === 'nsd') {
130 this.packageName = 'clone_' + packageContent['nsd:nsd-catalog'].nsd[0].name;
131 this.endPoint = environment.NSDESCRIPTORSCONTENT_URL;
132 packageContent['nsd:nsd-catalog'].nsd.forEach((nsd: NSDDetails) => {
133 nsd.id = 'clone_' + nsd.id;
134 nsd.name = 'clone_' + nsd.name;
135 nsd['short-name'] = 'clone_' + nsd['short-name'];
138 this.packageName = 'clone_' + packageContent['vnfd:vnfd-catalog'].vnfd[0].name;
139 this.endPoint = environment.VNFPACKAGESCONTENT_URL;
140 packageContent['vnfd:vnfd-catalog'].vnfd.forEach((vnfd: NSDDetails) => {
141 vnfd.id = 'clone_' + vnfd.id;
142 vnfd.name = 'clone_' + vnfd.name;
143 vnfd['short-name'] = 'clone_' + vnfd['short-name'];
146 this.clonePackage(packageContent);
149 * Create clone package and upload as TAR.GZ file
151 private clonePackage(packageContent: string): void {
152 const descriptorInfo: string = jsyaml.dump(packageContent);
153 const apiHeader: HttpHeaders = new HttpHeaders({
154 'Content-Type': 'application/gzip',
155 Accept: 'application/json',
156 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
158 const modalData: MODALCLOSERESPONSEDATA = {
161 this.sharedService.targzFile({ packageType: this.params.page, id: this.params.id, descriptor: descriptorInfo })
162 .then((content: ArrayBuffer): void => {
163 const apiURLHeader: APIURLHEADER = {
165 httpOptions: { headers: apiHeader }
167 this.restService.postResource(apiURLHeader, content).subscribe((result: { id: string }) => {
168 this.activeModal.close(modalData);
169 this.isLoadingResults = false;
170 this.notifierService.notify('success', this.translateService.instant('CLONESUCCESSFULLY'));
171 }, (error: ERRORDATA) => {
172 this.isLoadingResults = false;
173 this.restService.handleError(error, 'post');
175 }).catch((): void => {
176 this.isLoadingResults = false;
177 this.notifierService.notify('error', this.translateService.instant('ERROR'));