diff options
author | MichaelMorris <michael.morris@est.tech> | 2022-10-13 14:12:47 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2022-10-24 15:06:02 +0000 |
commit | 2edea35f59360fbc8b4f451fc573e02dc5f65010 (patch) | |
tree | 1b1ead43f07d813562b55e9dbad80efcd5308ea8 /catalog-model/src/main/java | |
parent | aa361f84ec4d137e7a64df8c7feaec6d2304c03e (diff) |
Support import of VFCs with property constraints
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Issue-ID: SDC-4216
Change-Id: I19dce9f929535aa22cad6a1d95a213f42dd6e99c
Diffstat (limited to 'catalog-model/src/main/java')
10 files changed, 176 insertions, 22 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java index f208891e21..22e6a6938e 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java @@ -19,6 +19,7 @@ */ package org.openecomp.sdc.be.model; +import java.util.ArrayList; import java.util.List; import lombok.Getter; import lombok.NoArgsConstructor; @@ -54,6 +55,6 @@ public class DataTypeDefinition extends DataTypeDataDefinition { } public List<PropertyConstraint> safeGetConstraints() { - return CollectionUtils.safeGetList(constraints); + return new ArrayList<PropertyConstraint>(CollectionUtils.safeGetList(constraints)); } } 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 9e5c2e49bc..d797d8a8ee 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 @@ -24,6 +24,7 @@ import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_P import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.google.common.collect.Maps; import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; @@ -36,16 +37,17 @@ import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import fj.data.Either; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.StringJoiner; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -87,11 +89,14 @@ import org.openecomp.sdc.be.model.operations.api.IPropertyOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint; import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint; +import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint; import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter; @@ -2146,6 +2151,16 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe 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); + break; + } else { + log.warn("The value of equal constraint is null"); + } + break; case IN_RANGE: if (value != null) { if (value instanceof JsonArray) { @@ -2227,6 +2242,16 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } } break; + case LENGTH: + if (value != null) { + int asInt = value.getAsInt(); + log.debug("Before adding value to length constraint. value = {}", asInt); + propertyConstraint = new LengthConstraint(asInt); + break; + } else { + log.warn("The value of length constraint is null"); + } + break; case MIN_LENGTH: if (value != null) { int asInt = value.getAsInt(); @@ -2237,6 +2262,16 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.warn("The value of MinLengthConstraint is null"); } break; + case MAX_LENGTH: + if (value != null) { + int asInt = value.getAsInt(); + log.debug("Before adding value to max length constraint. value = {}", asInt); + propertyConstraint = new MaxLengthConstraint(asInt); + break; + } else { + log.warn("The value of max length constraint is null"); + } + break; default: log.warn("Key {} is not supported. Ignored.", key); } @@ -2252,8 +2287,125 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe public PropertyConstraint deserialize(com.fasterxml.jackson.core.JsonParser json, DeserializationContext context) throws IOException { ObjectCodec oc = json.getCodec(); JsonNode node = oc.readTree(json); + PropertyConstraint propertyConstraint = null; + + Iterator<Entry<String, JsonNode>> fieldsIterator = node.fields(); + while (fieldsIterator.hasNext()) { + Entry<String, JsonNode> field = fieldsIterator.next(); + ConstraintType constraintType = ConstraintType.findByType(field.getKey()).orElse(null); + JsonNode value = field.getValue(); + + if (constraintType == null) { + log.warn("ConstraintType was not found for constraint name:{}", field.getKey()); + } else { + if (value == null) { + log.warn("The value of " + constraintType + " constraint is null"); + } + switch (constraintType) { + case EQUAL: + propertyConstraint = deserializeConstraintWithStringOperand(value, EqualConstraint.class); + break; + case IN_RANGE: + propertyConstraint = deserializeInRangeConstraintConstraint(value); + break; + case GREATER_THAN: + propertyConstraint = deserializeConstraintWithStringOperand(value, GreaterThanConstraint.class); + break; + case LESS_THAN: + propertyConstraint = deserializeConstraintWithStringOperand(value, LessThanConstraint.class); + break; + case GREATER_OR_EQUAL: + propertyConstraint = deserializeConstraintWithStringOperand(value, GreaterOrEqualConstraint.class); + break; + case LESS_OR_EQUAL: + propertyConstraint = deserializeConstraintWithStringOperand(value, LessOrEqualConstraint.class); + break; + case VALID_VALUES: + propertyConstraint = deserializeValidValuesConstraint(value); + break; + case LENGTH: + propertyConstraint = deserializeConstraintWithIntegerOperand(value, LengthConstraint.class); + break; + case MIN_LENGTH: + propertyConstraint = deserializeConstraintWithIntegerOperand(value, MinLengthConstraint.class); + break; + case MAX_LENGTH: + propertyConstraint = deserializeConstraintWithIntegerOperand(value, MaxLengthConstraint.class); + break; + default: + log.warn("Key {} is not supported. Ignored.", field.getKey()); + } + } + } + + return propertyConstraint; + } + + private PropertyConstraint deserializeConstraintWithStringOperand(JsonNode value, Class<? extends PropertyConstraint> constraintClass) { + String asString = value.asText(); + log.debug("Before adding value to {} object. value = {}", constraintClass, asString); + try { + return constraintClass.getConstructor(String.class).newInstance(asString); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException exception) { + log.error("Error deserializing constraint", exception); + return null; + } + } + + private PropertyConstraint deserializeConstraintWithIntegerOperand(JsonNode value, Class<? extends PropertyConstraint> constraintClass) { + Integer asInt = value.asInt(); + log.debug("Before adding value to {} object. value = {}", constraintClass, asInt); + try { + return constraintClass.getConstructor(Integer.class).newInstance(asInt); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | SecurityException exception) { + log.error("Error deserializing constraint", exception); + return null; + } + } + + private PropertyConstraint deserializeInRangeConstraintConstraint(JsonNode value) { + if (value instanceof ArrayNode) { + ArrayNode rangeArray = (ArrayNode) value; + if (rangeArray.size() != 2) { + log.error("The range constraint content is invalid. value = {}", value); + } else { + InRangeConstraint rangeConstraint = new InRangeConstraint(); + String minValue = rangeArray.get(0).asText(); + String maxValue; + JsonNode maxElement = rangeArray.get(1); + if (maxElement.isNull()) { + maxValue = null; + } else { + maxValue = maxElement.asText(); + } + rangeConstraint.setRangeMinValue(minValue); + rangeConstraint.setRangeMaxValue(maxValue); + return rangeConstraint; + } + } return null; } + + private PropertyConstraint deserializeValidValuesConstraint(JsonNode value) { + ArrayNode rangeArray = (ArrayNode) value; + if (rangeArray.size() == 0) { + log.error("The valid values constraint content is invalid. value = {}", value); + } else { + ValidValuesConstraint vvConstraint = new ValidValuesConstraint(); + List<String> validValues = new ArrayList<>(); + for (JsonNode jsonElement : rangeArray) { + String item = jsonElement.asText(); + validValues.add(item); + } + vvConstraint.setValidValues(validValues); + return vvConstraint; + } + return null; + } + + } } 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 4b4c0a66d0..ef94baf33b 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 @@ -43,7 +43,7 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint { 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(errorMessage, propertyName, propertyValue.length == 1 ? propertyValue[0] : Arrays.toString(propertyValue)); } return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType()); } 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 c4b26f3077..0fdc570262 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 @@ -28,30 +28,28 @@ 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 - private String constraintValue; + @Getter + private String equal; private Object typed; - public EqualConstraint(String constraintValue) { + public EqualConstraint(String equal) { super(); - this.constraintValue = constraintValue; - } - - public String getConstraintValue() { - return constraintValue; + this.equal = equal; } @Override public void initialize(ToscaType propertyType) throws ConstraintValueDoNotMatchPropertyTypeException { - if (propertyType.isValidValue(constraintValue)) { - typed = propertyType.convert(constraintValue); + if (propertyType.isValidValue(equal)) { + typed = propertyType.convert(equal); } else { throw new ConstraintValueDoNotMatchPropertyTypeException( - "constraintValue constraint has invalid value <" + constraintValue + "> property type is <" + propertyType.toString() + ">"); + "constraintValue constraint has invalid value <" + equal + "> property type is <" + propertyType.toString() + ">"); } } @@ -63,8 +61,6 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria } } else if (typed == null) { fail(propertyValue); - } else if (typed instanceof Comparable && typed != propertyValue) { - fail(propertyValue); } else if (!typed.equals(propertyValue)) { fail(propertyValue); } @@ -81,11 +77,11 @@ public class EqualConstraint extends AbstractPropertyConstraint implements Seria private void fail(Object propertyValue) throws ConstraintViolationException { throw new ConstraintViolationException( - "Equal constraint violation, the reference is <" + constraintValue + "> but the value to compare is <" + propertyValue + ">"); + "Equal constraint violation, the reference is <" + equal + "> 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); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be %s", equal); } } 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 d32ab721fc..00a84610c5 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 @@ -62,6 +62,6 @@ public class GreaterOrEqualConstraint extends AbstractComparablePropertyConstrai @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%f property value must be >= %f", greaterOrEqual); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than or equal to %s", 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 e50c8fe3d0..a819a8e358 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 @@ -68,6 +68,6 @@ public class GreaterThanConstraint extends AbstractComparablePropertyConstraint @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%f property value must be > %f", greaterThan); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be greater than %s", 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 d574816c5f..ad871e5087 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 @@ -125,7 +125,7 @@ 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), + return getErrorMessage(toscaType, e, propertyName, "%s 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 457c824ac6..191993ad89 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 @@ -28,7 +28,11 @@ 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 org.openecomp.sdc.be.model.tosca.constraints.exception.PropertyConstraintException; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +@AllArgsConstructor +@NoArgsConstructor public class LengthConstraint extends AbstractPropertyConstraint { @NotNull 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 62cec05a73..86c6383677 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 @@ -68,6 +68,6 @@ public class LessOrEqualConstraint extends AbstractComparablePropertyConstraint @Override public String getErrorMessage(ToscaType toscaType, ConstraintFunctionalException e, String propertyName) { - return getErrorMessage(toscaType, e, propertyName, "%s property value must be <= %s", lessOrEqual); + return getErrorMessage(toscaType, e, propertyName, "%s property value must be less than or equal to %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 1f6cb63693..d0bea82157 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 @@ -30,6 +30,7 @@ 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; +@Getter @AllArgsConstructor public class LessThanConstraint extends AbstractComparablePropertyConstraint { @@ -60,6 +61,6 @@ 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); + return getErrorMessage(toscaType, e, propertyName, "%s value must be less than %s", lessThan); } } |