Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / impl / ToscaConverterUtil.java
1 package org.openecomp.core.impl;
2
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;
9
10 import java.lang.reflect.Field;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Objects;
14 import java.util.Optional;
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
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;
24
25   private static Logger LOGGER = LoggerFactory.getLogger(ToscaConverterUtil.class.getName());
26
27   static {
28     defaultValueKeys =
29         Stream.of(DEFAULT, DEFAULT_CAPITAL).collect(Collectors.toSet());
30   }
31
32   public static <T> Optional<T> createObjectFromClass(String objectId,
33                                                       Object objectCandidate,
34                                                       Class<T> classToCreate) {
35     try {
36       return createObjectUsingSetters(objectCandidate, classToCreate);
37     } catch (Exception ex) {
38       throw new CoreException(
39           new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId)
40               .build(), ex);
41     }
42   }
43
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();
49     }
50
51     Map<String, Object> objectAsMap = (Map<String, Object>) objectCandidate;
52     Field[] classFields = classToCreate.getDeclaredFields();
53     T result = classToCreate.newInstance();
54
55     for (Field field : classFields) {
56       Object fieldValueToAssign = objectAsMap.get(field.getName());
57       String methodName = SET + StringUtils.capitalize(field.getName());
58
59       if(shouldSetterMethodNeedsToGetInvoked(classToCreate, field, fieldValueToAssign, methodName)) {
60         classToCreate.getMethod(methodName, field.getType()).invoke(result, fieldValueToAssign);
61       }
62     }
63
64     return Optional.of(result);
65   }
66   private static <T> boolean shouldSetterMethodNeedsToGetInvoked(Class<T> classToCreate,
67                                                                  Field field,
68                                                                  Object fieldValueToAssign,
69                                                                  String methodName) {
70
71     try {
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);
77       return false;
78     }
79   }
80
81   public static Optional<Object> getDefaultValue(Object entryValue,
82                                        Object objectToAssignDefaultValue) {
83     if (!(entryValue instanceof Map)
84         || Objects.isNull(objectToAssignDefaultValue)) {
85       return Optional.empty();
86     }
87
88     return Optional.ofNullable(getDefaultParameterValue((Map<String, Object>) entryValue));
89   }
90
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);
95
96     if (CollectionUtils.isNotEmpty(keys)) {
97       defaultValue = entryValue.get(keys.iterator().next());
98     }
99
100     return defaultValue;
101   }
102 }