From 655d6f798878cd69b6c02c54d4bbed83d01fd937 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 28 Aug 2018 14:13:02 +0100 Subject: Improvements to Parameter Srvice Add proper handling for optional parameters Add setName() to the parameter group interface to force definition of that method Add some convenience metnods to parameter service Issue-ID: POLICY-1035 Change-Id: Iaa9226f45215b00aff9b78ab5fc120c24a0af46d Signed-off-by: liamfallon --- .../parameters/GroupMapValidationResult.java | 23 +++++- .../common/parameters/GroupValidationResult.java | 84 +++++++++++++++------- .../common/parameters/ParameterException.java | 2 +- .../policy/common/parameters/ParameterGroup.java | 7 ++ .../policy/common/parameters/ParameterService.java | 28 +++++++- .../policy/common/parameters/ValidationResult.java | 9 +++ .../policy/common/parameters/ValidationStatus.java | 8 +++ 7 files changed, 130 insertions(+), 31 deletions(-) (limited to 'common-parameters/src/main/java/org/onap') 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 index e607ff98..3baacb1f 100644 --- 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 @@ -89,7 +89,7 @@ public class GroupMapValidationResult implements ValidationResult { * @param message The validation message explaining the validation status */ @Override - public void setResult(ValidationStatus status, String message) { + public void setResult(final ValidationStatus status, final String message) { setResult(status); this.message = message; } @@ -108,13 +108,32 @@ public class GroupMapValidationResult implements ValidationResult { } } + /** + * 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(String entryName, ValidationResult mapEntryValidationResult) { + 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"); 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 index ac9dc7ba..fc2f6ca5 100644 --- 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 @@ -48,6 +48,11 @@ public class GroupValidationResult implements ValidationResult { public GroupValidationResult(final ParameterGroup parameterGroup) { this.parameterGroup = parameterGroup; + // Parameter group definitions may be optional + if (parameterGroup == null) { + return; + } + // Add a validation result per field for (Field field : parameterGroup.getClass().getDeclaredFields()) { // Exclude system fields @@ -86,15 +91,15 @@ public class GroupValidationResult implements ValidationResult { // Nested parameter groups are allowed if (ParameterGroup.class.isAssignableFrom(fieldType)) { - return new GroupValidationResult((ParameterGroup) field.get(parameterGroup)); + 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); @@ -115,7 +120,7 @@ public class GroupValidationResult implements ValidationResult { if (mapObject == null) { throw new ParameterRuntimeException("map parameter \"" + fieldName + "\" is null"); } - + Map incomingMap = (Map) mapObject; for (Entry mapEntry : incomingMap.entrySet()) { @@ -184,7 +189,7 @@ public class GroupValidationResult implements ValidationResult { } /** - * Set the validation result on on a parameter group. + * 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 @@ -196,9 +201,8 @@ public class GroupValidationResult implements ValidationResult { } /** - * Set the validation result on 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 + * 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 */ @@ -211,27 +215,23 @@ public class GroupValidationResult implements ValidationResult { } /** - * Set the validation result on a parameter group. + * Set the validation result on a parameter in a parameter group. * - * @param parameterGroupName The name of the 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 parameterGroupName, final ValidationStatus status, final String message) { - ParameterValidationResult parameterValidationResult; - try { - parameterValidationResult = (ParameterValidationResult) validationResultMap.get(parameterGroupName); - } catch (ClassCastException e) { - throw new ParameterRuntimeException("parameter not a regular parameter: " + parameterGroupName, e); - } + public void setResult(final String parameterName, final ValidationStatus status, final String message) { + ValidationResult validationResult = validationResultMap.get(parameterName); - if (parameterValidationResult == null) { - throw new ParameterRuntimeException( - "no regular parameter field exists for parameter: " + parameterGroupName); + if (validationResult == null) { + throw new ParameterRuntimeException("no parameter field exists for parameter: " + parameterName); } - // Set the status of the parameter group and the field - parameterValidationResult.setResult(status, message); + // 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); } @@ -241,7 +241,7 @@ public class GroupValidationResult implements ValidationResult { * @param parameterName The name of the parameter field * @param nestedValidationResult The validation result from a nested field */ - public void setResult(String parameterName, ValidationResult nestedValidationResult) { + public void setResult(final String parameterName, final ValidationResult nestedValidationResult) { GroupValidationResult groupValidationResult; try { groupValidationResult = (GroupValidationResult) validationResultMap.get(parameterName); @@ -283,6 +283,32 @@ public class GroupValidationResult implements ValidationResult { 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); + } + /** * Gets the validation result. * @@ -301,9 +327,15 @@ public class GroupValidationResult implements ValidationResult { validationResultBuilder.append(initialIndentation); validationResultBuilder.append("parameter group \""); - validationResultBuilder.append(parameterGroup.getName()); - validationResultBuilder.append("\" type \""); - validationResultBuilder.append(parameterGroup.getClass().getCanonicalName()); + + if (parameterGroup != null) { + validationResultBuilder.append(parameterGroup.getName()); + validationResultBuilder.append("\" type \""); + validationResultBuilder.append(parameterGroup.getClass().getCanonicalName()); + } + else { + validationResultBuilder.append("UNDEFINED"); + } validationResultBuilder.append("\" "); validationResultBuilder.append(status); validationResultBuilder.append(", "); 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 3a6e17e8..0b29a4e6 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 @@ -88,7 +88,7 @@ public class ParameterException extends Exception { * @param throwable the top level exception * @return cascaded message string */ - public static String buildCascadedMessage(Throwable throwable) { + public static String buildCascadedMessage(final Throwable throwable) { final StringBuilder builder = new StringBuilder(); builder.append(throwable.getMessage()); 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 48e8379f..8bfa183b 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 @@ -34,6 +34,13 @@ public interface ParameterGroup { */ public String getName(); + /** + * Set the group name. + * + * @param name the group name + */ + public void setName(final String name); + /** * Validate parameters. * 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 db6995c5..358e5535 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 @@ -60,6 +60,20 @@ public abstract class ParameterService { } } + /** + * Register a parameter group with the parameter service. + * + * @param parameterGroup the parameter group + * @param overwrite if true, overwrite the current value if set + */ + public static void register(final ParameterGroup parameterGroup, final boolean overwrite) { + if (overwrite && parameterGroupMap.containsKey(parameterGroup.getName())) { + deregister(parameterGroup); + } + + register(parameterGroup); + } + /** * Remove a parameter group from the parameter service. * @@ -73,14 +87,24 @@ public abstract class ParameterService { } } + /** + * Remove a parameter group from the parameter service. + * + * @param parameterGroup the parameter group + */ + public static void deregister(final ParameterGroup parameterGroup) { + deregister(parameterGroup.getName()); + } + /** * Get a parameter group from the parameter service. * * @param parameterGroupName the name of the parameter group * @return The parameter group */ - public static ParameterGroup get(final String parameterGroupName) { - final ParameterGroup parameterGroup = parameterGroupMap.get(parameterGroupName); + public static T get(final String parameterGroupName) { + @SuppressWarnings("unchecked") + final T parameterGroup = (T) parameterGroupMap.get(parameterGroupName); if (parameterGroup == null) { throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not found in parameter service"); diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java index b97ccda0..309fd001 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationResult.java @@ -47,6 +47,15 @@ public interface ValidationResult { return getStatus().isValid(); } + /** + * Checks if the result is clean. + * + * @return true, if is clean + */ + default boolean isClean() { + return getStatus().isClean(); + } + /** * Gets the validation result. * diff --git a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java index ff453a1b..8692506f 100644 --- a/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java +++ b/common-parameters/src/main/java/org/onap/policy/common/parameters/ValidationStatus.java @@ -33,4 +33,12 @@ public enum ValidationStatus { public boolean isValid() { return !this.equals(INVALID); } + + /** + * Check if the validation was clean. + * @return true if the validation is clean + */ + public boolean isClean() { + return this.equals(CLEAN); + } } -- cgit 1.2.3-korg