From d22b09492b0dceeefe49eb67ef1675f2ac4f0cd8 Mon Sep 17 00:00:00 2001 From: Barath Kumar R Date: Tue, 14 Jul 2020 11:05:24 +0530 Subject: [PATCH] NG-UI BUG 1118 YAML not supported in config text-area when launching NS * Fixed the textarea that can accept the yaml/yml/json values. Change-Id: I019d4fec3ea2584b72f5d8a539226bc0abff4cf7 Signed-off-by: Barath Kumar R --- .../InstantiateNetSliceTemplateComponent.ts | 51 +++++++++++++------ .../instantiate-ns/InstantiateNsComponent.ts | 49 ++++++++++++------ src/services/SharedService.ts | 5 ++ 3 files changed, 74 insertions(+), 31 deletions(-) diff --git a/src/app/packages/instantiate-net-slice-template/InstantiateNetSliceTemplateComponent.ts b/src/app/packages/instantiate-net-slice-template/InstantiateNetSliceTemplateComponent.ts index ed5e414..330edf8 100644 --- a/src/app/packages/instantiate-net-slice-template/InstantiateNetSliceTemplateComponent.ts +++ b/src/app/packages/instantiate-net-slice-template/InstantiateNetSliceTemplateComponent.ts @@ -207,8 +207,11 @@ export class InstantiateNetSliceTemplateComponent implements OnInit { }); delete this.netSliceInstantiateForm.value.config; } else { - this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); - return; + const getConfigJson: string = jsyaml.load(this.netSliceInstantiateForm.value.config, { json: true }); + Object.keys(getConfigJson).forEach((item: string) => { + this.netSliceInstantiateForm.value[item] = getConfigJson[item]; + }); + delete this.netSliceInstantiateForm.value.config; } } this.isLoadingResults = true; @@ -259,24 +262,40 @@ export class InstantiateNetSliceTemplateComponent implements OnInit { /** Config file process @private */ public configFile(files: FileList): void { if (files && files.length === 1) { - this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { - const getConfigJson: string = jsyaml.load(fileContent, { json: true }); - // tslint:disable-next-line: no-backbone-get-set-outside-model - this.netSliceInstantiateForm.get('config').setValue(JSON.stringify(getConfigJson)); - }).catch((err: string): void => { - if (err === 'typeError') { - this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); - } else { - this.notifierService.notify('error', this.translateService.instant('ERROR')); - } - this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); - this.fileInputConfig.nativeElement.value = null; - }); + const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase(); + if (fileFormat === 'yaml' || fileFormat === 'yml') { + this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { + const getConfigJson: string = jsyaml.load(fileContent, { json: true }); + // tslint:disable-next-line: no-backbone-get-set-outside-model + this.netSliceInstantiateForm.get('config').setValue(JSON.stringify(getConfigJson)); + }).catch((err: string): void => { + if (err === 'typeError') { + this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); + } else { + this.notifierService.notify('error', this.translateService.instant('ERROR')); + } + this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); + this.fileInputConfig.nativeElement.value = null; + }); + } else if (fileFormat === 'json') { + this.sharedService.getFileString(files, 'json').then((fileContent: string): void => { + const getConfigJson: string = jsyaml.load(fileContent, { json: true }); + // tslint:disable-next-line: no-backbone-get-set-outside-model + this.netSliceInstantiateForm.get('config').setValue(JSON.stringify(getConfigJson)); + }).catch((err: string): void => { + if (err === 'typeError') { + this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR')); + } else { + this.notifierService.notify('error', this.translateService.instant('ERROR')); + } + this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); + this.fileInputConfig.nativeElement.value = null; + }); + } } else if (files && files.length > 1) { this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); } this.fileInputConfigLabel.nativeElement.innerText = files[0].name; this.fileInputConfig.nativeElement.value = null; } - } diff --git a/src/app/packages/instantiate-ns/InstantiateNsComponent.ts b/src/app/packages/instantiate-ns/InstantiateNsComponent.ts index 515a24b..74569f1 100644 --- a/src/app/packages/instantiate-ns/InstantiateNsComponent.ts +++ b/src/app/packages/instantiate-ns/InstantiateNsComponent.ts @@ -197,8 +197,11 @@ export class InstantiateNsComponent implements OnInit { }); delete this.instantiateForm.value.config; } else { - this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); - return; + const getConfigJson: string = jsyaml.load(this.instantiateForm.value.config, { json: true }); + Object.keys(getConfigJson).forEach((item: string) => { + this.instantiateForm.value[item] = getConfigJson[item]; + }); + delete this.instantiateForm.value.config; } } const apiURLHeader: APIURLHEADER = { @@ -246,19 +249,35 @@ export class InstantiateNsComponent implements OnInit { /** Config file process @private */ public configFile(files: FileList): void { if (files && files.length === 1) { - this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { - const getConfigJson: string = jsyaml.load(fileContent, { json: true }); - // tslint:disable-next-line: no-backbone-get-set-outside-model - this.instantiateForm.get('config').setValue(JSON.stringify(getConfigJson)); - }).catch((err: string): void => { - if (err === 'typeError') { - this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); - } else { - this.notifierService.notify('error', this.translateService.instant('ERROR')); - } - this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); - this.fileInputConfig.nativeElement.value = null; - }); + const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase(); + if (fileFormat === 'yaml' || fileFormat === 'yml') { + this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { + // tslint:disable-next-line: no-backbone-get-set-outside-model + this.instantiateForm.get('config').setValue(fileContent); + }).catch((err: string): void => { + if (err === 'typeError') { + this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); + } else { + this.notifierService.notify('error', this.translateService.instant('ERROR')); + } + this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); + this.fileInputConfig.nativeElement.value = null; + }); + } else if (fileFormat === 'json') { + this.sharedService.getFileString(files, 'json').then((fileContent: string): void => { + const getConfigJson: string = jsyaml.load(fileContent, { json: true }); + // tslint:disable-next-line: no-backbone-get-set-outside-model + this.instantiateForm.get('config').setValue(JSON.stringify(getConfigJson)); + }).catch((err: string): void => { + if (err === 'typeError') { + this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR')); + } else { + this.notifierService.notify('error', this.translateService.instant('ERROR')); + } + this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); + this.fileInputConfig.nativeElement.value = null; + }); + } } else if (files && files.length > 1) { this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); } diff --git a/src/services/SharedService.ts b/src/services/SharedService.ts index d35e41d..f2f730e 100644 --- a/src/services/SharedService.ts +++ b/src/services/SharedService.ts @@ -304,6 +304,11 @@ export class SharedService { return tag; } + /** Fetch file extension @public */ + public fetchFileExtension(fileInfo: FileList): string { + return fileInfo[0].name.substring(fileInfo[0].name.lastIndexOf('.') + 1); + } + /** Method to validate file extension and size @private */ private vaildataFileInfo(fileInfo: File, fileType: string): boolean { const extension: string = fileInfo.name.substring(fileInfo.name.lastIndexOf('.') + 1); -- 2.17.1