diff options
author | siddharth0905 <siddharth.singh4@amdocs.com> | 2019-04-10 17:49:51 +0530 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2019-04-11 07:11:04 +0000 |
commit | 6896c1b309aaa50dca820169b9f1ae3f8af84294 (patch) | |
tree | 8da13382863f2e4adc9724dd046b535032ae58c7 /catalog-model/src/main/java/org | |
parent | 4568e48647b15b5654a669651ddf536d590288b0 (diff) |
Apply Valid Value Constraints validation
Apply Valid Value Constraints validation for FE and BE in
Property Assignment, Input, Service Consumption screen
Change-Id: I01c7523bad702f003cd52fd88bc69fe950b2b4f3
Issue-ID: SDC-2224
Signed-off-by: siddharth0905 <siddharth.singh4@amdocs.com>
Diffstat (limited to 'catalog-model/src/main/java/org')
18 files changed, 449 insertions, 103 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/OperationInput.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/OperationInput.java new file mode 100644 index 0000000000..e11bf9620b --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/OperationInput.java @@ -0,0 +1,36 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openecomp.sdc.be.model; + +import java.util.List; + +import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; + +public class OperationInput extends OperationInputDefinition { + private List<PropertyConstraint> constraints; + + public OperationInput(OperationInputDefinition operationInputDefinition) { + super(operationInputDefinition); + } + + public List<PropertyConstraint> getConstraints() { + return constraints; + } + + public void setConstraints(List<PropertyConstraint> constraints) { + this.constraints = constraints; + } +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java index 9a5c52dde4..ff9a6e5018 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/PropertyConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; @@ -31,4 +32,6 @@ public interface PropertyConstraint { void validate(Object propertyValue) throws ConstraintViolationException; void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException; + + String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException exception, String propertyName); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java index 0ee7c9e5bd..10dd919fc7 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java @@ -955,7 +955,7 @@ public class ToscaOperationFacade { } GraphVertex vertex = getVertexEither.left().value(); - Map<String, PropertyDataDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDataDefinition(e.getValue()))); + Map<String, PropertyDefinition> inputsMap = inputs.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new PropertyDefinition(e.getValue()))); StorageOperationStatus status = topologyTemplateOperation.addToscaDataToToscaElement(vertex, EdgeLabelEnum.INPUTS, VertexTypeEnum.INPUTS, inputsMap, JsonPresentationFields.NAME); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java index 92c564cad6..6384146df9 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java @@ -20,9 +20,17 @@ package org.openecomp.sdc.be.model.tosca; +import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; +import java.util.List; import java.util.Locale; +import java.util.Map; + +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; /** * The primitive type that TOSCA YAML supports. @@ -30,31 +38,101 @@ import java.util.Locale; * @author mkv */ public enum ToscaType { - STRING, INTEGER, FLOAT, BOOLEAN, TIMESTAMP, VERSION; - - public boolean isValidValue(String value) { - switch (this) { - case BOOLEAN: - return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false"); - case FLOAT: - return isFloat(value); - case INTEGER: - return isInteger(value); - case STRING: - return true; - case TIMESTAMP: - try { - DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value); - return true; - } catch (ParseException e) { - return false; - } - case VERSION: - return VersionUtil.isValid(value); - default: - return false; - } - } + STRING("string"), + INTEGER("integer"), + FLOAT("float"), + BOOLEAN("boolean"), + TIMESTAMP("timestamp"), + VERSION("version"), + LIST("list"), + MAP("map"), + SCALAR_UNIT("scalar-unit"), + SCALAR_UNIT_SIZE("scalar-unit.size"), + SCALAR_UNIT_TIME("scalar-unit.time"), + SCALAR_UNIT_FREQUENCY("scalar-unit.frequency"); + + private String type; + + ToscaType(String type) { + this.type = type; + } + + public String getType() { + return type; + } + + public ToscaType getToscaType(String typeName) { + if (typeName == null) { + return null; + } + + for (ToscaType type : ToscaType.values()) { + if (type.getType().equals(typeName)) { + return type; + } + } + return null; + } + + public boolean isValidValue(String value) { + switch (this) { + case BOOLEAN: + return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false"); + case FLOAT: + return isFloat(value); + case INTEGER: + return isInteger(value); + case STRING: + case SCALAR_UNIT: + case SCALAR_UNIT_SIZE: + case SCALAR_UNIT_TIME: + case SCALAR_UNIT_FREQUENCY: + return true; + case TIMESTAMP: + try { + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value); + return true; + } catch (ParseException e) { + return false; + } + case VERSION: + return VersionUtil.isValid(value); + case LIST: + return isList(value); + case MAP: + return isMap(value); + default: + return false; + } + } + + private boolean isList(String value) { + ObjectMapper objectMapper = new ObjectMapper(); + try { + objectMapper.readValue(value, + new TypeReference<List<Object>>() { + }); + + } catch (IOException e) { + return false; + } + + return true; + } + + private boolean isMap(String value) { + ObjectMapper objectMapper = new ObjectMapper(); + try { + objectMapper.readValue(value, + new TypeReference<Map<String, Object>>() { + }); + + } catch (IOException e) { + return false; + } + + return true; + } private boolean isFloat(String value) { try { @@ -74,31 +152,76 @@ public enum ToscaType { return true; } - public Object convert(String value) { - switch (this) { - case STRING: - return value; - case BOOLEAN: - return Boolean.valueOf(value); - case FLOAT: - return Double.valueOf(value); - case INTEGER: - return Long.valueOf(value); - case TIMESTAMP: - try { - return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value); - } catch (ParseException e) { - throw new IllegalArgumentException("Value must be a valid timestamp", e); - } - case VERSION: - return VersionUtil.parseVersion(value); - default: - return null; - } - } + public Object convert(String value) { + switch (this) { + case STRING: + case SCALAR_UNIT: + case SCALAR_UNIT_SIZE: + case SCALAR_UNIT_TIME: + case SCALAR_UNIT_FREQUENCY: + return value; + case BOOLEAN: + return Boolean.valueOf(value); + case FLOAT: + return Double.valueOf(value); + case INTEGER: + return Long.valueOf(value); + case TIMESTAMP: + try { + return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value); + } catch (ParseException e) { + throw new IllegalArgumentException("Value must be a valid timestamp", e); + } + case VERSION: + return VersionUtil.parseVersion(value); + case LIST: + try { + return ConstraintUtil.parseToCollection(value, new TypeReference<List<Object>>() {}); + } catch (ConstraintValueDoNotMatchPropertyTypeException e) { + throw new IllegalArgumentException("Value must be a valid timestamp", e); + } + case MAP: + try { + return ConstraintUtil.parseToCollection(value, new TypeReference<Map<String, Object>>() {}); + } catch (ConstraintValueDoNotMatchPropertyTypeException e) { + throw new IllegalArgumentException("Value must be a valid timestamp", e); + } + default: + return null; + } + } + + public static boolean isPrimitiveType(String dataTypeName) { - @Override + if (!ToscaPropertyType.MAP.getType().equals(dataTypeName) && !ToscaPropertyType.LIST.getType() + .equals(dataTypeName)) { + + return isValidType(dataTypeName) != null; + } + + return false; + } + + public static ToscaType isValidType(String typeName) { + if (typeName == null) { + return null; + } + + for (ToscaType type : ToscaType.values()) { + if (type.getType().equals(typeName)) { + return type; + } + } + return null; + } + + @Override public String toString() { return name().toLowerCase(); } + + public static boolean isCollectionType(String type) { + return ToscaPropertyType.MAP.getType().equals(type) + || ToscaPropertyType.LIST.getType().equals(type); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java index fc96373d62..041a86d283 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java @@ -20,13 +20,20 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.util.Arrays; + import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.version.ApplicationVersionException; public abstract class AbstractPropertyConstraint implements PropertyConstraint { + private static final String INVALID_VALUE_ERROR_MESSAGE = + "Unsupported value provided for %s property supported value type is %s."; + @Override public void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException { try { @@ -36,4 +43,21 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { "String value [" + propertyTextValue + "] is not valid for type [" + toscaType + "]", e); } } + + public String getErrorMessage(ToscaType toscaType, + ConstraintFunctionalException e, + String propertyName, + String errorMessage, + String... propertyValue) { + if (e instanceof ConstraintViolationException) { + return String.format(errorMessage, propertyName, Arrays.toString(propertyValue)); + } + + return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType()); + } + + @Override + public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + //Initialization not needed for few constraints for now might be needed in future + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintType.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintType.java index 6fb699124a..d1cc766525 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintType.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintType.java @@ -25,9 +25,9 @@ import java.util.List; public enum ConstraintType { - EQUAL("equal", "equal"), + EQUAL("equal", "equal"), - IN_RANGE("inRange"), + IN_RANGE("inRange","in_range"), GREATER_THAN("greaterThan", "greater_than"), @@ -35,11 +35,17 @@ public enum ConstraintType { LESS_OR_EQUAL("lessOrEqual", "less_or_equal"), + LENGTH("length", "length"), + MIN_LENGTH("minLength", "min_length"), + MAX_LENGTH("maxLength", "max_length"), + VALID_VALUES("validValues", "valid_values"), - LESS_THAN("lessThan", "less_than"); + LESS_THAN("lessThan", "less_than"), + + SCHEMA("schema", "schema"); List<String> types; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java index 3c1f0ab711..0bbc0f755f 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ConstraintUtil.java @@ -20,19 +20,26 @@ package org.openecomp.sdc.be.model.tosca.constraints; -import org.openecomp.sdc.be.model.tosca.ToscaType; -import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; - import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Utility class to validate constraints types. */ public final class ConstraintUtil { + private static final Logger logger = LoggerFactory.getLogger(ConstraintUtil.class); + private ConstraintUtil() { } @@ -46,7 +53,7 @@ public final class ConstraintUtil { * In case the type is not {@link ToscaType#STRING}. */ public static void checkStringType(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - if (!ToscaType.STRING.equals(propertyType)) { + if (ToscaType.STRING != propertyType) { throw new ConstraintValueDoNotMatchPropertyTypeException( "Invalid property type <" + propertyType.toString() + ">"); } @@ -95,8 +102,7 @@ public final class ConstraintUtil { * if the converted value is not a comparable */ @SuppressWarnings("rawtypes") - public static Comparable convertToComparable(ToscaType propertyType, String value) - throws ConstraintValueDoNotMatchPropertyTypeException { + public static Comparable convertToComparable(ToscaType propertyType, String value) { Object comparableObj = propertyType.convert(value); if (!(comparableObj instanceof Comparable)) { throw new IllegalArgumentException( @@ -142,4 +148,18 @@ public final class ConstraintUtil { throw new IntrospectionException("Cannot retrieve constraint reference " + e.getMessage()); } } + + public static <T> T parseToCollection(String value, TypeReference<T> typeReference) + throws ConstraintValueDoNotMatchPropertyTypeException { + T objectMap; + ObjectMapper objectMapper = new ObjectMapper(); + try { + objectMap = objectMapper.readValue(value, typeReference); + } catch (IOException e) { + logger.error(e.getMessage(), e); + throw new ConstraintValueDoNotMatchPropertyTypeException("The value [" + value + "] is not valid"); + } + + return objectMap; + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java index 7ae64e0ccf..206fc3dc82 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/EqualConstraint.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,34 +20,41 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.io.Serializable; + import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -public class EqualConstraint extends AbstractPropertyConstraint { - - @NotNull - private String equal; +@SuppressWarnings("serial") +public class EqualConstraint extends AbstractPropertyConstraint implements Serializable { - // @JsonIgnore + @NotNull + private String constraintValue; private Object typed; - public EqualConstraint(String equal) { - super(); - this.equal = equal; - } + public EqualConstraint(String constraintValue) { + super(); + this.constraintValue = constraintValue; + } - @Override - public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - if (propertyType.isValidValue(equal)) { - typed = propertyType.convert(equal); - } else { - throw new ConstraintValueDoNotMatchPropertyTypeException("equal constraint has invalid value <" + equal - + "> property type is <" + propertyType.toString() + ">"); - } - } + public String getConstraintValue() { + return constraintValue; + } + + @Override + public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + if (propertyType.isValidValue(constraintValue)) { + typed = propertyType.convert(constraintValue); + } else { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "constraintValue constraint has invalid value <" + constraintValue + + "> property type is <" + propertyType.toString() + ">"); + } + } @Override public void validate(Object propertyValue) throws ConstraintViolationException { @@ -57,13 +64,20 @@ public class EqualConstraint extends AbstractPropertyConstraint { } } else if (typed == null) { fail(propertyValue); + } else if (typed instanceof Comparable && typed != propertyValue) { + fail(propertyValue); } else if (!typed.equals(propertyValue)) { fail(propertyValue); } } - private void fail(Object propertyValue) throws ConstraintViolationException { - throw new ConstraintViolationException("Equal constraint violation, the reference is <" + equal - + "> but the value to compare is <" + propertyValue + ">"); + private void fail(Object propertyValue) throws ConstraintViolationException { + throw new ConstraintViolationException("Equal constraint violation, the reference is <" + constraintValue + + "> but the value to compare is <" + propertyValue + ">"); + } + + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", constraintValue); } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java index 63d7ed8872..6549bbc432 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterOrEqualConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.tosca.constraints; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; @@ -46,4 +47,9 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai throw new ConstraintViolationException(propertyValue + " <= " + greaterOrEqual); } } + + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%f property value must be >= %f", greaterOrEqual); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java index b599a16a10..a278e8c7b9 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/GreaterThanConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.tosca.constraints; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; @@ -28,13 +29,13 @@ import javax.validation.constraints.NotNull; public class GreaterThanConstraint extends AbstractComparablePropertyConstraint { + @NotNull + private String greaterThan; + public GreaterThanConstraint(String greaterThan) { this.greaterThan = greaterThan; } - @NotNull - private String greaterThan; - @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { initialize(greaterThan, propertyType); @@ -55,4 +56,8 @@ public class GreaterThanConstraint extends AbstractComparablePropertyConstraint this.greaterThan = greaterThan; } + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%f property value must be > %f", greaterThan); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java index 19a2fe4230..ad3d3525d0 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/InRangeConstraint.java @@ -21,12 +21,15 @@ package org.openecomp.sdc.be.model.tosca.constraints; import com.google.common.collect.Lists; + +import java.util.List; + import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -import java.util.List; public class InRangeConstraint extends AbstractPropertyConstraint { @@ -111,4 +114,10 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } } + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%f property value must be between >= [%s] and <= [%s]", + String.valueOf(min), String.valueOf(max)); + } + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java index c07ac7e2e3..0c5317b10b 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LengthConstraint.java @@ -20,22 +20,42 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.util.List; +import java.util.Map; + +import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -public class LengthConstraint extends AbstractStringPropertyConstraint { +public class LengthConstraint extends AbstractPropertyConstraint { @NotNull private Integer length; - @Override - protected void doValidate(String propertyValue) throws ConstraintViolationException { - if (propertyValue.length() != length) { + protected void doValidate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue instanceof String && String.valueOf(propertyValue).length() != length) { throw new ConstraintViolationException("The length of the value is not equals to [" + length + "]"); + } else if (propertyValue instanceof List && ((List) propertyValue).size() != length) { + throw new ConstraintViolationException("The size of the list is not equals to [" + length + "]"); + } else if (propertyValue instanceof Map && ((Map) propertyValue).size() != length) { + throw new ConstraintViolationException("The size of the map is not equals to [" + length + "]"); } } + public void validate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue == null) { + throw new ConstraintViolationException("Value to validate is null"); + } + + if(!(propertyValue instanceof String || propertyValue instanceof List || propertyValue instanceof Map)) { + throw new ConstraintViolationException("This constraint can only be applied on String/List/Map value"); + } + + doValidate(propertyValue); + } + public Integer getLength() { return length; } @@ -44,4 +64,9 @@ public class LengthConstraint extends AbstractStringPropertyConstraint { this.length = length; } + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s length must be %s", String.valueOf(length)); + } + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java index 7d61dd31df..3f085ec596 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessOrEqualConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.tosca.constraints; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; @@ -30,7 +31,6 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint @NotNull private String lessOrEqual; - public LessOrEqualConstraint(String lessOrEqual) { this.lessOrEqual = lessOrEqual; } @@ -55,4 +55,8 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint this.lessOrEqual = lessOrEqual; } + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must be <= %s", lessOrEqual); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java index e2db77b45a..01eb6cdc32 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/LessThanConstraint.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.model.tosca.constraints; import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; @@ -47,4 +48,8 @@ public class LessThanConstraint extends AbstractComparablePropertyConstraint { } } + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s value must be < %s", lessThan); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MaxLengthConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MaxLengthConstraint.java index 8b7ce49cbc..c8fef91ea6 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MaxLengthConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MaxLengthConstraint.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,11 +20,16 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.util.List; +import java.util.Map; + +import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -public class MaxLengthConstraint extends AbstractStringPropertyConstraint { +public class MaxLengthConstraint extends AbstractPropertyConstraint { @NotNull private Integer maxLength; @@ -37,10 +42,13 @@ public class MaxLengthConstraint extends AbstractStringPropertyConstraint { super(); } - @Override - protected void doValidate(String propertyValue) throws ConstraintViolationException { - if (propertyValue.length() > maxLength) { + protected void doValidate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue instanceof String && String.valueOf(propertyValue).length() > maxLength) { throw new ConstraintViolationException("The length of the value is greater than [" + maxLength + "]"); + } else if (propertyValue instanceof List && ((List) propertyValue).size() > maxLength) { + throw new ConstraintViolationException("The size of the list is greater than [" + maxLength + "]"); + } else if (propertyValue instanceof Map && ((Map) propertyValue).size() > maxLength) { + throw new ConstraintViolationException("The size of the map is greater than [" + maxLength + "]"); } } @@ -51,4 +59,22 @@ public class MaxLengthConstraint extends AbstractStringPropertyConstraint { public void setMaxLength(Integer maxLength) { this.maxLength = maxLength; } + + @Override + public void validate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue == null) { + throw new ConstraintViolationException("Value to validate is null"); + } + + if(!(propertyValue instanceof String || propertyValue instanceof List || propertyValue instanceof Map)) { + throw new ConstraintViolationException("This constraint can only be applied on String/List/Map value"); + } + + doValidate(propertyValue); + } + + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s maximum length must be [%s]", String.valueOf(maxLength)); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java index bc51f4fa5a..4f55970d79 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/MinLengthConstraint.java @@ -20,11 +20,16 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.util.List; +import java.util.Map; + +import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -public class MinLengthConstraint extends AbstractStringPropertyConstraint { +public class MinLengthConstraint extends AbstractPropertyConstraint { @NotNull private Integer minLength; @@ -37,10 +42,13 @@ public class MinLengthConstraint extends AbstractStringPropertyConstraint { super(); } - @Override - protected void doValidate(String propertyValue) throws ConstraintViolationException { - if (propertyValue.length() < minLength) { + protected void doValidate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue instanceof String && String.valueOf(propertyValue).length() < minLength) { throw new ConstraintViolationException("The length of the value is less than [" + minLength + "]"); + } else if (propertyValue instanceof List && ((List) propertyValue).size() < minLength) { + throw new ConstraintViolationException("The size of the list is less than [" + minLength + "]"); + } else if (propertyValue instanceof Map && ((Map) propertyValue).size() < minLength) { + throw new ConstraintViolationException("The size of the map is less than [" + minLength + "]"); } } @@ -52,4 +60,19 @@ public class MinLengthConstraint extends AbstractStringPropertyConstraint { this.minLength = minLength; } + @Override + public void validate(Object propertyValue) throws ConstraintViolationException { + if (propertyValue == null) { + throw new ConstraintViolationException("Value to validate is null"); + } + if(!(propertyValue instanceof String || propertyValue instanceof List || propertyValue instanceof Map)) { + throw new ConstraintViolationException("This constraint can only be applied on String/List/Map value"); + } + doValidate(propertyValue); + } + + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s minimum length must be [%s]", String.valueOf(minLength)); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/PatternConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/PatternConstraint.java index db26c33440..d3644c578d 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/PatternConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/PatternConstraint.java @@ -20,10 +20,13 @@ package org.openecomp.sdc.be.model.tosca.constraints; +import java.util.regex.Pattern; + +import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -import java.util.regex.Pattern; public class PatternConstraint extends AbstractStringPropertyConstraint { @@ -43,4 +46,10 @@ public class PatternConstraint extends AbstractStringPropertyConstraint { throw new ConstraintViolationException("The value do not match pattern " + pattern); } } + + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must match the regular expression %s", + pattern); + } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java index 0b35a9be8e..1ddf9c9e0e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/ValidValuesConstraint.java @@ -21,13 +21,16 @@ package org.openecomp.sdc.be.model.tosca.constraints; import com.google.common.collect.Sets; + +import java.util.List; +import java.util.Set; + import org.openecomp.sdc.be.model.tosca.ToscaType; +import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunctionalException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException; import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import javax.validation.constraints.NotNull; -import java.util.List; -import java.util.Set; public class ValidValuesConstraint extends AbstractPropertyConstraint { @@ -77,4 +80,9 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { this.validValues = validValues; } + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", + String.join(",", validValues)); + } } |