Update vulnerable package dependencies
[sdc.git] / openecomp-be / lib / openecomp-heat-lib / src / main / java / org / openecomp / sdc / heat / datatypes / DefinedHeatParameterTypes.java
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 package org.openecomp.sdc.heat.datatypes;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import lombok.AllArgsConstructor;
27 import lombok.Getter;
28 import org.apache.commons.lang3.math.NumberUtils;
29 import org.apache.commons.lang3.ClassUtils;
30 import org.onap.sdc.tosca.datatypes.model.ScalarUnitValidator;
31
32 @AllArgsConstructor
33 @Getter
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;
38
39     static {
40         stringToDefinedType = new HashMap<>();
41         for (final DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) {
42             stringToDefinedType.put(definedHeatParameterType.type, definedHeatParameterType);
43         }
44     }
45
46     private String type;
47
48     public static DefinedHeatParameterTypes findByHeatResource(final String type) {
49         return stringToDefinedType.get(type);
50     }
51
52     /**
53      * Is value is from given type boolean.
54      *
55      * @param value         the value
56      * @param parameterType the parameter type
57      * @return the boolean
58      */
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) {
63                 case NUMBER:
64                     if (scalarUnitValidator.isValueScalarUnit(value, ToscaScalarUnitSize.class) || scalarUnitValidator
65                         .isValueScalarUnit(value, ToscaScalarUnitTime.class) || scalarUnitValidator
66                         .isValueScalarUnit(value, ToscaScalarUnitFrequency.class)) {
67                         return isValueString(value);
68                     }
69                     return NumberUtils.isNumber(String.valueOf(value));
70                 case BOOLEAN:
71                     return HeatBoolean.isValueBoolean(value);
72                 case COMMA_DELIMITED_LIST:
73                     return isValueCommaDelimitedList(value);
74                 case JSON:
75                     return isValueJson(value);
76                 case STRING:
77                     return isValueString(value);
78                 default:
79             }
80         }
81         return false;
82     }
83
84     public static boolean isNovaServerEnvValueIsFromRightType(final Object value) {
85         return isValueIsFromGivenType(value, COMMA_DELIMITED_LIST.getType()) || isValueIsFromGivenType(value, STRING.getType());
86     }
87
88     private static boolean isValueCommaDelimitedList(final Object value) {
89         return value instanceof List || String.valueOf(value).contains(",") || isValueIsFromGivenType(value, DefinedHeatParameterTypes.STRING.type);
90     }
91
92     private static boolean isValueString(final Object value) {
93         return value instanceof String || ClassUtils.isPrimitiveOrWrapper(value.getClass());
94     }
95
96     private static boolean isValueJson(final Object value) {
97         return (value instanceof Map) || (value instanceof List);
98     }
99
100     public static boolean isEmptyValueInEnv(final Object value) {
101         return Objects.isNull(value);
102     }
103 }