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 org.openecomp.sdc.be.model.tosca.ToscaType;
24 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
26 import java.beans.IntrospectionException;
27 import java.beans.Introspector;
28 import java.beans.PropertyDescriptor;
29 import java.lang.reflect.InvocationTargetException;
32 * Utility class to validate constraints types.
34 public final class ConstraintUtil {
36 private ConstraintUtil() {
40 * Validates that the {@link ToscaType} specified is a
41 * {@link ToscaType#STRING}.
44 * The property tosca type.
45 * @throws ConstraintValueDoNotMatchPropertyTypeException
46 * In case the type is not {@link ToscaType#STRING}.
48 public static void checkStringType(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
49 if (!ToscaType.STRING.equals(propertyType)) {
50 throw new ConstraintValueDoNotMatchPropertyTypeException(
51 "Invalid property type <" + propertyType.toString() + ">");
56 * Verify that the given tosca type is supported for comparison
59 * the tosca type to check
60 * @throws ConstraintValueDoNotMatchPropertyTypeException
61 * if the property type cannot be compared
63 public static void checkComparableType(ToscaType propertyType)
64 throws ConstraintValueDoNotMatchPropertyTypeException {
65 // The validity of the value is already assured by us with our
66 // ToscaType.convert() method
67 // here we just want to check that the constraint is not used on
68 // unsupported type as boolean
69 switch (propertyType) {
77 throw new ConstraintValueDoNotMatchPropertyTypeException(
78 "Constraint is invalid for property type <" + propertyType.toString() + ">");
80 throw new ConstraintValueDoNotMatchPropertyTypeException(
81 "Invalid property type <" + propertyType.toString() + ">");
86 * Convert a string value following its type throw exception if it cannot be
87 * converted to a comparable
90 * the type of the property
92 * the value to convert
93 * @return the converted comparable
94 * @throws ConstraintValueDoNotMatchPropertyTypeException
95 * if the converted value is not a comparable
97 @SuppressWarnings("rawtypes")
98 public static Comparable convertToComparable(ToscaType propertyType, String value)
99 throws ConstraintValueDoNotMatchPropertyTypeException {
100 Object comparableObj = propertyType.convert(value);
101 if (!(comparableObj instanceof Comparable)) {
102 throw new IllegalArgumentException(
103 "Try to convert a value of a type which is not comparable [" + propertyType + "] to Comparable");
105 return (Comparable) comparableObj;
109 public static class ConstraintInformation {
110 public ConstraintInformation(String name, Object reference, String value, String type) {
113 this.reference = reference;
120 private Object reference;
121 private String value;
125 public static ConstraintInformation getConstraintInformation(Object constraint) throws IntrospectionException {
126 PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(constraint.getClass())
127 .getPropertyDescriptors();
128 PropertyDescriptor firstDescriptor = null;
129 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
130 if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) {
131 firstDescriptor = propertyDescriptor;
135 if (firstDescriptor == null) {
136 throw new IntrospectionException("Cannot find constraint name");
139 return new ConstraintInformation(firstDescriptor.getName(),
140 firstDescriptor.getReadMethod().invoke(constraint), null, null);
141 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
142 throw new IntrospectionException("Cannot retrieve constraint reference " + e.getMessage());