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