| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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 Info Compose Package Model |
| 20 | */ |
| 21 | import { HttpClient, HttpHeaders } from '@angular/common/http'; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 22 | import { Component, ElementRef, Injector, Input, OnInit, ViewChild } from '@angular/core'; |
| 23 | import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 24 | import { Router } from '@angular/router'; |
| 25 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 26 | import { TranslateService } from '@ngx-translate/core'; |
| 27 | import { NotifierService } from 'angular-notifier'; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 28 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION, URLPARAMS } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 29 | import { DataService } from 'DataService'; |
| 30 | import { environment } from 'environment'; |
| 31 | import * as jsyaml from 'js-yaml'; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 32 | import { NSConfigData } from 'NSCONFIGTEMPLATEMODEL'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 33 | import * as pako from 'pako'; |
| 34 | import { RestService } from 'RestService'; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 35 | import { SharedService, isNullOrUndefined } from 'SharedService'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 36 | import { VNFD } from 'VNFDModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 37 | |
| 38 | /** This is added globally by the tar.js library */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 39 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 40 | declare 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.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 51 | // eslint-disable-next-line @angular-eslint/component-class-suffix |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 52 | export 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 69 | public submitted = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 70 | |
| 71 | /** To handle loader status for API call @public */ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 72 | public isLoadingResults = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 73 | |
| 74 | /** Give the message for the loading @public */ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 75 | public message = 'PLEASEWAIT'; |
| 76 | |
| 77 | /** contains NSD name @public */ |
| 78 | public nsName: {}[] = []; |
| 79 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 80 | /** set the ns archieve file @public */ |
| 81 | public okafile: File; |
| 82 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 83 | /** 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.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 98 | /** 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 110 | /** Data of NS config @public */ |
| 111 | public details: NSConfigData; |
| 112 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 113 | /** Data of OKA packages @public */ |
| 114 | public packageData: VNFD; |
| 115 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 116 | /** Data of NF packages @public */ |
| 117 | public nsConfigData: NSConfigData[] = []; |
| 118 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 119 | /** Contains selected file name @public */ |
| 120 | public selectedFileName: string = 'Choose file...'; |
| 121 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 122 | /** Contains all profile methods */ |
| 123 | public profileSelect: TYPESECTION[] = []; |
| 124 | |
| 125 | /** Contains all profile methods */ |
| 126 | public operationType: string; |
| 127 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 128 | /** 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 133 | |
| 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 146 | /** Input contains component objects @public */ |
| 147 | @Input() public params: URLPARAMS; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 148 | |
| 149 | /** Holds the end point @private */ |
| 150 | private endPoint: string; |
| 151 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 152 | /** ModalData instance of modal @private */ |
| 153 | private modalData: MODALCLOSERESPONSEDATA; |
| 154 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 155 | /** 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.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 174 | 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 190 | } |
| 191 | |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 192 | /** convenience getter for easy access to form fields */ |
| 193 | get f(): FormGroup['controls'] { return this.packagesForm.controls; } |
| 194 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 195 | /** |
| 196 | * Lifecyle Hooks the trigger before component is instantiate |
| 197 | */ |
| 198 | public ngOnInit(): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 199 | this.initializeForm(); |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 200 | if (this.params.page === 'ns-config-template') { |
| 201 | this.template = true; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 202 | this.oka = false; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 203 | this.getNsdPackageDetails(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 204 | this.getFormControl('profile_type').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 205 | this.getFormControl('description').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 206 | this.getFormControl('package').disable(); |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 207 | } else if (this.params.page === 'ns-config-template-edit') { |
| 208 | this.template = true; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 209 | this.oka = false; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 210 | this.getNsdPackageDetails(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 211 | this.getFormControl('profile_type').disable(); |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 212 | this.getFormControl('nsdId').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 213 | this.getFormControl('description').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 214 | this.getFormControl('package').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 215 | } else if (this.params.page === 'oka-packages') { |
| 216 | this.oka = true; |
| 217 | this.template = false; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 218 | this.getFormControl('nsdId').disable(); |
| 219 | this.getFormControl('config').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 220 | } 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.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 233 | this.getFormControl('profile_type').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 234 | this.getFormControl('description').disable(); |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 235 | } |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 236 | this.operationType = this.params.operationType; |
| 237 | this.setValidatorsForOperation(); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** initialize Forms @public */ |
| 241 | public initializeForm(): void { |
| 242 | this.packagesForm = this.formBuilder.group({ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 243 | name: ['', [Validators.required]], |
| 244 | nsdId: [null, [Validators.required]], |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 245 | config: [null, [Validators.required]], |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 246 | profile_type: [null], |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 247 | description: ['', [Validators.required]], |
| 248 | package: [''] |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 249 | }); |
| 250 | } |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 251 | /** 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 270 | |
| 271 | /** Get NSD Package details @public */ |
| 272 | public getNsdPackageDetails(): void { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 273 | this.restService.getResource(environment.NSDESCRIPTORSCONTENT_URL) |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 274 | .subscribe((nsdPackageData: []): void => { |
| 275 | nsdPackageData.forEach((nsData: VNFD): void => { |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 276 | 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.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 291 | /** 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.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 301 | this.packagesForm.patchValue({ name: this.package_name, description: nsdPackageData.description, profile_type: nsdPackageData.profile_type }); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 302 | this.fileInput.nativeElement.value = null; |
| 303 | }, (error: ERRORDATA): void => { |
| 304 | this.restService.handleError(error, 'get'); |
| 305 | }); |
| 306 | } |
| 307 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 308 | /** 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 329 | }); |
| 330 | } |
| 331 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 332 | /** Create packages @public */ |
| 333 | public createPackages(): void { |
| 334 | this.submitted = true; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 335 | this.modalData = { |
| 336 | message: 'Done' |
| 337 | }; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 338 | this.addNullValueForInvalidFiles(); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 339 | this.sharedService.cleanForm(this.packagesForm); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 340 | 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 346 | if (!this.packagesForm.invalid) { |
| 347 | this.isLoadingResults = true; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 348 | 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.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 366 | } 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 389 | } 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 409 | |
| 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.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 453 | 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 456 | } else { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 457 | const getConfigJson: string = jsyaml.load(this.packagesForm.value.config, { json: true }); |
| 458 | this.packagesForm.value.config = getConfigJson; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 459 | } |
| 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 469 | /** Create packages @public */ |
| 470 | private createPackageApi(packageContent: ArrayBuffer | SharedArrayBuffer): void { |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 471 | 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 476 | const apiURLHeader: APIURLHEADER = { |
| 477 | url: this.endPoint, |
| 478 | httpOptions: { headers: this.headers } |
| 479 | }; |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 480 | this.restService.postResource(apiURLHeader, packageContent).subscribe((result: { id: string }): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 481 | this.isLoadingResults = false; |
| 482 | this.activeModal.close(); |
| 483 | this.composeNSPackages(result.id); |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 484 | }, (error: ERRORDATA): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 485 | this.isLoadingResults = false; |
| 486 | this.restService.handleError(error, 'post'); |
| 487 | }); |
| 488 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 489 | |
| 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 551 | /** 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 587 | /** 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 R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 599 | this.router.navigate([packageUrl, id]).catch((): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 600 | // 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 R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 606 | const composerName: string = 'NGUI Composer'; |
| 607 | const composerDefaultVersion: string = '1.0'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 608 | if (descriptorType === 'ns-package') { |
| 609 | packageYaml = { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 610 | nsd: { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 611 | nsd: [ |
| 612 | { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 613 | id: this.packagesForm.value.name, |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 614 | 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 624 | } |
| 625 | ] |
| 626 | } |
| 627 | }; |
| 628 | } else { |
| 629 | packageYaml = { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 630 | 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 637 | { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 638 | id: 'default-df', |
| 639 | 'instantiation-level': [], |
| 640 | 'vdu-profile': [] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 641 | } |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 642 | ], |
| 643 | 'ext-cpd': [], |
| 644 | vdu: [], |
| 645 | 'sw-image-desc': [], |
| 646 | 'virtual-storage-desc': [] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 647 | } |
| 648 | }; |
| 649 | } |
| Barath Kumar R | 86e497d | 2021-05-04 17:16:14 +0530 | [diff] [blame] | 650 | return jsyaml.dump(packageYaml, { sortKeys: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 651 | } |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 652 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 653 | /** 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.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 702 | /** 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 707 | } |