diff options
author | JvD_Ericsson <jeff.van.dam@est.tech> | 2022-11-21 09:55:26 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-12-02 09:44:21 +0000 |
commit | be7ba43b95f13bb390cdd77a15c35072781e5546 (patch) | |
tree | ae20f499478a455e9355ff0bc6bd47010184b5ea /catalog-model/src/main | |
parent | 1336da0b9a713b9d6cf1e3f6ea709c50be43d4ab (diff) |
Fix numeric constraint values generated as strings
When importing a vfc the property constraints are now
checked to see if they are the same type as the property
If not it will attempt to convert to that type
Issue-ID: SDC-4274
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: I32c1d930166d10142ad9ca6914550c7946e9128c
Diffstat (limited to 'catalog-model/src/main')
10 files changed, 362 insertions, 121 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java index dc97e25af1..9e06b42852 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java @@ -2139,20 +2139,20 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe JsonArray jsonArray = new JsonArray(); if (src instanceof InRangeConstraint) { InRangeConstraint rangeConstraint = (InRangeConstraint) src; - jsonArray.add(JsonParser.parseString(rangeConstraint.getRangeMinValue())); - jsonArray.add(JsonParser.parseString(rangeConstraint.getRangeMaxValue())); + jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getRangeMinValue()))); + jsonArray.add(JsonParser.parseString(String.valueOf(rangeConstraint.getRangeMaxValue()))); result.add("inRange", jsonArray); } else if (src instanceof GreaterThanConstraint) { GreaterThanConstraint greaterThanConstraint = (GreaterThanConstraint) src; - jsonArray.add(JsonParser.parseString(greaterThanConstraint.getGreaterThan())); + jsonArray.add(JsonParser.parseString(String.valueOf(greaterThanConstraint.getGreaterThan()))); result.add("greaterThan", jsonArray); } else if (src instanceof LessThanConstraint) { LessThanConstraint lessThanConstraint = (LessThanConstraint) src; - jsonArray.add(JsonParser.parseString(lessThanConstraint.getLessThan())); + jsonArray.add(JsonParser.parseString(String.valueOf(lessThanConstraint.getLessThan()))); result.add("lessThan", jsonArray); } else if (src instanceof LessOrEqualConstraint) { LessOrEqualConstraint lessOrEqualConstraint = (LessOrEqualConstraint) src; - jsonArray.add(JsonParser.parseString(lessOrEqualConstraint.getLessOrEqual())); + jsonArray.add(JsonParser.parseString(String.valueOf(lessOrEqualConstraint.getLessOrEqual()))); result.add("lessOrEqual", jsonArray); } else { log.warn("PropertyConstraint {} is not supported. Ignored.", src.getClass().getName()); @@ -2173,38 +2173,32 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe Entry<String, JsonElement> element = set.iterator().next(); String key = element.getKey(); JsonElement value = element.getValue(); + Object typedValue = getTypedValue(element.getValue()); ConstraintType constraintType = ConstraintType.findByType(key).orElse(null); if (constraintType == null) { log.warn("ConstraintType was not found for constraint name:{}", key); } else { switch (constraintType) { case EQUAL: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to EqualConstraint object. value = {}", asString); - propertyConstraint = new EqualConstraint(asString); + if ( typedValue != null) { + log.debug("Before adding value to EqualConstraint object. value = {}", typedValue); + propertyConstraint = new EqualConstraint(typedValue); break; } else { log.warn("The value of equal constraint is null"); } break; case IN_RANGE: - if (value != null) { - if (value instanceof JsonArray) { - JsonArray rangeArray = (JsonArray) value; - if (rangeArray.size() != 2 || rangeArray.contains(new JsonPrimitive(""))) { - log.error("The range constraint content is invalid. value = {}", value); + if (typedValue != null) { + if (typedValue instanceof ArrayList) { + ArrayList rangeArray = (ArrayList) typedValue; + if (rangeArray.size() != 2 || rangeArray.contains("")) { + log.error("The range constraint content is invalid. value = {}", typedValue); throw new JsonSyntaxException("The range constraint content is invalid"); } else { InRangeConstraint rangeConstraint = new InRangeConstraint(); - String minValue = rangeArray.get(0).getAsString(); - String maxValue; - JsonElement maxElement = rangeArray.get(1); - if (maxElement.isJsonNull()) { - maxValue = String.valueOf(maxElement.getAsJsonNull()); - } else { - maxValue = maxElement.getAsString(); - } + Object minValue = rangeArray.get(0); + Object maxValue = rangeArray.get(1); rangeConstraint.setRangeMinValue(minValue); rangeConstraint.setRangeMaxValue(maxValue); propertyConstraint = rangeConstraint; @@ -2215,58 +2209,49 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } break; case GREATER_THAN: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString); - propertyConstraint = new GreaterThanConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to GreaterThanConstraint object. value = {}", typedValue); + propertyConstraint = new GreaterThanConstraint(typedValue); break; } else { log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL); } break; case LESS_THAN: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to LessThanConstraint object. value = {}", asString); - propertyConstraint = new LessThanConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to LessThanConstraint object. value = {}", typedValue); + propertyConstraint = new LessThanConstraint(typedValue); break; } else { log.warn("The value of LessThanConstraint is null"); } break; case GREATER_OR_EQUAL: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString); - propertyConstraint = new GreaterOrEqualConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to GreaterThanConstraint object. value = {}", typedValue); + propertyConstraint = new GreaterOrEqualConstraint(typedValue); break; } else { log.warn("The value of GreaterOrEqualConstraint is null"); } break; case LESS_OR_EQUAL: - if (value != null) { - String asString = value.getAsString(); - log.debug("Before adding value to LessOrEqualConstraint object. value = {}", asString); - propertyConstraint = new LessOrEqualConstraint(asString); + if (typedValue != null) { + log.debug("Before adding value to LessOrEqualConstraint object. value = {}", typedValue); + propertyConstraint = new LessOrEqualConstraint(typedValue); } else { log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL); } break; case VALID_VALUES: - if (value != null) { - JsonArray rangeArray = (JsonArray) value; - if (rangeArray.size() == 0 || rangeArray.contains(new JsonPrimitive(""))) { - log.error("The valid values constraint content is invalid. value = {}", value); + if (typedValue != null) { + ArrayList validValuesArray = (ArrayList)typedValue; + if (validValuesArray.size() == 0 || validValuesArray.contains("")) { + log.error("The valid values constraint content is invalid. value = {}", typedValue); throw new JsonSyntaxException("The valid values constraint content is invalid"); } else { ValidValuesConstraint vvConstraint = new ValidValuesConstraint(); - List<String> validValues = new ArrayList<>(); - for (JsonElement jsonElement : rangeArray) { - String item = jsonElement.getAsString(); - validValues.add(item); - } - vvConstraint.setValidValues(validValues); + vvConstraint.setValidValues(validValuesArray); propertyConstraint = vvConstraint; } } @@ -2318,6 +2303,37 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return propertyConstraint; } + + private Object getTypedValue(JsonElement je) { + if (je.isJsonNull()) + return null; + if (je.isJsonPrimitive()) { + return getJsonPrimitive(je.getAsJsonPrimitive()); + } + if (je.isJsonArray()) { + ArrayList<Object> array = new ArrayList<>(); + for (JsonElement e : je.getAsJsonArray()) { + array.add(getJsonPrimitive(e.getAsJsonPrimitive())); + } + return array; + } + return je; + } + + private Object getJsonPrimitive(JsonPrimitive je) { + if (je.isBoolean()) + return je.getAsBoolean(); + if (je.isString()) + return je.getAsString(); + if (je.isNumber()){ + double number = je.getAsNumber().floatValue(); + if ((number % 1) == 0) { + return je.getAsNumber().intValue(); + } + return number; + } + return null; + } } public static class PropertyConstraintJacksonDeserializer extends com.fasterxml.jackson.databind.JsonDeserializer<PropertyConstraint> { @@ -2388,7 +2404,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe String asString = value.asText(); log.debug("Before adding value to {} object. value = {}", constraintClass, asString); try { - return constraintClass.getConstructor(String.class).newInstance(asString); + return constraintClass.getConstructor(Object.class).newInstance(asString); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException exception) { log.error("Error deserializing constraint", exception); @@ -2437,7 +2453,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.error("The valid values constraint content is invalid. value = {}", value); } else { ValidValuesConstraint vvConstraint = new ValidValuesConstraint(); - List<String> validValues = new ArrayList<>(); + List<Object> validValues = new ArrayList<>(); for (JsonNode jsonElement : rangeArray) { String item = jsonElement.asText(); validValues.add(item); 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 85758707b8..f59ebe6922 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 @@ -49,6 +49,7 @@ public enum ToscaType { VERSION("version"), LIST("list"), MAP("map"), + RANGE("range"), SCALAR_UNIT("scalar-unit"), SCALAR_UNIT_SIZE("scalar-unit.size"), SCALAR_UNIT_TIME("scalar-unit.time"), @@ -93,6 +94,31 @@ public enum ToscaType { return ToscaPropertyType.MAP.getType().equals(type) || ToscaPropertyType.LIST.getType().equals(type); } + public Boolean isValueTypeValid(Object value) { + switch (this) { + case BOOLEAN: + return value.equals(true) || value.equals(false); + case FLOAT: + return value instanceof Float; + case INTEGER: + case RANGE: + return value instanceof Integer; + case STRING: + case SCALAR_UNIT: + case SCALAR_UNIT_SIZE: + case SCALAR_UNIT_TIME: + case SCALAR_UNIT_FREQUENCY: + case TIMESTAMP: + case VERSION: + return value instanceof String; + case LIST: + case MAP: + return true; + default: + return false; + } + } + public boolean isValidValue(String value) { switch (this) { case BOOLEAN: @@ -172,6 +198,7 @@ public enum ToscaType { return Boolean.valueOf(value); case FLOAT: return Float.valueOf(value); + case RANGE: case INTEGER: return Long.valueOf(value); case TIMESTAMP: diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java index 42fb1eb00c..e7730bd612 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java @@ -50,6 +50,12 @@ public abstract class AbstractComparablePropertyConstraint extends AbstractPrope protected abstract void doValidate(Object propertyValue) throws ConstraintViolationException; + public abstract boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException; + + public abstract String getConstraintValueAsString(); + + public abstract void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException; + @Override public void validate(Object propertyValue) throws ConstraintViolationException { if (propertyValue == null) { 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 0fdc570262..9e865733c9 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 @@ -21,6 +21,8 @@ package org.openecomp.sdc.be.model.tosca.constraints; import java.io.Serializable; import javax.validation.constraints.NotNull; +import lombok.Setter; +import lombok.Getter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -28,25 +30,25 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintFunction 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.constraints.exception.PropertyConstraintException; -import lombok.Getter; @SuppressWarnings("serial") public class EqualConstraint extends AbstractPropertyConstraint implements Serializable { - @NotNull @Getter - private String equal; + @Setter + @NotNull + private Object equal; private Object typed; - public EqualConstraint(String equal) { + public EqualConstraint(Object equal) { super(); this.equal = equal; } @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - if (propertyType.isValidValue(equal)) { - typed = propertyType.convert(equal); + if (propertyType.isValidValue(String.valueOf(equal))) { + typed = propertyType.convert(String.valueOf(equal)); } else { throw new ConstraintValueDoNotMatchPropertyTypeException( "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">"); @@ -82,6 +84,29 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", equal); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", String.valueOf(equal)); + } + + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">"); + } + if (equal == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "equal constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(equal); + } + + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + equal = toscaType.convert(String.valueOf(equal)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "equal constraint has invalid values <" + equal.toString() + "> property type is <" + propertyType + ">"); + } } } 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 00a84610c5..b45cf9791e 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 @@ -37,11 +37,11 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraint public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstraint { @NotNull - private String greaterOrEqual; + private Object greaterOrEqual; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(greaterOrEqual, propertyType); + initialize(String.valueOf(greaterOrEqual), propertyType); } @Override @@ -62,6 +62,36 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", greaterOrEqual); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", String.valueOf(greaterOrEqual)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">"); + } + if (greaterOrEqual == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterOrEqual constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(greaterOrEqual); + } + + @Override + public String getConstraintValueAsString() { + return String.valueOf(greaterOrEqual); + } + + @Override + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + greaterOrEqual = toscaType.convert(String.valueOf(greaterOrEqual)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterOrEqual constraint has invalid values <" + greaterOrEqual.toString() + "> property type is <" + propertyType + ">"); + } } } 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 a819a8e358..9d638cfb3f 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 @@ -20,6 +20,9 @@ package org.openecomp.sdc.be.model.tosca.constraints; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -28,18 +31,17 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; -public class GreaterThanConstraint extends AbstractComparablePropertyConstraint { +@Getter +@Setter +@AllArgsConstructor +public class GreaterThanConstraint<T> extends AbstractComparablePropertyConstraint { @NotNull - private String greaterThan; - - public GreaterThanConstraint(String greaterThan) { - this.greaterThan = greaterThan; - } + private T greaterThan; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(greaterThan, propertyType); + initialize(String.valueOf(greaterThan), propertyType); } @Override @@ -58,16 +60,38 @@ public class GreaterThanConstraint extends AbstractComparablePropertyConstraint } } - public String getGreaterThan() { - return greaterThan; + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", String.valueOf(greaterThan)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterThan constraint has invalid values <" + greaterThan.toString() + "> property type is <" + propertyType + ">"); + } + if (greaterThan == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterThan constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(greaterThan); } - public void setGreaterThan(String greaterThan) { - this.greaterThan = greaterThan; + @Override + public String getConstraintValueAsString() { + return String.valueOf(greaterThan); } @Override - public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", greaterThan); + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + greaterThan = (T) toscaType.convert(String.valueOf(greaterThan)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "greaterThan constraint has invalid values <" + greaterThan.toString() + "> property type is <" + propertyType + ">"); + } } } 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 ad871e5087..2256f7d311 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 @@ -22,6 +22,10 @@ package org.openecomp.sdc.be.model.tosca.constraints; import com.google.common.collect.Lists; import java.util.List; import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -30,19 +34,20 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +@NoArgsConstructor +@Getter +@Setter public class InRangeConstraint extends AbstractPropertyConstraint { - private List<String> inRange; + @NonNull + private List<Object> inRange; private Comparable min; private Comparable max; - public InRangeConstraint(List<String> inRange) { + public InRangeConstraint(List<Object> inRange) { this.inRange = inRange; } - public InRangeConstraint() { - } - @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { // Perform verification that the property type is supported for @@ -52,8 +57,8 @@ public class InRangeConstraint extends AbstractPropertyConstraint { if (inRange == null || inRange.size() != 2) { throw new ConstraintValueDoNotMatchPropertyTypeException("In range constraint must have two elements."); } - String minRawText = inRange.get(0); - String maxRawText = inRange.get(1); + String minRawText = String.valueOf(inRange.get(0)); + String maxRawText = String.valueOf(inRange.get(1)); if (!propertyType.isValidValue(minRawText)) { throw new ConstraintValueDoNotMatchPropertyTypeException( "Invalid min value for in range constraint [" + minRawText + "] as it does not follow the property type [" + propertyType + "]"); @@ -90,7 +95,7 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } @NotNull - public String getRangeMinValue() { + public Object getRangeMinValue() { if (inRange != null) { return inRange.get(0); } else { @@ -98,16 +103,16 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } } - public void setRangeMinValue(String minValue) { + public void setRangeMinValue(Object minValue) { if (inRange == null) { - inRange = Lists.newArrayList(minValue, ""); + inRange = Lists.newArrayList(minValue, null); } else { inRange.set(0, minValue); } } @NotNull - public String getRangeMaxValue() { + public Object getRangeMaxValue() { if (inRange != null) { return inRange.get(1); } else { @@ -115,9 +120,9 @@ public class InRangeConstraint extends AbstractPropertyConstraint { } } - public void setRangeMaxValue(String maxValue) { + public void setRangeMaxValue(Object maxValue) { if (inRange == null) { - inRange = Lists.newArrayList("", maxValue); + inRange = Lists.newArrayList(null, maxValue); } else { inRange.set(1, maxValue); } @@ -128,4 +133,32 @@ public class InRangeConstraint extends AbstractPropertyConstraint { return getErrorMessage(toscaType, e, propertyName, "%s property value must be between >= [%s] and <= [%s]", String.valueOf(min), String.valueOf(max)); } + + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">"); + } + if (inRange == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "inRange constraint has invalid value <> property type is <" + propertyType + ">"); + } + for (Object value : inRange) { + if (!toscaType.isValueTypeValid(value)) { + return false; + } + } + return true; + } + + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + inRange.replaceAll(obj -> toscaType.convert(String.valueOf(obj))); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "inRange constraint has invalid values <" + inRange + "> property type is <" + propertyType + ">"); + } + } } 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 86c6383677..0f64a4f9f5 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 @@ -20,6 +20,9 @@ package org.openecomp.sdc.be.model.tosca.constraints; import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -28,18 +31,17 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; -public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint { +@Getter +@Setter +@AllArgsConstructor +public class LessOrEqualConstraint<T> extends AbstractComparablePropertyConstraint { @NotNull - private String lessOrEqual; - - public LessOrEqualConstraint(String lessOrEqual) { - this.lessOrEqual = lessOrEqual; - } + private T lessOrEqual; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(lessOrEqual, propertyType); + initialize(String.valueOf(lessOrEqual), propertyType); } @Override @@ -58,16 +60,38 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint } } - public String getLessOrEqual() { - return lessOrEqual; + @Override + public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { + return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", String.valueOf(lessOrEqual)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessOrEqual constraint has invalid values <" + lessOrEqual.toString() + "> property type is <" + propertyType + ">"); + } + if (lessOrEqual == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessOrEqual constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(lessOrEqual); } - public void setLessOrEqual(String lessOrEqual) { - this.lessOrEqual = lessOrEqual; + @Override + public String getConstraintValueAsString() { + return String.valueOf(lessOrEqual); } @Override - public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %s", lessOrEqual); + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + lessOrEqual = (T) toscaType.convert(String.valueOf(lessOrEqual)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessOrEqual constraint has invalid values <" + lessOrEqual.toString() + "> property type is <" + propertyType + ">"); + } } } 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 740ee8b544..92ab9278b2 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 @@ -22,6 +22,7 @@ package org.openecomp.sdc.be.model.tosca.constraints; import javax.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.Setter; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -31,15 +32,16 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolatio import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; @Getter +@Setter @AllArgsConstructor public class LessThanConstraint extends AbstractComparablePropertyConstraint { @NotNull - private String lessThan; + private Object lessThan; @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - initialize(lessThan, propertyType); + initialize(String.valueOf(lessThan), propertyType); } @Override @@ -60,6 +62,36 @@ public class LessThanConstraint extends AbstractComparablePropertyConstraint { @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", lessThan); + return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", String.valueOf(lessThan)); + } + + @Override + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">"); + } + if (lessThan == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessThan constraint has invalid value <> property type is <" + propertyType + ">"); + } + return toscaType.isValueTypeValid(lessThan); + } + + @Override + public String getConstraintValueAsString() { + return String.valueOf(lessThan); + } + + @Override + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + lessThan = toscaType.convert(String.valueOf(lessThan)); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "lessThan constraint has invalid values <" + lessThan.toString() + "> property type is <" + propertyType + ">"); + } } } 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 1cafc213bb..f6bc589521 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 @@ -25,6 +25,10 @@ import com.google.common.collect.Sets; import java.util.List; import java.util.Set; import javax.validation.constraints.NotNull; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.Setter; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.model.PropertyConstraint; @@ -34,20 +38,20 @@ import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoN import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintViolationException; import org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +@NoArgsConstructor public class ValidValuesConstraint extends AbstractPropertyConstraint { private static final String PROPERTY_TYPE_IS = "> property type is <"; + @Getter + @Setter @NotNull - private List<String> validValues; + private List<Object> validValues; private Set<Object> validValuesTyped; - public ValidValuesConstraint(List<String> validValues) { + public ValidValuesConstraint(List<Object> validValues) { this.validValues = validValues; } - public ValidValuesConstraint() { - } - @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { validValuesTyped = Sets.newHashSet(); @@ -55,12 +59,12 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <> property type is <" + propertyType.toString() + ">"); } - for (String value : validValues) { - if (!propertyType.isValidValue(value)) { + for (Object value : validValues) { + if (!propertyType.isValidValue(String.valueOf(value))) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType.toString() + ">"); } else { - validValuesTyped.add(propertyType.convert(value)); + validValuesTyped.add(propertyType.convert(String.valueOf(value))); } } } @@ -75,8 +79,8 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <> property type is <" + propertyType + ">"); } - for (String value : validValues) { - if (!toscaType.isValidValue(value)) { + for (Object value : validValues) { + if (!toscaType.isValidValue(String.valueOf(value))) { throw new ConstraintValueDoNotMatchPropertyTypeException( "validValues constraint has invalid value <" + value + PROPERTY_TYPE_IS + propertyType + ">"); } @@ -105,14 +109,6 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { } } - public List<String> getValidValues() { - return validValues; - } - - public void setValidValues(List<String> validValues) { - this.validValues = validValues; - } - @Override public ConstraintType getConstraintType() { return ConstraintType.VALID_VALUES; @@ -120,6 +116,34 @@ public class ValidValuesConstraint extends AbstractPropertyConstraint { @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)); + return getErrorMessage(toscaType, e, propertyName, "%s valid value must be one of the following: [%s]", String.join(",", String.valueOf(validValues))); + } + + public boolean validateValueType(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + if (toscaType == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "validValues constraint has invalid values <" + validValues + "> property type is <" + propertyType + ">"); + } + if (validValues == null) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "validValues constraint has invalid value <> property type is <" + propertyType + ">"); + } + for (Object value : validValues) { + if (!toscaType.isValueTypeValid(value)) { + return false; + } + } + return true; + } + + public void changeConstraintValueTypeTo(String propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { + ToscaType toscaType = ToscaType.getToscaType(propertyType); + try { + validValues.replaceAll(obj -> toscaType.convert(String.valueOf(obj))); + } catch (Exception e) { + throw new ConstraintValueDoNotMatchPropertyTypeException( + "validValues constraint has invalid values <" + validValues + "> property type is <" + propertyType + ">"); + } } } |