fix NPE in extract 83/24683/2
authortalio <tali.orenbach@amdocs.com>
Tue, 21 Nov 2017 08:00:27 +0000 (10:00 +0200)
committerAvi Gaffa <avi.gaffa@amdocs.com>
Tue, 21 Nov 2017 09:30:22 +0000 (09:30 +0000)
fix NPE when extracting components without images / flavors

Issue - Id : SDC-694

Change-Id: I3e4444132a40a2567833375ceea1951803dd16b0
Signed-off-by: talio <tali.orenbach@amdocs.com>
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
openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-api/src/main/java/org/openecomp/core/converter/ServiceTemplateReaderService.java
openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterImpl.java
openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/ToscaConverterUtil.java
openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/services/ServiceTemplateReaderServiceImpl.java

index 93b9524..19e48ec 100644 (file)
@@ -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) {
index f38b7e0..e7ff3aa 100644 (file)
@@ -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,
index 4120c69..20ac641 100644 (file)
@@ -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;
+  }
 }
index 8155fcc..22780af 100644 (file)
@@ -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