aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl
diff options
context:
space:
mode:
authorJvD_Ericsson <jeff.van.dam@est.tech>2022-11-21 09:55:26 +0000
committerMichael Morris <michael.morris@est.tech>2022-12-02 09:44:21 +0000
commitbe7ba43b95f13bb390cdd77a15c35072781e5546 (patch)
treeae20f499478a455e9355ff0bc6bd47010184b5ea /catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl
parent1336da0b9a713b9d6cf1e3f6ea709c50be43d4ab (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/java/org/openecomp/sdc/be/model/operations/impl')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java116
1 files changed, 66 insertions, 50 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);