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.constraints;
23 import com.fasterxml.jackson.core.type.TypeReference;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import java.beans.IntrospectionException;
26 import java.beans.Introspector;
27 import java.beans.PropertyDescriptor;
28 import java.io.IOException;
29 import java.lang.reflect.InvocationTargetException;
31 import org.openecomp.sdc.be.model.tosca.ToscaType;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Utility class to validate constraints types.
39 public final class ConstraintUtil {
41 private static final Logger logger = LoggerFactory.getLogger(ConstraintUtil.class);
43 private ConstraintUtil() {
47 * Validates that the {@link ToscaType} specified is a
48 * {@link ToscaType#STRING}.
51 * The property tosca type.
52 * @throws ConstraintValueDoNotMatchPropertyTypeException
53 * In case the type is not {@link ToscaType#STRING}.
55 public static void checkStringType(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
56 if (ToscaType.STRING != propertyType) {
57 throw new ConstraintValueDoNotMatchPropertyTypeException(
58 "Invalid property type <" + propertyType.toString() + ">");
63 * Verify that the given tosca type is supported for comparison
66 * the tosca type to check
67 * @throws ConstraintValueDoNotMatchPropertyTypeException
68 * if the property type cannot be compared
70 public static void checkComparableType(ToscaType propertyType)
71 throws ConstraintValueDoNotMatchPropertyTypeException {
72 // The validity of the value is already assured by us with our
73 // ToscaType.convert() method
74 // here we just want to check that the constraint is not used on
75 // unsupported type as boolean
76 switch (propertyType) {
84 throw new ConstraintValueDoNotMatchPropertyTypeException(
85 "Constraint is invalid for property type <" + propertyType.toString() + ">");
87 throw new ConstraintValueDoNotMatchPropertyTypeException(
88 "Invalid property type <" + propertyType.toString() + ">");
93 * Convert a string value following its type throw exception if it cannot be
94 * converted to a comparable
97 * the type of the property
99 * the value to convert
100 * @return the converted comparable
101 * @throws ConstraintValueDoNotMatchPropertyTypeException
102 * if the converted value is not a comparable
104 @SuppressWarnings("rawtypes")
105 public static Comparable convertToComparable(ToscaType propertyType, String value) {
106 Object comparableObj = propertyType.convert(value);
107 if (!(comparableObj instanceof Comparable)) {
108 throw new IllegalArgumentException(
109 "Try to convert a value of a type which is not comparable [" + propertyType + "] to Comparable");
111 return (Comparable) comparableObj;
115 public static class ConstraintInformation {
116 public ConstraintInformation(String name, Object reference, String value, String type) {
119 this.reference = reference;
126 private Object reference;
127 private String value;
131 public static ConstraintInformation getConstraintInformation(Object constraint) throws IntrospectionException {
132 PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(constraint.getClass())
133 .getPropertyDescriptors();
134 PropertyDescriptor firstDescriptor = null;
135 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
136 if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) {
137 firstDescriptor = propertyDescriptor;
141 if (firstDescriptor == null) {
142 throw new IntrospectionException("Cannot find constraint name");
145 return new ConstraintInformation(firstDescriptor.getName(),
146 firstDescriptor.getReadMethod().invoke(constraint), null, null);
147 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
148 throw new IntrospectionException("Cannot retrieve constraint reference " + e.getMessage());
152 public static <T> T parseToCollection(String value, TypeReference<T> typeReference)
153 throws ConstraintValueDoNotMatchPropertyTypeException {
155 ObjectMapper objectMapper = new ObjectMapper();
157 objectMap = objectMapper.readValue(value, typeReference);
158 } catch (IOException e) {
159 logger.error(e.getMessage(), e);
160 throw new ConstraintValueDoNotMatchPropertyTypeException("The value [" + value + "] is not valid");