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