98ca5d02ab5f35a04dec3565720f5de68b2f19e3
[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
35 /**
36  * The primitive type that TOSCA YAML supports.
37  *
38  * @author mkv
39  */
40 @AllArgsConstructor
41 public enum ToscaType {
42     // @formatter:off
43         STRING("string"),
44         INTEGER("integer"),
45         FLOAT("float"),
46         BOOLEAN("boolean"),
47         TIMESTAMP("timestamp"),
48         VERSION("version"),
49         LIST("list"),
50         MAP("map"),
51         SCALAR_UNIT("scalar-unit"),
52         SCALAR_UNIT_SIZE("scalar-unit.size"),
53         SCALAR_UNIT_TIME("scalar-unit.time"),
54         SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
55     // @formatter:on
56
57     @Getter
58     private final String type;
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 boolean isValidValue(String value) {
96         switch (this) {
97             case BOOLEAN:
98                 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
99             case FLOAT:
100                 return isFloat(value);
101             case INTEGER:
102                 return isInteger(value);
103             case STRING:
104             case SCALAR_UNIT:
105             case SCALAR_UNIT_SIZE:
106             case SCALAR_UNIT_TIME:
107             case SCALAR_UNIT_FREQUENCY:
108                 return true;
109             case TIMESTAMP:
110                 try {
111                     new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
112                     return true;
113                 } catch (ParseException e) {
114                     return false;
115                 }
116             case VERSION:
117                 return VersionUtil.isValid(value);
118             case LIST:
119                 return isList(value);
120             case MAP:
121                 return isMap(value);
122             default:
123                 return false;
124         }
125     }
126
127     private boolean isList(String value) {
128         ObjectMapper objectMapper = new ObjectMapper();
129         try {
130             objectMapper.readValue(value, new TypeReference<List<Object>>() {
131             });
132         } catch (IOException e) {
133             return false;
134         }
135         return true;
136     }
137
138     private boolean isMap(String value) {
139         ObjectMapper objectMapper = new ObjectMapper();
140         try {
141             objectMapper.readValue(value, new TypeReference<Map<String, Object>>() {
142             });
143         } catch (IOException e) {
144             return false;
145         }
146         return true;
147     }
148
149     private boolean isFloat(String value) {
150         try {
151             Float.valueOf(value);
152         } catch (NumberFormatException e) {
153             return false;
154         }
155         return true;
156     }
157
158     private boolean isInteger(String value) {
159         try {
160             Long.valueOf(value);
161         } catch (NumberFormatException e) {
162             return false;
163         }
164         return true;
165     }
166
167     public Object convert(String value) {
168         switch (this) {
169             case STRING:
170             case SCALAR_UNIT:
171             case SCALAR_UNIT_SIZE:
172             case SCALAR_UNIT_TIME:
173             case SCALAR_UNIT_FREQUENCY:
174                 return value;
175             case BOOLEAN:
176                 return Boolean.valueOf(value);
177             case FLOAT:
178                 return Float.valueOf(value);
179             case INTEGER:
180                 return Long.valueOf(value);
181             case TIMESTAMP:
182                 try {
183                     return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
184                 } catch (ParseException e) {
185                     throw new IllegalArgumentException("Value must be a valid timestamp", e);
186                 }
187             case VERSION:
188                 return VersionUtil.parseVersion(value);
189             case LIST:
190                 try {
191                     return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {
192                     });
193                 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
194                     throw new IllegalArgumentException("Value must be a valid List", e);
195                 }
196             case MAP:
197                 try {
198                     return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {
199                     });
200                 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
201                     throw new IllegalArgumentException("Value must be a valid Map", e);
202                 }
203             default:
204                 return null;
205         }
206     }
207
208     @Override
209     public String toString() {
210         return name().toLowerCase();
211     }
212 }