2236622222a82198551ddb5abb6c6e8928ed8178
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.impl;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.openecomp.core.converter.errors.CreateToscaObjectErrorBuilder;
21 import org.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.common.utils.CommonUtil;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25
26 import java.util.HashSet;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Optional;
30 import java.util.Set;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33
34 public class ToscaConverterUtil {
35
36   private static final String DEFAULT = "default";
37   private static final String DEFAULT_CAPITAL = "Default";
38   private static final Set<String> DEFAULT_VALUE_KEYS;
39
40   static {
41     DEFAULT_VALUE_KEYS =
42         Stream.of(DEFAULT, DEFAULT_CAPITAL).collect(Collectors.toSet());
43   }
44
45   private ToscaConverterUtil() {
46     // static utility methods only, prevent instantiation
47   }
48
49   static <T> Optional<T> createObjectFromClass(String objectId,
50                                                       Object objectCandidate,
51                                                       Class<T> classToCreate) {
52     try {
53       return CommonUtil.createObjectUsingSetters(objectCandidate, classToCreate);
54     } catch (Exception ex) {
55       throw new CoreException(
56           new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId)
57               .build(), ex);
58     }
59   }
60
61
62   static Optional<Object> getDefaultValue(Object entryValue,
63                                        Object objectToAssignDefaultValue) {
64     if (!(entryValue instanceof Map)
65         || Objects.isNull(objectToAssignDefaultValue)) {
66       return Optional.empty();
67     }
68
69     return Optional.ofNullable(getDefaultParameterValue((Map<String, Object>) entryValue));
70   }
71
72   private static Object getDefaultParameterValue(Map<String, Object> entryValue) {
73     Object defaultValue = null;
74     Set<String> keys = new HashSet<>(entryValue.keySet());
75     keys.retainAll(DEFAULT_VALUE_KEYS);
76
77     if (CollectionUtils.isNotEmpty(keys)) {
78       defaultValue = entryValue.get(keys.iterator().next());
79     }
80
81     return defaultValue;
82   }
83 }