From b91470aaf45ccd77c56a7882f0babc9b4a2b9637 Mon Sep 17 00:00:00 2001 From: talio Date: Sun, 19 Nov 2017 09:35:13 +0200 Subject: import tosca bug fix bug when converting parameters, when importing CSAR file Issue - Id : SDC-646 Change-Id: Ie9c38f5e51e673a7c89add9e4e42fad93f966aa4 Signed-off-by: talio --- .../impl/GlobalSubstitutionServiceTemplate.java | 27 +++++- .../openecomp/core/impl/ToscaConverterImpl.java | 106 ++++++++++----------- .../openecomp/core/impl/ToscaConverterUtil.java | 65 +++++++++++++ 3 files changed, 139 insertions(+), 59 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterUtil.java (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl') 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> removeExistingGlobalTypes(Map nodes){ + private Optional> removeExistingGlobalTypes(Map nodes){ Map nodeTypesToAdd = new HashMap<>(); ServiceTemplate serviceTemplate = globalServiceTemplates.get("openecomp/nodes.yml"); @@ -75,13 +75,34 @@ public class GlobalSubstitutionServiceTemplate extends ServiceTemplate { return Optional.of(nodes); } - Map globalNodeTypes = serviceTemplate.getNode_types(); + Map globalNodeTypes = getAllGlobalNodeTypes(); for(Map.Entry nodeTypeEntry : nodes.entrySet()){ if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){ - nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeEntry.getValue()); + Optional nodeType = + ToscaConverterUtil + .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class); + + nodeType + .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue)); } } return Optional.of(nodeTypesToAdd); } + + private Map getAllGlobalNodeTypes(){ + Map globalNodeTypes = new HashMap<>(); + + for(Map.Entry 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 = 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 nodeTypeEntry : nodeTypes.entrySet()) { - DataModelUtil - .addNodeType(serviceTemplate, nodeTypeEntry.getKey(), - (NodeType) createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), - NodeType.class)); + Optional 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 entry : mapToConvert.entrySet()) { - ParameterDefinition parameterDefinition = - (ParameterDefinition) createObjectFromClass( + Optional 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> 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)){ + if(!(sectionToConvert instanceof Map)) { throw new CoreException( new SubstitutionMappingsConverterErrorBuilder( - sectionName, "Map").build()); + sectionName, sectionToConvert.getClass().getSimpleName()).build()); } - return convertSubstitutionMappongsSection((Map) sectionToConvert); + return convertSection(sectionToConvert); } - private Map> convertSubstitutionMappongsSection(Map sectionToConvert) { + private Map> convertSection(Object sectionToConvert) { + + Map sectionAsMap = (Map)sectionToConvert; Map> convertedSection = new HashMap<>(); - if (MapUtils.isEmpty(sectionToConvert)) { + + if (MapUtils.isEmpty(sectionAsMap)) { return null; } - for (Map.Entry entry : sectionToConvert.entrySet()) { + for (Map.Entry entry : sectionAsMap.entrySet()) { if (entry.getValue() instanceof List) { convertedSection.put(entry.getKey(), (List) entry.getValue()); } @@ -441,27 +442,20 @@ public class ToscaConverterImpl implements ToscaConverter { } for (Map.Entry capabilityAssignmentEntry : capabilities.entrySet()) { Map tempMap = new HashMap<>(); - tempMap.put(capabilityAssignmentEntry.getKey(), - (CapabilityAssignment) createObjectFromClass - (capabilityAssignmentEntry.getKey(), capabilityAssignmentEntry.getValue(), CapabilityAssignment.class)); - convertedCapabilities.add(tempMap); + Optional 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 Optional createObjectFromClass(String objectId, + Object objectCandidate, + Class classToCreate) { + try { + return createObjectUsingSetters(objectCandidate, classToCreate); + } catch (Exception e) { + throw new CoreException( + new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId, e.getMessage()) + .build()); + } + } + + private static Optional createObjectUsingSetters(Object objectCandidate, + Class classToCreate) throws Exception { + if(!(objectCandidate instanceof Map)){ + return Optional.empty(); + } + + Map objectAsMap = (Map) 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 boolean shouldSetterMethodNeedsToGetInvoked(Class classToCreate, + Field field, + Object fieldValueToAssign, + String methodName) { + + try { + return Objects.nonNull(fieldValueToAssign) + && Objects.nonNull(classToCreate.getMethod(methodName, field.getType())); + } catch (NoSuchMethodException e) { + return false; + } + } +} -- cgit 1.2.3-korg