Declare properties as policies
[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 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;
30
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;
35
36 /**
37  * Utility class to validate constraints types.
38  */
39 public final class ConstraintUtil {
40
41     private static final Logger logger = LoggerFactory.getLogger(ConstraintUtil.class);
42
43     private ConstraintUtil() {
44     }
45
46     /**
47      * Validates that the {@link ToscaType} specified is a
48      * {@link ToscaType#STRING}.
49      *
50      * @param propertyType
51      *            The property tosca type.
52      * @throws ConstraintValueDoNotMatchPropertyTypeException
53      *             In case the type is not {@link ToscaType#STRING}.
54      */
55     public static void checkStringType(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
56         if (ToscaType.STRING != propertyType) {
57             throw new ConstraintValueDoNotMatchPropertyTypeException(
58                     "Invalid property type <" + propertyType.toString() + ">");
59         }
60     }
61
62     /**
63      * Verify that the given tosca type is supported for comparison
64      *
65      * @param propertyType
66      *            the tosca type to check
67      * @throws ConstraintValueDoNotMatchPropertyTypeException
68      *             if the property type cannot be compared
69      */
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) {
77         case FLOAT:
78         case INTEGER:
79         case TIMESTAMP:
80         case VERSION:
81             break;
82         case STRING:
83         case BOOLEAN:
84             throw new ConstraintValueDoNotMatchPropertyTypeException(
85                     "Constraint is invalid for property type <" + propertyType.toString() + ">");
86         default:
87             throw new ConstraintValueDoNotMatchPropertyTypeException(
88                     "Invalid property type <" + propertyType.toString() + ">");
89         }
90     }
91
92     /**
93      * Convert a string value following its type throw exception if it cannot be
94      * converted to a comparable
95      *
96      * @param propertyType
97      *            the type of the property
98      * @param value
99      *            the value to convert
100      * @return the converted comparable
101      * @throws ConstraintValueDoNotMatchPropertyTypeException
102      *             if the converted value is not a comparable
103      */
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");
110         } else {
111             return (Comparable) comparableObj;
112         }
113     }
114
115     public static class ConstraintInformation {
116         public ConstraintInformation(String name, Object reference, String value, String type) {
117
118             this.name = name;
119             this.reference = reference;
120             this.value = value;
121             this.type = type;
122
123         }
124
125         private String name;
126         private Object reference;
127         private String value;
128         private String type;
129     }
130
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;
138                 break;
139             }
140         }
141         if (firstDescriptor == null) {
142             throw new IntrospectionException("Cannot find constraint name");
143         }
144         try {
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());
149         }
150     }
151
152     public static <T> T parseToCollection(String value, TypeReference<T> typeReference)
153             throws ConstraintValueDoNotMatchPropertyTypeException {
154         T objectMap;
155         ObjectMapper objectMapper = new ObjectMapper();
156         try {
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");
161         }
162
163         return objectMap;
164     }
165 }