diff options
author | 2017-11-19 09:35:13 +0200 | |
---|---|---|
committer | 2017-11-19 11:10:19 +0200 | |
commit | b91470aaf45ccd77c56a7882f0babc9b4a2b9637 (patch) | |
tree | 2cff08e3ad0d8679d65de9ef20051ecf0c083cba /openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main | |
parent | 3e54d3de51fa86255ca5059c59fd68aea0f59488 (diff) |
import tosca bug
fix bug when converting parameters, when importing CSAR file
Issue - Id : SDC-646
Change-Id: Ie9c38f5e51e673a7c89add9e4e42fad93f966aa4
Signed-off-by: talio <tali.orenbach@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main')
3 files changed, 139 insertions, 59 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java index 778445c513..4ff8af495c 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java @@ -67,7 +67,7 @@ public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { setTosca_definitions_version(DEFININTION_VERSION); } - public Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){ + private Optional<Map<String, NodeType>> removeExistingGlobalTypes(Map<String, NodeType> nodes){ Map<String, NodeType> nodeTypesToAdd = new HashMap<>(); ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml"); @@ -75,13 +75,34 @@ public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { return Optional.of(nodes); } - Map<String, NodeType> globalNodeTypes = serviceTemplate.getNode_types(); + Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes(); for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){ if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){ - nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeEntry.getValue()); + Optional<NodeType> nodeType = + ToscaConverterUtil + .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class); + + nodeType + .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue)); } } return Optional.of(nodeTypesToAdd); } + + private Map<String, NodeType> getAllGlobalNodeTypes(){ + Map<String, NodeType> globalNodeTypes = new HashMap<>(); + + for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){ + if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){ + globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types()); + } + } + + return globalNodeTypes; + } + + private boolean isNodesServiceTemplate(String filename) { + return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml"); + } } 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 685f39c3cb..f38b7e096a 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 @@ -9,7 +9,6 @@ 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; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; @@ -141,7 +140,7 @@ public class ToscaConverterImpl implements ToscaConverter { Optional<ServiceTemplate> serviceTemplate = getServiceTemplateFromCsar(fileName, csarFiles); serviceTemplate.ifPresent( - serviceTemplate1 -> addServiceTemplate(serviceTemplateName, serviceTemplate1, + serviceTemplateValue -> addServiceTemplate(serviceTemplateName, serviceTemplateValue, serviceTemplates)); } @@ -242,10 +241,12 @@ public class ToscaConverterImpl implements ToscaConverter { } for (Map.Entry<String, Object> nodeTypeEntry : nodeTypes.entrySet()) { - DataModelUtil - .addNodeType(serviceTemplate, nodeTypeEntry.getKey(), - (NodeType) createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), - NodeType.class)); + Optional<NodeType> nodeType = ToscaConverterUtil + .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), + NodeType.class); + + nodeType.ifPresent(nodeTypeValue -> DataModelUtil + .addNodeType(serviceTemplate, nodeTypeEntry.getKey(), nodeTypeValue)); } } @@ -278,11 +279,28 @@ public class ToscaConverterImpl implements ToscaConverter { } for (Map.Entry<String, Object> entry : mapToConvert.entrySet()) { - ParameterDefinition parameterDefinition = - (ParameterDefinition) createObjectFromClass( + Optional<ParameterDefinition> parameterDefinition = + ToscaConverterUtil.createObjectFromClass( entry.getKey(), entry.getValue(), ParameterDefinition.class); - addToServiceTemplateAccordingToSection( - serviceTemplate, inputsOrOutputs, entry.getKey(), parameterDefinition); + + parameterDefinition.ifPresent(parameterDefinitionValue -> { + handleDefaultValue(entry.getValue(), parameterDefinition.get()); + addToServiceTemplateAccordingToSection( + serviceTemplate, inputsOrOutputs, entry.getKey(), parameterDefinition.get()); + } ); + } + } + + private void handleDefaultValue(Object entryValue, + ParameterDefinition parameterDefinition) { + if(!(entryValue instanceof Map) + || Objects.isNull(parameterDefinition)){ + return; + } + + Object defaultValue = ((Map) entryValue).get("default"); + if(Objects.nonNull(defaultValue)) { + parameterDefinition.set_default(defaultValue); } } @@ -328,57 +346,40 @@ public class ToscaConverterImpl implements ToscaConverter { SubstitutionMapping substitutionMapping = new SubstitutionMapping(); substitutionMapping.setNode_type((String) substitutionMappings.get(nodeType)); - setSubstitutionMappingsSection( - capabilities, substitutionMapping, + substitutionMapping.setCapabilities( convertSubstitutionMappingsSections(capabilities, substitutionMappings.get(capabilities))); - setSubstitutionMappingsSection( - requirements, substitutionMapping, + substitutionMapping.setRequirements( convertSubstitutionMappingsSections(requirements, substitutionMappings.get(requirements))); return substitutionMapping; } - private void setSubstitutionMappingsSection(String sectionName, - SubstitutionMapping substitutionMapping, - Map<String, List<String>> 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<String, List<String>> convertSubstitutionMappingsSections(String sectionName, Object sectionToConvert) { + if(Objects.isNull(sectionToConvert)){ return null; } - if(!(sectionToConvert instanceof Map)){ + if(!(sectionToConvert instanceof Map)) { throw new CoreException( new SubstitutionMappingsConverterErrorBuilder( - sectionName, "Map").build()); + sectionName, sectionToConvert.getClass().getSimpleName()).build()); } - return convertSubstitutionMappongsSection((Map<String, Object>) sectionToConvert); + return convertSection(sectionToConvert); } - private Map<String, List<String>> convertSubstitutionMappongsSection(Map<String, Object> sectionToConvert) { + private Map<String, List<String>> convertSection(Object sectionToConvert) { + + Map<String, Object> sectionAsMap = (Map<String, Object>)sectionToConvert; Map<String, List<String>> convertedSection = new HashMap<>(); - if (MapUtils.isEmpty(sectionToConvert)) { + + if (MapUtils.isEmpty(sectionAsMap)) { return null; } - for (Map.Entry<String, Object> entry : sectionToConvert.entrySet()) { + for (Map.Entry<String, Object> entry : sectionAsMap.entrySet()) { if (entry.getValue() instanceof List) { convertedSection.put(entry.getKey(), (List<String>) entry.getValue()); } @@ -441,27 +442,20 @@ public class ToscaConverterImpl implements ToscaConverter { } for (Map.Entry<String, Object> capabilityAssignmentEntry : capabilities.entrySet()) { Map<String, CapabilityAssignment> tempMap = new HashMap<>(); - tempMap.put(capabilityAssignmentEntry.getKey(), - (CapabilityAssignment) createObjectFromClass - (capabilityAssignmentEntry.getKey(), capabilityAssignmentEntry.getValue(), CapabilityAssignment.class)); - convertedCapabilities.add(tempMap); + Optional<CapabilityAssignment> capabilityAssignment = ToscaConverterUtil.createObjectFromClass + (capabilityAssignmentEntry.getKey(), capabilityAssignmentEntry.getValue(), + CapabilityAssignment.class); + + capabilityAssignment.ifPresent(capabilityAssignmentValue -> { + tempMap.put(capabilityAssignmentEntry.getKey(), capabilityAssignmentValue); + convertedCapabilities.add(tempMap); + } + ); + } return convertedCapabilities; } - private Object createObjectFromClass(String nodeTypeId, - Object objectCandidate, - Class classToCreate) { - try { - return JsonUtil.json2Object(objectCandidate.toString(), classToCreate); - } catch (Exception e) { - //todo - return error to user? - throw new CoreException(new ErrorCode.ErrorCodeBuilder() - .withCategory(ErrorCategory.APPLICATION) - .withMessage("Can't create " + classToCreate.getSimpleName() + " from " + - nodeTypeId).build()); - } - } private boolean isMainServiceTemplate(String fileName) { return fileName.endsWith(mainStName); diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterUtil.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterUtil.java new file mode 100644 index 0000000000..4120c6994c --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterUtil.java @@ -0,0 +1,65 @@ +package org.openecomp.core.impl; + +import org.apache.commons.lang.StringUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.openecomp.core.converter.errors.CreateToscaObjectErrorBuilder; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; + +import java.lang.reflect.Field; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +public class ToscaConverterUtil { + private static final String set = "set"; + + public static <T> Optional<T> createObjectFromClass(String objectId, + Object objectCandidate, + Class<T> classToCreate) { + try { + return createObjectUsingSetters(objectCandidate, classToCreate); + } catch (Exception e) { + throw new CoreException( + new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId, e.getMessage()) + .build()); + } + } + + private static <T> Optional<T> createObjectUsingSetters(Object objectCandidate, + Class<T> classToCreate) throws Exception { + if(!(objectCandidate instanceof Map)){ + return Optional.empty(); + } + + Map<String, Object> objectAsMap = (Map<String, Object>) objectCandidate; + Field[] classFields = classToCreate.getDeclaredFields(); + T result = classToCreate.newInstance(); + + for(Field field : classFields){ + Object fieldValueToAssign = objectAsMap.get(field.getName()); + String methodName = set + StringUtils.capitalize(field.getName()); + + if(shouldSetterMethodNeedsToGetInvoked(classToCreate, field, fieldValueToAssign, methodName)){ + classToCreate.getMethod(methodName, field.getType()).invoke(result, fieldValueToAssign); + } + } + + return Optional.of(result); + } + + private static <T> boolean shouldSetterMethodNeedsToGetInvoked(Class<T> classToCreate, + Field field, + Object fieldValueToAssign, + String methodName) { + + try { + return Objects.nonNull(fieldValueToAssign) + && Objects.nonNull(classToCreate.getMethod(methodName, field.getType())); + } catch (NoSuchMethodException e) { + return false; + } + } +} |