push addional code
[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   // TODO : ASK SEGEV ABOUT STRING
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           String valAsString = String.valueOf(value);
77           return valAsString.split(",") instanceof String[];
78
79         case JSON:
80           return (value instanceof Map) || (value instanceof List);
81
82         case STRING:
83           //return value instanceof String;
84           return true;
85
86         default:
87         // return false;
88       }
89     }
90
91     return false;
92   }
93
94   public static boolean isNovaServerEnvValueIsFromRightType(Object value) {
95     return isValueIsFromGivenType(value, COMMA_DELIMITED_LIST.getType())
96         || isValueIsFromGivenType(value, STRING.getType());
97   }
98
99   public static boolean isEmptyValueInEnv(Object value) {
100     return Objects.isNull(value);
101   }
102
103   public String getType() {
104     return type;
105   }
106
107   public void setType(String type) {
108     this.type = type;
109   }
110 }