re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / ConstraintUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.tosca.constraints;
22
23 import org.openecomp.sdc.be.model.tosca.ToscaType;
24 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
25
26 import java.beans.IntrospectionException;
27 import java.beans.Introspector;
28 import java.beans.PropertyDescriptor;
29 import java.lang.reflect.InvocationTargetException;
30
31 /**
32  * Utility class to validate constraints types.
33  */
34 public final class ConstraintUtil {
35
36     private ConstraintUtil() {
37     }
38
39     /**
40      * Validates that the {@link ToscaType} specified is a
41      * {@link ToscaType#STRING}.
42      *
43      * @param propertyType
44      *            The property tosca type.
45      * @throws ConstraintValueDoNotMatchPropertyTypeException
46      *             In case the type is not {@link ToscaType#STRING}.
47      */
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() + ">");
52         }
53     }
54
55     /**
56      * Verify that the given tosca type is supported for comparison
57      *
58      * @param propertyType
59      *            the tosca type to check
60      * @throws ConstraintValueDoNotMatchPropertyTypeException
61      *             if the property type cannot be compared
62      */
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) {
70         case FLOAT:
71         case INTEGER:
72         case TIMESTAMP:
73         case VERSION:
74             break;
75         case STRING:
76         case BOOLEAN:
77             throw new ConstraintValueDoNotMatchPropertyTypeException(
78                     "Constraint is invalid for property type <" + propertyType.toString() + ">");
79         default:
80             throw new ConstraintValueDoNotMatchPropertyTypeException(
81                     "Invalid property type <" + propertyType.toString() + ">");
82         }
83     }
84
85     /**
86      * Convert a string value following its type throw exception if it cannot be
87      * converted to a comparable
88      *
89      * @param propertyType
90      *            the type of the property
91      * @param value
92      *            the value to convert
93      * @return the converted comparable
94      * @throws ConstraintValueDoNotMatchPropertyTypeException
95      *             if the converted value is not a comparable
96      */
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");
104         } else {
105             return (Comparable) comparableObj;
106         }
107     }
108
109     public static class ConstraintInformation {
110         public ConstraintInformation(String name, Object reference, String value, String type) {
111
112             this.name = name;
113             this.reference = reference;
114             this.value = value;
115             this.type = type;
116
117         }
118
119         private String name;
120         private Object reference;
121         private String value;
122         private String type;
123     }
124
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;
132                 break;
133             }
134         }
135         if (firstDescriptor == null) {
136             throw new IntrospectionException("Cannot find constraint name");
137         }
138         try {
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());
143         }
144     }
145 }