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=========================================================
20 package org.openecomp.sdc.heat.datatypes;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.Objects;
26 import lombok.AllArgsConstructor;
28 import org.apache.commons.lang3.math.NumberUtils;
29 import org.apache.commons.lang3.ClassUtils;
30 import org.onap.sdc.tosca.datatypes.model.ScalarUnitValidator;
34 public enum DefinedHeatParameterTypes {
35 NUMBER("number"), STRING("string"), COMMA_DELIMITED_LIST("comma_delimited_list"), JSON("json"), BOOLEAN("boolean");
36 private static ScalarUnitValidator scalarUnitValidator = ScalarUnitValidator.getInstance();
37 private static Map<String, DefinedHeatParameterTypes> stringToDefinedType;
40 stringToDefinedType = new HashMap<>();
41 for (final DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) {
42 stringToDefinedType.put(definedHeatParameterType.type, definedHeatParameterType);
48 public static DefinedHeatParameterTypes findByHeatResource(final String type) {
49 return stringToDefinedType.get(type);
53 * Is value is from given type boolean.
55 * @param value the value
56 * @param parameterType the parameter type
59 public static boolean isValueIsFromGivenType(final Object value, final String parameterType) {
60 final DefinedHeatParameterTypes definedType = findByHeatResource(parameterType);
61 if (Objects.nonNull(definedType)) {
62 switch (definedType) {
64 if (scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitSize.class) || scalarUnitValidator
65 .isValueScalarUnit(value, ToscaScalarUnitTime.class) || scalarUnitValidator
66 .isValueScalarUnit(value, ToscaScalarUnitFrequency.class)) {
67 return isValueString(value);
69 return NumberUtils.isNumber(String.valueOf(value));
71 return HeatBoolean.isValueBoolean(value);
72 case COMMA_DELIMITED_LIST:
73 return isValueCommaDelimitedList(value);
75 return isValueJson(value);
77 return isValueString(value);
84 public static boolean isNovaServerEnvValueIsFromRightType(final Object value) {
85 return isValueIsFromGivenType(value, COMMA_DELIMITED_LIST.getType()) || isValueIsFromGivenType(value, STRING.getType());
88 private static boolean isValueCommaDelimitedList(final Object value) {
89 return value instanceof List || String.valueOf(value).contains(",") || isValueIsFromGivenType(value, DefinedHeatParameterTypes.STRING.type);
92 private static boolean isValueString(final Object value) {
93 return value instanceof String || ClassUtils.isPrimitiveOrWrapper(value.getClass());
96 private static boolean isValueJson(final Object value) {
97 return (value instanceof Map) || (value instanceof List);
100 public static boolean isEmptyValueInEnv(final Object value) {
101 return Objects.isNull(value);