aboutsummaryrefslogtreecommitdiffstats
path: root/common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@ericsson.com>2018-07-30 10:22:27 +0100
committerliamfallon <liam.fallon@ericsson.com>2018-07-31 16:06:47 +0100
commit73ba8039930ba56f6a64a7acc4126dc50b77070d (patch)
treebb46330718aa99e9753a69715c0ea54e990aca4d /common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java
parentc36939ee0e648f4ac28b9cdc538991ced0c603bd (diff)
Improve validation, add hierarchical validation
Parameter validaiton updated to generically support nested groups of parameters, and nested maps of parameters. Unit test showing JSON parameter input added. Unit test showing YAML parameter inout added. Test parameter group classes moved into subdirectory This allows parameters to be unmarshaled seamlessly from JSON and YAML files. Change-Id: I768e11f31ee7f62299c4d5d95ab68a005d1aff16 Issue-ID: POLICY-922 Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java')
-rw-r--r--common-parameters/src/main/java/org/onap/policy/common/parameters/ParameterService.java87
1 files changed, 45 insertions, 42 deletions
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 f411937d..db6995c5 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,5 +1,4 @@
-package org.onap.policy.common.parameters;
-/*
+/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2018 Ericsson. All rights reserved.
* ================================================================================
@@ -19,25 +18,27 @@ package org.onap.policy.common.parameters;
* ============LICENSE_END=========================================================
*/
+package org.onap.policy.common.parameters;
+
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
- * The parameter service makes ONAP PF parameters available to all classes in a JVM.
- *
- * 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.
- *
- * 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.
+ * 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 {
// The map holding the parameters
- private static Map<Class<? extends AbstractParameters>, AbstractParameters> parameterMap = new ConcurrentHashMap<>();
+ private static Map<String, ParameterGroup> parameterGroupMap = new ConcurrentHashMap<>();
/**
* This class is an abstract static class that cannot be extended.
@@ -46,69 +47,71 @@ public abstract class ParameterService {
}
/**
- * Register parameters with the parameter service.
+ * Register a parameter group with the parameter service.
*
- * @param <P> the generic type
- * @param parametersClass the class of the parameter, used to index the parameter
- * @param parameters the parameters
+ * @param parameterGroup the parameter group
*/
- public static <P extends AbstractParameters> void registerParameters(final P parameters) {
- parameterMap.put(parameters.getClass(), parameters);
+ public static void register(final ParameterGroup parameterGroup) {
+ if (!parameterGroupMap.containsKey(parameterGroup.getName())) {
+ parameterGroupMap.put(parameterGroup.getName(), parameterGroup);
+ } else {
+ throw new ParameterRuntimeException(
+ "\"" + parameterGroup.getName() + "\" already registered in parameter service");
+ }
}
/**
- * Remove parameters from the parameter service.
+ * Remove a parameter group from the parameter service.
*
- * @param <P> the generic type
- * @param parametersClass the class of the parameter, used to index the parameter
+ * @param parameterGroupName the name of the parameter group
*/
- public static <P extends AbstractParameters> void deregisterParameters(final Class<P> parametersClass) {
- parameterMap.remove(parametersClass);
+ public static void deregister(final String parameterGroupName) {
+ if (parameterGroupMap.containsKey(parameterGroupName)) {
+ parameterGroupMap.remove(parameterGroupName);
+ } else {
+ throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not registered in parameter service");
+ }
}
/**
- * Get parameters from the parameter service.
+ * Get a parameter group from the parameter service.
*
- * @param <P> the generic type
- * @param parametersClass the class of the parameter, used to index the parameter
- * @return The parameter
+ * @param parameterGroupName the name of the parameter group
+ * @return The parameter group
*/
- @SuppressWarnings("unchecked")
- public static <P extends AbstractParameters> P getParameters(final Class<P> parametersClass) {
- final P parameter = (P) parameterMap.get(parametersClass);
+ public static ParameterGroup get(final String parameterGroupName) {
+ final ParameterGroup parameterGroup = parameterGroupMap.get(parameterGroupName);
- if (parameter == null) {
- throw new ParameterRuntimeException(
- "Parameters for " + parametersClass.getCanonicalName() + " not found in parameter service");
+ if (parameterGroup == null) {
+ throw new ParameterRuntimeException("\"" + parameterGroupName + "\" not found in parameter service");
}
- return parameter;
+ return parameterGroup;
}
/**
- * Check if parameters is defined on the parameter service.
+ * Check if a parameter group is defined on the parameter service.
*
- * @param <P> the generic type
- * @param parametersClass the class of the parameter, used to index the parameter
+ * @param parameterGroupName the name of the parameter group
* @return true if the parameter is defined
*/
- public static <P extends AbstractParameters> boolean existsParameters(final Class<P> parametersClass) {
- return parameterMap.get(parametersClass) != null;
+ public static boolean contains(final String parameterGroupName) {
+ return parameterGroupMap.containsKey(parameterGroupName) && parameterGroupMap.get(parameterGroupName) != null;
}
/**
- * Get all the entries in the parameters map.
+ * Get all parameter groups.
*
* @return The entries
*/
- public static Set<Entry<Class<? extends AbstractParameters>, AbstractParameters>> getAll() {
- return parameterMap.entrySet();
+ public static Set<Entry<String, ParameterGroup>> getAll() {
+ return parameterGroupMap.entrySet();
}
/**
- * Clear all parameters in the parameter service.
+ * Clear all parameter groups in the parameter service.
*/
public static void clear() {
- parameterMap.clear();
+ parameterGroupMap.clear();
}
}