2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.tosca;
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;
32 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
36 * The primitive type that TOSCA YAML supports.
40 public enum ToscaType {
45 TIMESTAMP("timestamp"),
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");
56 ToscaType(String type) {
60 public String getType() {
64 public static ToscaType getToscaType(String typeName) {
65 if (typeName == null) {
69 for (ToscaType type : ToscaType.values()) {
70 if (type.getType().equals(typeName)) {
77 public boolean isValidValue(String value) {
80 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
82 return isFloat(value);
84 return isInteger(value);
87 case SCALAR_UNIT_SIZE:
88 case SCALAR_UNIT_TIME:
89 case SCALAR_UNIT_FREQUENCY:
93 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
95 } catch (ParseException e) {
99 return VersionUtil.isValid(value);
101 return isList(value);
109 private boolean isList(String value) {
110 ObjectMapper objectMapper = new ObjectMapper();
112 objectMapper.readValue(value,
113 new TypeReference<List<Object>>() {
116 } catch (IOException e) {
123 private boolean isMap(String value) {
124 ObjectMapper objectMapper = new ObjectMapper();
126 objectMapper.readValue(value,
127 new TypeReference<Map<String, Object>>() {
130 } catch (IOException e) {
137 private boolean isFloat(String value) {
139 Float.valueOf(value);
140 } catch (NumberFormatException e) {
146 private boolean isInteger(String value) {
149 } catch (NumberFormatException e) {
155 public Object convert(String value) {
159 case SCALAR_UNIT_SIZE:
160 case SCALAR_UNIT_TIME:
161 case SCALAR_UNIT_FREQUENCY:
164 return Boolean.valueOf(value);
166 return Float.valueOf(value);
168 return Long.valueOf(value);
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);
176 return VersionUtil.parseVersion(value);
179 return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {});
180 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
181 throw new IllegalArgumentException("Value must be a valid List", e);
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);
194 public static boolean isPrimitiveType(String dataTypeName) {
196 if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType()
197 .equals(dataTypeName)) {
199 return isValidType(dataTypeName) != null;
205 public static ToscaType isValidType(String typeName) {
206 if (typeName == null) {
210 for (ToscaType type : ToscaType.values()) {
211 if (type.getType().equals(typeName)) {
219 public String toString() {
220 return name().toLowerCase();
223 public static boolean isCollectionType(String type) {
224 return ToscaPropertyType.MAP.getType().equals(type)
225 || ToscaPropertyType.LIST.getType().equals(type);