summaryrefslogtreecommitdiffstats
path: root/policy-management/src/main
diff options
context:
space:
mode:
authorJorge Hernandez <jh1730@att.com>2018-02-07 15:36:34 -0600
committerJorge Hernandez <jh1730@att.com>2018-02-07 15:50:23 -0600
commitecf719c9cee21ff7aac0f868cb72704ab5ed4497 (patch)
treebd12bc456459d913e3107222f4a75d96b896e4cb /policy-management/src/main
parent2fb42b323b23922a93d2f320d32a8ac10e862792 (diff)
expose immutable list of filters to its users
+ additional related junits Change-Id: I00293cd9aa911dfb3d658cad4ee0441ad3410e9c Issue-ID: POLICY-164 Signed-off-by: Jorge Hernandez <jh1730@att.com>
Diffstat (limited to 'policy-management/src/main')
-rw-r--r--policy-management/src/main/java/org/onap/policy/drools/protocol/coders/EventProtocolCoder.java8
-rw-r--r--policy-management/src/main/java/org/onap/policy/drools/protocol/coders/JsonProtocolFilter.java156
-rw-r--r--policy-management/src/main/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolset.java88
3 files changed, 149 insertions, 103 deletions
diff --git a/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/EventProtocolCoder.java b/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/EventProtocolCoder.java
index 7f1d9b7e..54466775 100644
--- a/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/EventProtocolCoder.java
+++ b/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/EventProtocolCoder.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * policy-management
+ * ONAP
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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.
@@ -86,14 +86,14 @@ public interface EventProtocolCoder {
/**
* @return the filter
*/
- public synchronized JsonProtocolFilter getFilter() {
+ public JsonProtocolFilter getFilter() {
return filter;
}
/**
* @param filter the filter to set
*/
- public synchronized void setFilter(JsonProtocolFilter filter) {
+ public void setFilter(JsonProtocolFilter filter) {
this.filter = filter;
}
diff --git a/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/JsonProtocolFilter.java b/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/JsonProtocolFilter.java
index 12d0ffe5..c5f82a47 100644
--- a/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/JsonProtocolFilter.java
+++ b/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/JsonProtocolFilter.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * policy-management
+ * ONAP
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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,7 @@ package org.onap.policy.drools.protocol.coders;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
import org.onap.policy.drools.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,15 +33,14 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
- * JSON Protocol Filter. Evaluates an JSON string and evaluates if it
- * passes its filters.
+ * JSON Protocol Filter.
*/
public class JsonProtocolFilter {
/**
* Logger
*/
- private static Logger logger = LoggerFactory.getLogger(JsonProtocolFilter.class);
+ private static final Logger logger = LoggerFactory.getLogger(JsonProtocolFilter.class);
/**
* Helper class to collect Filter information
@@ -49,12 +49,12 @@ public class JsonProtocolFilter {
/**
* Field name
*/
- protected String name;
+ private String name;
/**
* Field Value regex
*/
- protected String regex;
+ private String regex;
/**
* Filter Constructor
@@ -63,8 +63,8 @@ public class JsonProtocolFilter {
* @param regex field regex value
*/
public FilterRule(String name, String regex) {
- this.name = name;
- this.regex = regex;
+ this.setName(name);
+ this.setRegex(regex);
}
/**
@@ -97,6 +97,9 @@ public class JsonProtocolFilter {
* @param name field name
*/
public void setName(String name) {
+ if (name == null || name.isEmpty())
+ throw new IllegalArgumentException("filter field name must be provided");
+
this.name = name;
}
@@ -105,6 +108,9 @@ public class JsonProtocolFilter {
* @param regex
*/
public void setRegex(String regex) {
+ if (regex == null || regex.isEmpty())
+ this.regex = ".*";
+
this.regex = regex;
}
@@ -119,7 +125,7 @@ public class JsonProtocolFilter {
/**
* all the filters to be applied
*/
- protected List<FilterRule> rules = new ArrayList<>();
+ protected List<FilterRule> rules = new CopyOnWriteArrayList<>();
/**
*
@@ -154,12 +160,25 @@ public class JsonProtocolFilter {
/**
*
- * @param rawFilters raw filter initialization
+ * @param filters filter list
*
* @throws IllegalArgumentException an invalid input has been provided
*/
public JsonProtocolFilter(List<FilterRule> filters) throws IllegalArgumentException {
- this.rules = filters;
+ List<FilterRule> temp = new ArrayList<>();
+ for (FilterRule rule : filters) {
+ if (rule.getName() == null || rule.getName().isEmpty()) {
+ continue;
+ }
+
+ if (rule.getRegex() == null || rule.getRegex().isEmpty()) {
+ rule.setRegex(".*");
+ }
+
+ temp.add(rule);
+ }
+
+ this.rules.addAll(temp);
}
/**
@@ -179,38 +198,38 @@ public class JsonProtocolFilter {
*
* @throws IllegalArgumentException an invalid input has been provided
*/
- public synchronized boolean accept(JsonElement json) throws IllegalArgumentException {
+ public boolean accept(JsonElement json) throws IllegalArgumentException {
if (json == null) {
throw new IllegalArgumentException("no JSON provided");
}
-
+
+ if (!json.isJsonObject()) {
+ return false;
+ }
+
if (rules.isEmpty()) {
return true;
}
-
+
try {
- if (!json.isJsonObject()) {
- return false;
- }
-
JsonObject event = json.getAsJsonObject();
for (FilterRule filter: rules) {
- if (filter.regex == null ||
- filter.regex.isEmpty() ||
- ".*".equals(filter.regex)) {
+ if (filter.getRegex() == null ||
+ filter.getRegex().isEmpty() ||
+ ".*".equals(filter.getRegex())) {
// Only check for presence
- if (!event.has(filter.name)) {
+ if (!event.has(filter.getName())) {
return false;
}
} else {
- JsonElement field = event.get(filter.name);
+ JsonElement field = event.get(filter.getName());
if (field == null) {
return false;
}
String fieldValue = field.getAsString();
- if (!fieldValue.matches(filter.regex)) {
+ if (!fieldValue.matches(filter.getRegex())) {
return false;
}
}
@@ -229,7 +248,7 @@ public class JsonProtocolFilter {
*
* @throws IllegalArgumentException an invalid input has been provided
*/
- public synchronized boolean accept(String json) throws IllegalArgumentException {
+ public boolean accept(String json) throws IllegalArgumentException {
if (json == null || json.isEmpty()) {
throw new IllegalArgumentException("no JSON provided");
}
@@ -255,47 +274,78 @@ public class JsonProtocolFilter {
}
public List<FilterRule> getRules() {
- return rules;
+ return new ArrayList<>(this.rules);
}
- public synchronized void setRules(List<FilterRule> rulesFilters) {
- this.rules = rulesFilters;
- }
-
- public synchronized void deleteRules(String name) {
- for (FilterRule rule : new ArrayList<>(this.rules)) {
- if (rule.name.equals(name)) {
- this.rules.remove(rule);
- }
- }
- }
-
public List<FilterRule> getRules(String name) {
+ if (name == null || name.isEmpty())
+ throw new IllegalArgumentException("no rule name provided");
+
ArrayList<FilterRule> temp = new ArrayList<>();
- for (FilterRule rule : new ArrayList<>(this.rules)) {
- if (rule.name.equals(name)) {
- temp.add(rule);
- }
+ for (FilterRule rule : this.rules) {
+ if (rule.getName().equals(name)) {
+ temp.add(rule);
+ }
}
return temp;
}
+
+ public void setRules(List<FilterRule> rulesFilters) {
+ if (rulesFilters == null)
+ throw new IllegalArgumentException("no rules provided");
+
+ this.rules.clear();
+ this.rules.addAll(rulesFilters);
+ }
- public synchronized void deleteRule(String name, String regex) {
- for (FilterRule rule : new ArrayList<>(this.rules)) {
- if (rule.name.equals(name) && rule.regex.equals(regex)) {
- this.rules.remove(rule);
+ public void deleteRules(String name) {
+ if (name == null || name.isEmpty())
+ throw new IllegalArgumentException("no rule name provided");
+
+ List<FilterRule> temp = new ArrayList<>();
+ for (FilterRule rule : this.rules) {
+ if (rule.name.equals(name)) {
+ temp.add(rule);
+ }
+ }
+ this.rules.removeAll(temp);
+ }
+
+ public void deleteRule(String name, String regex) {
+ if (name == null || name.isEmpty())
+ throw new IllegalArgumentException("no rule name provided");
+
+ String nonNullRegex = regex;
+ if (regex == null || regex.isEmpty()) {
+ nonNullRegex = ".*";
+ }
+
+ List<FilterRule> temp = new ArrayList<>();
+ for (FilterRule rule : this.rules) {
+ if (rule.name.equals(name) && rule.getRegex().equals(nonNullRegex)) {
+ temp.add(rule);
}
}
+
+ this.rules.removeAll(temp);
}
- public synchronized void addRule(String name, String regex) {
- for (FilterRule rule : new ArrayList<>(this.rules)) {
- if (rule.name.equals(name) && rule.regex.equals(regex)) {
- return;
+ public void addRule(String name, String regex) {
+ if (name == null || name.isEmpty())
+ throw new IllegalArgumentException("no rule name provided");
+
+ String nonNullRegex = regex;
+ if (regex == null || regex.isEmpty()) {
+ nonNullRegex = ".*";
+ }
+
+ for (FilterRule rule : this.rules) {
+ if (rule.getName().equals(name) && rule.getRegex().equals(regex)) {
+ return;
}
}
-
- this.rules.add(new FilterRule(name,regex));
+
+ this.rules.add(new FilterRule(name, nonNullRegex));
}
@Override
diff --git a/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolset.java b/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolset.java
index 07206f95..cb039ee5 100644
--- a/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolset.java
+++ b/policy-management/src/main/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolset.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * policy-management
+ * ONAP
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 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,24 +20,6 @@
package org.onap.policy.drools.protocol.coders;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Type;
-import java.time.Instant;
-import java.time.ZonedDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.onap.policy.drools.controller.DroolsController;
-import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
-import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomCoder;
-import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
-import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomJacksonCoder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -53,6 +35,22 @@ import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.time.Instant;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.onap.policy.drools.controller.DroolsController;
+import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
+import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomCoder;
+import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
+import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomJacksonCoder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Protocol Coding/Decoding Toolset
@@ -87,7 +85,7 @@ public abstract class ProtocolCoderToolset {
/**
* Protocols and associated Filters
*/
- protected final List<CoderFilters> coders = new ArrayList<>();
+ protected final List<CoderFilters> coders = new CopyOnWriteArrayList<>();
/**
* Tree model (instead of class model) generic parsing to be able to inspect elements
@@ -115,7 +113,6 @@ public abstract class ProtocolCoderToolset {
if (topic == null || controllerId == null || groupId == null || artifactId == null
|| codedClass == null || filters == null || topic.isEmpty() || controllerId.isEmpty()) {
- // TODO
throw new IllegalArgumentException("Invalid input");
}
@@ -134,6 +131,9 @@ public abstract class ProtocolCoderToolset {
* @return the decoder filters or null if not found
*/
public CoderFilters getCoder(String classname) {
+ if (classname == null || classname.isEmpty())
+ throw new IllegalArgumentException("no classname provided");
+
for (final CoderFilters decoder : this.coders) {
if (decoder.factClass.equals(classname)) {
return decoder;
@@ -143,12 +143,12 @@ public abstract class ProtocolCoderToolset {
}
/**
- * get all coder filters in use
+ * get a copy of the coder filters in use
*
* @return coder filters
*/
public List<CoderFilters> getCoders() {
- return this.coders;
+ return new ArrayList<>(this.coders);
}
/**
@@ -158,38 +158,35 @@ public abstract class ProtocolCoderToolset {
* @param filter filter
*/
public void addCoder(String eventClass, JsonProtocolFilter filter, int modelClassLoaderHash) {
- synchronized (this) {
- for (final CoderFilters coder : this.coders) {
- if (coder.factClass.equals(eventClass)) {
- // this is a better check than checking pointers, just
- // in case classloader is different and this is just an update
- coder.factClass = eventClass;
- coder.filter = filter;
- coder.modelClassLoaderHash = modelClassLoaderHash;
- return;
- }
+ if (eventClass == null || eventClass.isEmpty())
+ throw new IllegalArgumentException("no event class provided");
+
+ for (final CoderFilters coder : this.coders) {
+ if (coder.getCodedClass().equals(eventClass)) {
+ coder.setFilter(filter);
+ coder.setFromClassLoaderHash(modelClassLoaderHash);
+ return;
}
}
-
this.coders.add(new CoderFilters(eventClass, filter, modelClassLoaderHash));
}
/**
* remove coder
- *
- * @param eventClass decoder
- * @param filter filter
+ * @param eventClass event class
*/
public void removeCoders(String eventClass) {
- synchronized (this) {
- final Iterator<CoderFilters> codersIt = this.coders.iterator();
- while (codersIt.hasNext()) {
- final CoderFilters coder = codersIt.next();
- if (coder.factClass.equals(eventClass)) {
- codersIt.remove();
- }
+ if (eventClass == null || eventClass.isEmpty())
+ throw new IllegalArgumentException("no event class provided");
+
+ List<CoderFilters> temp = new ArrayList<>();
+ for (final CoderFilters coder : this.coders) {
+ if (coder.factClass.equals(eventClass)) {
+ temp.add(coder);
}
}
+
+ this.coders.removeAll(temp);
}
/**
@@ -258,7 +255,6 @@ public abstract class ProtocolCoderToolset {
// Don't parse if it is not necessary
if (this.coders.isEmpty()) {
- // TODO this is an error
throw new IllegalStateException("No coders available");
}