blob: 125a7267f5d299041849bc40eac16b23b6d468d8 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, Injector, Input, OnInit } from '@angular/core';
23import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
25import { NotifierService } from 'angular-notifier';
26import { APIURLHEADER, ERRORDATA, GETAPIURLHEADER, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
27import { environment } from 'environment';
28import * as jsyaml from 'js-yaml';
Barath Kumar R063a3f12020-12-29 16:35:09 +053029import { NSDATACREATION, NSDDetails } from 'NSDModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053030import { RestService } from 'RestService';
31import { SharedService } from 'SharedService';
Barath Kumar R063a3f12020-12-29 16:35:09 +053032import { VNFDATA } from 'VNFDModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053033
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 */
45export 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)
Barath Kumar R063a3f12020-12-29 16:35:09 +0530103 .subscribe((nsData: NSDDetails[]): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530104 this.modifyContent(nsData);
Barath Kumar R063a3f12020-12-29 16:35:09 +0530105 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530106 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 {
kumaran.m3b4814a2020-05-01 19:48:54 +0530129 if (this.params.page === 'nsd') {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530130 const nsPackageContent: NSDATACREATION = jsyaml.load(packageInfo.toString());
131 this.packageName = 'clone_' + nsPackageContent.nsd.nsd[0].name;
kumaran.m3b4814a2020-05-01 19:48:54 +0530132 this.endPoint = environment.NSDESCRIPTORSCONTENT_URL;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530133 nsPackageContent.nsd.nsd.forEach((nsd: NSDDetails): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530134 nsd.id = 'clone_' + nsd.id;
135 nsd.name = 'clone_' + nsd.name;
kumaran.m3b4814a2020-05-01 19:48:54 +0530136 });
Barath Kumar R063a3f12020-12-29 16:35:09 +0530137 this.clonePackage(nsPackageContent);
kumaran.m3b4814a2020-05-01 19:48:54 +0530138 } else {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530139 const vnfPackageContent: VNFDATA = jsyaml.load(packageInfo.toString());
140 this.packageName = 'clone_' + vnfPackageContent.vnfd['product-name'];
kumaran.m3b4814a2020-05-01 19:48:54 +0530141 this.endPoint = environment.VNFPACKAGESCONTENT_URL;
Barath Kumar R063a3f12020-12-29 16:35:09 +0530142 vnfPackageContent.vnfd.id = 'clone_' + vnfPackageContent.vnfd.id;
143 vnfPackageContent.vnfd['product-name'] = 'clone_' + vnfPackageContent.vnfd['product-name'];
144 this.clonePackage(vnfPackageContent);
kumaran.m3b4814a2020-05-01 19:48:54 +0530145 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530146 }
147 /**
148 * Create clone package and upload as TAR.GZ file
149 */
Barath Kumar R063a3f12020-12-29 16:35:09 +0530150 private clonePackage(packageContent: NSDATACREATION | VNFDATA): void {
kumaran.m3b4814a2020-05-01 19:48:54 +0530151 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 };
Barath Kumar R063a3f12020-12-29 16:35:09 +0530166 // tslint:disable-next-line: completed-docs
167 this.restService.postResource(apiURLHeader, content).subscribe((result: { id: string }): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530168 this.activeModal.close(modalData);
169 this.isLoadingResults = false;
170 this.notifierService.notify('success', this.translateService.instant('CLONESUCCESSFULLY'));
Barath Kumar R063a3f12020-12-29 16:35:09 +0530171 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530172 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}