1 package org.openecomp.core.impl;
 
   3 import org.apache.commons.collections4.CollectionUtils;
 
   4 import org.apache.commons.lang.StringUtils;
 
   5 import org.openecomp.core.converter.errors.CreateToscaObjectErrorBuilder;
 
   6 import org.openecomp.sdc.common.errors.CoreException;
 
   7 import org.openecomp.sdc.logging.api.Logger;
 
   8 import org.openecomp.sdc.logging.api.LoggerFactory;
 
  10 import java.lang.reflect.Field;
 
  11 import java.util.HashSet;
 
  13 import java.util.Objects;
 
  14 import java.util.Optional;
 
  16 import java.util.stream.Collectors;
 
  17 import java.util.stream.Stream;
 
  19 public class ToscaConverterUtil {
 
  20   private static final String SET = "set";
 
  21   private static final String DEFAULT = "default";
 
  22   private static final String DEFAULT_CAPITAL = "Default";
 
  23   private static Set<String> defaultValueKeys;
 
  25   private static Logger LOGGER = LoggerFactory.getLogger(ToscaConverterUtil.class.getName());
 
  29         Stream.of(DEFAULT, DEFAULT_CAPITAL).collect(Collectors.toSet());
 
  32   public static <T> Optional<T> createObjectFromClass(String objectId,
 
  33                                                       Object objectCandidate,
 
  34                                                       Class<T> classToCreate) {
 
  36       return createObjectUsingSetters(objectCandidate, classToCreate);
 
  37     } catch (Exception ex) {
 
  38       throw new CoreException(
 
  39           new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId)
 
  44   private static <T> Optional<T> createObjectUsingSetters(Object objectCandidate,
 
  45                                                           Class<T> classToCreate) throws Exception {
 
  46     if (Objects.isNull(objectCandidate)
 
  47         || !(objectCandidate instanceof Map)) {
 
  48       return Optional.empty();
 
  51     Map<String, Object> objectAsMap = (Map<String, Object>) objectCandidate;
 
  52     Field[] classFields = classToCreate.getDeclaredFields();
 
  53     T result = classToCreate.newInstance();
 
  55     for (Field field : classFields) {
 
  56       Object fieldValueToAssign = objectAsMap.get(field.getName());
 
  57       String methodName = SET + StringUtils.capitalize(field.getName());
 
  59       if(shouldSetterMethodNeedsToGetInvoked(classToCreate, field, fieldValueToAssign, methodName)) {
 
  60         classToCreate.getMethod(methodName, field.getType()).invoke(result, fieldValueToAssign);
 
  64     return Optional.of(result);
 
  66   private static <T> boolean shouldSetterMethodNeedsToGetInvoked(Class<T> classToCreate,
 
  68                                                                  Object fieldValueToAssign,
 
  72       return Objects.nonNull(fieldValueToAssign)
 
  73           && Objects.nonNull(classToCreate.getMethod(methodName, field.getType()));
 
  74     } catch (NoSuchMethodException e) {
 
  75       LOGGER.debug(String.format("Could not extract method '%s' from class '%s'. returning false " +
 
  76               "with filedType '%s'.", methodName, classToCreate, field.getType()), e);
 
  81   public static Optional<Object> getDefaultValue(Object entryValue,
 
  82                                        Object objectToAssignDefaultValue) {
 
  83     if (!(entryValue instanceof Map)
 
  84         || Objects.isNull(objectToAssignDefaultValue)) {
 
  85       return Optional.empty();
 
  88     return Optional.ofNullable(getDefaultParameterValue((Map<String, Object>) entryValue));
 
  91   private static Object getDefaultParameterValue(Map<String, Object> entryValue) {
 
  92     Object defaultValue = null;
 
  93     Set<String> keys = new HashSet<>(entryValue.keySet());
 
  94     keys.retainAll(defaultValueKeys);
 
  96     if (CollectionUtils.isNotEmpty(keys)) {
 
  97       defaultValue = entryValue.get(keys.iterator().next());