From 7f6929e5ea6da4015090b3adac73b35f9b3afb18 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 12 Mar 2020 15:16:44 -0400 Subject: Fix sonar issues in drools-applications Fixed various sonar issues, including moving some code to policy-common. Fixed some eclipse warnings, including deprecated junit APIs. Issue-ID: POLICY-2426 Signed-off-by: Jim Hahn Change-Id: I10b40c1e5af62308f2e4c315e4d399aafff2998f --- .../controlloop/drl/legacy/ControlLoopParams.java | 4 +- .../eventmanager/ControlLoopEventManager2.java | 2 - .../eventmanager/EventManagerServices.java | 8 +- .../processor/ControlLoopProcessor.java | 3 +- .../policy/controlloop/utils/ControlLoopUtils.java | 211 --------------------- 5 files changed, 9 insertions(+), 219 deletions(-) (limited to 'controlloop/common/eventmanager/src/main/java/org') diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java index 769a6d971..3eac66112 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParams.java @@ -35,5 +35,7 @@ public class ControlLoopParams implements Serializable { private String policyName; private String policyScope; private String policyVersion; - private ToscaPolicy toscaPolicy; + + // not serializable, thus must be transient + private transient ToscaPolicy toscaPolicy; } diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager2.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager2.java index f2e99a9ad..69cbe7839 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager2.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManager2.java @@ -100,8 +100,6 @@ public class ControlLoopEventManager2 implements ManagerContext, Serializable { FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR } - // TODO limit the number of policies that may be executed for a single event? - /** * {@code True} if this object was created by this JVM instance, {@code false} * otherwise. This will be {@code false} if this object is reconstituted from a diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/EventManagerServices.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/EventManagerServices.java index d8668e47d..3fbedcb1f 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/EventManagerServices.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/eventmanager/EventManagerServices.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.Properties; import lombok.Getter; import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.utils.properties.PropertyObjectUtils; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider; import org.onap.policy.controlloop.actor.guard.GuardConfig; @@ -38,7 +39,6 @@ import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager; import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerImpl; import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams; import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub; -import org.onap.policy.controlloop.utils.ControlLoopUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -86,8 +86,8 @@ public class EventManagerServices { Properties props = new Properties(); props.load(inpstr); - Map parameters = ControlLoopUtils.toObject(props, ACTOR_SERVICE_PROPERTIES); - ControlLoopUtils.compressLists(parameters); + Map parameters = PropertyObjectUtils.toObject(props, ACTOR_SERVICE_PROPERTIES); + PropertyObjectUtils.compressLists(parameters); actorService.configure(parameters); actorService.start(); @@ -157,7 +157,7 @@ public class EventManagerServices { */ public OperationHistoryDataManagerImpl makeDataManager(Properties props) { try { - Map parameters = ControlLoopUtils.toObject(props, DATA_MANAGER_PROPERTIES); + Map parameters = PropertyObjectUtils.toObject(props, DATA_MANAGER_PROPERTIES); OperationHistoryDataManagerParams params = Util.translate(DATA_MANAGER_PROPERTIES, parameters, OperationHistoryDataManagerParams.class); ValidationResult result = params.validate(DATA_MANAGER_PROPERTIES); diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java index b6c7c8b7a..104e28cd0 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/processor/ControlLoopProcessor.java @@ -61,8 +61,9 @@ public class ControlLoopProcessor implements Serializable { @Getter private transient ToscaPolicy toscaOpPolicy; + // not serializable, thus must be transient @Getter - private DroolsPolicy domainOpPolicy; + private transient DroolsPolicy domainOpPolicy; /** * Construct an instance from yaml. diff --git a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java index d311b07fc..3e990efc4 100644 --- a/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java +++ b/controlloop/common/eventmanager/src/main/java/org/onap/policy/controlloop/utils/ControlLoopUtils.java @@ -18,14 +18,6 @@ package org.onap.policy.controlloop.utils; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.onap.policy.controlloop.ControlLoopException; import org.onap.policy.controlloop.drl.legacy.ControlLoopParams; import org.onap.policy.controlloop.processor.ControlLoopProcessor; @@ -39,7 +31,6 @@ import org.slf4j.LoggerFactory; public class ControlLoopUtils { public static final Logger logger = LoggerFactory.getLogger(ControlLoopUtils.class); - private static final Pattern NAME_PAT = Pattern.compile("(.*)\\[(\\d{1,3})\\]"); private ControlLoopUtils() { super(); @@ -59,206 +50,4 @@ public class ControlLoopUtils { return null; } } - - // TODO move the following to policy-common/utils - - /** - * Converts a set of properties to a Map. Supports json-path style property names with - * "." separating components, where components may have an optional subscript. - * - * @param properties properties to be converted - * @param prefix properties whose names begin with this prefix are included. The - * prefix is stripped from the name before adding the value to the map - * @return a hierarchical map representing the properties - */ - public static Map toObject(Properties properties, String prefix) { - String dottedPrefix = prefix + (prefix.isEmpty() || prefix.endsWith(".") ? "" : "."); - int pfxlen = dottedPrefix.length(); - - Map map = new LinkedHashMap<>(); - - for (String name : properties.stringPropertyNames()) { - if (name.startsWith(dottedPrefix)) { - String[] components = name.substring(pfxlen).split("[.]"); - setProperty(map, components, properties.getProperty(name)); - } - } - - return map; - } - - /** - * Sets a property within a hierarchical map. - * - * @param map map into which the value should be placed - * @param names property name components - * @param value value to be placed into the map - */ - private static void setProperty(Map map, String[] names, String value) { - Map node = map; - - final int lastComp = names.length - 1; - - // process all but the final component - for (int comp = 0; comp < lastComp; ++comp) { - node = getNode(node, names[comp]); - } - - // process the final component - String name = names[lastComp]; - Matcher matcher = NAME_PAT.matcher(name); - - if (!matcher.matches()) { - // no subscript - node.put(name, value); - return; - } - - // subscripted - List array = getArray(node, matcher.group(1)); - int index = Integer.parseInt(matcher.group(2)); - expand(array, index); - array.set(index, value); - } - - /** - * Gets a node. - * - * @param map map from which to get the object - * @param name name of the element to get from the map, with an optional subscript - * @return a Map - */ - @SuppressWarnings("unchecked") - private static Map getNode(Map map, String name) { - Matcher matcher = NAME_PAT.matcher(name); - - if (!matcher.matches()) { - // no subscript - return getObject(map, name); - } - - // subscripted - List array = getArray(map, matcher.group(1)); - int index = Integer.parseInt(matcher.group(2)); - expand(array, index); - - Object item = array.get(index); - if (item instanceof Map) { - return (Map) item; - - } else { - LinkedHashMap result = new LinkedHashMap<>(); - array.set(index, result); - return result; - } - } - - /** - * Ensures that an array's size is large enough to hold the specified element. - * - * @param array array to be expanded - * @param index index of the desired element - */ - private static void expand(List array, int index) { - while (array.size() <= index) { - array.add(null); - } - } - - /** - * Gets an object (i.e., Map) from a map. If the particular element is not a Map, then - * it is replaced with an empty Map. - * - * @param map map from which to get the object - * @param name name of the element to get from the map, without any subscript - * @return a Map - */ - private static Map getObject(Map map, String name) { - @SuppressWarnings("unchecked") - Map result = (Map) map.compute(name, (key, value) -> { - if (value instanceof Map) { - return value; - } else { - return new LinkedHashMap<>(); - } - }); - - return result; - } - - /** - * Gets an array from a map. If the particular element is not an array, then it is - * replaced with an empty array. - * - * @param map map from which to get the array - * @param name name of the element to get from the map, without any subscript - * @return an array - */ - private static List getArray(Map map, String name) { - @SuppressWarnings("unchecked") - List result = (List) map.compute(name, (key, value) -> { - if (value instanceof List) { - return value; - } else { - return new ArrayList<>(); - } - }); - - return result; - } - - /** - * Compresses lists contained within a generic object, removing all {@code null} - * items. - * - * @param object object to be compressed - * @return the original object, modified in place - */ - public static Object compressLists(Object object) { - if (object instanceof Map) { - @SuppressWarnings("unchecked") - Map asMap = (Map) object; - compressMapValues(asMap); - - } else if (object instanceof List) { - @SuppressWarnings("unchecked") - List asList = (List) object; - compressListItems(asList); - } - - // else: ignore anything else - - return object; - } - - /** - * Walks a hierarchical map and removes {@code null} items found in any Lists. - * - * @param map map whose lists are to be compressed - */ - private static void compressMapValues(Map map) { - for (Object value : map.values()) { - compressLists(value); - } - } - - /** - * Removes {@code null} items from the list. In addition, it walks the items within - * the list, compressing them, as well. - * - * @param list the list to be compressed - */ - private static void compressListItems(List list) { - Iterator iter = list.iterator(); - while (iter.hasNext()) { - Object item = iter.next(); - if (item == null) { - // null item - remove it - iter.remove(); - - } else { - compressLists(item); - } - } - } } -- cgit 1.2.3-korg