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.text.SimpleDateFormat;
29 import java.util.List;
30 import java.util.Locale;
33 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
34 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
37 * The primitive type that TOSCA YAML supports.
41 public enum ToscaType {
46 TIMESTAMP("timestamp"),
50 SCALAR_UNIT("scalar-unit"),
51 SCALAR_UNIT_SIZE("scalar-unit.size"),
52 SCALAR_UNIT_TIME("scalar-unit.time"),
53 SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
57 ToscaType(String type) {
61 public String getType() {
65 public static ToscaType getToscaType(String typeName) {
66 if (typeName == null) {
70 for (ToscaType type : ToscaType.values()) {
71 if (type.getType().equals(typeName)) {
78 public boolean isValidValue(String value) {
81 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
83 return isFloat(value);
85 return isInteger(value);
88 case SCALAR_UNIT_SIZE:
89 case SCALAR_UNIT_TIME:
90 case SCALAR_UNIT_FREQUENCY:
94 new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
96 } catch (ParseException e) {
100 return VersionUtil.isValid(value);
102 return isList(value);
110 private boolean isList(String value) {
111 ObjectMapper objectMapper = new ObjectMapper();
113 objectMapper.readValue(value,
114 new TypeReference<List<Object>>() {
117 } catch (IOException e) {
124 private boolean isMap(String value) {
125 ObjectMapper objectMapper = new ObjectMapper();
127 objectMapper.readValue(value,
128 new TypeReference<Map<String, Object>>() {
131 } catch (IOException e) {
138 private boolean isFloat(String value) {
140 Float.valueOf(value);
141 } catch (NumberFormatException e) {
147 private boolean isInteger(String value) {
150 } catch (NumberFormatException e) {
156 public Object convert(String value) {
160 case SCALAR_UNIT_SIZE:
161 case SCALAR_UNIT_TIME:
162 case SCALAR_UNIT_FREQUENCY:
165 return Boolean.valueOf(value);
167 return Float.valueOf(value);
169 return Long.valueOf(value);
172 return new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US).parse(value);
173 } catch (ParseException e) {
174 throw new IllegalArgumentException("Value must be a valid timestamp", e);
177 return VersionUtil.parseVersion(value);
180 return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {});
181 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
182 throw new IllegalArgumentException("Value must be a valid List", e);
186 return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {});
187 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
188 throw new IllegalArgumentException("Value must be a valid Map", e);
195 public static boolean isPrimitiveType(String dataTypeName) {
197 if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType()
198 .equals(dataTypeName)) {
200 return isValidType(dataTypeName) != null;
206 public static ToscaType isValidType(String typeName) {
207 if (typeName == null) {
211 for (ToscaType type : ToscaType.values()) {
212 if (type.getType().equals(typeName)) {
220 public String toString() {
221 return name().toLowerCase();
224 public static boolean isCollectionType(String type) {
225 return ToscaPropertyType.MAP.getType().equals(type)
226 || ToscaPropertyType.LIST.getType().equals(type);