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;
26 import lombok.NoArgsConstructor;
27 import lombok.NonNull;
29 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
30 import org.openecomp.sdc.be.model.PropertyConstraint;
31 import org.openecomp.sdc.be.model.tosca.ToscaType;
32 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException;
33 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
34 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException;
35 import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException;
40 public class InRangeConstraint extends AbstractPropertyConstraint {
43 private List<Object> inRange;
44 private Comparable min;
45 private Comparable max;
47 public InRangeConstraint(List<Object> inRange) {
48 this.inRange = inRange;
52 public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
53 // Perform verification that the property type is supported for
56 ConstraintUtil.checkComparableType(propertyType);
57 if (inRange == null || inRange.size() != 2) {
58 throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements.");
60 String minRawText = String.valueOf(inRange.get(0));
61 String maxRawText = String.valueOf(inRange.get(1));
62 if (!propertyType.isValidValue(minRawText)) {
63 throw new ConstraintValueDoNotMatchPropertyTypeException(
64 "Invalid min value for in range constraint [" + minRawText + "] as it does not follow the property type [" + propertyType + "]");
66 if (!propertyType.isValidValue(maxRawText)) {
67 throw new ConstraintValueDoNotMatchPropertyTypeException(
68 "Invalid max value for in range constraint [" + maxRawText + "] as it does not follow the property type [" + propertyType + "]");
70 min = ConstraintUtil.convertToComparable(propertyType, minRawText);
71 max = ConstraintUtil.convertToComparable(propertyType, maxRawText);
75 public void validate(Object propertyValue) throws ConstraintViolationException {
76 if (propertyValue == null) {
77 throw new ConstraintViolationException("Value to check is null");
79 if (!(min.getClass().isAssignableFrom(propertyValue.getClass()))) {
80 throw new ConstraintViolationException(
81 "Value to check is not comparable to range type, value type [" + propertyValue.getClass() + "], range type [" + min.getClass() + "]");
83 if (min.compareTo(propertyValue) > 0 || max.compareTo(propertyValue) < 0) {
84 throw new ConstraintViolationException("The value [" + propertyValue + "] is out of range " + inRange);
89 public ConstraintType getConstraintType() {
90 return ConstraintType.IN_RANGE;
94 public void validateValueOnUpdate(PropertyConstraint newConstraint) throws PropertyConstraintException {
98 public Object getRangeMinValue() {
99 if (inRange != null) {
100 return inRange.get(0);
106 public void setRangeMinValue(Object minValue) {
107 if (inRange == null) {
108 inRange = Lists.newArrayList(minValue, null);
110 inRange.set(0, minValue);
115 public Object getRangeMaxValue() {
116 if (inRange != null) {
117 return inRange.get(1);
123 public void setRangeMaxValue(Object maxValue) {
124 if (inRange == null) {
125 inRange = Lists.newArrayList(null, maxValue);
127 inRange.set(1, maxValue);
132 public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) {
133 return getErrorMessage(toscaType, e, propertyName, "%s property value must be in a range of %s", String.valueOf(min),
134 String.valueOf(max));
137 public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
138 ToscaType toscaType = ToscaType.getToscaType(propertyType);
139 if (toscaType == null) {
140 throw new ConstraintValueDoNotMatchPropertyTypeException(
141 "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");
143 if (inRange == null) {
144 throw new ConstraintValueDoNotMatchPropertyTypeException(
145 "inRange constraint has invalid value <> property type is <" + propertyType + ">");
147 for (Object value : inRange) {
148 if (!toscaType.isValueTypeValid(value)) {
155 public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
156 ToscaType toscaType = ToscaType.getToscaType(propertyType);
158 inRange.replaceAll(obj -> toscaType.convert(String.valueOf(obj)));
159 } catch (Exception e) {
160 throw new ConstraintValueDoNotMatchPropertyTypeException(
161 "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">");