4205f0bee3de6acc67562184297b6bdfb511d92d
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.heat.datatypes;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 import lombok.AllArgsConstructor;
28 import lombok.Getter;
29 import org.apache.commons.lang.math.NumberUtils;
30 import org.apache.commons.lang3.ClassUtils;
31 import org.onap.sdc.tosca.datatypes.model.ScalarUnitValidator;
32
33 @AllArgsConstructor
34 @Getter
35 public enum DefinedHeatParameterTypes {
36     NUMBER("number"),
37     STRING("string"),
38     COMMA_DELIMITED_LIST("comma_delimited_list"),
39     JSON("json"),
40     BOOLEAN("boolean");
41
42     private static ScalarUnitValidator scalarUnitValidator = ScalarUnitValidator.getInstance();
43     private static Map<String, DefinedHeatParameterTypes> stringToDefinedType;
44
45     static {
46         stringToDefinedType = new HashMap<>();
47         for (final DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) {
48             stringToDefinedType.put(definedHeatParameterType.type, definedHeatParameterType);
49         }
50     }
51
52     private String type;
53
54     public static DefinedHeatParameterTypes findByHeatResource(final String type) {
55         return stringToDefinedType.get(type);
56     }
57
58     /**
59      * Is value is from given type boolean.
60      *
61      * @param value         the value
62      * @param parameterType the parameter type
63      * @return the boolean
64      */
65     public static boolean isValueIsFromGivenType(final Object value, final String parameterType) {
66         final DefinedHeatParameterTypes definedType = findByHeatResource(parameterType);
67
68         if (Objects.nonNull(definedType)) {
69             switch (definedType) {
70                 case NUMBER:
71                     if (scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitSize.class) ||
72                         scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitTime.class) ||
73                         scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitFrequency.class)) {
74                         return isValueString(value);
75                     }
76                     return NumberUtils.isNumber(String.valueOf(value));
77
78                 case BOOLEAN:
79                     return HeatBoolean.isValueBoolean(value);
80
81                 case COMMA_DELIMITED_LIST:
82                     return isValueCommaDelimitedList(value);
83
84                 case JSON:
85                     return isValueJson(value);
86
87                 case STRING:
88                     return isValueString(value);
89                 default:
90             }
91         }
92
93         return false;
94     }
95
96     public static boolean isNovaServerEnvValueIsFromRightType(final Object value) {
97         return isValueIsFromGivenType(value, COMMA_DELIMITED_LIST.getType())
98                 || isValueIsFromGivenType(value, STRING.getType());
99     }
100
101     private static boolean isValueCommaDelimitedList(final Object value) {
102         return value instanceof List
103                 || String.valueOf(value).contains(",")
104                 || isValueIsFromGivenType(value, DefinedHeatParameterTypes.STRING.type);
105     }
106
107     private static boolean isValueString(final Object value) {
108         return value instanceof String
109                 || ClassUtils.isPrimitiveOrWrapper(value.getClass());
110     }
111
112     private static boolean isValueJson(final Object value) {
113         return (value instanceof Map) || (value instanceof List);
114     }
115
116     public static boolean isEmptyValueInEnv(final Object value) {
117         return Objects.isNull(value);
118     }
119
120 }