diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-08-28 14:13:02 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-08-28 14:13:38 +0100 |
commit | 655d6f798878cd69b6c02c54d4bbed83d01fd937 (patch) | |
tree | 9410bfe42f9095efb4f2871fbf08c5e7723f5a2f /common-parameters/src/main/java | |
parent | f133be23c3fab4609e969a89ad64869bc8a7c38f (diff) |
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 <liam.fallon@ericsson.com>
Diffstat (limited to 'common-parameters/src/main/java')
7 files changed, 130 insertions, 31 deletions
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; } @@ -112,9 +112,28 @@ 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); @@ -284,6 +284,32 @@ public class GroupValidationResult implements ValidationResult { } /** + * 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. * * @param initialIndentation the indentation to use on the main result output @@ -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 @@ -35,6 +35,13 @@ public interface ParameterGroup { 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 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 @@ -61,6 +61,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. * * @param parameterGroupName the name of the parameter group @@ -74,13 +88,23 @@ 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 extends ParameterGroup> 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 @@ -48,6 +48,15 @@ public interface ValidationResult { } /** + * Checks if the result is clean. + * + * @return true, if is clean + */ + default boolean isClean() { + return getStatus().isClean(); + } + + /** * Gets the validation result. * * @return the full 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); + } } |