aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org/onap/policy/common/parameters
diff options
context:
space:
mode:
Diffstat (limited to 'common-parameters/src/main/java/org/onap/policy/common/parameters')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java56
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java432
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java137
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java292
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java112
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java391
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java71
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java4
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java24
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java19
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java17
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java30
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java19
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java36
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java32
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResultImpl.java22
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java146
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java37
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java5
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java5
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java5
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java5
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Pattern.java38
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java43
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Valid.java35
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java57
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java42
27 files changed, 1333 insertions, 779 deletions
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java
index f8eebcf1..e1620530 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidationResult.java
@@ -1,6 +1,6 @@
-/*
+/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@ package org.onap.policy.common.parameters;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.function.BiConsumer;
import java.util.function.Function;
/**
@@ -65,6 +68,19 @@ public class BeanValidationResult extends ValidationResultImpl {
}
/**
+ * Adds a result to this result.
+ * @param name name of the object of this result
+ * @param object object being validated
+ * @param status status of the new result
+ * @param message new result message
+ * @return {@code true} if the status is {@code null} or valid, {@code false} if the
+ * status is invalid
+ */
+ public boolean addResult(String name, Object object, ValidationStatus status, String message) {
+ return addResult(new ObjectValidationResult(name, object, status, message));
+ }
+
+ /**
* Validates that a sub-object within the bean is not {@code null}.
*
* @param subName name of the sub-object
@@ -72,7 +88,7 @@ public class BeanValidationResult extends ValidationResultImpl {
* @return {@code true} if the value is not null, {@code false} otherwise
*/
public boolean validateNotNull(String subName, Object subObject) {
- ObjectValidationResult result = new ObjectValidationResult(subName, subObject);
+ var result = new ObjectValidationResult(subName, subObject);
if (result.validateNotNull()) {
return true;
@@ -110,10 +126,10 @@ public class BeanValidationResult extends ValidationResultImpl {
return true;
}
- BeanValidationResult result = new BeanValidationResult(listName, null);
+ var result = new BeanValidationResult(listName, null);
for (T item : list) {
if (item == null) {
- result.addResult(new ObjectValidationResult("item", item, ValidationStatus.INVALID, "null"));
+ result.addResult("item", item, ValidationStatus.INVALID, "null");
} else {
result.addResult(itemValidator.apply(item));
}
@@ -129,6 +145,34 @@ public class BeanValidationResult extends ValidationResultImpl {
}
/**
+ * Validates the entries in a map.
+ *
+ * @param mapName name of the list
+ * @param map map whose entries are to be validated, or {@code null}
+ * @param entryValidator function to validate an entry in the map
+ * @return {@code true} if all entries in the map are valid, {@code false} otherwise
+ */
+ public <V> boolean validateMap(String mapName, Map<String, V> map,
+ BiConsumer<BeanValidationResult, Entry<String, V>> entryValidator) {
+ if (map == null) {
+ return true;
+ }
+
+ var result = new BeanValidationResult(mapName, null);
+ for (Entry<String, V> ent : map.entrySet()) {
+ entryValidator.accept(result, ent);
+ }
+
+ if (result.isValid()) {
+ return true;
+
+ } else {
+ addResult(result);
+ return false;
+ }
+ }
+
+ /**
* Gets the validation result.
*
* @param initialIndentation the indentation to use on the main result output
@@ -142,7 +186,7 @@ public class BeanValidationResult extends ValidationResultImpl {
return null;
}
- StringBuilder builder = new StringBuilder();
+ var builder = new StringBuilder();
builder.append(initialIndentation);
builder.append('"');
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
new file mode 100644
index 00000000..4ed7e42c
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/BeanValidator.java
@@ -0,0 +1,432 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.parameters.annotations.ClassName;
+import org.onap.policy.common.parameters.annotations.Max;
+import org.onap.policy.common.parameters.annotations.Min;
+import org.onap.policy.common.parameters.annotations.NotBlank;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.parameters.annotations.Pattern;
+import org.onap.policy.common.parameters.annotations.Size;
+import org.onap.policy.common.parameters.annotations.Valid;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Bean validator, supporting the parameter annotations.
+ */
+public class BeanValidator {
+ public static final Logger logger = LoggerFactory.getLogger(BeanValidator.class);
+
+ /**
+ * Validates top level fields within an object. For each annotated field, it retrieves
+ * the value using the public "getter" method for the field. If there is no public
+ * "getter" method, then it throws an exception. Otherwise, it validates the retrieved
+ * value based on the annotations. This recurses through super classes looking for
+ * fields to be verified, but it does not examine any interfaces.
+ *
+ * @param name name of the object being validated
+ * @param object object to be validated. If {@code null}, then an empty result is
+ * returned
+ * @return the validation result
+ */
+ public BeanValidationResult validateTop(String name, Object object) {
+ var result = new BeanValidationResult(name, object);
+ if (object == null) {
+ return result;
+ }
+
+ // check class hierarchy - don't need to check interfaces
+ for (Class<?> clazz = object.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
+ validateFields(result, object, clazz);
+ }
+
+ return result;
+ }
+
+ /**
+ * Adds validators based on the annotations that are available.
+ *
+ * @param validator where to add the validators
+ */
+ protected void addValidators(ValueValidator validator) {
+ validator.addAnnotation(NotNull.class, this::verNotNull);
+ validator.addAnnotation(NotBlank.class, this::verNotBlank);
+ validator.addAnnotation(Size.class, this::verSize);
+ validator.addAnnotation(Max.class, this::verMax);
+ validator.addAnnotation(Min.class, this::verMin);
+ validator.addAnnotation(Pattern.class, this::verRegex);
+ validator.addAnnotation(ClassName.class, this::verClassName);
+ validator.addAnnotation(Valid.class, this::verCascade);
+ }
+
+ /**
+ * Performs validation of all annotated fields found within the class.
+ *
+ * @param result validation results are added here
+ * @param object object whose fields are to be validated
+ * @param clazz class, within the object's hierarchy, to be examined for fields to be
+ * verified
+ */
+ private void validateFields(BeanValidationResult result, Object object, Class<?> clazz) {
+ for (Field field : clazz.getDeclaredFields()) {
+ var validator = makeFieldValidator(clazz, field);
+ validator.validateField(result, object);
+ }
+ }
+
+ /**
+ * Verifies that the value is not null.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verNotNull(BeanValidationResult result, String fieldName, Object value) {
+ if (value == null) {
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is null");
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Verifies that the value is not blank. Note: this does <i>not</i> verify that the
+ * value is not {@code null}.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verNotBlank(BeanValidationResult result, String fieldName, Object value) {
+ if (value instanceof String && StringUtils.isBlank(value.toString())) {
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID, "is blank");
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Verifies that the value has the specified number of elements.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param annot annotation against which the value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verSize(BeanValidationResult result, String fieldName, Size annot, Object value) {
+ int size;
+ if (value instanceof Collection) {
+ size = ((Collection<?>) value).size();
+
+ } else if (value instanceof Map) {
+ size = ((Map<?, ?>) value).size();
+
+ } else {
+ return true;
+ }
+
+
+ if (size < annot.min()) {
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ "minimum number of elements: " + annot.min());
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Verifies that the value matches a regular expression.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param annot annotation against which the value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verRegex(BeanValidationResult result, String fieldName, Pattern annot, Object value) {
+ try {
+ if (value instanceof String && com.google.re2j.Pattern.matches(annot.regexp(), value.toString())) {
+ return true;
+ }
+
+ } catch (RuntimeException e) {
+ logger.warn("validation error for regular expression: {}", annot.regexp(), e);
+ }
+
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ "does not match regular expression " + annot.regexp());
+ return false;
+ }
+
+ /**
+ * Verifies that the value is <= the minimum value.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param annot annotation against which the value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verMax(BeanValidationResult result, String fieldName, Max annot, Object value) {
+ if (!(value instanceof Number)) {
+ return true;
+ }
+
+ Number num = (Number) value;
+ if (num instanceof Integer || num instanceof Long) {
+ if (num.longValue() <= annot.value()) {
+ return true;
+ }
+
+ } else if (num instanceof Float || num instanceof Double) {
+ if (num.doubleValue() <= annot.value()) {
+ return true;
+ }
+
+ } else {
+ return true;
+ }
+
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ "exceeds the maximum value: " + annot.value());
+ return false;
+ }
+
+ /**
+ * Verifies that the value is >= the minimum value.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param annot annotation against which the value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verMin(BeanValidationResult result, String fieldName, Min annot, Object value) {
+ return verMin(result, fieldName, annot.value(), value);
+ }
+
+ /**
+ * Verifies that the value is >= the minimum value.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param min minimum against which the value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verMin(BeanValidationResult result, String fieldName, long min, Object value) {
+ if (!(value instanceof Number)) {
+ return true;
+ }
+
+ Number num = (Number) value;
+ if (num instanceof Integer || num instanceof Long) {
+ if (num.longValue() >= min) {
+ return true;
+ }
+
+ } else if (num instanceof Float || num instanceof Double) {
+ if (num.doubleValue() >= min) {
+ return true;
+ }
+
+ } else {
+ return true;
+ }
+
+ result.addResult(fieldName, xlate(value), ValidationStatus.INVALID,
+ "is below the minimum value: " + min);
+ return false;
+ }
+
+ /**
+ * Verifies that the value is a valid class name.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verClassName(BeanValidationResult result, String fieldName, Object value) {
+ if (!(value instanceof String)) {
+ return true;
+ }
+
+ try {
+ Class.forName(value.toString());
+ return true;
+
+ } catch (final ClassNotFoundException exp) {
+ result.addResult(fieldName, value, ValidationStatus.INVALID, "class is not in the classpath");
+ return false;
+ }
+ }
+
+ /**
+ * Verifies that the value is valid by recursively invoking
+ * {@link #validateTop(String, Object)}.
+ *
+ * @param result where to add the validation result
+ * @param fieldName field whose value is being verified
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verCascade(BeanValidationResult result, String fieldName, Object value) {
+ if (value == null || value instanceof Collection || value instanceof Map) {
+ return true;
+ }
+
+ BeanValidationResult result2 = (value instanceof ParameterGroup ? ((ParameterGroup) value).validate()
+ : validateTop(fieldName, value));
+
+ if (result2.isClean()) {
+ return true;
+ }
+
+ result.addResult(result2);
+
+ return result2.isValid();
+ }
+
+ /**
+ * Validates the items in a collection.
+ *
+ * @param result where to add the validation result
+ * @param fieldName name of the field containing the collection
+ * @param itemValidator validator for individual items within the list
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verCollection(BeanValidationResult result, String fieldName, ValueValidator itemValidator,
+ Object value) {
+
+ if (!(value instanceof Collection)) {
+ return true;
+ }
+
+ Collection<?> list = (Collection<?>) value;
+
+ var result2 = new BeanValidationResult(fieldName, value);
+ var count = 0;
+ for (Object item : list) {
+ itemValidator.validateValue(result2, String.valueOf(count++), item);
+ }
+
+ if (result2.isClean()) {
+ return true;
+ }
+
+ result.addResult(result2);
+ return false;
+ }
+
+ /**
+ * Validates the items in a Map.
+ *
+ * @param result where to add the validation result
+ * @param fieldName name of the field containing the map
+ * @param keyValidator validator for an individual key within the Map entry
+ * @param valueValidator validator for an individual value within the Map entry
+ * @param value value to be verified
+ * @return {@code true} if the next check should be performed, {@code false} otherwise
+ */
+ public boolean verMap(BeanValidationResult result, String fieldName, ValueValidator keyValidator,
+ ValueValidator valueValidator, Object value) {
+
+ if (!(value instanceof Map)) {
+ return true;
+ }
+
+ Map<?, ?> map = (Map<?, ?>) value;
+
+ var result2 = new BeanValidationResult(fieldName, value);
+
+ for (Entry<?, ?> entry : map.entrySet()) {
+ String name = getEntryName(entry);
+
+ var result3 = new BeanValidationResult(name, entry);
+ keyValidator.validateValue(result3, "key", entry.getKey());
+ valueValidator.validateValue(result3, "value", entry.getValue());
+
+ if (!result3.isClean()) {
+ result2.addResult(result3);
+ }
+ }
+
+ if (result2.isClean()) {
+ return true;
+ }
+
+ result.addResult(result2);
+ return false;
+ }
+
+ /**
+ * Gets a name for an entry.
+ *
+ * @param entry entry whose name is to be determined
+ * @return a name for the entry
+ */
+ protected <K, V> String getEntryName(Map.Entry<K, V> entry) {
+ var key = entry.getKey();
+ if (key == null) {
+ return "";
+ }
+
+ return key.toString();
+ }
+
+ /**
+ * Makes a field validator.
+ *
+ * @param clazz class containing the field
+ * @param field field of interest
+ * @return a validator for the given field
+ */
+ protected FieldValidator makeFieldValidator(Class<?> clazz, Field field) {
+ return new FieldValidator(this, clazz, field);
+ }
+
+ /**
+ * Translates a value to something printable, for use by
+ * {@link ObjectValidationResult}. This default method simply returns the original
+ * value.
+ *
+ * @param value value to be translated
+ * @return the translated value
+ */
+ public Object xlate(Object value) {
+ return value;
+ }
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java
deleted file mode 100644
index f35d1970..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/CommonGroupValidationResult.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.common.parameters;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * This class holds the result of the validation of a parameter group.
- */
-public abstract class CommonGroupValidationResult implements ValidationResult {
-
-
- private final String messagePrefix;
-
- /**
- * Validation status for the entire class.
- */
- protected ValidationStatus status = ValidationStatus.CLEAN;
-
- /**
- * Status message.
- */
- protected String message;
-
- /**
- * Validation results for each parameter in the group.
- */
- protected final Map<String, ValidationResult> validationResultMap = new LinkedHashMap<>();
-
-
- /**
- * Constructs the object.
- *
- * @param messagePrefix status message prefix
- */
- public CommonGroupValidationResult(String messagePrefix) {
- this.messagePrefix = messagePrefix;
- this.message = messagePrefix + status.toString();
- }
-
- /**
- * Gets the status of validation.
- *
- * @return the status
- */
- @Override
- public ValidationStatus getStatus() {
- return status;
- }
-
- /**
- * Set the validation result on a parameter group.
- *
- * @param status The validation status the parameter group is receiving
- * @param message The validation message explaining the validation status
- */
- @Override
- public void setResult(ValidationStatus status, String message) {
- setResult(status);
- this.message = message;
- }
-
- /**
- * Set the validation result on a parameter group. On a sequence of calls, the most
- * serious validation status is recorded, assuming the status enum ordinal increase in
- * order of severity
- *
- * @param status The validation status the parameter group is receiving
- */
- public void setResult(final ValidationStatus status) {
- if (this.status.ordinal() < status.ordinal()) {
- this.status = status;
- this.message = messagePrefix + status;
- }
- }
-
- /**
- * Gets the validation result.
- *
- * @param initialIndentation the indentation to use on the main result output
- * @param subIndentation the indentation to use on sub parts of the result output
- * @param showClean output information on clean fields
- * @return the result
- */
- @Override
- public String getResult(final String initialIndentation, final String subIndentation, final boolean showClean) {
- if (status == ValidationStatus.CLEAN && !showClean) {
- return null;
- }
-
- StringBuilder result = new StringBuilder();
-
- result.append(initialIndentation);
-
- addGroupTypeName(result);
-
- result.append(status);
- result.append(", ");
- result.append(message);
- result.append('\n');
-
- for (ValidationResult fieldResult : validationResultMap.values()) {
- String msg = fieldResult.getResult(initialIndentation + subIndentation, subIndentation, showClean);
- if (msg != null) {
- result.append(msg);
- }
- }
-
- return result.toString();
- }
-
- /**
- * Adds the group type and name to the result string.
- *
- * @param result result string
- */
- protected abstract void addGroupTypeName(StringBuilder result);
-}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java
new file mode 100644
index 00000000..d441c286
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/FieldValidator.java
@@ -0,0 +1,292 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters;
+
+import com.google.gson.annotations.SerializedName;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedParameterizedType;
+import java.lang.reflect.AnnotatedType;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Collection;
+import java.util.Map;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.parameters.annotations.NotNull;
+
+/**
+ * Validator of the contents of a field, supporting the parameter annotations.
+ */
+public class FieldValidator extends ValueValidator {
+
+ /**
+ * {@code True} if there is a field-level annotation, {@code false} otherwise.
+ */
+ @Getter
+ @Setter(AccessLevel.PROTECTED)
+ private boolean fieldAnnotated = false;
+
+ /**
+ * Class containing the field of interest.
+ */
+ private final Class<?> clazz;
+
+ /**
+ * Field of interest.
+ */
+ private final Field field;
+
+ /**
+ * Name of the field when serialized (i.e., as the client would know it).
+ */
+ private final String serializedName;
+
+ /**
+ * Method to retrieve the field's value.
+ */
+ private Method accessor;
+
+
+ /**
+ * Constructs the object.
+ *
+ * @param validator provider of validation methods
+ * @param clazz class containing the field
+ * @param field field whose value is to be validated
+ */
+ public FieldValidator(BeanValidator validator, Class<?> clazz, Field field) {
+ this.clazz = clazz;
+ this.field = field;
+
+ String fieldName = field.getName();
+ if (fieldName.contains("$")) {
+ serializedName = fieldName;
+ return;
+ }
+
+ SerializedName serAnnot = field.getAnnotation(SerializedName.class);
+ serializedName = (serAnnot != null ? serAnnot.value() : fieldName);
+
+ validator.addValidators(this);
+ addListValidator(validator);
+ addMapValidator(validator);
+
+ if (checkers.isEmpty()) {
+ // has no annotations - nothing to check
+ return;
+ }
+
+ // verify the field type is of interest
+ int mod = field.getModifiers();
+ if (Modifier.isStatic(mod)) {
+ classOnly(clazz.getName() + "." + fieldName + " is annotated but the field is static");
+ checkers.clear();
+ return;
+ }
+
+ // get the field's "getter" method
+ accessor = getAccessor(clazz, fieldName);
+ if (accessor == null) {
+ classOnly(clazz.getName() + "." + fieldName + " is annotated but has no \"get\" method");
+ checkers.clear();
+ return;
+ }
+
+ // determine if null is allowed
+ if (field.getAnnotation(NotNull.class) != null || clazz.getAnnotation(NotNull.class) != null) {
+ setNullAllowed(false);
+ }
+ }
+
+ /**
+ * Adds validators for the individual items within a collection, if the field is a
+ * collection.
+ *
+ * @param validator provider of validation methods
+ */
+ private void addListValidator(BeanValidator validator) {
+ if (!Collection.class.isAssignableFrom(field.getType())) {
+ return;
+ }
+
+ var tannot = field.getAnnotatedType();
+ if (!(tannot instanceof AnnotatedParameterizedType)) {
+ return;
+ }
+
+ AnnotatedType[] targs = ((AnnotatedParameterizedType) tannot).getAnnotatedActualTypeArguments();
+ if (targs.length != 1) {
+ return;
+ }
+
+ var itemValidator = new ItemValidator(validator, targs[0]);
+ if (itemValidator.isEmpty()) {
+ return;
+ }
+
+ checkers.add((result, fieldName, value) -> validator.verCollection(result, fieldName, itemValidator, value));
+ }
+
+ /**
+ * Adds validators for the individual entries within a map, if the field is a map.
+ *
+ * @param validator provider of validation methods
+ */
+ private void addMapValidator(BeanValidator validator) {
+ if (!Map.class.isAssignableFrom(field.getType())) {
+ return;
+ }
+
+ var tannot = field.getAnnotatedType();
+ if (!(tannot instanceof AnnotatedParameterizedType)) {
+ return;
+ }
+
+ AnnotatedType[] targs = ((AnnotatedParameterizedType) tannot).getAnnotatedActualTypeArguments();
+ if (targs.length != 2) {
+ return;
+ }
+
+ var keyValidator = new ItemValidator(validator, targs[0]);
+ var valueValidator = new ItemValidator(validator, targs[1]);
+ if (keyValidator.isEmpty() && valueValidator.isEmpty()) {
+ return;
+ }
+
+ checkers.add((result, fieldName, value) -> validator.verMap(result, fieldName, keyValidator, valueValidator,
+ value));
+ }
+
+ /**
+ * Performs validation of a single field.
+ *
+ * @param result validation results are added here
+ * @param object object whose field is to be validated
+ */
+ public void validateField(BeanValidationResult result, Object object) {
+ if (isEmpty()) {
+ // has no annotations - nothing to check
+ return;
+ }
+
+ // get the value
+ Object value = getValue(object, accessor);
+
+ validateValue(result, serializedName, value);
+ }
+
+ /**
+ * Gets the value from the object using the accessor function.
+ *
+ * @param object object whose value is to be retrieved
+ * @param accessor "getter" method
+ * @return the object's value
+ */
+ private Object getValue(Object object, Method accessor) {
+ try {
+ return accessor.invoke(object);
+
+ } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+ throw new IllegalArgumentException(clazz.getName() + "." + field.getName() + " accessor threw an exception",
+ e);
+ }
+ }
+
+ /**
+ * Throws an exception if there are field-level annotations.
+ *
+ * @param exceptionMessage exception message
+ */
+ private void classOnly(String exceptionMessage) {
+ if (isFieldAnnotated()) {
+ throw new IllegalArgumentException(exceptionMessage);
+ }
+ }
+
+ /**
+ * Gets an annotation from the field or the class.
+ *
+ * @param annotClass annotation class of interest
+ * @return the annotation, or {@code null} if neither the field nor the class has the
+ * desired annotation
+ */
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
+
+ // field annotation takes precedence over class annotation
+ var annot = field.getAnnotation(annotClass);
+ if (annot != null) {
+ setFieldAnnotated(true);
+ return annot;
+ }
+
+ return clazz.getAnnotation(annotClass);
+ }
+
+ /**
+ * Gets an accessor method for the given field.
+ *
+ * @param clazz class whose methods are to be searched
+ * @param fieldName field whose "getter" is to be identified
+ * @return the field's "getter" method, or {@code null} if it is not found
+ */
+ private Method getAccessor(Class<?> clazz, String fieldName) {
+ var capname = StringUtils.capitalize(fieldName);
+ var accessor2 = getMethod(clazz, "get" + capname);
+ if (accessor2 != null) {
+ return accessor2;
+ }
+
+ return getMethod(clazz, "is" + capname);
+ }
+
+ /**
+ * Gets the "getter" method having the specified name.
+ *
+ * @param clazz class whose methods are to be searched
+ * @param methodName name of the method of interest
+ * @return the method, or {@code null} if it is not found
+ */
+ private Method getMethod(Class<?> clazz, String methodName) {
+ for (Method method : clazz.getMethods()) {
+ if (methodName.equals(method.getName()) && validMethod(method)) {
+ return method;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Determines if a method is a valid "getter".
+ *
+ * @param method method to be checked
+ * @return {@code true} if the method is a valid "getter", {@code false} otherwise
+ */
+ private boolean validMethod(Method method) {
+ int mod = method.getModifiers();
+ return !(Modifier.isStatic(mod) || method.getReturnType() == void.class || method.getParameterCount() != 0);
+ }
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java
deleted file mode 100644
index 2a616dba..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupMapValidationResult.java
+++ /dev/null
@@ -1,112 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.common.parameters;
-
-import java.lang.reflect.Field;
-import java.util.Map;
-import java.util.Map.Entry;
-
-/**
- * This class holds the result of the validation of a map of parameter groups.
- */
-public class GroupMapValidationResult extends CommonGroupValidationResult {
- // The name of the parameter group map
- final String mapParameterName;
-
- /**
- * Constructor, create the group map validation result.
- *
- * @param field the map parameter field
- * @param mapObject the value of the map parameter field
- */
- protected GroupMapValidationResult(final Field field, final Object mapObject) {
- super(ParameterConstants.PARAMETER_GROUP_MAP_HAS_STATUS_MESSAGE);
-
- this.mapParameterName = field.getName();
-
- // Cast the map object to a map of parameter groups keyed by string, we can't type check maps
- // due to restrictions on generics so we have to check each entry key is a string and each entry
- // value is a parameter group
- @SuppressWarnings("unchecked")
- Map<String, ParameterGroup> parameterGroupMap = (Map<String, ParameterGroup>) mapObject;
-
- // Add a validation result per map entry
- for (Entry<String, ParameterGroup> parameterGroupMapEntry : parameterGroupMap.entrySet()) {
- // Create a validation status entry for the map
- validationResultMap.put(parameterGroupMapEntry.getKey(),
- new GroupValidationResult(parameterGroupMapEntry.getValue()));
- }
- }
-
- /**
- * Gets the name of the parameter being validated.
- *
- * @return the name
- */
- @Override
- public String getName() {
- return mapParameterName;
- }
-
- /**
- * Set the validation result on a parameter map entry.
- *
- * @param entryName The name of the parameter map entry
- * @param status The validation status for the entry
- * @param message The validation message for the entry
- */
- public void setResult(final String entryName, final ValidationStatus status, final String message) {
- ValidationResult validationResult = validationResultMap.get(entryName);
- if (validationResult == null) {
- throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists");
- }
-
- // Set the status of the parameter group and replace the field result
- validationResult.setResult(status, message);
- this.setResult(status);
- }
-
-
- /**
- * Set the validation result on a parameter map entry.
- *
- * @param entryName The name of the parameter map entry
- * @param mapEntryValidationResult The validation result for the entry
- */
- public void setResult(final String entryName, final ValidationResult mapEntryValidationResult) {
- ValidationResult validationResult = validationResultMap.get(entryName);
- if (validationResult == null) {
- throw new ParameterRuntimeException("no entry with name \"" + entryName + "\" exists");
- }
-
- // Set the status of the parameter group and replace the field result
- validationResultMap.put(entryName, mapEntryValidationResult);
- this.setResult(mapEntryValidationResult.getStatus());
- }
-
- @Override
- protected void addGroupTypeName(StringBuilder result) {
- result.append("parameter group map \"");
- result.append(mapParameterName);
- result.append("\" ");
- }
-} \ No newline at end of file
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java
deleted file mode 100644
index fd680451..00000000
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/GroupValidationResult.java
+++ /dev/null
@@ -1,391 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.common.parameters;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import org.apache.commons.lang3.StringUtils;
-
-/**
- * This class holds the result of the validation of a parameter group.
- */
-public class GroupValidationResult extends CommonGroupValidationResult {
- // The parameter group which the validation result applies
- private final ParameterGroup parameterGroup;
-
- /**
- * Constructor, create the field validation result with default arguments.
- *
- * @param parameterGroup the parameter group being validated
- */
- public GroupValidationResult(final ParameterGroup parameterGroup) {
- super(ParameterConstants.PARAMETER_GROUP_HAS_STATUS_MESSAGE);
-
- this.parameterGroup = parameterGroup;
-
- // Parameter group definitions may be optional
- if (parameterGroup == null) {
- return;
- }
-
- // Add a validation result for all fields in the declared class
- for (Field field : parameterGroup.getClass().getDeclaredFields()) {
- // Check if a validation result should be added for this declared field
- if (isIncludedField(field)) {
- // Set a validation result for the field
- validationResultMap.put(field.getName(), getSetValidationResult(field, parameterGroup));
- }
- }
-
- // Add a validation result for protected and public fields in super classes
- for (Field field : getSuperclassFields(parameterGroup.getClass().getSuperclass())) {
- // Check if a validation result should be added for this declared field
- if (isIncludedField(field)) {
- // Set a validation result for the field
- validationResultMap.putIfAbsent(field.getName(), getSetValidationResult(field, parameterGroup));
- }
- }
- }
-
- /**
- * Construct a validation result for a field, updating "this" status.
- *
- * @param field The parameter field
- * @param ParameterGroup The parameter group containing the field
- * @return the validation result
- * @throws Exception on accessing private fields
- */
- private ValidationResult getSetValidationResult(Field field, ParameterGroup parameterGroup) {
- ValidationResult result = getValidationResult(field, parameterGroup);
- setResult(result.getStatus());
-
- return result;
- }
-
- /**
- * Construct a validation result for a field.
- *
- * @param field The parameter field
- * @param ParameterGroup The parameter group containing the field
- * @return the validation result
- * @throws Exception on accessing private fields
- */
- private ValidationResult getValidationResult(final Field field, final ParameterGroup parameterGroup) {
- final String fieldName = field.getName();
- final Class<?> fieldType = field.getType();
- final Object fieldObject = getObjectField(parameterGroup, field);
-
- // perform null checks
- ParameterValidationResult result = new ParameterValidationResult(field, fieldObject);
- if (!result.isValid()) {
- return result;
- }
-
- // Nested parameter groups are allowed
- if (ParameterGroup.class.isAssignableFrom(fieldType)) {
- if (null != fieldObject) {
- return ((ParameterGroup) fieldObject).validate();
- }
- else {
- return new GroupValidationResult((ParameterGroup) fieldObject);
- }
- }
-
- // Nested maps of parameter groups are allowed
- if (Map.class.isAssignableFrom(field.getType())) {
- checkMapIsParameterGroupMap(fieldName, fieldObject);
- return new GroupMapValidationResult(field, fieldObject);
- }
-
- // Collections of parameter groups are not allowed
- if (Collection.class.isAssignableFrom(field.getType())) {
- checkCollection4ParameterGroups(fieldName, fieldObject);
- return result;
- }
-
- // It's a regular parameter
- return result;
- }
-
- /**
- * Get the value of a field in an object using a getter found with reflection.
- *
- * @param targetObject The object on which to read the field value
- * @param fieldName The name of the field
- * @return The field value
- */
- private Object getObjectField(final Object targetObject, final Field field) {
- String getterMethodName;
-
- // Check for Boolean fields, the convention for boolean getters is that they start with "is"
- // If the field name already starts with "is" then the getter has the field name otherwise
- // the field name is prepended with "is"
- if (boolean.class.equals(field.getType())) {
- if (field.getName().startsWith("is")) {
- getterMethodName = field.getName();
- } else {
- getterMethodName = "is" + StringUtils.capitalize(field.getName());
- }
- } else {
- getterMethodName = "get" + StringUtils.capitalize(field.getName());
- }
-
- // Look up the getter method for the field
- Method getterMethod;
- try {
- getterMethod = targetObject.getClass().getMethod(getterMethodName, (Class<?>[]) null);
- } catch (NoSuchMethodException | SecurityException e) {
- throw new ParameterRuntimeException("could not get getter method for parameter \"" + field.getName() + "\"",
- e);
- }
-
- // Invoke the getter
- try {
- return getterMethod.invoke(targetObject, (Object[]) null);
- } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
- throw new ParameterRuntimeException("error calling getter method for parameter \"" + field.getName() + "\"",
- e);
- }
- }
-
- /**
- * Check if this field is a map of parameter groups indexed by string keys.
- *
- * @param fieldName the name of the collection field.
- * @param mapObject the map object to check
- */
- private void checkMapIsParameterGroupMap(String fieldName, Object mapObject) {
- if (mapObject == null) {
- throw new ParameterRuntimeException("map parameter \"" + fieldName + "\" is null");
- }
-
- Map<?, ?> incomingMap = (Map<?, ?>) mapObject;
-
- for (Entry<?, ?> mapEntry : incomingMap.entrySet()) {
- // Check the key is a string
- if (!String.class.isAssignableFrom(mapEntry.getKey().getClass())) {
- throw new ParameterRuntimeException("map entry is not a parameter group keyed by a string, key \""
- + mapEntry.getKey() + "\" in map \"" + fieldName + "\" is not a string");
- }
-
- // Check the value is a parameter group
- if (!ParameterGroup.class.isAssignableFrom(mapEntry.getValue().getClass())) {
- throw new ParameterRuntimeException("map entry is not a parameter group keyed by a string, value \""
- + mapEntry.getValue() + "\" in map \"" + fieldName + "\" is not a parameter group");
- }
- }
- }
-
- /**
- * Check if this field contains parameter groups.
- *
- * @param fieldName the name of the collection field.
- * @param collectionObject the collection object to check
- */
- private void checkCollection4ParameterGroups(final String fieldName, final Object collectionObject) {
- if (collectionObject == null) {
- throw new ParameterRuntimeException("collection parameter \"" + fieldName + "\" is null");
- }
-
- Collection<?> collection2Check = (Collection<?>) collectionObject;
-
- for (Object collectionMember : collection2Check) {
- if (ParameterGroup.class.isAssignableFrom(collectionMember.getClass())) {
- throw new ParameterRuntimeException("collection parameter \"" + fieldName + "\" is illegal,"
- + " parameter groups are not allowed as collection members");
- }
- }
- }
-
- /**
- * Gets the parameter group for this validation result.
- *
- * @return the parameter class
- */
- public ParameterGroup getParameterGroup() {
- return parameterGroup;
- }
-
- /**
- * Gets the name of the parameter group being validated.
- *
- * @return the name
- */
- @Override
- public String getName() {
- return parameterGroup.getName();
- }
-
- /**
- * Set the validation result on a parameter in a parameter group.
- *
- * @param parameterName The name of the parameter
- * @param status The validation status the field is receiving
- * @param message The validation message explaining the validation status
- */
- public void setResult(final String parameterName, final ValidationStatus status, final String message) {
- ValidationResult validationResult = validationResultMap.get(parameterName);
-
- if (validationResult == null) {
- throw new ParameterRuntimeException("no parameter field exists for parameter: " + parameterName);
- }
-
- // Set the status and the message on the result irrespective of validation result type
- validationResult.setResult(status, message);
-
- // Set the status of this result
- this.setResult(status);
- }
-
- /**
- * Set the validation result on a nested parameter group.
- *
- * @param parameterName The name of the parameter field
- * @param nestedValidationResult The validation result from a nested field
- */
- public void setResult(final String parameterName, final ValidationResult nestedValidationResult) {
- GroupValidationResult groupValidationResult;
- try {
- groupValidationResult = (GroupValidationResult) validationResultMap.get(parameterName);
- } catch (ClassCastException e) {
- throw new ParameterRuntimeException("parameter is not a nested group parameter: " + parameterName, e);
- }
-
- if (groupValidationResult == null) {
- throw new ParameterRuntimeException("no nested parameter field exists for parameter: " + parameterName);
- }
-
- // Set the status of the parameter group and replace the field result
- validationResultMap.put(parameterName, nestedValidationResult);
- this.setResult(nestedValidationResult.getStatus());
- }
-
- /**
- * Set the validation result on a nested parameter group map entry.
- *
- * @param parameterName The name of the parameter field
- * @param key The key of the map entry
- * @param nestedMapValidationResult The validation result from a nested map entry
- */
- public void setResult(final String parameterName, final String key,
- final ValidationResult nestedMapValidationResult) {
- GroupMapValidationResult groupMapValidationResult;
- try {
- groupMapValidationResult = (GroupMapValidationResult) validationResultMap.get(parameterName);
- } catch (ClassCastException e) {
- throw new ParameterRuntimeException("parameter is not a nested group map parameter: " + parameterName, e);
- }
-
- if (groupMapValidationResult == null) {
- throw new ParameterRuntimeException("no group map parameter field exists for parameter: " + parameterName);
- }
-
- // Set the status of the parameter group and the field
- groupMapValidationResult.setResult(key, nestedMapValidationResult);
- this.setResult(nestedMapValidationResult.getStatus());
- }
-
- /**
- * Set the validation status on a group map entry.
- *
- * @param parameterName The name of the parameter field
- * @param key The key of the map entry
- * @param status The validation status of the entry
- * @param message The message for the parameter group
- */
- public void setResult(final String parameterName, final String key, final ValidationStatus status,
- final String message) {
- GroupMapValidationResult groupMapValidationResult;
- try {
- groupMapValidationResult = (GroupMapValidationResult) validationResultMap.get(parameterName);
- } catch (ClassCastException e) {
- throw new ParameterRuntimeException("parameter is not a nested group map parameter: " + parameterName, e);
- }
-
- if (groupMapValidationResult == null) {
- throw new ParameterRuntimeException("no group map parameter field exists for parameter: " + parameterName);
- }
-
- // Set the status of the parameter group and the field
- groupMapValidationResult.setResult(key, status, message);
- this.setResult(status);
- }
-
- @Override
- protected void addGroupTypeName(StringBuilder result) {
- result.append("parameter group \"");
-
- if (parameterGroup != null) {
- result.append(parameterGroup.getName());
- result.append("\" type \"");
- result.append(parameterGroup.getClass().getName());
- } else {
- result.append("UNDEFINED");
- }
-
- result.append("\" ");
- }
-
-
- /**
- * Check if a field should be included for validation.
- *
- * @param field the field to check for inclusion
- * @return true of the field should be included
- */
- private boolean isIncludedField(final Field field) {
- return !field.getName().startsWith("$") && !field.getName().startsWith("_")
- && !Modifier.isStatic(field.getModifiers());
- }
-
- /**
- * Get the public and protected fields of super classes.
- * @param firstSuperClass the first superclass to check
- *
- * @return a set of the superclass fields
- */
- private List<Field> getSuperclassFields(final Class<?> firstSuperClass) {
- List<Field> superclassFields = new ArrayList<>();
-
- Class<?> currentClass = firstSuperClass;
- while (currentClass.getSuperclass() != null) {
- for (Field field : currentClass.getDeclaredFields()) {
- // Check if this field is public or protected
- if (Modifier.isPublic(field.getModifiers()) || Modifier.isProtected(field.getModifiers())) {
- superclassFields.add(field);
- }
- }
-
- // Check the next super class down
- currentClass = currentClass.getSuperclass();
- }
-
- return superclassFields;
- }
-}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java
new file mode 100644
index 00000000..44b70cdf
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ItemValidator.java
@@ -0,0 +1,71 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedType;
+
+/**
+ * Validator of an "item", which is typically found in a collection, or the key or value
+ * components of an entry in a Map.
+ */
+public class ItemValidator extends ValueValidator {
+ private final AnnotatedType annotatedType;
+
+ /**
+ * Constructs the object.
+ *
+ * @param validator provider of validation methods
+ * @param annotatedType a type having validation annotations to be
+ * applied to the item
+ */
+ public ItemValidator(BeanValidator validator, AnnotatedType annotatedType) {
+ this(validator, annotatedType, true);
+ }
+
+ /**
+ * Constructs the object.
+ *
+ * @param validator provider of validation methods
+ * @param annotatedType a type having validation annotations to be
+ * applied to the item
+ * @param addValidators {@code true} if to add validators
+ */
+ public ItemValidator(BeanValidator validator, AnnotatedType annotatedType, boolean addValidators) {
+ this.annotatedType = annotatedType;
+
+ if (addValidators) {
+ validator.addValidators(this);
+ }
+ }
+
+ /**
+ * Gets an annotation from the field or the class.
+ *
+ * @param annotClass annotation class of interest
+ * @return the annotation, or {@code null} if the {@link #annotatedType} does
+ * not contain the desired annotation
+ */
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
+ return annotatedType.getAnnotation(annotClass);
+ }
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java
index e5597206..af1884a3 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ObjectValidationResult.java
@@ -1,6 +1,6 @@
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@ public class ObjectValidationResult extends ValidationResultImpl {
*
* @param name name of the object of this result
* @param object object being validated
+ * @param status result status
+ * @param message result message
*/
public ObjectValidationResult(String name, Object object, ValidationStatus status, String message) {
super(name, object, status, message);
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java
index 240b1f40..300d49c7 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterConstants.java
@@ -1,45 +1,43 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.common.parameters;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
/**
* This static class holds the values of constants for parameter handling.
*/
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ParameterConstants {
// Indentation is 0 on the left and 2 for each level of hierarchy
public static final String DEFAULT_INITIAL_RESULT_INDENTATION = "";
public static final String DEFAULT_RESULT_INDENTATION = " ";
-
+
// By default we do not show validation results for parameters that are validated as clean
public static final boolean DO_NOT_SHOW_CLEAN_RESULTS = false;
-
+
// Messages for clean validations
public static final String PARAMETER_GROUP_HAS_STATUS_MESSAGE = "parameter group has status ";
public static final String PARAMETER_GROUP_MAP_HAS_STATUS_MESSAGE = "parameter group map has status ";
public static final String PARAMETER_HAS_STATUS_MESSAGE = "parameter has status ";
-
- /**
- * Private constructor to prevent subclassing.
- */
- private ParameterConstants() {
- // Private constructor to prevent subclassing
- }
-} \ No newline at end of file
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java
index 0b29a4e6..aa3105ef 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterException.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +21,8 @@
package org.onap.policy.common.parameters;
+import lombok.Getter;
+
/**
* Exception thrown oon parameter reading, validation, and check errors.
*
@@ -29,6 +32,7 @@ public class ParameterException extends Exception {
private static final long serialVersionUID = -8507246953751956974L;
// The object on which the exception was thrown
+ @Getter
private final transient Object object;
/**
@@ -84,28 +88,19 @@ public class ParameterException extends Exception {
/**
* Build a cascaded message from an exception and all its nested exceptions.
- *
+ *
* @param throwable the top level exception
* @return cascaded message string
*/
public static String buildCascadedMessage(final Throwable throwable) {
- final StringBuilder builder = new StringBuilder();
+ final var builder = new StringBuilder();
builder.append(throwable.getMessage());
- for (Throwable t = throwable; t != null; t = t.getCause()) {
+ for (var t = throwable; t != null; t = t.getCause()) {
builder.append("\ncaused by: ");
builder.append(t.getMessage());
}
return builder.toString();
}
-
- /**
- * Get the object on which the exception was thrown.
- *
- * @return The object on which the exception was thrown
- */
- public Object getObject() {
- return object;
- }
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java
index 8bfa183b..ab610933 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroup.java
@@ -1,19 +1,20 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
@@ -29,28 +30,28 @@ package org.onap.policy.common.parameters;
public interface ParameterGroup {
/**
* Get the group name.
- *
+ *
* @return the group name
*/
public String getName();
/**
* Set the group name.
- *
+ *
* @param name the group name
*/
public void setName(final String name);
/**
* Validate parameters.
- *
+ *
* @return the result of the parameter validation
*/
- GroupValidationResult validate();
+ BeanValidationResult validate();
/**
* Check if the parameters are valid.
- *
+ *
* @return true if the parameters are valid
*/
default boolean isValid() {
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java
index f0f34ac1..8a987bdd 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterGroupImpl.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,9 @@
package org.onap.policy.common.parameters;
+import lombok.AllArgsConstructor;
import lombok.Getter;
+import lombok.NoArgsConstructor;
import lombok.Setter;
import org.onap.policy.common.parameters.annotations.NotBlank;
import org.onap.policy.common.parameters.annotations.NotNull;
@@ -32,30 +34,16 @@ import org.onap.policy.common.parameters.annotations.NotNull;
@NotBlank
@Getter
@Setter
+@NoArgsConstructor
+@AllArgsConstructor
public class ParameterGroupImpl implements ParameterGroup {
/**
- * Group name. Note: this MUST not be "private" or it will not be validated.
+ * Group name.
*/
- protected String name;
-
- /**
- * Constructs the object, with a {@code null} name.
- */
- public ParameterGroupImpl() {
- this.name = null;
- }
-
- /**
- * Constructs the object.
- *
- * @param name the group's name
- */
- public ParameterGroupImpl(String name) {
- this.name = name;
- }
+ private String name;
@Override
- public GroupValidationResult validate() {
- return new GroupValidationResult(this);
+ public BeanValidationResult validate() {
+ return new BeanValidator().validateTop(getClass().getSimpleName(), this);
}
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java
index 071593a9..4eb79447 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterRuntimeException.java
@@ -1,25 +1,28 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.common.parameters;
+import lombok.Getter;
+
/**
* A run time exception thrown on parameter validations.
*
@@ -29,6 +32,7 @@ public class ParameterRuntimeException extends RuntimeException {
private static final long serialVersionUID = -8507246953751956974L;
// The object on which the exception was thrown
+ @Getter
private final transient Object object;
/**
@@ -81,13 +85,4 @@ public class ParameterRuntimeException extends RuntimeException {
public String getCascadedMessage() {
return ParameterException.buildCascadedMessage(this);
}
-
- /**
- * Get the object on which the exception was thrown.
- *
- * @return The object
- */
- public Object getObject() {
- return object;
- }
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java
index 358e5535..a368d25f 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java
@@ -1,19 +1,20 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
@@ -24,37 +25,32 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
/**
* The parameter service makes ONAP PF parameter groups available to all classes in a JVM.
- *
+ *
* <p>The reason for having a parameter service is to avoid having to pass parameters down long call chains in modules
* such as PDPs and editors. The parameter service makes correct and verified parameters available statically.
- *
+ *
* <p>The parameter service must be used with care because changing a parameter set anywhere in a JVM will affect all
* users of those parameters anywhere in the JVM.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
-public abstract class ParameterService {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ParameterService {
// The map holding the parameters
private static Map<String, ParameterGroup> parameterGroupMap = new ConcurrentHashMap<>();
/**
- * This class is an abstract static class that cannot be extended.
- */
- private ParameterService() {
- }
-
- /**
* Register a parameter group with the parameter service.
*
* @param parameterGroup the parameter group
*/
public static void register(final ParameterGroup parameterGroup) {
- if (!parameterGroupMap.containsKey(parameterGroup.getName())) {
- parameterGroupMap.put(parameterGroup.getName(), parameterGroup);
- } else {
+ if (parameterGroupMap.putIfAbsent(parameterGroup.getName(), parameterGroup) != null) {
throw new ParameterRuntimeException(
"\"" + parameterGroup.getName() + "\" already registered in parameter service");
}
@@ -70,7 +66,7 @@ public abstract class ParameterService {
if (overwrite && parameterGroupMap.containsKey(parameterGroup.getName())) {
deregister(parameterGroup);
}
-
+
register(parameterGroup);
}
@@ -80,9 +76,7 @@ public abstract class ParameterService {
* @param parameterGroupName the name of the parameter group
*/
public static void deregister(final String parameterGroupName) {
- if (parameterGroupMap.containsKey(parameterGroupName)) {
- parameterGroupMap.remove(parameterGroupName);
- } else {
+ if (parameterGroupMap.remove(parameterGroupName) == null) {
throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not registered in parameter service");
}
}
@@ -104,7 +98,7 @@ public abstract class ParameterService {
*/
public static <T extends ParameterGroup> T get(final String parameterGroupName) {
@SuppressWarnings("unchecked")
- final T parameterGroup = (T) parameterGroupMap.get(parameterGroupName);
+ final var parameterGroup = (T) parameterGroupMap.get(parameterGroupName);
if (parameterGroup == null) {
throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not found in parameter service");
@@ -120,7 +114,7 @@ public abstract class ParameterService {
* @return true if the parameter is defined
*/
public static boolean contains(final String parameterGroupName) {
- return parameterGroupMap.containsKey(parameterGroupName) && parameterGroupMap.get(parameterGroupName) != null;
+ return parameterGroupMap.get(parameterGroupName) != null;
}
/**
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
index 35837376..7e56230d 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterValidationResult.java
@@ -1,7 +1,7 @@
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ public class ParameterValidationResult implements ValidationResult {
* @param parameterValue field's value
*/
private void checkMinValue(final Field field, final Object parameterValue) {
- Min minAnnot = field.getAnnotation(Min.class);
+ var minAnnot = field.getAnnotation(Min.class);
if (minAnnot != null && ((Number) parameterValue).longValue() < minAnnot.value()) {
setResult(ValidationStatus.INVALID, "must be >= " + minAnnot.value());
}
@@ -86,7 +86,7 @@ public class ParameterValidationResult implements ValidationResult {
* @param parameterValue field's value
*/
private void checkMaxValue(final Field field, final Object parameterValue) {
- Max maxAnnot = field.getAnnotation(Max.class);
+ var maxAnnot = field.getAnnotation(Max.class);
if (maxAnnot != null && ((Number) parameterValue).longValue() > maxAnnot.value()) {
setResult(ValidationStatus.INVALID, "must be <= " + maxAnnot.value());
}
@@ -102,7 +102,7 @@ public class ParameterValidationResult implements ValidationResult {
* @return the field's annotation, or {@code null} if it does not exist
*/
private static <T extends Annotation> T getAnyAnnotation(final Field field, Class<T> annotClass) {
- T annot = field.getAnnotation(annotClass);
+ var annot = field.getAnnotation(annotClass);
if (annot != null) {
return annot;
}
@@ -157,21 +157,13 @@ public class ParameterValidationResult implements ValidationResult {
return null;
}
- StringBuilder validationResultBuilder = new StringBuilder();
-
- validationResultBuilder.append(initialIndentation);
- validationResultBuilder.append("field \"");
- validationResultBuilder.append(getName());
- validationResultBuilder.append("\" type \"");
- validationResultBuilder.append(field.getType().getName());
- validationResultBuilder.append("\" value \"");
- validationResultBuilder.append(parameterValue);
- validationResultBuilder.append("\" ");
- validationResultBuilder.append(getStatus());
- validationResultBuilder.append(", ");
- validationResultBuilder.append(message);
- validationResultBuilder.append('\n');
-
- return validationResultBuilder.toString();
+ return initialIndentation
+ + "field \"" + getName()
+ + "\" type \"" + field.getType().getName()
+ + "\" value \"" + parameterValue
+ + "\" " + getStatus()
+ + ", "
+ + message
+ + '\n';
}
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResultImpl.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResultImpl.java
index 1d8aa239..45898818 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResultImpl.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResultImpl.java
@@ -1,6 +1,7 @@
/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,12 +21,14 @@
package org.onap.policy.common.parameters;
+import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Basic implementation of a ValidationResult.
*/
@Getter
+@AllArgsConstructor
public abstract class ValidationResultImpl implements ValidationResult {
public static final String ITEM_HAS_STATUS_MESSAGE = "item has status ";
@@ -56,27 +59,12 @@ public abstract class ValidationResultImpl implements ValidationResult {
* @param name name of the object of this result
* @param object object being validated
*/
- public ValidationResultImpl(String name, Object object) {
+ protected ValidationResultImpl(String name, Object object) {
this.name = name;
this.object = object;
}
/**
- * Constructs the object.
- *
- * @param name name of the object of this result
- * @param object object being validated
- * @param status the validation status
- * @param message the validation message explaining the validation status
- */
- public ValidationResultImpl(String name, Object object, ValidationStatus status, String message) {
- this.name = name;
- this.object = object;
- this.status = status;
- this.message = message;
- }
-
- /**
* Validates that the value is not {@code null}.
*
* @return {@code true} if the value is not null, {@code false} otherwise
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java
new file mode 100644
index 00000000..faf41008
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValueValidator.java
@@ -0,0 +1,146 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import org.onap.policy.common.parameters.annotations.NotNull;
+
+/**
+ * Validator of a value.
+ * <p/>
+ * Note: this currently does not support Min/Max validation of "short" or "byte"; these
+ * annotations are simply ignored for these types.
+ */
+@NoArgsConstructor
+public class ValueValidator {
+
+ /**
+ * {@code True} if the value is allowed to be {@code null}, {@code false} otherwise.
+ * Subclasses are expected to set this, typically based on the validation annotations
+ * associated with the value.
+ */
+ @Getter
+ @Setter(AccessLevel.PROTECTED)
+ private boolean nullAllowed = true;
+
+ /**
+ * Predicates to invoke to validate an object.
+ * <p/>
+ * Note: each predicate is expected to return {@code true} if the next check is
+ * allowed to proceed, {@code false} otherwise. In addition, if {@link #nullAllowed}
+ * is {@code true}, then the predicates must be prepared to deal with a {@code null}
+ * Object as their input parameter.
+ */
+ protected List<Checker> checkers = new ArrayList<>(10);
+
+ /**
+ * Determines if the validator has anything to check.
+ *
+ * @return {@code true} if the validator is empty (i.e., has nothing to check)
+ */
+ public boolean isEmpty() {
+ return checkers.isEmpty();
+ }
+
+ /**
+ * Performs validation of a single field.
+ *
+ * @param result validation results are added here
+ * @param fieldName field whose value is being verified
+ * @param value value to be validated
+ */
+ protected void validateValue(BeanValidationResult result, String fieldName, Object value) {
+
+ if (value == null && isNullAllowed()) {
+ // value is null and null is allowed - just return
+ return;
+ }
+
+ for (Checker checker : checkers) {
+ if (!checker.test(result, fieldName, value)) {
+ // invalid - don't bother with additional checks
+ return;
+ }
+ }
+ }
+
+ /**
+ * Looks for an annotation at the class or field level. If an annotation is found at
+ * either the field or class level, then it adds a verifier to
+ * {@link ValueValidator#checkers}.
+ *
+ * @param annotClass class of annotation to find
+ * @param checker function to validate the value
+ */
+ public <T extends Annotation> void addAnnotation(Class<T> annotClass, Checker checker) {
+ var annot = getAnnotation(annotClass);
+ if (annot != null) {
+ checkers.add(checker);
+
+ if (annotClass == NotNull.class) {
+ setNullAllowed(false);
+ }
+ }
+ }
+
+ /**
+ * Looks for an annotation at the class or field level. If an annotation is found at
+ * either the field or class level, then it adds a verifier to
+ * {@link ValueValidator#checkers}.
+ *
+ * @param annotClass class of annotation to find
+ * @param checker function to validate the value
+ */
+ public <T extends Annotation> void addAnnotation(Class<T> annotClass, CheckerWithAnnot<T> checker) {
+ var annot = getAnnotation(annotClass);
+ if (annot != null) {
+ checkers.add((result, fieldName, value) -> checker.test(result, fieldName, annot, value));
+ }
+ }
+
+ /**
+ * Gets an annotation from the field or the class. The default method simply returns
+ * {@code null}.
+ *
+ * @param annotClass annotation class of interest
+ * @return the annotation, or {@code null} if neither the field nor the class has the
+ * desired annotation
+ */
+ public <T extends Annotation> T getAnnotation(Class<T> annotClass) {
+ return null;
+ }
+
+ // functions to validate a value extracted from a field
+
+ public static interface Checker {
+ boolean test(BeanValidationResult result, String fieldName, Object value);
+ }
+
+ public static interface CheckerWithAnnot<T extends Annotation> {
+ boolean test(BeanValidationResult result, String fieldName, T annotation, Object value);
+ }
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java
new file mode 100644
index 00000000..14d76fd7
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/ClassName.java
@@ -0,0 +1,37 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE_USE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that a field (i.e., String) identifies the name of a class in the classpath.
+ */
+@Retention(RUNTIME)
+@Target({FIELD, TYPE_USE})
+public @interface ClassName {
+
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java
index 1d8fd90f..f28fd2cf 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Max.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,13 +21,14 @@
package org.onap.policy.common.parameters.annotations;
import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
-@Target(FIELD)
+@Target({FIELD, TYPE_USE})
public @interface Max {
/**
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java
index 9ad6d7e0..305d9810 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Min.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,13 +21,14 @@
package org.onap.policy.common.parameters.annotations;
import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
-@Target(FIELD)
+@Target({FIELD, TYPE_USE})
public @interface Min {
/**
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java
index 169fa593..0744beb5 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotBlank.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ package org.onap.policy.common.parameters.annotations;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
@@ -31,7 +32,7 @@ import java.lang.annotation.Target;
* Indicates that a field (i.e., String) may not be empty.
*/
@Retention(RUNTIME)
-@Target({TYPE, FIELD})
+@Target({TYPE, FIELD, TYPE_USE})
public @interface NotBlank {
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java
index 3c7bc8b7..81356005 100644
--- a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/NotNull.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ package org.onap.policy.common.parameters.annotations;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
@@ -31,7 +32,7 @@ import java.lang.annotation.Target;
* Indicates that a field may not be null.
*/
@Retention(RUNTIME)
-@Target({TYPE, FIELD})
+@Target({TYPE, FIELD, TYPE_USE})
public @interface NotNull {
}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Pattern.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Pattern.java
new file mode 100644
index 00000000..91a74d70
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Pattern.java
@@ -0,0 +1,38 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE_USE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(RUNTIME)
+@Target({FIELD, TYPE_USE})
+public @interface Pattern {
+
+ /**
+ * Regular expression to be matched.
+ */
+ String regexp();
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java
new file mode 100644
index 00000000..160e0124
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Size.java
@@ -0,0 +1,43 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE_USE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates the size of a Map or Collection.
+ */
+@Retention(RUNTIME)
+@Target({FIELD, TYPE_USE})
+public @interface Size {
+
+ /**
+ * The minimum size allowed.
+ *
+ * @return the minimum size allowed
+ */
+ int min();
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Valid.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Valid.java
new file mode 100644
index 00000000..227a726f
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/annotations/Valid.java
@@ -0,0 +1,35 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.TYPE_USE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(RUNTIME)
+@Target({TYPE, FIELD, TYPE_USE})
+public @interface Valid {
+
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java
new file mode 100644
index 00000000..c73d135a
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupConstraint.java
@@ -0,0 +1,57 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021, 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.validation;
+
+import jakarta.validation.Constraint;
+import jakarta.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Documented
+@Constraint(validatedBy = ParameterGroupValidator.class)
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ParameterGroupConstraint {
+
+ /**
+ * Get error Message.
+ *
+ * @return error Message
+ */
+ String message() default "validation error(s) on parameters";
+
+ /**
+ * Get groups.
+ *
+ * @return Class arrays
+ */
+ Class<?>[] groups() default {};
+
+ /**
+ * Get payload.
+ *
+ * @return Class arrays
+ */
+ Class<? extends Payload>[] payload() default {};
+}
diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java
new file mode 100644
index 00000000..024ca139
--- /dev/null
+++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/validation/ParameterGroupValidator.java
@@ -0,0 +1,42 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021, 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.parameters.validation;
+
+import jakarta.validation.ConstraintValidator;
+import jakarta.validation.ConstraintValidatorContext;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ParameterGroup;
+
+public class ParameterGroupValidator implements ConstraintValidator<ParameterGroupConstraint, ParameterGroup> {
+
+ @Override
+ public boolean isValid(ParameterGroup value, ConstraintValidatorContext context) {
+ if (value == null) {
+ return true;
+ }
+ final BeanValidationResult result = value.validate();
+ if (!result.isValid()) {
+ context.buildConstraintViolationWithTemplate(result.getMessage()).addConstraintViolation();
+ }
+ return result.isValid();
+ }
+
+}