Catalog alignment
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / constraints / InRangeConstraint.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.google.common.collect.Lists;
24 import org.openecomp.sdc.be.model.PropertyConstraint;
25 import java.util.List;
26 import org.openecomp.sdc.be.model.tosca.ToscaType;
27 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
28 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
29 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
30 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
31
32 import javax.validation.constraints.NotNull;
33
34 public class InRangeConstraint extends AbstractPropertyConstraint {
35
36     private List<String> inRange;
37
38     private Comparable min;
39     private Comparable max;
40
41     public InRangeConstraint(List<String> inRange) {
42         this.inRange = inRange;
43     }
44
45     public InRangeConstraint() { }
46
47     @Override
48     public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
49         // Perform verification that the property type is supported for
50         // comparison
51         ConstraintUtil.checkComparableType(propertyType);
52         if (inRange == null || inRange.size() != 2) {
53             throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
54         }
55         String minRawText = inRange.get(0);
56         String maxRawText = inRange.get(1);
57         if (!propertyType.isValidValue(minRawText)) {
58             throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid min value for in range constraint ["
59                     + minRawText + "] as it does not follow the property type [" + propertyType + "]");
60         }
61         if (!propertyType.isValidValue(maxRawText)) {
62             throw new ConstraintValueDoNotMatchPropertyTypeException("Invalid max value for in range constraint ["
63                     + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
64         }
65         min = ConstraintUtil.convertToComparable(propertyType, minRawText);
66         max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
67     }
68
69     @Override
70     public void validate(Object propertyValue) throws ConstraintViolationException {
71         if (propertyValue == null) {
72             throw new ConstraintViolationException("Value to check is null");
73         }
74         if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
75             throw new ConstraintViolationException("Value to check is not comparable to range type, value type ["
76                     + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
77         }
78         if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
79             throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
80         }
81     }
82
83     @Override
84     public ConstraintType getConstraintType() {
85         return ConstraintType.IN_RANGE;
86     }
87
88     @Override
89     public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
90
91     }
92
93     @NotNull
94     public String getRangeMinValue() {
95         if (inRange != null) {
96             return inRange.get(0);
97         } else {
98             return null;
99         }
100     }
101
102     public void setRangeMinValue(String minValue) {
103         if (inRange == null) {
104             inRange = Lists.newArrayList(minValue, "");
105         } else {
106             inRange.set(0, minValue);
107         }
108     }
109
110     @NotNull
111     public String getRangeMaxValue() {
112         if (inRange != null) {
113             return inRange.get(1);
114         } else {
115             return null;
116         }
117     }
118
119     public void setRangeMaxValue(String maxValue) {
120         if (inRange == null) {
121             inRange = Lists.newArrayList("", maxValue);
122         } else {
123             inRange.set(1, maxValue);
124         }
125     }
126
127     @Override
128     public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
129         return getErrorMessage(toscaType, e, propertyName, "%f property value must be between >= [%s] and <= [%s]",
130                 String.valueOf(min), String.valueOf(max));
131     }
132
133 }