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