From 0872b963f2b2c0db594cea153efd09ad43c48888 Mon Sep 17 00:00:00 2001 From: talio Date: Wed, 15 Nov 2017 16:18:32 +0200 Subject: import tosca added exception when recognizing an invalid substitution mappings section inside imported csar Issue-Id : SDC-661 Change-Id: Id608b066d5e440ff5462ec3ba5742d3cff50bf5a Signed-off-by: talio --- .../openecomp/core/impl/ToscaConverterImpl.java | 49 +++++++++++++++++++--- .../services/ServiceTemplateReaderServiceImpl.java | 4 -- 2 files changed, 43 insertions(+), 10 deletions(-) (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core') diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java index 9b694c5207..685f39c3cb 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java @@ -1,10 +1,12 @@ package org.openecomp.core.impl; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; import org.openecomp.core.converter.ServiceTemplateReaderService; import org.openecomp.core.converter.ToscaConverter; import org.openecomp.core.converter.datatypes.Constants; import org.openecomp.core.converter.datatypes.CsarFileTypes; +import org.openecomp.core.converter.errors.SubstitutionMappingsConverterErrorBuilder; import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.json.JsonUtil; @@ -326,16 +328,51 @@ public class ToscaConverterImpl implements ToscaConverter { SubstitutionMapping substitutionMapping = new SubstitutionMapping(); substitutionMapping.setNode_type((String) substitutionMappings.get(nodeType)); - substitutionMapping.setCapabilities( - convertSubstitutionMappingsSections((Map) substitutionMappings.get(capabilities))); - substitutionMapping.setRequirements( - convertSubstitutionMappingsSections((Map) substitutionMappings.get(requirements))); + setSubstitutionMappingsSection( + capabilities, substitutionMapping, + convertSubstitutionMappingsSections(capabilities, substitutionMappings.get(capabilities))); + setSubstitutionMappingsSection( + requirements, substitutionMapping, + convertSubstitutionMappingsSections(requirements, substitutionMappings.get(requirements))); return substitutionMapping; } - private Map> convertSubstitutionMappingsSections( - Map sectionToConvert) { + private void setSubstitutionMappingsSection(String sectionName, + SubstitutionMapping substitutionMapping, + Map> convertedSection) { + if(MapUtils.isEmpty(convertedSection) + || StringUtils.isEmpty(sectionName) + || Objects.isNull(substitutionMapping)){ + return; + } + + switch (sectionName){ + case requirements: + substitutionMapping.setRequirements(convertedSection); + break; + case capabilities: + substitutionMapping.setCapabilities(convertedSection); + break; + } + } + + private Map> convertSubstitutionMappingsSections(String sectionName, + Object sectionToConvert) { + if(Objects.isNull(sectionToConvert)){ + return null; + } + + if(!(sectionToConvert instanceof Map)){ + throw new CoreException( + new SubstitutionMappingsConverterErrorBuilder( + sectionName, "Map").build()); + } + + return convertSubstitutionMappongsSection((Map) sectionToConvert); + } + + private Map> convertSubstitutionMappongsSection(Map sectionToConvert) { Map> convertedSection = new HashMap<>(); if (MapUtils.isEmpty(sectionToConvert)) { return null; diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java index fa8532546c..8155fcc7f6 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java @@ -1,11 +1,7 @@ package org.openecomp.core.impl.services; import org.openecomp.core.converter.ServiceTemplateReaderService; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCategory; -import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.tosca.services.YamlUtil; -import org.yaml.snakeyaml.error.YAMLException; import java.util.HashMap; import java.util.Map; -- cgit 1.2.3-korg