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