f59ebe6922875f0bced67d3457f0347e6129ee97
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / ToscaType.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.be.model.tosca;
21
22 import com.fasterxml.jackson.core.type.TypeReference;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.IOException;
25 import java.text.ParseException;
26 import java.text.SimpleDateFormat;
27 import java.util.List;
28 import java.util.Locale;
29 import java.util.Map;
30 import lombok.AllArgsConstructor;
31 import lombok.Getter;
32 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
34 import org.openecomp.sdc.be.model.tosca.validators.TimestampValidator;
35
36 /**
37  * The primitive type that TOSCA YAML supports.
38  *
39  * @author mkv
40  */
41 @AllArgsConstructor
42 public enum ToscaType {
43     // @formatter:off
44         STRING("string"),
45         INTEGER("integer"),
46         FLOAT("float"),
47         BOOLEAN("boolean"),
48         TIMESTAMP("timestamp"),
49         VERSION("version"),
50         LIST("list"),
51         MAP("map"),
52     RANGE("range"),
53         SCALAR_UNIT("scalar-unit"),
54         SCALAR_UNIT_SIZE("scalar-unit.size"),
55         SCALAR_UNIT_TIME("scalar-unit.time"),
56         SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
57     // @formatter:on
58
59     @Getter
60     private final String type;
61
62     public static ToscaType getToscaType(String typeName) {
63         if (typeName == null) {
64             return null;
65         }
66         for (ToscaType type : ToscaType.values()) {
67             if (type.getType().equals(typeName)) {
68                 return type;
69             }
70         }
71         return null;
72     }
73
74     public static boolean isPrimitiveType(String dataTypeName) {
75         if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType().equals(dataTypeName)) {
76             return isValidType(dataTypeName) != null;
77         }
78         return false;
79     }
80
81     public static ToscaType isValidType(String typeName) {
82         if (typeName == null) {
83             return null;
84         }
85         for (ToscaType type : ToscaType.values()) {
86             if (type.getType().equals(typeName)) {
87                 return type;
88             }
89         }
90         return null;
91     }
92
93     public static boolean isCollectionType(String type) {
94         return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type);
95     }
96
97     public Boolean isValueTypeValid(Object value) {
98         switch (this) {
99             case BOOLEAN:
100                 return value.equals(true) || value.equals(false);
101             case FLOAT:
102                 return value instanceof Float;
103             case INTEGER:
104             case RANGE:
105                 return value instanceof Integer;
106             case STRING:
107             case SCALAR_UNIT:
108             case SCALAR_UNIT_SIZE:
109             case SCALAR_UNIT_TIME:
110             case SCALAR_UNIT_FREQUENCY:
111             case TIMESTAMP:
112             case VERSION:
113                 return value instanceof String;
114             case LIST:
115             case MAP:
116                 return true;
117             default:
118                 return false;
119         }
120     }
121
122     public boolean isValidValue(String value) {
123         switch (this) {
124             case BOOLEAN:
125                 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
126             case FLOAT:
127                 return isFloat(value);
128             case INTEGER:
129                 return isInteger(value);
130             case STRING:
131             case SCALAR_UNIT:
132             case SCALAR_UNIT_SIZE:
133             case SCALAR_UNIT_TIME:
134             case SCALAR_UNIT_FREQUENCY:
135                 return true;
136             case TIMESTAMP:
137                 return TimestampValidator.getInstance().isValid(value, null);
138             case VERSION:
139                 return VersionUtil.isValid(value);
140             case LIST:
141                 return isList(value);
142             case MAP:
143                 return isMap(value);
144             default:
145                 return false;
146         }
147     }
148
149     private boolean isList(String value) {
150         ObjectMapper objectMapper = new ObjectMapper();
151         try {
152             objectMapper.readValue(value, new TypeReference<List<Object>>() {
153             });
154         } catch (IOException e) {
155             return false;
156         }
157         return true;
158     }
159
160     private boolean isMap(String value) {
161         ObjectMapper objectMapper = new ObjectMapper();
162         try {
163             objectMapper.readValue(value, new TypeReference<Map<String, Object>>() {
164             });
165         } catch (IOException e) {
166             return false;
167         }
168         return true;
169     }
170
171     private boolean isFloat(String value) {
172         try {
173             Float.valueOf(value);
174         } catch (NumberFormatException e) {
175             return false;
176         }
177         return true;
178     }
179
180     private boolean isInteger(String value) {
181         try {
182             Long.valueOf(value);
183         } catch (NumberFormatException e) {
184             return false;
185         }
186         return true;
187     }
188
189     public Object convert(String value) {
190         switch (this) {
191             case STRING:
192             case SCALAR_UNIT:
193             case SCALAR_UNIT_SIZE:
194             case SCALAR_UNIT_TIME:
195             case SCALAR_UNIT_FREQUENCY:
196                 return value;
197             case BOOLEAN:
198                 return Boolean.valueOf(value);
199             case FLOAT:
200                 return Float.valueOf(value);
201             case RANGE:
202             case INTEGER:
203                 return Long.valueOf(value);
204             case TIMESTAMP:
205                 try {
206                     return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
207                 } catch (ParseException e) {
208                     throw new IllegalArgumentException("Value must be a valid timestamp", e);
209                 }
210             case VERSION:
211                 return VersionUtil.parseVersion(value);
212             case LIST:
213                 try {
214                     return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {
215                     });
216                 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
217                     throw new IllegalArgumentException("Value must be a valid List", e);
218                 }
219             case MAP:
220                 try {
221                     return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {
222                     });
223                 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
224                     throw new IllegalArgumentException("Value must be a valid Map", e);
225                 }
226             default:
227                 return null;
228         }
229     }
230
231     @Override
232     public String toString() {
233         return name().toLowerCase();
234     }
235 }