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"),
52 SCALAR_UNIT("scalar-unit"),
53 SCALAR_UNIT_SIZE("scalar-unit.size"),
54 SCALAR_UNIT_TIME("scalar-unit.time"),
55 SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
59 private final String type;
61 public static ToscaType getToscaType(String typeName) {
62 if (typeName == null) {
65 for (ToscaType type : ToscaType.values()) {
66 if (type.getType().equals(typeName)) {
73 public static boolean isPrimitiveType(String dataTypeName) {
74 if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType().equals(dataTypeName)) {
75 return isValidType(dataTypeName) != null;
80 public static ToscaType isValidType(String typeName) {
81 if (typeName == null) {
84 for (ToscaType type : ToscaType.values()) {
85 if (type.getType().equals(typeName)) {
92 public static boolean isCollectionType(String type) {
93 return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type);
96 public boolean isValidValue(String value) {
99 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
101 return isFloat(value);
103 return isInteger(value);
106 case SCALAR_UNIT_SIZE:
107 case SCALAR_UNIT_TIME:
108 case SCALAR_UNIT_FREQUENCY:
111 return TimestampValidator.getInstance().isValid(value, null);
113 return VersionUtil.isValid(value);
115 return isList(value);
123 private boolean isList(String value) {
124 ObjectMapper objectMapper = new ObjectMapper();
126 objectMapper.readValue(value, new TypeReference<List<Object>>() {
128 } catch (IOException e) {
134 private boolean isMap(String value) {
135 ObjectMapper objectMapper = new ObjectMapper();
137 objectMapper.readValue(value, new TypeReference<Map<String, Object>>() {
139 } catch (IOException e) {
145 private boolean isFloat(String value) {
147 Float.valueOf(value);
148 } catch (NumberFormatException e) {
154 private boolean isInteger(String value) {
157 } catch (NumberFormatException e) {
163 public Object convert(String value) {
167 case SCALAR_UNIT_SIZE:
168 case SCALAR_UNIT_TIME:
169 case SCALAR_UNIT_FREQUENCY:
172 return Boolean.valueOf(value);
174 return Float.valueOf(value);
176 return Long.valueOf(value);
179 return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
180 } catch (ParseException e) {
181 throw new IllegalArgumentException("Value must be a valid timestamp", e);
184 return VersionUtil.parseVersion(value);
187 return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {
189 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
190 throw new IllegalArgumentException("Value must be a valid List", e);
194 return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {
196 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
197 throw new IllegalArgumentException("Value must be a valid Map", e);
205 public String toString() {
206 return name().toLowerCase();