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