Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify FilterListener to allow multiple values for an attribute (to form a whitelist) #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/main/java/org/lwes/listener/FilterListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
package org.lwes.listener;
/**
* This is a filtering multicast listener. It can filter by event name or any attribute of an event.
* Specifying an attribute name more than once with multiple values creates a whitelist.
*
* Show only Test::Event events
* java org.lwes.listener.FilterListener -m 224.0.0.1 -p 6001 -e "Test::Event"
*
* Show only events with eid=1234 AND aid=someapp
* java org.lwes.listener.FilterListener -m 224.0.0.1 -p 6001 -a "eid=1234,aid=someapp"
*
* Show only events with aid=someapp OR aid=anotherapp
* java org.lwes.listener.FilterListener -m 224.0.0.1 -p 6001 -a "eid=1234,aid=someapp,aid=anotherapp"
*
* Events are printed to stdout one per line.
*
* User: frank.maritato
Expand All @@ -33,11 +37,7 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;

public class FilterListener implements EventHandler {

Expand All @@ -59,7 +59,7 @@ public class FilterListener implements EventHandler {
private String attrList = null;

private List<String> eventNames;
private Map<String, String> eventAttrs;
private Map<String, Set<String>> eventAttrs;

public void processArguments(String[] args) {
CmdLineParser parser = new CmdLineParser(this);
Expand All @@ -72,12 +72,19 @@ public void processArguments(String[] args) {
throw new RuntimeException(e.getMessage(), e);
}
if (attrList != null && !attrList.isEmpty()) {
eventAttrs = new HashMap<String, String>();
eventAttrs = new HashMap<String, Set<String>>();
String[] kvList = attrList.split(",");
for (String kv : kvList) {
String[] pairs = kv.trim().split("=");
if (pairs.length == 2) {
eventAttrs.put(pairs[0].trim(), pairs[1].trim());
if (eventAttrs.containsKey(pairs[0].trim())) {
eventAttrs.get(pairs[0].trim()).add(pairs[1].trim());
}
else {
HashSet<String> hashSet = new HashSet<String>();
hashSet.add(pairs[1].trim());
eventAttrs.put(pairs[0].trim(), hashSet);
}
}
else {
log.warn(kv + " not a valid k=v pair");
Expand Down Expand Up @@ -122,12 +129,25 @@ public static void main(String[] args) throws UnknownHostException,
new FilterListener().run(args);
}

public Map<String, String> getEventAttrs() {
public Map<String, Set<String>> getEventAttrs() {
return eventAttrs;
}

public void setEventAttrs(Map<String, String> eventAttrs) {
this.eventAttrs = eventAttrs;
/**
* You can still use setEventAttrs the old way by passing an int as the second parameter,
* that way you would not have to rewrite your code if you were using this method
*/
public void setEventAttrs(Map<String, String> eventAttrs, int ignored) {
this.eventAttrs = new HashMap<String, Set<String>>();
for (String key : eventAttrs.keySet()) {
Set<String> valSet = new HashSet<String>(1);
valSet.add(eventAttrs.get(key));
this.eventAttrs.put(key, valSet);
}
}

public void setEventAttrs(Map<String, Set<String>> eventAttrSets) {
eventAttrs = eventAttrSets;
}

public List<String> getEventNames() {
Expand All @@ -154,10 +174,10 @@ public Event match(Event event) {
if (eventAttrs != null) {
for (String key : eventAttrs.keySet()) {
Object o = event.get(key);
String val = eventAttrs.get(key);
Set<String> vals = eventAttrs.get(key);
// If more than one kv pair is submitted, events must meet
// all conditions. If any of them fail, return.
if (!(o != null && val.equals(o.toString()))) {
if (!(o != null && vals.contains(o.toString()))) {
return null;
}
}
Expand Down
30 changes: 17 additions & 13 deletions src/test/java/org/lwes/listener/FilterListenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -65,15 +63,17 @@ public void testManuallySet() {
"-m", "224.0.0.0",
"-p", "0000"
});
Map<String, String> inattrs = new HashMap<String, String>();
inattrs.put("eid", "1");
Map<String, Set<String>> inattrs = new HashMap<String, Set<String>>();
Set<String> s = new TreeSet<String>();
s.add("1");
inattrs.put("eid", s);
filterListener.setEventAttrs(inattrs);

// Verify the argument was parsed properly
Map<String,String> attrs = filterListener.getEventAttrs();
Map<String,Set<String>> attrs = filterListener.getEventAttrs();
Assert.assertNotNull(attrs);
Assert.assertEquals(1, attrs.size());
Assert.assertEquals("1", attrs.get("eid"));
Assert.assertTrue(attrs.get("eid").contains("1"));

filterListener.destroy();
Assert.assertNull(filterListener.getEventAttrs());
Expand Down Expand Up @@ -146,10 +146,11 @@ public void testSingleAttribute() {
});

// Verify the argument was parsed properly
Map<String,String> attrs = filterListener.getEventAttrs();
Map<String,Set<String>> attrs = filterListener.getEventAttrs();
Assert.assertNotNull(attrs);
Assert.assertEquals(1, attrs.size());
Assert.assertEquals("1", attrs.get("eid"));
Assert.assertEquals(1, attrs.get("eid").size());
Assert.assertTrue(attrs.get("eid").contains("1"));

// Verify matching functions properly
MapEvent evt = new MapEvent("Test::One");
Expand All @@ -170,15 +171,18 @@ public void testMultipleAttributes() {
new String[]{
"-m", "224.0.0.0",
"-p", "0000",
"-a", "eid=1,pid=5"
"-a", "eid=1,pid=5,eid=z"
});

// Verify the argument was parsed properly
Map<String,String> attrs = filterListener.getEventAttrs();
Map<String,Set<String>> attrs = filterListener.getEventAttrs();
Assert.assertNotNull(attrs);
Assert.assertEquals(2, attrs.size());
Assert.assertEquals("1", attrs.get("eid"));
Assert.assertEquals("5", attrs.get("pid"));
Assert.assertEquals(2, attrs.get("eid").size());
Assert.assertTrue(attrs.get("eid").contains("1"));
Assert.assertTrue(attrs.get("eid").contains("z"));
Assert.assertEquals(1, attrs.get("pid").size());
Assert.assertTrue(attrs.get("pid").contains("5"));

// Verify multiple arguments get matched properly
MapEvent evt = new MapEvent("Test::One");
Expand Down