blob: d294f7f8520f4938d1d5aa78c7cf5b676a91c805 [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 Info Compose Package Model
20 */
21import { HttpClient, HttpHeaders } from '@angular/common/http';
SANDHYA.JS07decc02024-07-01 21:50:48 +053022import { Component, ElementRef, Injector, Input, OnInit, ViewChild } from '@angular/core';
23import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
kumaran.m3b4814a2020-05-01 19:48:54 +053024import { Router } from '@angular/router';
25import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26import { TranslateService } from '@ngx-translate/core';
27import { NotifierService } from 'angular-notifier';
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +053028import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION, URLPARAMS } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053029import { DataService } from 'DataService';
30import { environment } from 'environment';
31import * as jsyaml from 'js-yaml';
SANDHYA.JS07decc02024-07-01 21:50:48 +053032import { NSConfigData } from 'NSCONFIGTEMPLATEMODEL';
kumaran.m3b4814a2020-05-01 19:48:54 +053033import * as pako from 'pako';
34import { RestService } from 'RestService';
SANDHYA.JS07decc02024-07-01 21:50:48 +053035import { SharedService, isNullOrUndefined } from 'SharedService';
SANDHYA.JS26570112024-07-05 21:35:46 +053036import { VNFD } from 'VNFDModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053037
38/** This is added globally by the tar.js library */
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053039// eslint-disable-next-line @typescript-eslint/no-explicit-any
kumaran.m3b4814a2020-05-01 19:48:54 +053040declare const Tar: any;
41
42/**
43 * Creating component
44 * @Component takes ComposePackages.html as template url
45 */
46@Component({
47 templateUrl: './ComposePackages.html',
48 styleUrls: ['./ComposePackages.scss']
49})
50/** Exporting a class @exports ComposePackages */
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053051// eslint-disable-next-line @angular-eslint/component-class-suffix
kumaran.m3b4814a2020-05-01 19:48:54 +053052export class ComposePackages implements OnInit {
53 /** Invoke service injectors @public */
54 public injector: Injector;
55
56 /** dataService to pass the data from one component to another @public */
57 public dataService: DataService;
58
59 /** Varaibles to hold http client @public */
60 public httpClient: HttpClient;
61
62 /** Instance for active modal service @public */
63 public activeModal: NgbActiveModal;
64
65 /** FormGroup instance added to the form @ html @public */
66 public packagesForm: FormGroup;
67
68 /** Form submission Add */
SANDHYA.JS07decc02024-07-01 21:50:48 +053069 public submitted = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053070
71 /** To handle loader status for API call @public */
SANDHYA.JS07decc02024-07-01 21:50:48 +053072 public isLoadingResults = false;
kumaran.m3b4814a2020-05-01 19:48:54 +053073
74 /** Give the message for the loading @public */
SANDHYA.JS07decc02024-07-01 21:50:48 +053075 public message = 'PLEASEWAIT';
76
77 /** contains NSD name @public */
78 public nsName: {}[] = [];
79
SANDHYA.JS26570112024-07-05 21:35:46 +053080 /** set the ns archieve file @public */
81 public okafile: File;
82
SANDHYA.JS07decc02024-07-01 21:50:48 +053083 /** contains NSD details @public */
84 public nsdDetails: {}[];
85
86 /** contains NSD details filtered by id @public */
87 public nsdName: string;
88
89 /** Contains config details @public */
90 public config: string;
91
92 /** contains NSD details filtered by name @public */
93 public nsId: string;
94
95 /** Check if template or not @public */
96 public template = false;
97
SANDHYA.JS26570112024-07-05 21:35:46 +053098 /** Check if template or not @public */
99 public oka = false;
100
101 /** Check if template or not @public */
102 public package_name: string;
103
104 /** Element ref for fileInputConfigLabel @public */
105 @ViewChild('fileInput') fileInput: ElementRef<HTMLInputElement>;
106
107 /** Element ref for fileInputConfigLabel @public */
108 @ViewChild('fileInputLabel') fileInputLabel: ElementRef<HTMLLabelElement>;
109
SANDHYA.JS07decc02024-07-01 21:50:48 +0530110 /** Data of NS config @public */
111 public details: NSConfigData;
112
SANDHYA.JS26570112024-07-05 21:35:46 +0530113 /** Data of OKA packages @public */
114 public packageData: VNFD;
115
SANDHYA.JS07decc02024-07-01 21:50:48 +0530116 /** Data of NF packages @public */
117 public nsConfigData: NSConfigData[] = [];
118
SANDHYA.JS26570112024-07-05 21:35:46 +0530119 /** Contains selected file name @public */
120 public selectedFileName: string = 'Choose file...';
121
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530122 /** Contains all profile methods */
123 public profileSelect: TYPESECTION[] = [];
124
125 /** Contains all profile methods */
126 public operationType: string;
127
SANDHYA.JS07decc02024-07-01 21:50:48 +0530128 /** Element ref for fileInputConfig @public */
129 @ViewChild('fileInputConfig') fileInputConfig: ElementRef<HTMLInputElement>;
130
131 /** Element ref for fileInputConfigLabel @public */
132 @ViewChild('fileInputConfigLabel') fileInputConfigLabel: ElementRef<HTMLLabelElement>;
kumaran.m3b4814a2020-05-01 19:48:54 +0530133
134 /** FormBuilder instance added to the formBuilder @private */
135 private formBuilder: FormBuilder;
136
137 /** Instance of the rest service @private */
138 private restService: RestService;
139
140 /** Notifier service to popup notification @private */
141 private notifierService: NotifierService;
142
143 /** Controls the header form @private */
144 private headers: HttpHeaders;
145
SANDHYA.JS07decc02024-07-01 21:50:48 +0530146 /** Input contains component objects @public */
147 @Input() public params: URLPARAMS;
kumaran.m3b4814a2020-05-01 19:48:54 +0530148
149 /** Holds the end point @private */
150 private endPoint: string;
151
SANDHYA.JS07decc02024-07-01 21:50:48 +0530152 /** ModalData instance of modal @private */
153 private modalData: MODALCLOSERESPONSEDATA;
154
kumaran.m3b4814a2020-05-01 19:48:54 +0530155 /** Contains all methods related to shared @private */
156 private sharedService: SharedService;
157
158 /** Holds teh instance of AuthService class of type AuthService @private */
159 private router: Router;
160
161 /** Contains tranlsate instance @private */
162 private translateService: TranslateService;
163
164 constructor(injector: Injector) {
165 this.injector = injector;
166 this.dataService = this.injector.get(DataService);
167 this.restService = this.injector.get(RestService);
168 this.activeModal = this.injector.get(NgbActiveModal);
169 this.notifierService = this.injector.get(NotifierService);
170 this.formBuilder = this.injector.get(FormBuilder);
171 this.router = this.injector.get(Router);
172 this.translateService = this.injector.get(TranslateService);
173 this.sharedService = this.injector.get(SharedService);
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530174 this.profileSelect = [
175 {
176 title: 'Infra Config Profile',
177 value: 'infra_config_profiles'
178 },
179 {
180 title: 'Infra Controller Profile',
181 value: 'infra_controller_profiles'
182 }, {
183 title: 'App Profile',
184 value: 'app_profiles'
185 }, {
186 title: 'Resource Profile',
187 value: 'resource_profiles'
188 }
189 ];
kumaran.m3b4814a2020-05-01 19:48:54 +0530190 }
191
Barath Kumar R063a3f12020-12-29 16:35:09 +0530192 /** convenience getter for easy access to form fields */
193 get f(): FormGroup['controls'] { return this.packagesForm.controls; }
194
kumaran.m3b4814a2020-05-01 19:48:54 +0530195 /**
196 * Lifecyle Hooks the trigger before component is instantiate
197 */
198 public ngOnInit(): void {
kumaran.m3b4814a2020-05-01 19:48:54 +0530199 this.initializeForm();
SANDHYA.JS07decc02024-07-01 21:50:48 +0530200 if (this.params.page === 'ns-config-template') {
201 this.template = true;
SANDHYA.JS26570112024-07-05 21:35:46 +0530202 this.oka = false;
SANDHYA.JS07decc02024-07-01 21:50:48 +0530203 this.getNsdPackageDetails();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530204 this.getFormControl('profile_type').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530205 this.getFormControl('description').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530206 this.getFormControl('package').disable();
SANDHYA.JS07decc02024-07-01 21:50:48 +0530207 } else if (this.params.page === 'ns-config-template-edit') {
208 this.template = true;
SANDHYA.JS26570112024-07-05 21:35:46 +0530209 this.oka = false;
SANDHYA.JS07decc02024-07-01 21:50:48 +0530210 this.getNsdPackageDetails();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530211 this.getFormControl('profile_type').disable();
SANDHYA.JS07decc02024-07-01 21:50:48 +0530212 this.getFormControl('nsdId').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530213 this.getFormControl('description').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530214 this.getFormControl('package').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530215 } else if (this.params.page === 'oka-packages') {
216 this.oka = true;
217 this.template = false;
SANDHYA.JS07decc02024-07-01 21:50:48 +0530218 this.getFormControl('nsdId').disable();
219 this.getFormControl('config').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530220 } else if (this.params.page === 'oka-packages-edit') {
221 this.oka = true;
222 this.template = false;
223 this.addNullValueForInvalidFiles();
224 this.packagesForm.value.package = '';
225 this.getOkaDetails();
226 this.getFormControl('nsdId').disable();
227 this.getFormControl('config').disable();
228 } else {
229 this.oka = false;
230 this.template = false;
231 this.getFormControl('nsdId').disable();
232 this.getFormControl('config').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530233 this.getFormControl('profile_type').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530234 this.getFormControl('description').disable();
SANDHYA.JS07decc02024-07-01 21:50:48 +0530235 }
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530236 this.operationType = this.params.operationType;
237 this.setValidatorsForOperation();
kumaran.m3b4814a2020-05-01 19:48:54 +0530238 }
239
240 /** initialize Forms @public */
241 public initializeForm(): void {
242 this.packagesForm = this.formBuilder.group({
SANDHYA.JS07decc02024-07-01 21:50:48 +0530243 name: ['', [Validators.required]],
244 nsdId: [null, [Validators.required]],
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530245 config: [null, [Validators.required]],
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530246 profile_type: [null],
SANDHYA.JS26570112024-07-05 21:35:46 +0530247 description: ['', [Validators.required]],
248 package: ['']
SANDHYA.JS07decc02024-07-01 21:50:48 +0530249 });
250 }
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530251 /** Get NSD Package details @public */
252 public setValidatorsForOperation(): void {
253 if (this.params.page === 'oka-packages') {
254 this.packagesForm.get('name').setValidators([Validators.required]);
255 this.packagesForm.get('description').setValidators([Validators.required]);
256 this.packagesForm.get('package').setValidators([Validators.required]);
257 this.packagesForm.get('profile_type').setValidators([Validators.required]);
258 } else if (this.params.page === 'oka-packages-edit') {
259 this.packagesForm.get('profile_type').clearValidators();
260 this.packagesForm.get('name').clearValidators();
261 this.packagesForm.get('description').clearValidators();
262 this.packagesForm.get('package').clearValidators();
263 }
264 this.packagesForm.get('profile_type').updateValueAndValidity();
265 this.packagesForm.get('name').updateValueAndValidity();
266 this.packagesForm.get('description').updateValueAndValidity();
267 this.packagesForm.get('package').updateValueAndValidity();
268 }
269
SANDHYA.JS07decc02024-07-01 21:50:48 +0530270
271 /** Get NSD Package details @public */
272 public getNsdPackageDetails(): void {
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530273 this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL)
SANDHYA.JS26570112024-07-05 21:35:46 +0530274 .subscribe((nsdPackageData: []): void => {
275 nsdPackageData.forEach((nsData: VNFD): void => {
SANDHYA.JS07decc02024-07-01 21:50:48 +0530276 const names: {} = {
277 nsName: nsData.name,
278 nsId: nsData._id
279 };
280 this.nsName.push(names);
281 });
282 this.nsdDetails = this.nsName;
283 if (this.params.page === 'ns-config-template-edit') {
284 this.getNSConfigDetails(environment.NSCONFIGTEMPLATE_URL + '/' + this.params.id, this.nsdDetails);
285 }
286 }, (error: ERRORDATA): void => {
287 this.restService.handleError(error, 'get');
288 });
289 }
290
SANDHYA.JS26570112024-07-05 21:35:46 +0530291 /** Get NSD Package details @public */
292 public getOkaDetails(): void {
293 this.addNullValueForInvalidFiles();
294 this.restService.getResource(environment.OKAPACKAGES_URL + '/' + this.params.id)
295 .subscribe((nsdPackageData: VNFD): void => {
296 this.packageData = nsdPackageData;
297 this.packagesForm.value.package = '';
298 this.package_name = nsdPackageData.name;
299 const package_file = nsdPackageData._admin.storage.zipfile;
300 this.selectedFileName = package_file;
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530301 this.packagesForm.patchValue({ name: this.package_name, description: nsdPackageData.description, profile_type: nsdPackageData.profile_type });
SANDHYA.JS26570112024-07-05 21:35:46 +0530302 this.fileInput.nativeElement.value = null;
303 }, (error: ERRORDATA): void => {
304 this.restService.handleError(error, 'get');
305 });
306 }
307
SANDHYA.JS07decc02024-07-01 21:50:48 +0530308 /** Get the NSD Content List & patch value in edit form @public */
309 public getNSConfigDetails(URL: string, name: {}[]): void {
310 this.restService.getResource(URL).subscribe((content: NSConfigData): void => {
311 this.nsConfigData.push(content);
312 this.details = this.nsConfigData[0];
313 const nsId: string = 'nsId';
314 // eslint-disable-next-line security/detect-object-injection
315 const nsdId: {}[] = name.filter((nsdData: {}[]): boolean => nsdData[nsId] === this.details.nsdId);
316 const nsName: string = 'nsName';
317 for (const data of nsdId) {
318 // eslint-disable-next-line security/detect-object-injection
319 this.nsdName = data[nsName];
320 }
321 if (!isNullOrUndefined(this.details.config)) {
322 this.config = jsyaml.dump(this.details.config);
323 }
324 this.packagesForm.patchValue({ name: this.details.name, nsdId: this.nsdName, config: this.config });
325 this.isLoadingResults = false;
326 }, (error: ERRORDATA): void => {
327 this.restService.handleError(error, 'get');
328 this.isLoadingResults = false;
kumaran.m3b4814a2020-05-01 19:48:54 +0530329 });
330 }
331
kumaran.m3b4814a2020-05-01 19:48:54 +0530332 /** Create packages @public */
333 public createPackages(): void {
334 this.submitted = true;
SANDHYA.JS07decc02024-07-01 21:50:48 +0530335 this.modalData = {
336 message: 'Done'
337 };
SANDHYA.JS26570112024-07-05 21:35:46 +0530338 this.addNullValueForInvalidFiles();
kumaran.m3b4814a2020-05-01 19:48:54 +0530339 this.sharedService.cleanForm(this.packagesForm);
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530340 if (this.params.page === 'ns-config-template-edit' || this.params.page === 'ns-config-template') {
341 if (this.packagesForm.value.config.trim() === '' || isNullOrUndefined(this.packagesForm.value.config)) {
342 this.packagesForm.controls.config.setErrors({ configIsEmpty: true });
343 this.packagesForm.get('config').setValue(this.packagesForm.get('config').value.trim());
344 }
345 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530346 if (!this.packagesForm.invalid) {
347 this.isLoadingResults = true;
SANDHYA.JS07decc02024-07-01 21:50:48 +0530348 if (this.params.page === 'ns-package' || this.params.page === 'vnf-package') {
349 if (this.params.page === 'ns-package') {
350 this.endPoint = environment.NSDESCRIPTORSCONTENT_URL;
351 } else if (this.params.page === 'vnf-package') {
352 this.endPoint = environment.VNFPACKAGESCONTENT_URL;
353 }
354 const descriptor: string = this.packageYaml(this.params.page);
355 try {
356 // eslint-disable-next-line @typescript-eslint/no-explicit-any
357 const tar: any = new Tar();
358 const out: Uint8Array = tar.append(this.packagesForm.value.name + '/' + this.packagesForm.value.name + '.yaml',
359 descriptor, { type: '0' });
360 const gzipContent: Uint8Array = pako.gzip(out);
361 this.createPackageApi(gzipContent.buffer);
362 } catch (e) {
363 this.isLoadingResults = false;
364 this.notifierService.notify('error', this.translateService.instant('ERROR'));
365 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530366 } else if (this.oka) {
367 try {
368 this.headers = new HttpHeaders({
369 Accept: 'application/json',
370 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
371 });
372 if (this.params.page === 'oka-packages') {
373 const apiURLHeader: APIURLHEADER = {
374 url: environment.OKAPACKAGES_URL,
375 httpOptions: { headers: this.headers }
376 };
377 this.saveFileData(apiURLHeader);
378 } else {
379 const apiURLHeader: APIURLHEADER = {
380 url: environment.OKAPACKAGES_URL + '/' + this.params.id,
381 httpOptions: { headers: this.headers }
382 };
383 this.editFileData(apiURLHeader);
384 }
385 } catch (e) {
386 this.isLoadingResults = false;
387 this.notifierService.notify('error', this.translateService.instant('ERROR'));
388 }
SANDHYA.JS07decc02024-07-01 21:50:48 +0530389 } else {
390 try {
391 this.headers = new HttpHeaders({
392 Accept: 'application/json',
393 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
394 });
395 if (this.params.page === 'ns-config-template') {
396 this.endPoint = environment.NSCONFIGTEMPLATE_URL;
397 this.createTemplate(this.endPoint);
398 } else if (this.params.page === 'ns-config-template-edit') {
399 this.endPoint = environment.NSCONFIGTEMPLATE_URL + '/' + this.params.id + '/' + 'template_content';
400 this.editTemplate(this.endPoint);
401 }
402 } catch (e) {
403 this.isLoadingResults = false;
404 this.notifierService.notify('error', this.translateService.instant('ERROR'));
405 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530406 }
407 }
408 }
SANDHYA.JS07decc02024-07-01 21:50:48 +0530409
410 /** Post config template @public */
411 public createTemplate(urlHeader: string): void {
412 this.isLoadingResults = true;
413 const apiURLHeader: APIURLHEADER = {
414 url: urlHeader,
415 httpOptions: { headers: this.headers }
416 };
417 if (isNullOrUndefined(this.packagesForm.value.config) || this.packagesForm.value.config === '') {
418 delete this.packagesForm.value.config;
419 } else {
420 const validJSON: boolean = this.sharedService.checkJson(this.packagesForm.value.config);
421 if (validJSON) {
422 this.packagesForm.value.config = JSON.parse(this.packagesForm.value.config);
423 } else {
424 const getConfigJson: string = jsyaml.load(this.packagesForm.value.config, { json: true });
425 this.packagesForm.value.config = getConfigJson;
426 }
427 }
428 const nsName: string = 'nsName';
429 // eslint-disable-next-line security/detect-object-injection
430 const nsdId: {}[] = this.nsdDetails.filter((nsdData: {}[]): boolean => nsdData[nsName] === this.packagesForm.value.nsdId);
431 for (const data of nsdId) {
432 // eslint-disable-next-line @typescript-eslint/dot-notation
433 this.nsId = data['nsId'];
434 }
435 this.packagesForm.value.nsdId = this.nsId;
436 this.restService.postResource(apiURLHeader, (this.packagesForm.value)).subscribe((result: {}): void => {
437 this.activeModal.close(this.modalData);
438 this.isLoadingResults = false;
439 this.notifierService.notify('success', this.translateService.instant('PAGE.NSCONFIGTEMPLATE.TEMPLATECREATEDSUCCESSFULLY'));
440 }, (error: ERRORDATA): void => {
441 this.restService.handleError(error, 'post');
442 this.isLoadingResults = false;
443 });
444 }
445
446 /** Edit config template @public */
447 public editTemplate(urlHeader: string): void {
448 this.isLoadingResults = true;
449 const apiURLHeader: APIURLHEADER = {
450 url: urlHeader,
451 httpOptions: { headers: this.headers }
452 };
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530453 const validJSON: boolean = this.sharedService.checkJson(this.packagesForm.value.config);
454 if (validJSON) {
455 this.packagesForm.value.config = JSON.parse(this.packagesForm.value.config);
SANDHYA.JS07decc02024-07-01 21:50:48 +0530456 } else {
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530457 const getConfigJson: string = jsyaml.load(this.packagesForm.value.config, { json: true });
458 this.packagesForm.value.config = getConfigJson;
SANDHYA.JS07decc02024-07-01 21:50:48 +0530459 }
460 this.restService.putResource(apiURLHeader, (this.packagesForm.value)).subscribe((result: {}): void => {
461 this.activeModal.close(this.modalData);
462 this.isLoadingResults = false;
463 this.notifierService.notify('success', this.translateService.instant('PAGE.NSCONFIGTEMPLATE.TEMPLATEEDITEDSUCCESSFULLY'));
464 }, (error: ERRORDATA): void => {
465 this.restService.handleError(error, 'post');
466 this.isLoadingResults = false;
467 });
468 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530469 /** Create packages @public */
470 private createPackageApi(packageContent: ArrayBuffer | SharedArrayBuffer): void {
SANDHYA.JS07decc02024-07-01 21:50:48 +0530471 this.headers = new HttpHeaders({
472 'Content-Type': 'application/gzip',
473 Accept: 'application/json',
474 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
475 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530476 const apiURLHeader: APIURLHEADER = {
477 url: this.endPoint,
478 httpOptions: { headers: this.headers }
479 };
Barath Kumar R063a3f12020-12-29 16:35:09 +0530480 this.restService.postResource(apiURLHeader, packageContent).subscribe((result: { id: string }): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530481 this.isLoadingResults = false;
482 this.activeModal.close();
483 this.composeNSPackages(result.id);
Barath Kumar R063a3f12020-12-29 16:35:09 +0530484 }, (error: ERRORDATA): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530485 this.isLoadingResults = false;
486 this.restService.handleError(error, 'post');
487 });
488 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530489
490 /** Drag and drop feature and fetchind the details of files @public */
491 public onFileDropped(files: FileList): void {
492 if (files && files.length === 1) {
493 const file: File = files[0];
494 if (this.sharedService.vaildataFileInfo(file, 'gz')) {
495 this.removeValidationDragDropFiles();
496 this.okafile = file;
497 this.selectedFileName = '';
498 if (this.params.page === 'oka-packages-edit') {
499 this.selectedFileName = files[0].name;
500 } else {
501 this.fileInputLabel.nativeElement.innerText = files[0].name;
502 this.fileInput.nativeElement.value = null;
503 }
504 } else {
505 this.notifierService.notify('error', this.translateService.instant('GZFILETYPEERRROR'));
506 this.addNullValueForInvalidFiles();
507 }
508 } else if (files && files.length > 1) {
509 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
510 }
511 }
512
513 /** Add Null value for invalid Files @public */
514 public addNullValueForInvalidFiles(): void {
515 this.getFormControl('package').setValue('');
516 }
517
518 /** Remove validation after drag drop Files @public */
519 public removeValidationDragDropFiles(): void {
520 this.getFormControl('package').setValidators([]);
521 this.getFormControl('package').updateValueAndValidity();
522 }
523
524
525 /** Post the droped files and reload the page @public */
526 public saveFileData(urlHeader: APIURLHEADER): void {
527 this.isLoadingResults = true;
528 this.packagesForm.value.package = this.okafile;
529 this.restService.postResource(urlHeader, this.toFormData(this.packagesForm.value)).subscribe((result: {}): void => {
530 this.activeModal.close(this.modalData);
531 this.isLoadingResults = false;
532 this.notifierService.notify('success', this.translateService.instant('OKA Package Created Successfully'));
533 }, (error: ERRORDATA): void => {
534 this.restService.handleError(error, 'post');
535 this.isLoadingResults = false;
536 });
537 }
538
539 /** Post the droped files and reload the page @public */
540 public editFileData(urlHeader: APIURLHEADER): void {
541 this.isLoadingResults = true;
542 this.restService.patchResource(urlHeader, this.toFormData(this.packagesForm.value)).subscribe((result: {}): void => {
543 this.activeModal.close(this.modalData);
544 this.isLoadingResults = false;
545 this.notifierService.notify('success', this.translateService.instant('OKA Package Edited Successfully'));
546 }, (error: ERRORDATA): void => {
547 this.restService.handleError(error, 'post');
548 this.isLoadingResults = false;
549 });
550 }
SANDHYA.JS07decc02024-07-01 21:50:48 +0530551 /** Config file process @private */
552 public configFile(files: FileList): void {
553 if (files && files.length === 1) {
554 const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase();
555 if (fileFormat === 'yaml' || fileFormat === 'yml') {
556 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
557 this.packagesForm.get('config').setValue(fileContent);
558 }).catch((err: string): void => {
559 if (err === 'typeError') {
560 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
561 } else {
562 this.notifierService.notify('error', this.translateService.instant('ERROR'));
563 }
564 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
565 this.fileInputConfig.nativeElement.value = null;
566 });
567 } else if (fileFormat === 'json') {
568 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
569 const getConfigJson: string = jsyaml.load(fileContent, { json: true });
570 this.packagesForm.get('config').setValue(JSON.stringify(getConfigJson));
571 }).catch((err: string): void => {
572 if (err === 'typeError') {
573 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
574 } else {
575 this.notifierService.notify('error', this.translateService.instant('ERROR'));
576 }
577 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
578 this.fileInputConfig.nativeElement.value = null;
579 });
580 }
581 } else if (files && files.length > 1) {
582 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
583 }
584 this.fileInputConfigLabel.nativeElement.innerText = files[0].name;
585 this.fileInputConfig.nativeElement.value = null;
586 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530587 /** Compose NS Packages @private */
588 private composeNSPackages(id: string): void {
589 let packageUrl: string;
590 if (this.params.page === 'ns-package') {
591 packageUrl = '/packages/ns/compose/';
592 this.notifierService.notify('success', this.packagesForm.value.name + ' ' +
593 this.translateService.instant('PAGE.NSPACKAGE.CREATEDSUCCESSFULLY'));
594 } else if (this.params.page === 'vnf-package') {
595 packageUrl = '/packages/vnf/compose/';
596 this.notifierService.notify('success', this.packagesForm.value.name + ' ' +
597 this.translateService.instant('PAGE.VNFPACKAGE.CREATEDSUCCESSFULLY'));
598 }
Barath Kumar R063a3f12020-12-29 16:35:09 +0530599 this.router.navigate([packageUrl, id]).catch((): void => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530600 // Catch Navigation Error
601 });
602 }
603 /** Deafult template for NS and VNF Packages @private */
604 private packageYaml(descriptorType: string): string {
605 let packageYaml: {} = {};
Barath Kumar R063a3f12020-12-29 16:35:09 +0530606 const composerName: string = 'NGUI Composer';
607 const composerDefaultVersion: string = '1.0';
kumaran.m3b4814a2020-05-01 19:48:54 +0530608 if (descriptorType === 'ns-package') {
609 packageYaml = {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530610 nsd: {
kumaran.m3b4814a2020-05-01 19:48:54 +0530611 nsd: [
612 {
kumaran.m3b4814a2020-05-01 19:48:54 +0530613 id: this.packagesForm.value.name,
Barath Kumar R063a3f12020-12-29 16:35:09 +0530614 name: this.packagesForm.value.name,
615 version: composerDefaultVersion,
616 description: this.packagesForm.value.name + ' descriptor',
617 designer: composerName,
618 df: [
619 {
620 id: 'default-df',
621 'vnf-profile': []
622 }
623 ]
kumaran.m3b4814a2020-05-01 19:48:54 +0530624 }
625 ]
626 }
627 };
628 } else {
629 packageYaml = {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530630 vnfd: {
631 id: this.packagesForm.value.name,
632 'product-name': this.packagesForm.value.name,
633 version: composerDefaultVersion,
634 description: this.packagesForm.value.name + ' descriptor',
635 provider: composerName,
636 df: [
kumaran.m3b4814a2020-05-01 19:48:54 +0530637 {
Barath Kumar R063a3f12020-12-29 16:35:09 +0530638 id: 'default-df',
639 'instantiation-level': [],
640 'vdu-profile': []
kumaran.m3b4814a2020-05-01 19:48:54 +0530641 }
Barath Kumar R063a3f12020-12-29 16:35:09 +0530642 ],
643 'ext-cpd': [],
644 vdu: [],
645 'sw-image-desc': [],
646 'virtual-storage-desc': []
kumaran.m3b4814a2020-05-01 19:48:54 +0530647 }
648 };
649 }
Barath Kumar R86e497d2021-05-04 17:16:14 +0530650 return jsyaml.dump(packageYaml, { sortKeys: true });
kumaran.m3b4814a2020-05-01 19:48:54 +0530651 }
SANDHYA.JS07decc02024-07-01 21:50:48 +0530652
SANDHYA.JS26570112024-07-05 21:35:46 +0530653 /** Form data @private */
654 private toFormData<T>(formValue: T): FormData {
655 const formData: FormData = new FormData();
656 for (const key of Object.keys(formValue)) {
657 // eslint-disable-next-line security/detect-object-injection
658 const value: string = formValue[key];
659 if (key === 'name') {
660 if (this.params.page === 'oka-packages') {
661 if (!isNullOrUndefined(this.packagesForm.value.name)) {
662 formData.append(key, this.packagesForm.value.name);
663 }
664 } else if (this.params.page === 'oka-packages-edit') {
665 if (this.package_name.localeCompare(this.packagesForm.value.name) === 0) {
666 delete this.packagesForm.value.name;
667 } else {
668 formData.append(key, this.packagesForm.value.name);
669 }
670 }
671 else {
672 formData.append(key, '');
673 }
674 } else if (key === 'description') {
675 if (this.params.page === 'oka-packages') {
676 if (!isNullOrUndefined(this.packagesForm.value.description)) {
677 formData.append(key, this.packagesForm.value.description);
678 }
679 } else if (this.params.page === 'oka-packages-edit') {
680 if (this.packageData.description.localeCompare(this.packagesForm.value.description) === 0) {
681 delete this.packagesForm.value.description;
682 } else {
683 formData.append(key, this.packagesForm.value.description);
684 }
685 }
686 else {
687 formData.append(key, '');
688 }
689 } else if (key === 'package') {
690 if (!isNullOrUndefined(this.okafile)) {
691 formData.append(key, this.okafile);
692 } else {
693 delete this.packagesForm.value.package;
694 }
695 } else {
696 formData.append(key, value);
697 }
698 }
699 return formData;
700 }
701
SANDHYA.JS07decc02024-07-01 21:50:48 +0530702 /** Used to get the AbstractControl of controlName passed @private */
703 private getFormControl(controlName: string): AbstractControl {
704 // eslint-disable-next-line security/detect-object-injection
705 return this.packagesForm.controls[controlName];
706 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530707}