diff options
author | talio <tali.orenbach@amdocs.com> | 2017-11-21 10:00:27 +0200 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2017-11-21 09:30:22 +0000 |
commit | 2b79f16651701fdfa04eb100bc83f2a42b8ed609 (patch) | |
tree | f61d47888514142c0be011466f2d5c3526e9a07a /openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src | |
parent | 15dfe1e9a127bc73f671b9601d8b8c8e25c2d65f (diff) |
fix NPE in extract
fix NPE when extracting components without images / flavors
Issue - Id : SDC-694
Change-Id: I3e4444132a40a2567833375ceea1951803dd16b0
Signed-off-by: talio <tali.orenbach@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src')
3 files changed, 49 insertions, 29 deletions
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 f38b7e096a..e7ff3aa8ee 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,7 +1,6 @@ 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; @@ -284,26 +283,15 @@ public class ToscaConverterImpl implements ToscaConverter { entry.getKey(), entry.getValue(), ParameterDefinition.class); parameterDefinition.ifPresent(parameterDefinitionValue -> { - handleDefaultValue(entry.getValue(), parameterDefinition.get()); + Optional<Object> defaultValue = + ToscaConverterUtil.getDefaultValue(entry.getValue(), parameterDefinition.get()); + defaultValue.ifPresent(parameterDefinitionValue::set_default); 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); - } - } - private void addToServiceTemplateAccordingToSection(ServiceTemplate serviceTemplate, String inputsOrOutputs, String parameterId, 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 index 4120c6994c..20ac6413e7 100644 --- 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 @@ -1,24 +1,33 @@ package org.openecomp.core.impl; +import org.apache.commons.collections4.CollectionUtils; 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.HashSet; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class ToscaConverterUtil { - private static final String set = "set"; + private static final String SET = "set"; + private static final String DEFAULT = "default"; + private static final String DEFAULT_CAPITAL = "Default"; + private static Set<String> defaultValueKeys; + + static { + defaultValueKeys = + Stream.of(DEFAULT, DEFAULT_CAPITAL).collect(Collectors.toSet()); + } public static <T> Optional<T> createObjectFromClass(String objectId, - Object objectCandidate, - Class<T> classToCreate) { + Object objectCandidate, + Class<T> classToCreate) { try { return createObjectUsingSetters(objectCandidate, classToCreate); } catch (Exception e) { @@ -30,7 +39,8 @@ public class ToscaConverterUtil { private static <T> Optional<T> createObjectUsingSetters(Object objectCandidate, Class<T> classToCreate) throws Exception { - if(!(objectCandidate instanceof Map)){ + if (Objects.isNull(objectCandidate) + || !(objectCandidate instanceof Map)) { return Optional.empty(); } @@ -38,18 +48,17 @@ public class ToscaConverterUtil { Field[] classFields = classToCreate.getDeclaredFields(); T result = classToCreate.newInstance(); - for(Field field : classFields){ + for (Field field : classFields) { Object fieldValueToAssign = objectAsMap.get(field.getName()); - String methodName = set + StringUtils.capitalize(field.getName()); + String methodName = SET + StringUtils.capitalize(field.getName()); - if(shouldSetterMethodNeedsToGetInvoked(classToCreate, field, fieldValueToAssign, methodName)){ + 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, @@ -62,4 +71,26 @@ public class ToscaConverterUtil { return false; } } + + public static Optional<Object> getDefaultValue(Object entryValue, + Object objectToAssignDefaultValue) { + if (!(entryValue instanceof Map) + || Objects.isNull(objectToAssignDefaultValue)) { + return Optional.empty(); + } + + return Optional.ofNullable(getDefaultParameterValue((Map<String, Object>) entryValue)); + } + + private static Object getDefaultParameterValue(Map<String, Object> entryValue) { + Object defaultValue = null; + Set<String> keys = new HashSet<>(entryValue.keySet()); + keys.retainAll(defaultValueKeys); + + if (CollectionUtils.isNotEmpty(keys)) { + defaultValue = entryValue.get(keys.iterator().next()); + } + + return defaultValue; + } } 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 8155fcc7f6..22780af343 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 @@ -37,8 +37,9 @@ public class ServiceTemplateReaderServiceImpl implements ServiceTemplateReaderSe } @Override - public Object getNodeTypes(){ - return this.readServiceTemplate.get(nodeTypes); + public Map<String, Object> getNodeTypes(){ + return Objects.isNull(this.readServiceTemplate.get(nodeTypes)) ? new HashMap<>() + :(Map<String, Object>) this.readServiceTemplate.get(nodeTypes); } @Override |