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