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 | |
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')
5 files changed, 68 insertions, 58 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java index 93b95240d1..19e48eceaa 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/composition/CompositionDataExtractorImpl.java @@ -324,43 +324,33 @@ public class CompositionDataExtractorImpl implements CompositionDataExtractor { private Map<String,List<String>> getComponentImages(Map<String, NodeTemplate> computeNodeTemplates, ToscaServiceModel toscaServiceModel) { - Map<String,List<String>> computeImages = new HashMap<>(); - for (String component : computeNodeTemplates.keySet()) { - List<String> images = new ArrayList<>(); - Map<String,Object> properties = computeNodeTemplates.get(component).getProperties(); - - List<Object> imagesList = properties.entrySet() - .stream() - .filter(map -> map.getKey().equals("image")) - .map(map -> map.getValue()) - .collect(Collectors.toList()); - for (Object obj : imagesList) { - if (obj instanceof String) { - images.add((String) obj); - } else { - Map<String,String> objMap = new ObjectMapper().convertValue(obj,Map.class); - images.add(getInputs(toscaServiceModel,objMap.get("get_input"))); - } - } - computeImages.put(component,images); - } - return computeImages; + return getComponentProperty(ToscaConstants.COMPUTE_IMAGE, computeNodeTemplates, toscaServiceModel); } private Map<String,List<String>> getComponentComputeFlavor(Map<String, NodeTemplate> computeNodeTemplates, ToscaServiceModel toscaServiceModel) { - Map<String,List<String>> componentComputeFlavor = new HashMap<>(); + return getComponentProperty(ToscaConstants.COMPUTE_FLAVOR, computeNodeTemplates, toscaServiceModel); + } + + private Map<String, List<String>> getComponentProperty(String propertyName, + Map<String, NodeTemplate> computeNodeTemplates, + ToscaServiceModel toscaServiceModel) { + Map<String,List<String>> componentPropertyValues = new HashMap<>(); for (String component : computeNodeTemplates.keySet()) { List<String> computes = new ArrayList<>(); Map<String,Object> properties = computeNodeTemplates.get(component).getProperties(); - List<Object> computessList = properties.entrySet() + if(MapUtils.isEmpty(properties)){ + continue; + } + + List<Object> computesList = properties.entrySet() .stream() - .filter(map -> map.getKey().equals("flavor")) - .map(map -> map.getValue()) + .filter(map -> map.getKey().equals(propertyName)) + .map(Map.Entry::getValue) .collect(Collectors.toList()); - for (Object obj : computessList) { + for (Object obj : computesList) { if (obj instanceof String) { computes.add((String) obj); } else { @@ -368,9 +358,9 @@ public class CompositionDataExtractorImpl implements CompositionDataExtractor { computes.add(getInputs(toscaServiceModel, objMap.get("get_input"))); } } - componentComputeFlavor.put(component,computes); + componentPropertyValues.put(component,computes); } - return componentComputeFlavor; + return componentPropertyValues; } private String getInputs(ToscaServiceModel toscaServiceModel, String inputValue) { diff --git a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java index 09c823ce7e..1bc97b4d69 100644 --- a/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java +++ b/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java @@ -10,7 +10,7 @@ public interface ServiceTemplateReaderService { Object getToscaVersion(); - Object getNodeTypes(); + Map<String, Object> getNodeTypes(); Object getTopologyTemplate(); 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 |