Aaron Kruglikov

Added accumulation of events using the accumulator construct.

Change-Id: I020fc7a46427636f4ec934f1895402625af04788
...@@ -23,6 +23,9 @@ import org.apache.felix.scr.annotations.Deactivate; ...@@ -23,6 +23,9 @@ import org.apache.felix.scr.annotations.Deactivate;
23 import org.apache.felix.scr.annotations.Reference; 23 import org.apache.felix.scr.annotations.Reference;
24 import org.apache.felix.scr.annotations.ReferenceCardinality; 24 import org.apache.felix.scr.annotations.ReferenceCardinality;
25 import org.apache.felix.scr.annotations.Service; 25 import org.apache.felix.scr.annotations.Service;
26 +import org.onlab.util.AbstractAccumulator;
27 +import org.onlab.util.Accumulator;
28 +import org.onlab.util.SharedExecutors;
26 import org.onosproject.cfg.ComponentConfigEvent; 29 import org.onosproject.cfg.ComponentConfigEvent;
27 import org.onosproject.cfg.ComponentConfigService; 30 import org.onosproject.cfg.ComponentConfigService;
28 import org.onosproject.cfg.ComponentConfigStore; 31 import org.onosproject.cfg.ComponentConfigStore;
...@@ -37,7 +40,9 @@ import java.io.IOException; ...@@ -37,7 +40,9 @@ import java.io.IOException;
37 import java.io.InputStream; 40 import java.io.InputStream;
38 import java.util.Dictionary; 41 import java.util.Dictionary;
39 import java.util.Enumeration; 42 import java.util.Enumeration;
43 +import java.util.HashSet;
40 import java.util.Hashtable; 44 import java.util.Hashtable;
45 +import java.util.List;
41 import java.util.Map; 46 import java.util.Map;
42 import java.util.Set; 47 import java.util.Set;
43 48
...@@ -57,11 +62,19 @@ public class ComponentConfigManager implements ComponentConfigService { ...@@ -57,11 +62,19 @@ public class ComponentConfigManager implements ComponentConfigService {
57 private static final String COMPONENT_NULL = "Component name cannot be null"; 62 private static final String COMPONENT_NULL = "Component name cannot be null";
58 private static final String PROPERTY_NULL = "Property name cannot be null"; 63 private static final String PROPERTY_NULL = "Property name cannot be null";
59 64
65 + //Symbolic constants for use with the accumulator
66 + private static final int MAX_ITEMS = 100;
67 + private static final int MAX_BATCH_MILLIS = 1000;
68 + private static final int MAX_IDLE_MILLIS = 250;
69 +
60 private static final String RESOURCE_EXT = ".cfgdef"; 70 private static final String RESOURCE_EXT = ".cfgdef";
61 71
62 private final Logger log = getLogger(getClass()); 72 private final Logger log = getLogger(getClass());
63 73
64 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate(); 74 private final ComponentConfigStoreDelegate delegate = new InternalStoreDelegate();
75 + //TODO make accumulator properties configurable
76 + private final InternalAccumulator accumulator = new InternalAccumulator(SharedExecutors.getTimer(),
77 + MAX_ITEMS, MAX_BATCH_MILLIS, MAX_IDLE_MILLIS);
65 78
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ComponentConfigStore store; 80 protected ComponentConfigStore store;
...@@ -70,9 +83,10 @@ public class ComponentConfigManager implements ComponentConfigService { ...@@ -70,9 +83,10 @@ public class ComponentConfigManager implements ComponentConfigService {
70 protected ConfigurationAdmin cfgAdmin; 83 protected ConfigurationAdmin cfgAdmin;
71 84
72 // Locally maintained catalog of definitions. 85 // Locally maintained catalog of definitions.
73 - private final Map<String, Map<String, ConfigProperty>> properties = 86 + private final Map<String, Map<String, ConfigProperty>> properties =
74 Maps.newConcurrentMap(); 87 Maps.newConcurrentMap();
75 88
89 +
76 @Activate 90 @Activate
77 public void activate() { 91 public void activate() {
78 store.setDelegate(delegate); 92 store.setDelegate(delegate);
...@@ -181,6 +195,22 @@ public class ComponentConfigManager implements ComponentConfigService { ...@@ -181,6 +195,22 @@ public class ComponentConfigManager implements ComponentConfigService {
181 } 195 }
182 } 196 }
183 197
198 + private class InternalAccumulator extends AbstractAccumulator<String> implements Accumulator<String> {
199 +
200 + protected InternalAccumulator(java.util.Timer timer, int maxItems, int maxBatchMillis, int maxIdleMillis) {
201 + super(timer, maxItems, maxBatchMillis, maxIdleMillis);
202 + }
203 +
204 + @Override
205 + public void processItems(List items) {
206 + //Conversion to hashset removes duplicates
207 + Set<String> componentSet = new HashSet<String>(items);
208 + componentSet.forEach(ComponentConfigManager.this::triggerUpdate);
209 +
210 +
211 + }
212 + }
213 +
184 // Locates the property in the component map and replaces it with an 214 // Locates the property in the component map and replaces it with an
185 // updated copy. 215 // updated copy.
186 private void set(String componentName, String name, String value) { 216 private void set(String componentName, String name, String value) {
...@@ -189,7 +219,7 @@ public class ComponentConfigManager implements ComponentConfigService { ...@@ -189,7 +219,7 @@ public class ComponentConfigManager implements ComponentConfigService {
189 ConfigProperty prop = map.get(name); 219 ConfigProperty prop = map.get(name);
190 if (prop != null) { 220 if (prop != null) {
191 map.put(name, ConfigProperty.setProperty(prop, value)); 221 map.put(name, ConfigProperty.setProperty(prop, value));
192 - triggerUpdate(componentName); 222 + accumulator.add(componentName);
193 return; 223 return;
194 } 224 }
195 } 225 }
...@@ -205,7 +235,7 @@ public class ComponentConfigManager implements ComponentConfigService { ...@@ -205,7 +235,7 @@ public class ComponentConfigManager implements ComponentConfigService {
205 ConfigProperty prop = map.get(name); 235 ConfigProperty prop = map.get(name);
206 if (prop != null) { 236 if (prop != null) {
207 map.put(name, ConfigProperty.resetProperty(prop)); 237 map.put(name, ConfigProperty.resetProperty(prop));
208 - triggerUpdate(componentName); 238 + accumulator.add(componentName);
209 return; 239 return;
210 } 240 }
211 log.warn("Unable to reset non-existent property {} for component {}", 241 log.warn("Unable to reset non-existent property {} for component {}",
......