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=========================================================
20 package org.openecomp.sdc.be.model.tosca;
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;
30 import lombok.AllArgsConstructor;
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.
41 public enum ToscaType {
47 TIMESTAMP("timestamp"),
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");
58 private final String type;
60 public static ToscaType getToscaType(String typeName) {
61 if (typeName == null) {
64 for (ToscaType type : ToscaType.values()) {
65 if (type.getType().equals(typeName)) {
72 public static boolean isPrimitiveType(String dataTypeName) {
73 if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType().equals(dataTypeName)) {
74 return isValidType(dataTypeName) != null;
79 public static ToscaType isValidType(String typeName) {
80 if (typeName == null) {
83 for (ToscaType type : ToscaType.values()) {
84 if (type.getType().equals(typeName)) {
91 public static boolean isCollectionType(String type) {
92 return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type);
95 public boolean isValidValue(String value) {
98 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
100 return isFloat(value);
102 return isInteger(value);
105 case SCALAR_UNIT_SIZE:
106 case SCALAR_UNIT_TIME:
107 case SCALAR_UNIT_FREQUENCY:
111 new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
113 } catch (ParseException e) {
117 return VersionUtil.isValid(value);
119 return isList(value);
127 private boolean isList(String value) {
128 ObjectMapper objectMapper = new ObjectMapper();
130 objectMapper.readValue(value, new TypeReference<List<Object>>() {
132 } catch (IOException e) {
138 private boolean isMap(String value) {
139 ObjectMapper objectMapper = new ObjectMapper();
141 objectMapper.readValue(value, new TypeReference<Map<String, Object>>() {
143 } catch (IOException e) {
149 private boolean isFloat(String value) {
151 Float.valueOf(value);
152 } catch (NumberFormatException e) {
158 private boolean isInteger(String value) {
161 } catch (NumberFormatException e) {
167 public Object convert(String value) {
171 case SCALAR_UNIT_SIZE:
172 case SCALAR_UNIT_TIME:
173 case SCALAR_UNIT_FREQUENCY:
176 return Boolean.valueOf(value);
178 return Float.valueOf(value);
180 return Long.valueOf(value);
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);
188 return VersionUtil.parseVersion(value);
191 return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {
193 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
194 throw new IllegalArgumentException("Value must be a valid List", e);
198 return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {
200 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
201 throw new IllegalArgumentException("Value must be a valid Map", e);
209 public String toString() {
210 return name().toLowerCase();