2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.core.impl;
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;
24 import java.util.HashSet;
26 import java.util.Objects;
27 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
32 public class ToscaConverterUtil {
34 private static final String DEFAULT = "default";
35 private static final String DEFAULT_CAPITAL = "Default";
36 private static final Set<String> DEFAULT_VALUE_KEYS;
40 Stream.of(DEFAULT, DEFAULT_CAPITAL).collect(Collectors.toSet());
43 private ToscaConverterUtil() {
44 // static utility methods only, prevent instantiation
47 static <T> Optional<T> createObjectFromClass(String objectId,
48 Object objectCandidate,
49 Class<T> classToCreate) {
51 return CommonUtil.createObjectUsingSetters(objectCandidate, classToCreate);
52 } catch (Exception ex) {
53 throw new CoreException(
54 new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId)
60 static Optional<Object> getDefaultValue(Object entryValue,
61 Object objectToAssignDefaultValue) {
62 if (!(entryValue instanceof Map)
63 || Objects.isNull(objectToAssignDefaultValue)) {
64 return Optional.empty();
67 return Optional.ofNullable(getDefaultParameterValue((Map<String, Object>) entryValue));
70 private static Object getDefaultParameterValue(Map<String, Object> entryValue) {
71 Object defaultValue = null;
72 Set<String> keys = new HashSet<>(entryValue.keySet());
73 keys.retainAll(DEFAULT_VALUE_KEYS);
75 if (CollectionUtils.isNotEmpty(keys)) {
76 defaultValue = entryValue.get(keys.iterator().next());