From 363d9a6fea48bf6f34e4f1d4ecb37d33812c7626 Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Thu, 2 Jun 2022 10:46:40 +0100 Subject: Service Import - Read metadata from csar Also exports metadata to the tosca template and fixes service metadata not being set when the create button is clicked Issue-ID: SDC-4044 Signed-off-by: JvD_Ericsson Change-Id: I6f8428d9ed385335c05e7b994e622229ea00342b --- catalog-ui/src/app/utils/file-utils.ts | 18 +++ catalog-ui/src/app/utils/service-csar-reader.ts | 139 ++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 catalog-ui/src/app/utils/service-csar-reader.ts (limited to 'catalog-ui/src/app/utils') diff --git a/catalog-ui/src/app/utils/file-utils.ts b/catalog-ui/src/app/utils/file-utils.ts index d726736bd4..4b6f330354 100644 --- a/catalog-ui/src/app/utils/file-utils.ts +++ b/catalog-ui/src/app/utils/file-utils.ts @@ -66,6 +66,24 @@ export class FileUtils { "cancelable": true }); downloadLink.dispatchEvent(clickEvent); + } + public getEntryDefinitionFileNameFromCsarBlob = (csarBlob:Blob):Promise => { + let JSZip = require("jszip"); + return JSZip.loadAsync(csarBlob).then(zip => { + return zip.file("TOSCA-Metadata/TOSCA.meta").async("string"); + }).then((toscaMetaData: string) => { + let fileEntities:Array = toscaMetaData.replace("\r", "").split("\n"); + let entryDefinitionFilename:string = fileEntities.find(element => !element.search("Entry-Definitions")) + .replace("Entry-Definitions:", "").trim(); + return entryDefinitionFilename; + }); + } + + public getFileNameDataFromCsarBlob = (csarBlob:Blob, fileName:string):Promise => { + let JSZip = require("jszip"); + return JSZip.loadAsync(csarBlob).then(zip => { + return zip.file(fileName).async("string"); + }); } } diff --git a/catalog-ui/src/app/utils/service-csar-reader.ts b/catalog-ui/src/app/utils/service-csar-reader.ts new file mode 100644 index 0000000000..73b77559e2 --- /dev/null +++ b/catalog-ui/src/app/utils/service-csar-reader.ts @@ -0,0 +1,139 @@ +/* + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2022 Nordix Foundation. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +import {ServiceCsar, ToscaMetaEntry} from "../models"; +import {load} from 'js-yaml'; +import { ComponentType } from "./constants"; + +export class ServiceCsarReader { + + private serviceCsar = new ServiceCsar(); + + public read(serviceCsarBlob:Blob): Promise { + const jsZip = require("jszip"); + return new Promise((resolve) => { + jsZip.loadAsync(serviceCsarBlob).then(async zip => { + const toscaMetaFileContent = await zip.file("TOSCA-Metadata/TOSCA.meta").async("string"); + this.readToscaMeta(toscaMetaFileContent); + const entryDefinitionFileContent = await zip.file(this.serviceCsar.entryDefinitionFileName).async("string"); + this.readServiceMetadata(entryDefinitionFileContent); + const interfaceDefinitionFileContent = await zip.file(this.serviceCsar.interfaceDefinitionFileName).async("string"); + this.readServiceSubstitutionNode(interfaceDefinitionFileContent); + resolve(this.serviceCsar); + }); + }); + } + + private readToscaMeta(toscaMetaFileContent:string) { + let fileEntities:Array = toscaMetaFileContent.replace("\r", "").split("\n"); + for(let entity of fileEntities.filter(e => e)) { + let mapEntry:Array = entity.split(":"); + let key:string = mapEntry[0].trim(); + let value:string = mapEntry[1].trim(); + this.serviceCsar.toscaMeta.dataMap.set(key, value); + } + this.readEntryDefinitionFileName(); + this.readInterfaceDefinitionFileName(); + } + + private readEntryDefinitionFileName() { + this.serviceCsar.entryDefinitionFileName = this.serviceCsar.toscaMeta.getEntry(ToscaMetaEntry.ENTRY_DEFINITIONS); + } + + private readInterfaceDefinitionFileName() { + let fileNameArray:Array = this.serviceCsar.entryDefinitionFileName.split("."); + fileNameArray.splice(fileNameArray.length - 1, 0, "-interface."); + this.serviceCsar.interfaceDefinitionFileName = fileNameArray.join(""); + } + + private readServiceMetadata(entryDefinitionFileContent) { + const metadata = load(entryDefinitionFileContent).metadata; + this.setMetadata(metadata); + } + + private readServiceSubstitutionNode(interfaceDefinitionFileContent) { + const nodeTypes = load(interfaceDefinitionFileContent).node_types; + let nodeType = Object.keys(nodeTypes).values().next().value; + this.serviceCsar.substitutionNodeType = nodeTypes[nodeType]["derived_from"]; + } + + private setMetadata = (metadata:object) : void => { + let extraServiceMetadata: Map = new Map(); + this.serviceCsar.serviceMetadata.componentType = ComponentType.SERVICE; + this.serviceCsar.serviceMetadata.serviceType = "Service"; + Object.keys(metadata).forEach(variable => { + switch(variable) { + case "description": { + this.serviceCsar.serviceMetadata.description = metadata[variable]; + break; + } + case "name": { + this.serviceCsar.serviceMetadata.name = metadata[variable]; + break; + } + case "model": { + this.serviceCsar.serviceMetadata.model = metadata[variable]; + break; + } + case "category": { + this.serviceCsar.serviceMetadata.selectedCategory = metadata[variable]; + break; + } + case "serviceRole": { + this.serviceCsar.serviceMetadata.serviceRole = metadata[variable]; + break; + } + case "serviceFunction": { + this.serviceCsar.serviceMetadata.serviceFunction = metadata[variable]; + break; + } + case "environmentContext": { + if (metadata[variable] != null) { + this.serviceCsar.serviceMetadata.environmentContext = metadata[variable]; + } + break; + } + case "instantiationType": { + if (metadata[variable] != null) { + this.serviceCsar.serviceMetadata.instantiationType = metadata[variable]; + } + break; + } + case "ecompGeneratedNaming": { + if (metadata[variable] != null) { + this.serviceCsar.serviceMetadata.ecompGeneratedNaming = metadata[variable] == "false" ? false : true; + } + break; + } + case "namingPolicy": { + if (metadata["ecompGeneratedNaming"] != "false") { + this.serviceCsar.serviceMetadata.namingPolicy = metadata[variable]; + } + break; + } + default: { + extraServiceMetadata.set(variable, metadata[variable]) + break; + } + } + }); + this.serviceCsar.extraServiceMetadata = extraServiceMetadata; + } +} \ No newline at end of file -- cgit 1.2.3-korg