[SDC-29] Amdocs OnBoard 1707 initial commit.
[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
21 package org.openecomp.sdc.heat.datatypes;
22
23 import org.apache.commons.lang.math.NumberUtils;
24
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29
30 public enum DefinedHeatParameterTypes {
31   NUMBER("number"),
32   STRING("string"),
33   COMMA_DELIMITED_LIST("comma_delimited_list"),
34   JSON("json"),
35   BOOLEAN("boolean");
36
37   private static Map<String, DefinedHeatParameterTypes> stringToDefinedType = new HashMap<>();
38
39   static {
40     stringToDefinedType = new HashMap<>();
41     for (DefinedHeatParameterTypes definedHeatParameterType : DefinedHeatParameterTypes.values()) {
42       stringToDefinedType.put(definedHeatParameterType.type, definedHeatParameterType);
43     }
44   }
45
46   private String type;
47
48   DefinedHeatParameterTypes(String type) {
49     this.type = type;
50   }
51
52   public static DefinedHeatParameterTypes findByHeatResource(String type) {
53     return stringToDefinedType.get(type);
54   }
55
56   /**
57    * Is value is from given type boolean.
58    *
59    * @param value         the value
60    * @param parameterType the parameter type
61    * @return the boolean
62    */
63   public static boolean isValueIsFromGivenType(Object value, String parameterType) {
64     DefinedHeatParameterTypes definedType = findByHeatResource(parameterType);
65
66     if (Objects.nonNull(definedType)) {
67       switch (definedType) {
68         case NUMBER:
69           return NumberUtils.isNumber(String.valueOf(value));
70
71         case BOOLEAN:
72           return HeatBoolean.isValueBoolean(value);
73
74         case COMMA_DELIMITED_LIST:
75           String valAsString = String.valueOf(value);
76           return valAsString.split(",") instanceof String[];
77
78         case JSON:
79           return (value instanceof Map) || (value instanceof List);
80
81         case STRING:
82           return true;
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   public static boolean isEmptyValueInEnv(Object value) {
96     return Objects.isNull(value);
97   }
98
99   public String getType() {
100     return type;
101   }
102
103   public void setType(String type) {
104     this.type = type;
105   }
106 }