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=========================================================
20 package org.openecomp.sdc.be.model.tosca.constraints;
22 import com.google.common.collect.Lists;
23 import java.util.List;
24 import javax.validation.constraints.NotNull;
25 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
26 import org.openecomp.sdc.be.model.PropertyConstraint;
27 import org.openecomp.sdc.be.model.tosca.ToscaType;
28 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
29 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
30 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
31 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
33 public class InRangeConstraint extends AbstractPropertyConstraint {
35 private List<String> inRange;
36 private Comparable min;
37 private Comparable max;
39 public InRangeConstraint(List<String> inRange) {
40 this.inRange = inRange;
43 public InRangeConstraint() {
47 public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
48 // Perform verification that the property type is supported for
51 ConstraintUtil.checkComparableType(propertyType);
52 if (inRange == null || inRange.size() != 2) {
53 throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
55 String minRawText = inRange.get(0);
56 String maxRawText = inRange.get(1);
57 if (!propertyType.isValidValue(minRawText)) {
58 throw new ConstraintValueDoNotMatchPropertyTypeException(
59 "Invalid min value for in range constraint [" + minRawText + "] as it does not follow the property type [" + propertyType + "]");
61 if (!propertyType.isValidValue(maxRawText)) {
62 throw new ConstraintValueDoNotMatchPropertyTypeException(
63 "Invalid max value for in range constraint [" + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
65 min = ConstraintUtil.convertToComparable(propertyType, minRawText);
66 max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
70 public void validate(Object propertyValue) throws ConstraintViolationException {
71 if (propertyValue == null) {
72 throw new ConstraintViolationException("Value to check is null");
74 if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
75 throw new ConstraintViolationException(
76 "Value to check is not comparable to range type, value type [" + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
78 if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
79 throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
84 public ConstraintType getConstraintType() {
85 return ConstraintType.IN_RANGE;
89 public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
93 public String getRangeMinValue() {
94 if (inRange != null) {
95 return inRange.get(0);
101 public void setRangeMinValue(String minValue) {
102 if (inRange == null) {
103 inRange = Lists.newArrayList(minValue, "");
105 inRange.set(0, minValue);
110 public String getRangeMaxValue() {
111 if (inRange != null) {
112 return inRange.get(1);
118 public void setRangeMaxValue(String maxValue) {
119 if (inRange == null) {
120 inRange = Lists.newArrayList("", maxValue);
122 inRange.set(1, maxValue);
127 public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
128 return getErrorMessage(toscaType, e, propertyName, "%f property value must be between >= [%s] and <= [%s]", String.valueOf(min),
129 String.valueOf(max));