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