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;
34 import org.openecomp.sdc.be.model.tosca.validators.TimestampValidator;
37 * The primitive type that TOSCA YAML supports.
42 public enum ToscaType {
48 TIMESTAMP("timestamp"),
53 SCALAR_UNIT("scalar-unit"),
54 SCALAR_UNIT_SIZE("scalar-unit.size"),
55 SCALAR_UNIT_TIME("scalar-unit.time"),
56 SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
60 private final String type;
62 public static ToscaType getToscaType(String typeName) {
63 if (typeName == null) {
66 for (ToscaType type : ToscaType.values()) {
67 if (type.getType().equals(typeName)) {
74 public static boolean isPrimitiveType(String dataTypeName) {
75 if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType().equals(dataTypeName)) {
76 return isValidType(dataTypeName) != null;
81 public static ToscaType isValidType(String typeName) {
82 if (typeName == null) {
85 for (ToscaType type : ToscaType.values()) {
86 if (type.getType().equals(typeName)) {
93 public static boolean isCollectionType(String type) {
94 return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type);
97 public Boolean isValueTypeValid(Object value) {
100 return value.equals(true) || value.equals(false);
102 return value instanceof Float;
105 return value instanceof Integer;
108 case SCALAR_UNIT_SIZE:
109 case SCALAR_UNIT_TIME:
110 case SCALAR_UNIT_FREQUENCY:
113 return value instanceof String;
122 public boolean isValidValue(String value) {
125 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
127 return isFloat(value);
129 return isInteger(value);
132 case SCALAR_UNIT_SIZE:
133 case SCALAR_UNIT_TIME:
134 case SCALAR_UNIT_FREQUENCY:
137 return TimestampValidator.getInstance().isValid(value, null);
139 return VersionUtil.isValid(value);
141 return isList(value);
149 private boolean isList(String value) {
150 ObjectMapper objectMapper = new ObjectMapper();
152 objectMapper.readValue(value, new TypeReference<List<Object>>() {
154 } catch (IOException e) {
160 private boolean isMap(String value) {
161 ObjectMapper objectMapper = new ObjectMapper();
163 objectMapper.readValue(value, new TypeReference<Map<String, Object>>() {
165 } catch (IOException e) {
171 private boolean isFloat(String value) {
173 Float.valueOf(value);
174 } catch (NumberFormatException e) {
180 private boolean isInteger(String value) {
183 } catch (NumberFormatException e) {
189 public Object convert(String value) {
193 case SCALAR_UNIT_SIZE:
194 case SCALAR_UNIT_TIME:
195 case SCALAR_UNIT_FREQUENCY:
198 return Boolean.valueOf(value);
200 return Float.valueOf(value);
203 return Long.valueOf(value);
206 return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
207 } catch (ParseException e) {
208 throw new IllegalArgumentException("Value must be a valid timestamp", e);
211 return VersionUtil.parseVersion(value);
214 return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {
216 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
217 throw new IllegalArgumentException("Value must be a valid List", e);
221 return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {
223 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
224 throw new IllegalArgumentException("Value must be a valid Map", e);
232 public String toString() {
233 return name().toLowerCase();