aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main
diff options
context:
space:
mode:
authorvasraz <vasyl.razinkov@est.tech>2022-12-15 12:55:10 +0000
committerMichael Morris <michael.morris@est.tech>2023-01-03 14:23:08 +0000
commit5357edd8fb443613f4be4e8a30b03f2a7134e61d (patch)
tree97448037323fe103a59cb6fa511d0bb4d7526ce0 /catalog-model/src/main
parenta81c40773b3c819f079bd741d472ee954572cb34 (diff)
Improve message for constraints different errors
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech> Change-Id: I8babb10de4f0f25a49998fda077a7bb2554f86ba Issue-ID: SDC-4305
Diffstat (limited to 'catalog-model/src/main')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaType.java275
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractComparablePropertyConstraint.java3
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/constraints/AbstractPropertyConstraint.java5
3 files changed, 170 insertions, 113 deletions
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 a3daaa0381..1f942a9352 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
@@ -24,11 +24,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.openecomp.sdc.be.model.tosca.constraints.ConstraintUtil;
@@ -58,11 +60,19 @@ public enum ToscaType {
SCALAR_UNIT_BITRATE("scalar-unit.bitrate"),
SCALAR_UNIT_FREQUENCY("scalar-unit.frequency");
// @formatter:on
+ @Getter
+ private final String type;
- private static final String SCALAR_UNIT_BITRATE_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([TtGgMmKk]?i?[Bb]ps)$";
- private static final String SCALAR_UNIT_TIME_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([mun]?[dhms])$";
- private static final String SCALAR_UNIT_SIZE_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([TtGgMmKk]?i?[Bb])$";
- private static final String SCALAR_UNIT_FREQUENCY_PATTERN = "(^[0-9]+\\.?[0-9]*) ?([kMG]?Hz)$";
+ // @formatter:off
+ private static final List<String> SCALAR_UNIT_BITRATE_VALID_UNITS = Arrays.asList("TiBps", "TBps", "GiBps", "GBps", "MiBps", "MBps", "KiBps", "KBps", "Bps", "Tibps", "Tbps", "Gibps", "Gbps", "Mibps", "Mbps", "Kibps", "Kbps", "bps");
+ private static final String SCALAR_UNIT_BITRATE_PATTERN = "(^[0-9]+\\.?[0-9]*) ?(" + SCALAR_UNIT_BITRATE_VALID_UNITS.stream().collect(Collectors.joining("|")) + ")$";
+ private static final List<String> SCALAR_UNIT_TIME_VALID_UNITS = Arrays.asList("d", "h", "m", "s", "ms", "us", "ns");
+ private static final String SCALAR_UNIT_TIME_PATTERN = "(^[0-9]+\\.?[0-9]*) ?(" + SCALAR_UNIT_TIME_VALID_UNITS.stream().collect(Collectors.joining("|")) + ")$";
+ private static final List<String> SCALAR_UNIT_SIZE_VALID_UNITS = Arrays.asList("TiB", "TB", "GiB", "GB", "MiB", "MB", "KiB", "kB", "B");
+ private static final String SCALAR_UNIT_SIZE_PATTERN = "(^[0-9]+\\.?[0-9]*) ?(" + SCALAR_UNIT_SIZE_VALID_UNITS.stream().collect(Collectors.joining("|")) + ")$";
+ private static final List<String> SCALAR_UNIT_FREQUENCY_VALID_UNITS = Arrays.asList("GHz", "MHz", "kHz", "Hz");
+ private static final String SCALAR_UNIT_FREQUENCY_PATTERN = "(^[0-9]+\\.?[0-9]*) ?(" + SCALAR_UNIT_FREQUENCY_VALID_UNITS.stream().collect(Collectors.joining("|")) + ")$";
+ // @formatter:on
private static final double B_IN_TiB = Math.pow(1024, 4);
private static final double B_IN_GiB = Math.pow(1024, 3);
private static final double B_IN_MiB = Math.pow(1024, 2);
@@ -72,9 +82,6 @@ public enum ToscaType {
private static final double B_IN_MB = Math.pow(1000, 2);
private static final double B_IN_KB = Math.pow(1000, 1);
- @Getter
- private final String type;
-
public static ToscaType getToscaType(String typeName) {
if (typeName == null) {
return null;
@@ -208,6 +215,18 @@ public enum ToscaType {
return true;
}
+ public boolean isScalarUnit() {
+ switch (this) {
+ case SCALAR_UNIT_TIME:
+ case SCALAR_UNIT_SIZE:
+ case SCALAR_UNIT_BITRATE:
+ case SCALAR_UNIT_FREQUENCY:
+ return true;
+ default:
+ return false;
+ }
+ }
+
public Object convert(String value) {
switch (this) {
case STRING:
@@ -256,143 +275,177 @@ public enum ToscaType {
}
private Long convertScalarUnitSize(final String value) {
- final Matcher matcher = Pattern.compile(SCALAR_UNIT_SIZE_PATTERN).matcher(value.trim());
- if (matcher.find()) {
- switch (matcher.group(2)) {
- case "TiB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TiB);
- case "TB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TB);
- case "GiB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GiB);
- case "GB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GB);
- case "MiB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MiB);
- case "MB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MB);
- case "KiB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KiB);
- case "kB":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KB);
- case "B":
- return (long) (Double.parseDouble(matcher.group(1)));
- default:
- throw new IllegalArgumentException("Value must be a valid scalar-unit.size");
+ if (isScalarUnitSize(value)) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_SIZE_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "TiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TiB);
+ case "TB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TB);
+ case "GiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GiB);
+ case "GB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GB);
+ case "MiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MiB);
+ case "MB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MB);
+ case "KiB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KiB);
+ case "kB":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KB);
+ case "B":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.size");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.size': " + SCALAR_UNIT_SIZE_VALID_UNITS);
}
} else {
- throw new IllegalArgumentException("Value must be a valid scalar-unit.size");
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.size': " + SCALAR_UNIT_SIZE_VALID_UNITS);
}
}
private Long convertScalarUnitTime(final String value) {
- final Matcher matcher = Pattern.compile(SCALAR_UNIT_TIME_PATTERN).matcher(value.trim());
- if (matcher.find()) {
- switch (matcher.group(2)) {
- case "d":
- return (long) (Double.parseDouble(matcher.group(1)) * 24 * 60 * 60 * 1_000_000_000L); // 24hours * 60minutes * 60seconds
- case "h":
- return (long) (Double.parseDouble(matcher.group(1)) * 60 * 60 * 1_000_000_000L); // 60minutes * 60seconds
- case "m":
- return (long) (Double.parseDouble(matcher.group(1)) * 60 * 1_000_000_000L); // 60seconds
- case "s":
- return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000_000L);
- case "ms":
- return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000L);
- case "us":
- return (long) (Double.parseDouble(matcher.group(1)) * 1_000L);
- case "ns":
- return (long) (Double.parseDouble(matcher.group(1)));
- default:
- throw new IllegalArgumentException("Value must be a valid scalar-unit.time");
+ if (isScalarUnitTime(value)) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_TIME_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "d":
+ return (long) (Double.parseDouble(matcher.group(1)) * 24 * 60 * 60 * 1_000_000_000L); // 24hours * 60minutes * 60seconds
+ case "h":
+ return (long) (Double.parseDouble(matcher.group(1)) * 60 * 60 * 1_000_000_000L); // 60minutes * 60seconds
+ case "m":
+ return (long) (Double.parseDouble(matcher.group(1)) * 60 * 1_000_000_000L); // 60seconds
+ case "s":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000_000L);
+ case "ms":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000L);
+ case "us":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000L);
+ case "ns":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.time");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.time':" + SCALAR_UNIT_TIME_VALID_UNITS);
}
} else {
- throw new IllegalArgumentException("Value must be a valid scalar-unit.time");
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.time':" + SCALAR_UNIT_TIME_VALID_UNITS);
}
}
private Long convertScalarUnitFrequency(final String value) {
- final Matcher matcher = Pattern.compile(SCALAR_UNIT_FREQUENCY_PATTERN).matcher(value.trim());
- if (matcher.find()) {
- switch (matcher.group(2)) {
- case "GHz":
- return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000_000L);
- case "MHz":
- return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000L);
- case "kHz":
- return (long) (Double.parseDouble(matcher.group(1)) * 1_000L);
- case "Hz":
- return (long) (Double.parseDouble(matcher.group(1)));
- default:
- throw new IllegalArgumentException("Value must be a valid scalar-unit.frequency");
+ if (isScalarUnitFrequency(value)) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_FREQUENCY_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "GHz":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000_000L);
+ case "MHz":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000_000L);
+ case "kHz":
+ return (long) (Double.parseDouble(matcher.group(1)) * 1_000L);
+ case "Hz":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.frequency");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.frequency':" + SCALAR_UNIT_FREQUENCY_VALID_UNITS);
}
} else {
- throw new IllegalArgumentException("Value must be a valid scalar-unit.frequency");
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.frequency':" + SCALAR_UNIT_FREQUENCY_VALID_UNITS);
}
}
private Long convertScalarUnitBitrate(final String value) {
- final Matcher matcher = Pattern.compile(SCALAR_UNIT_BITRATE_PATTERN).matcher(value.trim());
- if (matcher.find()) {
- switch (matcher.group(2)) {
- case "TiBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_TiB);
- case "TBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_TB);
- case "GiBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_GiB);
- case "GBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_GB);
- case "MiBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_MiB);
- case "MBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_MB);
- case "KiBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_KiB);
- case "KBps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_KB);
- case "Bps":
- return (long) (Double.parseDouble(matcher.group(1)) * 8);
- case "Tibps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TiB);
- case "Tbps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TB);
- case "Gibps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GiB);
- case "Gbps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GB);
- case "Mibps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MiB);
- case "Mbps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MB);
- case "Kibps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KiB);
- case "Kbps":
- return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KB);
- case "bps":
- return (long) (Double.parseDouble(matcher.group(1)));
- default:
- throw new IllegalArgumentException("Value must be a valid scalar-unit.bitrate");
+ if (isScalarUnitBitrate(value)) {
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_BITRATE_PATTERN).matcher(value.trim());
+ if (matcher.find()) {
+ switch (matcher.group(2)) {
+ case "TiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_TiB);
+ case "TBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_TB);
+ case "GiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_GiB);
+ case "GBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_GB);
+ case "MiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_MiB);
+ case "MBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_MB);
+ case "KiBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_KiB);
+ case "KBps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8 * B_IN_KB);
+ case "Bps":
+ return (long) (Double.parseDouble(matcher.group(1)) * 8);
+ case "Tibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TiB);
+ case "Tbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_TB);
+ case "Gibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GiB);
+ case "Gbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_GB);
+ case "Mibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MiB);
+ case "Mbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_MB);
+ case "Kibps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KiB);
+ case "Kbps":
+ return (long) (Double.parseDouble(matcher.group(1)) * B_IN_KB);
+ case "bps":
+ return (long) (Double.parseDouble(matcher.group(1)));
+ default:
+ throw new IllegalArgumentException("Value must be a valid scalar-unit.bitrate");
+ }
+ } else {
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.bitrate':" + SCALAR_UNIT_BITRATE_VALID_UNITS);
}
} else {
- throw new IllegalArgumentException("Value must be a valid scalar-unit.bitrate");
+ throw new IllegalArgumentException("Value must be a valid 'scalar-unit.bitrate':" + SCALAR_UNIT_BITRATE_VALID_UNITS);
}
}
private boolean isScalarUnitBitrate(final String value) {
- return Pattern.compile(SCALAR_UNIT_BITRATE_PATTERN).matcher(value.trim()).find();
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_BITRATE_PATTERN).matcher(value.trim());
+ return matcher.find() && SCALAR_UNIT_BITRATE_VALID_UNITS.contains(matcher.group(2));
}
private boolean isScalarUnitSize(final String value) {
- return Pattern.compile(SCALAR_UNIT_SIZE_PATTERN).matcher(value.trim()).find();
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_SIZE_PATTERN).matcher(value.trim());
+ return matcher.find() && SCALAR_UNIT_SIZE_VALID_UNITS.contains(matcher.group(2));
}
private boolean isScalarUnitTime(final String value) {
- return Pattern.compile(SCALAR_UNIT_TIME_PATTERN).matcher(value.trim()).find();
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_TIME_PATTERN).matcher(value.trim());
+ return matcher.find() && SCALAR_UNIT_TIME_VALID_UNITS.contains(matcher.group(2));
}
private boolean isScalarUnitFrequency(final String value) {
- return Pattern.compile(SCALAR_UNIT_FREQUENCY_PATTERN).matcher(value.trim()).find();
+ final Matcher matcher = Pattern.compile(SCALAR_UNIT_FREQUENCY_PATTERN).matcher(value.trim());
+ return matcher.find() && SCALAR_UNIT_FREQUENCY_VALID_UNITS.contains(matcher.group(2));
}
+ public String getValidValues() {
+ switch (this) {
+ case SCALAR_UNIT_TIME:
+ return String.valueOf(SCALAR_UNIT_TIME_VALID_UNITS);
+ case SCALAR_UNIT_SIZE:
+ return String.valueOf(SCALAR_UNIT_SIZE_VALID_UNITS);
+ case SCALAR_UNIT_BITRATE:
+ return String.valueOf(SCALAR_UNIT_BITRATE_VALID_UNITS);
+ case SCALAR_UNIT_FREQUENCY:
+ return String.valueOf(SCALAR_UNIT_FREQUENCY_VALID_UNITS);
+ default:
+ return "";
+ }
+ }
}
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 4a8c742de9..6182eeb3b1 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
@@ -42,7 +42,8 @@ public abstract class AbstractComparablePropertyConstraint extends AbstractPrope
} else {
// Invalid value throw exception
throw new ConstraintValueDoNotMatchPropertyTypeException(
- "The value [" + rawTextValue + "] is not valid for the type [" + propertyType + "]");
+ "The value [" + rawTextValue + "] is not valid for the type [" + propertyType + "]."
+ + (propertyType.isScalarUnit() ? " Valid values are " + propertyType.getValidValues() : ""));
}
}
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 ef94baf33b..43bed0af67 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
@@ -35,7 +35,7 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint {
public void validate(ToscaType toscaType, String propertyTextValue) throws ConstraintViolationException {
try {
validate(toscaType.convert(propertyTextValue));
- } catch (IllegalArgumentException | ApplicationVersionException e) {
+ } catch (ApplicationVersionException e) {
throw new ConstraintViolationException("String value [" + propertyTextValue + "] is not valid for type [" + toscaType + "]", e);
}
}
@@ -45,6 +45,9 @@ public abstract class AbstractPropertyConstraint implements PropertyConstraint {
if (e instanceof ConstraintViolationException) {
return String.format(errorMessage, propertyName, propertyValue.length == 1 ? propertyValue[0] : Arrays.toString(propertyValue));
}
+ if (e instanceof ConstraintValueDoNotMatchPropertyTypeException) {
+ return e.getMessage();
+ }
return String.format(INVALID_VALUE_ERROR_MESSAGE, propertyName, toscaType.getType());
}