| 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'; |
| 33 | import { NSDDetails } from 'NSDModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 34 | import * as pako from 'pako'; |
| 35 | import { RestService } from 'RestService'; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 36 | import { SharedService, isNullOrUndefined } from 'SharedService'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 37 | import { VNFD } from 'VNFDModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 38 | |
| 39 | /** This is added globally by the tar.js library */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 40 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 41 | declare const Tar: any; |
| 42 | |
| 43 | /** |
| 44 | * Creating component |
| 45 | * @Component takes ComposePackages.html as template url |
| 46 | */ |
| 47 | @Component({ |
| 48 | templateUrl: './ComposePackages.html', |
| 49 | styleUrls: ['./ComposePackages.scss'] |
| 50 | }) |
| 51 | /** Exporting a class @exports ComposePackages */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 52 | // eslint-disable-next-line @angular-eslint/component-class-suffix |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 53 | export class ComposePackages implements OnInit { |
| 54 | /** Invoke service injectors @public */ |
| 55 | public injector: Injector; |
| 56 | |
| 57 | /** dataService to pass the data from one component to another @public */ |
| 58 | public dataService: DataService; |
| 59 | |
| 60 | /** Varaibles to hold http client @public */ |
| 61 | public httpClient: HttpClient; |
| 62 | |
| 63 | /** Instance for active modal service @public */ |
| 64 | public activeModal: NgbActiveModal; |
| 65 | |
| 66 | /** FormGroup instance added to the form @ html @public */ |
| 67 | public packagesForm: FormGroup; |
| 68 | |
| 69 | /** Form submission Add */ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 70 | public submitted = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 71 | |
| 72 | /** To handle loader status for API call @public */ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 73 | public isLoadingResults = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 74 | |
| 75 | /** Give the message for the loading @public */ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 76 | public message = 'PLEASEWAIT'; |
| 77 | |
| 78 | /** contains NSD name @public */ |
| 79 | public nsName: {}[] = []; |
| 80 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 81 | /** set the ns archieve file @public */ |
| 82 | public okafile: File; |
| 83 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 84 | /** contains NSD details @public */ |
| 85 | public nsdDetails: {}[]; |
| 86 | |
| 87 | /** contains NSD details filtered by id @public */ |
| 88 | public nsdName: string; |
| 89 | |
| 90 | /** Contains config details @public */ |
| 91 | public config: string; |
| 92 | |
| 93 | /** contains NSD details filtered by name @public */ |
| 94 | public nsId: string; |
| 95 | |
| 96 | /** Check if template or not @public */ |
| 97 | public template = false; |
| 98 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 99 | /** Check if template or not @public */ |
| 100 | public oka = false; |
| 101 | |
| 102 | /** Check if template or not @public */ |
| 103 | public package_name: string; |
| 104 | |
| 105 | /** Element ref for fileInputConfigLabel @public */ |
| 106 | @ViewChild('fileInput') fileInput: ElementRef<HTMLInputElement>; |
| 107 | |
| 108 | /** Element ref for fileInputConfigLabel @public */ |
| 109 | @ViewChild('fileInputLabel') fileInputLabel: ElementRef<HTMLLabelElement>; |
| 110 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 111 | /** Data of NS config @public */ |
| 112 | public details: NSConfigData; |
| 113 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 114 | /** Data of OKA packages @public */ |
| 115 | public packageData: VNFD; |
| 116 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 117 | /** Data of NF packages @public */ |
| 118 | public nsConfigData: NSConfigData[] = []; |
| 119 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 120 | /** Contains selected file name @public */ |
| 121 | public selectedFileName: string = 'Choose file...'; |
| 122 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 123 | /** Contains all profile methods */ |
| 124 | public profileSelect: TYPESECTION[] = []; |
| 125 | |
| 126 | /** Contains all profile methods */ |
| 127 | public operationType: string; |
| 128 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 129 | /** Element ref for fileInputConfig @public */ |
| 130 | @ViewChild('fileInputConfig') fileInputConfig: ElementRef<HTMLInputElement>; |
| 131 | |
| 132 | /** Element ref for fileInputConfigLabel @public */ |
| 133 | @ViewChild('fileInputConfigLabel') fileInputConfigLabel: ElementRef<HTMLLabelElement>; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 134 | |
| 135 | /** FormBuilder instance added to the formBuilder @private */ |
| 136 | private formBuilder: FormBuilder; |
| 137 | |
| 138 | /** Instance of the rest service @private */ |
| 139 | private restService: RestService; |
| 140 | |
| 141 | /** Notifier service to popup notification @private */ |
| 142 | private notifierService: NotifierService; |
| 143 | |
| 144 | /** Controls the header form @private */ |
| 145 | private headers: HttpHeaders; |
| 146 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 147 | /** Input contains component objects @public */ |
| 148 | @Input() public params: URLPARAMS; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 149 | |
| 150 | /** Holds the end point @private */ |
| 151 | private endPoint: string; |
| 152 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 153 | /** ModalData instance of modal @private */ |
| 154 | private modalData: MODALCLOSERESPONSEDATA; |
| 155 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 156 | /** Contains all methods related to shared @private */ |
| 157 | private sharedService: SharedService; |
| 158 | |
| 159 | /** Holds teh instance of AuthService class of type AuthService @private */ |
| 160 | private router: Router; |
| 161 | |
| 162 | /** Contains tranlsate instance @private */ |
| 163 | private translateService: TranslateService; |
| 164 | |
| 165 | constructor(injector: Injector) { |
| 166 | this.injector = injector; |
| 167 | this.dataService = this.injector.get(DataService); |
| 168 | this.restService = this.injector.get(RestService); |
| 169 | this.activeModal = this.injector.get(NgbActiveModal); |
| 170 | this.notifierService = this.injector.get(NotifierService); |
| 171 | this.formBuilder = this.injector.get(FormBuilder); |
| 172 | this.router = this.injector.get(Router); |
| 173 | this.translateService = this.injector.get(TranslateService); |
| 174 | this.sharedService = this.injector.get(SharedService); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 175 | this.profileSelect = [ |
| 176 | { |
| 177 | title: 'Infra Config Profile', |
| 178 | value: 'infra_config_profiles' |
| 179 | }, |
| 180 | { |
| 181 | title: 'Infra Controller Profile', |
| 182 | value: 'infra_controller_profiles' |
| 183 | }, { |
| 184 | title: 'App Profile', |
| 185 | value: 'app_profiles' |
| 186 | }, { |
| 187 | title: 'Resource Profile', |
| 188 | value: 'resource_profiles' |
| 189 | } |
| 190 | ]; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 193 | /** convenience getter for easy access to form fields */ |
| 194 | get f(): FormGroup['controls'] { return this.packagesForm.controls; } |
| 195 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 196 | /** |
| 197 | * Lifecyle Hooks the trigger before component is instantiate |
| 198 | */ |
| 199 | public ngOnInit(): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 200 | this.initializeForm(); |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 201 | if (this.params.page === 'ns-config-template') { |
| 202 | this.template = true; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 203 | this.oka = false; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 204 | this.getNsdPackageDetails(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 205 | this.getFormControl('profile_type').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 206 | this.getFormControl('description').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(); |
| 214 | } else if (this.params.page === 'oka-packages') { |
| 215 | this.oka = true; |
| 216 | this.template = false; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 217 | this.getFormControl('nsdId').disable(); |
| 218 | this.getFormControl('config').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 219 | } else if (this.params.page === 'oka-packages-edit') { |
| 220 | this.oka = true; |
| 221 | this.template = false; |
| 222 | this.addNullValueForInvalidFiles(); |
| 223 | this.packagesForm.value.package = ''; |
| 224 | this.getOkaDetails(); |
| 225 | this.getFormControl('nsdId').disable(); |
| 226 | this.getFormControl('config').disable(); |
| 227 | } else { |
| 228 | this.oka = false; |
| 229 | this.template = false; |
| 230 | this.getFormControl('nsdId').disable(); |
| 231 | this.getFormControl('config').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 232 | this.getFormControl('profile_type').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 233 | this.getFormControl('description').disable(); |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 234 | } |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 235 | this.operationType = this.params.operationType; |
| 236 | this.setValidatorsForOperation(); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** initialize Forms @public */ |
| 240 | public initializeForm(): void { |
| 241 | this.packagesForm = this.formBuilder.group({ |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 242 | name: ['', [Validators.required]], |
| 243 | nsdId: [null, [Validators.required]], |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 244 | config: [null], |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 245 | profile_type: [null], |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 246 | description: ['', [Validators.required]], |
| 247 | package: [''] |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 248 | }); |
| 249 | } |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 250 | /** Get NSD Package details @public */ |
| 251 | public setValidatorsForOperation(): void { |
| 252 | if (this.params.page === 'oka-packages') { |
| 253 | this.packagesForm.get('name').setValidators([Validators.required]); |
| 254 | this.packagesForm.get('description').setValidators([Validators.required]); |
| 255 | this.packagesForm.get('package').setValidators([Validators.required]); |
| 256 | this.packagesForm.get('profile_type').setValidators([Validators.required]); |
| 257 | } else if (this.params.page === 'oka-packages-edit') { |
| 258 | this.packagesForm.get('profile_type').clearValidators(); |
| 259 | this.packagesForm.get('name').clearValidators(); |
| 260 | this.packagesForm.get('description').clearValidators(); |
| 261 | this.packagesForm.get('package').clearValidators(); |
| 262 | } |
| 263 | this.packagesForm.get('profile_type').updateValueAndValidity(); |
| 264 | this.packagesForm.get('name').updateValueAndValidity(); |
| 265 | this.packagesForm.get('description').updateValueAndValidity(); |
| 266 | this.packagesForm.get('package').updateValueAndValidity(); |
| 267 | } |
| 268 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 269 | |
| 270 | /** Get NSD Package details @public */ |
| 271 | public getNsdPackageDetails(): void { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 272 | this.restService.getResource(environment.OKAPACKAGES_URL) |
| 273 | .subscribe((nsdPackageData: []): void => { |
| 274 | nsdPackageData.forEach((nsData: VNFD): void => { |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 275 | const names: {} = { |
| 276 | nsName: nsData.name, |
| 277 | nsId: nsData._id |
| 278 | }; |
| 279 | this.nsName.push(names); |
| 280 | }); |
| 281 | this.nsdDetails = this.nsName; |
| 282 | if (this.params.page === 'ns-config-template-edit') { |
| 283 | this.getNSConfigDetails(environment.NSCONFIGTEMPLATE_URL + '/' + this.params.id, this.nsdDetails); |
| 284 | } |
| 285 | }, (error: ERRORDATA): void => { |
| 286 | this.restService.handleError(error, 'get'); |
| 287 | }); |
| 288 | } |
| 289 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 290 | /** Get NSD Package details @public */ |
| 291 | public getOkaDetails(): void { |
| 292 | this.addNullValueForInvalidFiles(); |
| 293 | this.restService.getResource(environment.OKAPACKAGES_URL + '/' + this.params.id) |
| 294 | .subscribe((nsdPackageData: VNFD): void => { |
| 295 | this.packageData = nsdPackageData; |
| 296 | this.packagesForm.value.package = ''; |
| 297 | this.package_name = nsdPackageData.name; |
| 298 | const package_file = nsdPackageData._admin.storage.zipfile; |
| 299 | this.selectedFileName = package_file; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 300 | 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] | 301 | this.fileInput.nativeElement.value = null; |
| 302 | }, (error: ERRORDATA): void => { |
| 303 | this.restService.handleError(error, 'get'); |
| 304 | }); |
| 305 | } |
| 306 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 307 | /** Get the NSD Content List & patch value in edit form @public */ |
| 308 | public getNSConfigDetails(URL: string, name: {}[]): void { |
| 309 | this.restService.getResource(URL).subscribe((content: NSConfigData): void => { |
| 310 | this.nsConfigData.push(content); |
| 311 | this.details = this.nsConfigData[0]; |
| 312 | const nsId: string = 'nsId'; |
| 313 | // eslint-disable-next-line security/detect-object-injection |
| 314 | const nsdId: {}[] = name.filter((nsdData: {}[]): boolean => nsdData[nsId] === this.details.nsdId); |
| 315 | const nsName: string = 'nsName'; |
| 316 | for (const data of nsdId) { |
| 317 | // eslint-disable-next-line security/detect-object-injection |
| 318 | this.nsdName = data[nsName]; |
| 319 | } |
| 320 | if (!isNullOrUndefined(this.details.config)) { |
| 321 | this.config = jsyaml.dump(this.details.config); |
| 322 | } |
| 323 | this.packagesForm.patchValue({ name: this.details.name, nsdId: this.nsdName, config: this.config }); |
| 324 | this.isLoadingResults = false; |
| 325 | }, (error: ERRORDATA): void => { |
| 326 | this.restService.handleError(error, 'get'); |
| 327 | this.isLoadingResults = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 328 | }); |
| 329 | } |
| 330 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 331 | /** Create packages @public */ |
| 332 | public createPackages(): void { |
| 333 | this.submitted = true; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 334 | this.modalData = { |
| 335 | message: 'Done' |
| 336 | }; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 337 | this.addNullValueForInvalidFiles(); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 338 | this.sharedService.cleanForm(this.packagesForm); |
| 339 | if (!this.packagesForm.invalid) { |
| 340 | this.isLoadingResults = true; |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 341 | if (this.params.page === 'ns-package' || this.params.page === 'vnf-package') { |
| 342 | if (this.params.page === 'ns-package') { |
| 343 | this.endPoint = environment.NSDESCRIPTORSCONTENT_URL; |
| 344 | } else if (this.params.page === 'vnf-package') { |
| 345 | this.endPoint = environment.VNFPACKAGESCONTENT_URL; |
| 346 | } |
| 347 | const descriptor: string = this.packageYaml(this.params.page); |
| 348 | try { |
| 349 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 350 | const tar: any = new Tar(); |
| 351 | const out: Uint8Array = tar.append(this.packagesForm.value.name + '/' + this.packagesForm.value.name + '.yaml', |
| 352 | descriptor, { type: '0' }); |
| 353 | const gzipContent: Uint8Array = pako.gzip(out); |
| 354 | this.createPackageApi(gzipContent.buffer); |
| 355 | } catch (e) { |
| 356 | this.isLoadingResults = false; |
| 357 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 358 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 359 | } else if (this.oka) { |
| 360 | try { |
| 361 | this.headers = new HttpHeaders({ |
| 362 | Accept: 'application/json', |
| 363 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 364 | }); |
| 365 | if (this.params.page === 'oka-packages') { |
| 366 | const apiURLHeader: APIURLHEADER = { |
| 367 | url: environment.OKAPACKAGES_URL, |
| 368 | httpOptions: { headers: this.headers } |
| 369 | }; |
| 370 | this.saveFileData(apiURLHeader); |
| 371 | } else { |
| 372 | const apiURLHeader: APIURLHEADER = { |
| 373 | url: environment.OKAPACKAGES_URL + '/' + this.params.id, |
| 374 | httpOptions: { headers: this.headers } |
| 375 | }; |
| 376 | this.editFileData(apiURLHeader); |
| 377 | } |
| 378 | } catch (e) { |
| 379 | this.isLoadingResults = false; |
| 380 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 381 | } |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 382 | } else { |
| 383 | try { |
| 384 | this.headers = new HttpHeaders({ |
| 385 | Accept: 'application/json', |
| 386 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 387 | }); |
| 388 | if (this.params.page === 'ns-config-template') { |
| 389 | this.endPoint = environment.NSCONFIGTEMPLATE_URL; |
| 390 | this.createTemplate(this.endPoint); |
| 391 | } else if (this.params.page === 'ns-config-template-edit') { |
| 392 | this.endPoint = environment.NSCONFIGTEMPLATE_URL + '/' + this.params.id + '/' + 'template_content'; |
| 393 | this.editTemplate(this.endPoint); |
| 394 | } |
| 395 | } catch (e) { |
| 396 | this.isLoadingResults = false; |
| 397 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 398 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 402 | |
| 403 | /** Post config template @public */ |
| 404 | public createTemplate(urlHeader: string): void { |
| 405 | this.isLoadingResults = true; |
| 406 | const apiURLHeader: APIURLHEADER = { |
| 407 | url: urlHeader, |
| 408 | httpOptions: { headers: this.headers } |
| 409 | }; |
| 410 | if (isNullOrUndefined(this.packagesForm.value.config) || this.packagesForm.value.config === '') { |
| 411 | delete this.packagesForm.value.config; |
| 412 | } else { |
| 413 | const validJSON: boolean = this.sharedService.checkJson(this.packagesForm.value.config); |
| 414 | if (validJSON) { |
| 415 | this.packagesForm.value.config = JSON.parse(this.packagesForm.value.config); |
| 416 | } else { |
| 417 | const getConfigJson: string = jsyaml.load(this.packagesForm.value.config, { json: true }); |
| 418 | this.packagesForm.value.config = getConfigJson; |
| 419 | } |
| 420 | } |
| 421 | const nsName: string = 'nsName'; |
| 422 | // eslint-disable-next-line security/detect-object-injection |
| 423 | const nsdId: {}[] = this.nsdDetails.filter((nsdData: {}[]): boolean => nsdData[nsName] === this.packagesForm.value.nsdId); |
| 424 | for (const data of nsdId) { |
| 425 | // eslint-disable-next-line @typescript-eslint/dot-notation |
| 426 | this.nsId = data['nsId']; |
| 427 | } |
| 428 | this.packagesForm.value.nsdId = this.nsId; |
| 429 | this.restService.postResource(apiURLHeader, (this.packagesForm.value)).subscribe((result: {}): void => { |
| 430 | this.activeModal.close(this.modalData); |
| 431 | this.isLoadingResults = false; |
| 432 | this.notifierService.notify('success', this.translateService.instant('PAGE.NSCONFIGTEMPLATE.TEMPLATECREATEDSUCCESSFULLY')); |
| 433 | }, (error: ERRORDATA): void => { |
| 434 | this.restService.handleError(error, 'post'); |
| 435 | this.isLoadingResults = false; |
| 436 | }); |
| 437 | } |
| 438 | |
| 439 | /** Edit config template @public */ |
| 440 | public editTemplate(urlHeader: string): void { |
| 441 | this.isLoadingResults = true; |
| 442 | const apiURLHeader: APIURLHEADER = { |
| 443 | url: urlHeader, |
| 444 | httpOptions: { headers: this.headers } |
| 445 | }; |
| 446 | if (isNullOrUndefined(this.packagesForm.value.config) || this.packagesForm.value.config === '') { |
| 447 | delete this.packagesForm.value.config; |
| 448 | } else { |
| 449 | const validJSON: boolean = this.sharedService.checkJson(this.packagesForm.value.config); |
| 450 | if (validJSON) { |
| 451 | this.packagesForm.value.config = JSON.parse(this.packagesForm.value.config); |
| 452 | } else { |
| 453 | const getConfigJson: string = jsyaml.load(this.packagesForm.value.config, { json: true }); |
| 454 | this.packagesForm.value.config = getConfigJson; |
| 455 | } |
| 456 | } |
| 457 | this.restService.putResource(apiURLHeader, (this.packagesForm.value)).subscribe((result: {}): void => { |
| 458 | this.activeModal.close(this.modalData); |
| 459 | this.isLoadingResults = false; |
| 460 | this.notifierService.notify('success', this.translateService.instant('PAGE.NSCONFIGTEMPLATE.TEMPLATEEDITEDSUCCESSFULLY')); |
| 461 | }, (error: ERRORDATA): void => { |
| 462 | this.restService.handleError(error, 'post'); |
| 463 | this.isLoadingResults = false; |
| 464 | }); |
| 465 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 466 | /** Create packages @public */ |
| 467 | private createPackageApi(packageContent: ArrayBuffer | SharedArrayBuffer): void { |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 468 | this.headers = new HttpHeaders({ |
| 469 | 'Content-Type': 'application/gzip', |
| 470 | Accept: 'application/json', |
| 471 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 472 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 473 | const apiURLHeader: APIURLHEADER = { |
| 474 | url: this.endPoint, |
| 475 | httpOptions: { headers: this.headers } |
| 476 | }; |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 477 | this.restService.postResource(apiURLHeader, packageContent).subscribe((result: { id: string }): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 478 | this.isLoadingResults = false; |
| 479 | this.activeModal.close(); |
| 480 | this.composeNSPackages(result.id); |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 481 | }, (error: ERRORDATA): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 482 | this.isLoadingResults = false; |
| 483 | this.restService.handleError(error, 'post'); |
| 484 | }); |
| 485 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 486 | |
| 487 | /** Drag and drop feature and fetchind the details of files @public */ |
| 488 | public onFileDropped(files: FileList): void { |
| 489 | if (files && files.length === 1) { |
| 490 | const file: File = files[0]; |
| 491 | if (this.sharedService.vaildataFileInfo(file, 'gz')) { |
| 492 | this.removeValidationDragDropFiles(); |
| 493 | this.okafile = file; |
| 494 | this.selectedFileName = ''; |
| 495 | if (this.params.page === 'oka-packages-edit') { |
| 496 | this.selectedFileName = files[0].name; |
| 497 | } else { |
| 498 | this.fileInputLabel.nativeElement.innerText = files[0].name; |
| 499 | this.fileInput.nativeElement.value = null; |
| 500 | } |
| 501 | } else { |
| 502 | this.notifierService.notify('error', this.translateService.instant('GZFILETYPEERRROR')); |
| 503 | this.addNullValueForInvalidFiles(); |
| 504 | } |
| 505 | } else if (files && files.length > 1) { |
| 506 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /** Add Null value for invalid Files @public */ |
| 511 | public addNullValueForInvalidFiles(): void { |
| 512 | this.getFormControl('package').setValue(''); |
| 513 | } |
| 514 | |
| 515 | /** Remove validation after drag drop Files @public */ |
| 516 | public removeValidationDragDropFiles(): void { |
| 517 | this.getFormControl('package').setValidators([]); |
| 518 | this.getFormControl('package').updateValueAndValidity(); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /** Post the droped files and reload the page @public */ |
| 523 | public saveFileData(urlHeader: APIURLHEADER): void { |
| 524 | this.isLoadingResults = true; |
| 525 | this.packagesForm.value.package = this.okafile; |
| 526 | this.restService.postResource(urlHeader, this.toFormData(this.packagesForm.value)).subscribe((result: {}): void => { |
| 527 | this.activeModal.close(this.modalData); |
| 528 | this.isLoadingResults = false; |
| 529 | this.notifierService.notify('success', this.translateService.instant('OKA Package Created Successfully')); |
| 530 | }, (error: ERRORDATA): void => { |
| 531 | this.restService.handleError(error, 'post'); |
| 532 | this.isLoadingResults = false; |
| 533 | }); |
| 534 | } |
| 535 | |
| 536 | /** Post the droped files and reload the page @public */ |
| 537 | public editFileData(urlHeader: APIURLHEADER): void { |
| 538 | this.isLoadingResults = true; |
| 539 | this.restService.patchResource(urlHeader, this.toFormData(this.packagesForm.value)).subscribe((result: {}): void => { |
| 540 | this.activeModal.close(this.modalData); |
| 541 | this.isLoadingResults = false; |
| 542 | this.notifierService.notify('success', this.translateService.instant('OKA Package Edited Successfully')); |
| 543 | }, (error: ERRORDATA): void => { |
| 544 | this.restService.handleError(error, 'post'); |
| 545 | this.isLoadingResults = false; |
| 546 | }); |
| 547 | } |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 548 | /** Config file process @private */ |
| 549 | public configFile(files: FileList): void { |
| 550 | if (files && files.length === 1) { |
| 551 | const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase(); |
| 552 | if (fileFormat === 'yaml' || fileFormat === 'yml') { |
| 553 | this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { |
| 554 | this.packagesForm.get('config').setValue(fileContent); |
| 555 | }).catch((err: string): void => { |
| 556 | if (err === 'typeError') { |
| 557 | this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); |
| 558 | } else { |
| 559 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 560 | } |
| 561 | this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 562 | this.fileInputConfig.nativeElement.value = null; |
| 563 | }); |
| 564 | } else if (fileFormat === 'json') { |
| 565 | this.sharedService.getFileString(files, 'json').then((fileContent: string): void => { |
| 566 | const getConfigJson: string = jsyaml.load(fileContent, { json: true }); |
| 567 | this.packagesForm.get('config').setValue(JSON.stringify(getConfigJson)); |
| 568 | }).catch((err: string): void => { |
| 569 | if (err === 'typeError') { |
| 570 | this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR')); |
| 571 | } else { |
| 572 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 573 | } |
| 574 | this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 575 | this.fileInputConfig.nativeElement.value = null; |
| 576 | }); |
| 577 | } |
| 578 | } else if (files && files.length > 1) { |
| 579 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 580 | } |
| 581 | this.fileInputConfigLabel.nativeElement.innerText = files[0].name; |
| 582 | this.fileInputConfig.nativeElement.value = null; |
| 583 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 584 | /** Compose NS Packages @private */ |
| 585 | private composeNSPackages(id: string): void { |
| 586 | let packageUrl: string; |
| 587 | if (this.params.page === 'ns-package') { |
| 588 | packageUrl = '/packages/ns/compose/'; |
| 589 | this.notifierService.notify('success', this.packagesForm.value.name + ' ' + |
| 590 | this.translateService.instant('PAGE.NSPACKAGE.CREATEDSUCCESSFULLY')); |
| 591 | } else if (this.params.page === 'vnf-package') { |
| 592 | packageUrl = '/packages/vnf/compose/'; |
| 593 | this.notifierService.notify('success', this.packagesForm.value.name + ' ' + |
| 594 | this.translateService.instant('PAGE.VNFPACKAGE.CREATEDSUCCESSFULLY')); |
| 595 | } |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 596 | this.router.navigate([packageUrl, id]).catch((): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 597 | // Catch Navigation Error |
| 598 | }); |
| 599 | } |
| 600 | /** Deafult template for NS and VNF Packages @private */ |
| 601 | private packageYaml(descriptorType: string): string { |
| 602 | let packageYaml: {} = {}; |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 603 | const composerName: string = 'NGUI Composer'; |
| 604 | const composerDefaultVersion: string = '1.0'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 605 | if (descriptorType === 'ns-package') { |
| 606 | packageYaml = { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 607 | nsd: { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 608 | nsd: [ |
| 609 | { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 610 | id: this.packagesForm.value.name, |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 611 | name: this.packagesForm.value.name, |
| 612 | version: composerDefaultVersion, |
| 613 | description: this.packagesForm.value.name + ' descriptor', |
| 614 | designer: composerName, |
| 615 | df: [ |
| 616 | { |
| 617 | id: 'default-df', |
| 618 | 'vnf-profile': [] |
| 619 | } |
| 620 | ] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 621 | } |
| 622 | ] |
| 623 | } |
| 624 | }; |
| 625 | } else { |
| 626 | packageYaml = { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 627 | vnfd: { |
| 628 | id: this.packagesForm.value.name, |
| 629 | 'product-name': this.packagesForm.value.name, |
| 630 | version: composerDefaultVersion, |
| 631 | description: this.packagesForm.value.name + ' descriptor', |
| 632 | provider: composerName, |
| 633 | df: [ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 634 | { |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 635 | id: 'default-df', |
| 636 | 'instantiation-level': [], |
| 637 | 'vdu-profile': [] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 638 | } |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 639 | ], |
| 640 | 'ext-cpd': [], |
| 641 | vdu: [], |
| 642 | 'sw-image-desc': [], |
| 643 | 'virtual-storage-desc': [] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 644 | } |
| 645 | }; |
| 646 | } |
| Barath Kumar R | 86e497d | 2021-05-04 17:16:14 +0530 | [diff] [blame] | 647 | return jsyaml.dump(packageYaml, { sortKeys: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 648 | } |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 649 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 650 | /** Form data @private */ |
| 651 | private toFormData<T>(formValue: T): FormData { |
| 652 | const formData: FormData = new FormData(); |
| 653 | for (const key of Object.keys(formValue)) { |
| 654 | // eslint-disable-next-line security/detect-object-injection |
| 655 | const value: string = formValue[key]; |
| 656 | if (key === 'name') { |
| 657 | if (this.params.page === 'oka-packages') { |
| 658 | if (!isNullOrUndefined(this.packagesForm.value.name)) { |
| 659 | formData.append(key, this.packagesForm.value.name); |
| 660 | } |
| 661 | } else if (this.params.page === 'oka-packages-edit') { |
| 662 | if (this.package_name.localeCompare(this.packagesForm.value.name) === 0) { |
| 663 | delete this.packagesForm.value.name; |
| 664 | } else { |
| 665 | formData.append(key, this.packagesForm.value.name); |
| 666 | } |
| 667 | } |
| 668 | else { |
| 669 | formData.append(key, ''); |
| 670 | } |
| 671 | } else if (key === 'description') { |
| 672 | if (this.params.page === 'oka-packages') { |
| 673 | if (!isNullOrUndefined(this.packagesForm.value.description)) { |
| 674 | formData.append(key, this.packagesForm.value.description); |
| 675 | } |
| 676 | } else if (this.params.page === 'oka-packages-edit') { |
| 677 | if (this.packageData.description.localeCompare(this.packagesForm.value.description) === 0) { |
| 678 | delete this.packagesForm.value.description; |
| 679 | } else { |
| 680 | formData.append(key, this.packagesForm.value.description); |
| 681 | } |
| 682 | } |
| 683 | else { |
| 684 | formData.append(key, ''); |
| 685 | } |
| 686 | } else if (key === 'package') { |
| 687 | if (!isNullOrUndefined(this.okafile)) { |
| 688 | formData.append(key, this.okafile); |
| 689 | } else { |
| 690 | delete this.packagesForm.value.package; |
| 691 | } |
| 692 | } else { |
| 693 | formData.append(key, value); |
| 694 | } |
| 695 | } |
| 696 | return formData; |
| 697 | } |
| 698 | |
| SANDHYA.JS | 07decc0 | 2024-07-01 21:50:48 +0530 | [diff] [blame] | 699 | /** Used to get the AbstractControl of controlName passed @private */ |
| 700 | private getFormControl(controlName: string): AbstractControl { |
| 701 | // eslint-disable-next-line security/detect-object-injection |
| 702 | return this.packagesForm.controls[controlName]; |
| 703 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 704 | } |