From 1a64c83fc436e17f93e468f7d46ff9f5fcb081ce Mon Sep 17 00:00:00 2001 From: vasraz Date: Fri, 7 Feb 2020 10:14:06 +0000 Subject: Add support for simple yaml profile 1.2 Change-Id: I735d25c6b6c3344c4b742f09b3aeaf4d03c2d17c Signed-off-by: Vasyl Razinkov Issue-ID: SDC-2738 --- .../be/components/impl/ResourceImportManager.java | 19 +++++++++++++-- .../be/servlets/AbstractValidationsServlet.java | 8 ++++++- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 27 ++++++++++++++-------- 3 files changed, 41 insertions(+), 13 deletions(-) (limited to 'catalog-be/src/main') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index dfd7c6c58d..7e26cb329f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -23,6 +23,7 @@ package org.openecomp.sdc.be.components.impl; import fj.data.Either; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.auditing.api.AuditEventFactory; @@ -752,13 +753,27 @@ public class ResourceImportManager { } - private void setMetaDataFromJson(UploadResourceInfo resourceMetaData, Resource resource) { + private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) { this.populateResourceMetadata(resourceMetaData, resource); resource.setCreatorUserId(resourceMetaData.getContactId()); - List categories = resourceMetaData.getCategories(); + + final String payloadData = resourceMetaData.getPayloadData(); + if (payloadData != null) { + resource.setToscaVersion(getToscaVersion(payloadData)); + } + + final List categories = resourceMetaData.getCategories(); calculateResourceIsAbstract(resource, categories); } + private String getToscaVersion(final String payloadData) { + final String decodedPayload = new String(Base64.decodeBase64(payloadData)); + final Map mappedToscaTemplate = (Map) new Yaml().load(decodedPayload); + final Either findFirstToscaStringElement = + ImportUtils.findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); + return findFirstToscaStringElement.left().value(); + } + private void calculateResourceIsAbstract(Resource resource, List categories) { if (categories != null && !categories.isEmpty()) { CategoryDefinition categoryDef = categories.get(0); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java index 580fb737ec..fb1290310d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AbstractValidationsServlet.java @@ -87,7 +87,13 @@ public abstract class AbstractValidationsServlet extends BeGenericServlet { private static final Logger log = Logger.getLogger(AbstractValidationsServlet.class); private static final String TOSCA_SIMPLE_YAML_PREFIX = "tosca_simple_yaml_"; - private static final List TOSCA_DEFINITION_VERSIONS = Arrays.asList(TOSCA_SIMPLE_YAML_PREFIX + "1_0_0", TOSCA_SIMPLE_YAML_PREFIX + "1_1_0", "tosca_simple_profile_for_nfv_1_0_0", TOSCA_SIMPLE_YAML_PREFIX + "1_0", TOSCA_SIMPLE_YAML_PREFIX + "1_1"); + private static final List TOSCA_DEFINITION_VERSIONS = Arrays.asList( + TOSCA_SIMPLE_YAML_PREFIX + "1_0_0", + TOSCA_SIMPLE_YAML_PREFIX + "1_1_0", + "tosca_simple_profile_for_nfv_1_0_0", + TOSCA_SIMPLE_YAML_PREFIX + "1_0", + TOSCA_SIMPLE_YAML_PREFIX + "1_1", + TOSCA_SIMPLE_YAML_PREFIX + "1_2"); private static final List TOSCA_YML_CSAR_VALID_SUFFIX = Arrays.asList(".yml", ".yaml", ".csar"); protected ServletUtils servletUtils; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 64afee7904..1ca087d38a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -181,18 +181,22 @@ public class ToscaExportHandler { return Either.left(toscaRepresentation); } - public Either exportComponentInterface(Component component, - boolean isAssociatedComponent) { + public Either exportComponentInterface(final Component component, + final boolean isAssociatedComponent) { if (null == DEFAULT_IMPORTS) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); } - ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION); + String toscaVersion = null; + if (component instanceof Resource) { + toscaVersion = ((Resource) component).getToscaVersion(); + } + ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION); toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS)); - Map nodeTypes = new HashMap<>(); - Either toscaTemplateRes = convertInterfaceNodeType(new HashMap<>(), component, - toscaTemplate, nodeTypes, isAssociatedComponent); + final Map nodeTypes = new HashMap<>(); + final Either toscaTemplateRes = + convertInterfaceNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes, isAssociatedComponent); if (toscaTemplateRes.isRight()) { return Either.right(toscaTemplateRes.right().value()); } @@ -240,18 +244,21 @@ public class ToscaExportHandler { return Either.left(fillImports.left().value().left); } - private Either convertToToscaTemplate(Component component) { + private Either convertToToscaTemplate(final Component component) { if (null == DEFAULT_IMPORTS) { log.debug(FAILED_TO_GET_DEFAULT_IMPORTS_CONFIGURATION); return Either.right(ToscaError.GENERAL_ERROR); } log.trace("start tosca export for {}", component.getUniqueId()); - ToscaTemplate toscaTemplate = new ToscaTemplate(TOSCA_VERSION); - + String toscaVersion = null; + if (component instanceof Resource) { + toscaVersion = ((Resource) component).getToscaVersion(); + } + final ToscaTemplate toscaTemplate = new ToscaTemplate(toscaVersion != null ? toscaVersion : TOSCA_VERSION); toscaTemplate.setMetadata(convertMetadata(component)); toscaTemplate.setImports(new ArrayList<>(DEFAULT_IMPORTS)); - Map nodeTypes = new HashMap<>(); + final Map nodeTypes = new HashMap<>(); if (ModelConverter.isAtomicComponent(component)) { log.trace("convert component as node type"); return convertNodeType(new HashMap<>(), component, toscaTemplate, nodeTypes); -- cgit 1.2.3-korg