2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.heat.datatypes;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Objects;
27 import lombok.AllArgsConstructor;
29 import org.apache.commons.lang.math.NumberUtils;
30 import org.apache.commons.lang3.ClassUtils;
31 import org.onap.sdc.tosca.datatypes.model.ScalarUnitValidator;
35 public enum DefinedHeatParameterTypes {
38 COMMA_DELIMITED_LIST("comma_delimited_list"),
42 private static ScalarUnitValidator scalarUnitValidator = ScalarUnitValidator.getInstance();
43 private static Map<String, DefinedHeatParameterTypes> stringToDefinedType;
46 stringToDefinedType = new HashMap<>();
47 for (final DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) {
48 stringToDefinedType.put(definedHeatParameterType.type, definedHeatParameterType);
54 public static DefinedHeatParameterTypes findByHeatResource(final String type) {
55 return stringToDefinedType.get(type);
59 * Is value is from given type boolean.
61 * @param value the value
62 * @param parameterType the parameter type
65 public static boolean isValueIsFromGivenType(final Object value, final String parameterType) {
66 final DefinedHeatParameterTypes definedType = findByHeatResource(parameterType);
68 if (Objects.nonNull(definedType)) {
69 switch (definedType) {
71 if (scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitSize.class) ||
72 scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitTime.class) ||
73 scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitFrequency.class)) {
74 return isValueString(value);
76 return NumberUtils.isNumber(String.valueOf(value));
79 return HeatBoolean.isValueBoolean(value);
81 case COMMA_DELIMITED_LIST:
82 return isValueCommaDelimitedList(value);
85 return isValueJson(value);
88 return isValueString(value);
96 public static boolean isNovaServerEnvValueIsFromRightType(final Object value) {
97 return isValueIsFromGivenType(value, COMMA_DELIMITED_LIST.getType())
98 || isValueIsFromGivenType(value, STRING.getType());
101 private static boolean isValueCommaDelimitedList(final Object value) {
102 return value instanceof List
103 || String.valueOf(value).contains(",")
104 || isValueIsFromGivenType(value, DefinedHeatParameterTypes.STRING.type);
107 private static boolean isValueString(final Object value) {
108 return value instanceof String
109 || ClassUtils.isPrimitiveOrWrapper(value.getClass());
112 private static boolean isValueJson(final Object value) {
113 return (value instanceof Map) || (value instanceof List);
116 public static boolean isEmptyValueInEnv(final Object value) {
117 return Objects.isNull(value);