Thomas Vachuska
Committed by Gerrit Code Review

OpenFlowRuleProvider is now configurable with respect to flowPollFrequency.

Change-Id: I3a559a9cd65df1ae56d80017696452788fc08d91
...@@ -23,8 +23,6 @@ import org.onosproject.net.provider.Provider; ...@@ -23,8 +23,6 @@ import org.onosproject.net.provider.Provider;
23 */ 23 */
24 public interface FlowRuleProvider extends Provider { 24 public interface FlowRuleProvider extends Provider {
25 25
26 - static final int POLL_INTERVAL = 10;
27 -
28 /** 26 /**
29 * Instructs the provider to apply the specified flow rules to their 27 * Instructs the provider to apply the specified flow rules to their
30 * respective devices. 28 * respective devices.
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 package org.onosproject.net.statistic; 16 package org.onosproject.net.statistic;
17 17
18 import com.google.common.base.MoreObjects; 18 import com.google.common.base.MoreObjects;
19 -import org.onosproject.net.flow.FlowRuleProvider;
20 19
21 /** 20 /**
22 * Implementation of a load. 21 * Implementation of a load.
...@@ -29,6 +28,11 @@ public class DefaultLoad implements Load { ...@@ -29,6 +28,11 @@ public class DefaultLoad implements Load {
29 private final long time; 28 private final long time;
30 29
31 /** 30 /**
31 + * Indicates the flow statistics poll interval in seconds.
32 + */
33 + private static int pollInterval = 10;
34 +
35 + /**
32 * Creates an invalid load. 36 * Creates an invalid load.
33 */ 37 */
34 public DefaultLoad() { 38 public DefaultLoad() {
...@@ -50,9 +54,19 @@ public class DefaultLoad implements Load { ...@@ -50,9 +54,19 @@ public class DefaultLoad implements Load {
50 this.isValid = true; 54 this.isValid = true;
51 } 55 }
52 56
57 + /**
58 + * Sets the poll interval in seconds. Used solely for the purpose of
59 + * computing the load.
60 + *
61 + * @param newPollInterval poll interval duration in seconds
62 + */
63 + public static void setPollInterval(int newPollInterval) {
64 + pollInterval = newPollInterval;
65 + }
66 +
53 @Override 67 @Override
54 public long rate() { 68 public long rate() {
55 - return (current - previous) / FlowRuleProvider.POLL_INTERVAL; 69 + return (current - previous) / pollInterval;
56 } 70 }
57 71
58 @Override 72 @Override
......
...@@ -31,4 +31,10 @@ ...@@ -31,4 +31,10 @@
31 31
32 <description>ONOS OpenFlow protocol flow provider</description> 32 <description>ONOS OpenFlow protocol flow provider</description>
33 33
34 + <dependencies>
35 + <dependency>
36 + <groupId>org.osgi</groupId>
37 + <artifactId>org.osgi.compendium</artifactId>
38 + </dependency>
39 + </dependencies>
34 </project> 40 </project>
......
...@@ -15,87 +15,86 @@ ...@@ -15,87 +15,86 @@
15 */ 15 */
16 package org.onosproject.provider.of.flow.impl; 16 package org.onosproject.provider.of.flow.impl;
17 17
18 -import static org.slf4j.LoggerFactory.getLogger; 18 +import org.onlab.util.SharedExecutors;
19 -
20 -import java.util.concurrent.TimeUnit;
21 -
22 -import org.jboss.netty.util.HashedWheelTimer;
23 -import org.jboss.netty.util.Timeout;
24 -import org.jboss.netty.util.TimerTask;
25 import org.onosproject.openflow.controller.OpenFlowSwitch; 19 import org.onosproject.openflow.controller.OpenFlowSwitch;
26 import org.onosproject.openflow.controller.RoleState; 20 import org.onosproject.openflow.controller.RoleState;
27 -import org.onlab.util.Timer;
28 import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; 21 import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
29 import org.projectfloodlight.openflow.types.OFPort; 22 import org.projectfloodlight.openflow.types.OFPort;
30 import org.projectfloodlight.openflow.types.TableId; 23 import org.projectfloodlight.openflow.types.TableId;
31 import org.slf4j.Logger; 24 import org.slf4j.Logger;
32 25
33 -public class FlowStatsCollector implements TimerTask { 26 +import java.util.Timer;
27 +import java.util.TimerTask;
34 28
35 - private final Logger log = getLogger(getClass()); 29 +import static org.slf4j.LoggerFactory.getLogger;
36 -
37 - private final HashedWheelTimer timer = Timer.getTimer();
38 - private final OpenFlowSwitch sw;
39 - private final int refreshInterval;
40 30
41 - private Timeout timeout; 31 +/**
32 + * Collects flow statistics for the specified switch.
33 + */
34 +class FlowStatsCollector {
42 35
43 - private boolean stopTimer = false;; 36 + private final Logger log = getLogger(getClass());
44 37
45 - public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) { 38 + public static final int SECONDS = 1000;
46 - this.sw = sw;
47 - this.refreshInterval = refreshInterval;
48 - }
49 39
50 - @Override 40 + private final OpenFlowSwitch sw;
51 - public void run(Timeout timeout) throws Exception { 41 + private Timer timer;
52 - log.trace("Collecting stats for {}", this.sw.getStringId()); 42 + private TimerTask task;
53 43
54 - sendFlowStatistics(); 44 + private int pollInterval;
55 45
56 - if (!this.stopTimer) { 46 + /**
57 - log.trace("Scheduling stats collection in {} seconds for {}", 47 + * Creates a new collector for the given switch and poll frequency.
58 - this.refreshInterval, this.sw.getStringId()); 48 + *
59 - timeout.getTimer().newTimeout(this, refreshInterval, 49 + * @param timer timer to use for scheduling
60 - TimeUnit.SECONDS); 50 + * @param sw switch to pull
51 + * @param pollInterval poll frequency in seconds
52 + */
53 + FlowStatsCollector(Timer timer, OpenFlowSwitch sw, int pollInterval) {
54 + this.timer = timer;
55 + this.sw = sw;
56 + this.pollInterval = pollInterval;
61 } 57 }
62 58
63 - 59 + /**
60 + * Adjusts poll frequency.
61 + *
62 + * @param pollInterval poll frequency in seconds
63 + */
64 + synchronized void adjustPollInterval(int pollInterval) {
65 + this.pollInterval = pollInterval;
66 + task.cancel();
67 + task = new InternalTimerTask();
68 + timer.scheduleAtFixedRate(task, pollInterval * SECONDS, pollInterval * 1000);
64 } 69 }
65 70
66 - private void sendFlowStatistics() { 71 + private class InternalTimerTask extends TimerTask {
67 - if (log.isTraceEnabled()) { 72 + @Override
68 - log.trace("sendFlowStatistics {}:{}", sw.getStringId(), sw.getRole()); 73 + public void run() {
69 - } 74 + if (sw.getRole() == RoleState.MASTER) {
70 - if (sw.getRole() != RoleState.MASTER) { 75 + log.trace("Collecting stats for {}", sw.getStringId());
71 - // Switch not master.
72 - return;
73 - }
74 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest() 76 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
75 .setMatch(sw.factory().matchWildcardAll()) 77 .setMatch(sw.factory().matchWildcardAll())
76 .setTableId(TableId.ALL) 78 .setTableId(TableId.ALL)
77 .setOutPort(OFPort.NO_MASK) 79 .setOutPort(OFPort.NO_MASK)
78 .build(); 80 .build();
79 - 81 + sw.sendMsg(request);
80 - this.sw.sendMsg(request); 82 + }
81 - 83 + }
82 } 84 }
83 85
84 - public void start() { 86 + public synchronized void start() {
85 - 87 + // Initially start polling quickly. Then drop down to configured value
86 - /* 88 + log.debug("Starting Stats collection thread for {}", sw.getStringId());
87 - * Initially start polling quickly. Then drop down to configured value 89 + task = new InternalTimerTask();
88 - */ 90 + SharedExecutors.getTimer().scheduleAtFixedRate(task, 1 * SECONDS,
89 - log.info("Starting Stats collection thread for {}", 91 + pollInterval * SECONDS);
90 - this.sw.getStringId());
91 - timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
92 } 92 }
93 93
94 - public void stop() { 94 + public synchronized void stop() {
95 - log.info("Stopping Stats collection thread for {}", 95 + log.debug("Stopping Stats collection thread for {}", sw.getStringId());
96 - this.sw.getStringId()); 96 + task.cancel();
97 - this.stopTimer = true; 97 + task = null;
98 - timeout.cancel();
99 } 98 }
100 99
101 } 100 }
......
...@@ -15,21 +15,20 @@ ...@@ -15,21 +15,20 @@
15 */ 15 */
16 package org.onosproject.provider.of.flow.impl; 16 package org.onosproject.provider.of.flow.impl;
17 17
18 -import static org.slf4j.LoggerFactory.getLogger; 18 +import com.google.common.cache.Cache;
19 - 19 +import com.google.common.cache.CacheBuilder;
20 -import java.util.Collections; 20 +import com.google.common.cache.RemovalCause;
21 -import java.util.List; 21 +import com.google.common.cache.RemovalNotification;
22 -import java.util.Map; 22 +import com.google.common.collect.Maps;
23 -import java.util.Optional; 23 +import com.google.common.collect.Sets;
24 -import java.util.Set;
25 -import java.util.concurrent.TimeUnit;
26 -import java.util.stream.Collectors;
27 -
28 import org.apache.felix.scr.annotations.Activate; 24 import org.apache.felix.scr.annotations.Activate;
29 import org.apache.felix.scr.annotations.Component; 25 import org.apache.felix.scr.annotations.Component;
30 import org.apache.felix.scr.annotations.Deactivate; 26 import org.apache.felix.scr.annotations.Deactivate;
27 +import org.apache.felix.scr.annotations.Modified;
28 +import org.apache.felix.scr.annotations.Property;
31 import org.apache.felix.scr.annotations.Reference; 29 import org.apache.felix.scr.annotations.Reference;
32 import org.apache.felix.scr.annotations.ReferenceCardinality; 30 import org.apache.felix.scr.annotations.ReferenceCardinality;
31 +import org.onosproject.cfg.ComponentConfigService;
33 import org.onosproject.core.ApplicationId; 32 import org.onosproject.core.ApplicationId;
34 import org.onosproject.net.DeviceId; 33 import org.onosproject.net.DeviceId;
35 import org.onosproject.net.flow.CompletedBatchOperation; 34 import org.onosproject.net.flow.CompletedBatchOperation;
...@@ -43,6 +42,7 @@ import org.onosproject.net.flow.FlowRuleProviderRegistry; ...@@ -43,6 +42,7 @@ import org.onosproject.net.flow.FlowRuleProviderRegistry;
43 import org.onosproject.net.flow.FlowRuleProviderService; 42 import org.onosproject.net.flow.FlowRuleProviderService;
44 import org.onosproject.net.provider.AbstractProvider; 43 import org.onosproject.net.provider.AbstractProvider;
45 import org.onosproject.net.provider.ProviderId; 44 import org.onosproject.net.provider.ProviderId;
45 +import org.onosproject.net.statistic.DefaultLoad;
46 import org.onosproject.openflow.controller.Dpid; 46 import org.onosproject.openflow.controller.Dpid;
47 import org.onosproject.openflow.controller.OpenFlowController; 47 import org.onosproject.openflow.controller.OpenFlowController;
48 import org.onosproject.openflow.controller.OpenFlowEventListener; 48 import org.onosproject.openflow.controller.OpenFlowEventListener;
...@@ -50,6 +50,7 @@ import org.onosproject.openflow.controller.OpenFlowSwitch; ...@@ -50,6 +50,7 @@ import org.onosproject.openflow.controller.OpenFlowSwitch;
50 import org.onosproject.openflow.controller.OpenFlowSwitchListener; 50 import org.onosproject.openflow.controller.OpenFlowSwitchListener;
51 import org.onosproject.openflow.controller.RoleState; 51 import org.onosproject.openflow.controller.RoleState;
52 import org.onosproject.openflow.controller.ThirdPartyMessage; 52 import org.onosproject.openflow.controller.ThirdPartyMessage;
53 +import org.osgi.service.component.ComponentContext;
53 import org.projectfloodlight.openflow.protocol.OFBarrierRequest; 54 import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
54 import org.projectfloodlight.openflow.protocol.OFErrorMsg; 55 import org.projectfloodlight.openflow.protocol.OFErrorMsg;
55 import org.projectfloodlight.openflow.protocol.OFErrorType; 56 import org.projectfloodlight.openflow.protocol.OFErrorType;
...@@ -63,12 +64,19 @@ import org.projectfloodlight.openflow.protocol.OFStatsType; ...@@ -63,12 +64,19 @@ import org.projectfloodlight.openflow.protocol.OFStatsType;
63 import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg; 64 import org.projectfloodlight.openflow.protocol.errormsg.OFFlowModFailedErrorMsg;
64 import org.slf4j.Logger; 65 import org.slf4j.Logger;
65 66
66 -import com.google.common.cache.Cache; 67 +import java.util.Collections;
67 -import com.google.common.cache.CacheBuilder; 68 +import java.util.Dictionary;
68 -import com.google.common.cache.RemovalCause; 69 +import java.util.List;
69 -import com.google.common.cache.RemovalNotification; 70 +import java.util.Map;
70 -import com.google.common.collect.Maps; 71 +import java.util.Optional;
71 -import com.google.common.collect.Sets; 72 +import java.util.Set;
73 +import java.util.Timer;
74 +import java.util.concurrent.TimeUnit;
75 +import java.util.stream.Collectors;
76 +
77 +import static com.google.common.base.Strings.isNullOrEmpty;
78 +import static org.onlab.util.Tools.get;
79 +import static org.slf4j.LoggerFactory.getLogger;
72 80
73 /** 81 /**
74 * Provider which uses an OpenFlow controller to detect network end-station 82 * Provider which uses an OpenFlow controller to detect network end-station
...@@ -86,12 +94,21 @@ public class OpenFlowRuleProvider extends AbstractProvider ...@@ -86,12 +94,21 @@ public class OpenFlowRuleProvider extends AbstractProvider
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected OpenFlowController controller; 95 protected OpenFlowController controller;
88 96
97 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 + protected ComponentConfigService cfgService;
99 +
100 + private static final int DEFAULT_POLL_FREQUENCY = 10;
101 + @Property(name = "flowPollFrequency", intValue = DEFAULT_POLL_FREQUENCY,
102 + label = "Frequency (in seconds) for polling flow statistics")
103 + private int flowPollFrequency = DEFAULT_POLL_FREQUENCY;
104 +
89 private FlowRuleProviderService providerService; 105 private FlowRuleProviderService providerService;
90 106
91 private final InternalFlowProvider listener = new InternalFlowProvider(); 107 private final InternalFlowProvider listener = new InternalFlowProvider();
92 108
93 private Cache<Long, InternalCacheEntry> pendingBatches; 109 private Cache<Long, InternalCacheEntry> pendingBatches;
94 110
111 + private final Timer timer = new Timer("onos-openflow-collector");
95 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); 112 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
96 113
97 /** 114 /**
...@@ -102,40 +119,77 @@ public class OpenFlowRuleProvider extends AbstractProvider ...@@ -102,40 +119,77 @@ public class OpenFlowRuleProvider extends AbstractProvider
102 } 119 }
103 120
104 @Activate 121 @Activate
105 - public void activate() { 122 + public void activate(ComponentContext context) {
123 + cfgService.registerProperties(getClass());
106 providerService = providerRegistry.register(this); 124 providerService = providerRegistry.register(this);
107 controller.addListener(listener); 125 controller.addListener(listener);
108 controller.addEventListener(listener); 126 controller.addEventListener(listener);
109 127
110 - pendingBatches = CacheBuilder 128 + pendingBatches = createBatchCache();
111 - .newBuilder() 129 + createCollectors();
130 +
131 + log.info("Started");
132 + }
133 +
134 + @Deactivate
135 + public void deactivate(ComponentContext context) {
136 + cfgService.unregisterProperties(getClass(), false);
137 + stopCollectors();
138 + providerRegistry.unregister(this);
139 + providerService = null;
140 +
141 + log.info("Stopped");
142 + }
143 +
144 + @Modified
145 + public void modified(ComponentContext context) {
146 + Dictionary<?, ?> properties = context.getProperties();
147 + int newFlowPollFrequency;
148 + try {
149 + String s = get(properties, "flowPollFrequency");
150 + newFlowPollFrequency = isNullOrEmpty(s) ? flowPollFrequency : Integer.parseInt(s.trim());
151 +
152 + } catch (NumberFormatException | ClassCastException e) {
153 + newFlowPollFrequency = flowPollFrequency;
154 + }
155 +
156 + if (newFlowPollFrequency != flowPollFrequency) {
157 + flowPollFrequency = newFlowPollFrequency;
158 + adjustRate();
159 + }
160 +
161 + log.info("Settings: flowPollFrequency={}", flowPollFrequency);
162 + }
163 +
164 + private Cache<Long, InternalCacheEntry> createBatchCache() {
165 + return CacheBuilder.newBuilder()
112 .expireAfterWrite(10, TimeUnit.SECONDS) 166 .expireAfterWrite(10, TimeUnit.SECONDS)
113 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> { 167 .removalListener((RemovalNotification<Long, InternalCacheEntry> notification) -> {
114 if (notification.getCause() == RemovalCause.EXPIRED) { 168 if (notification.getCause() == RemovalCause.EXPIRED) {
115 - providerService 169 + providerService.batchOperationCompleted(notification.getKey(),
116 - .batchOperationCompleted(notification 170 + notification.getValue().failedCompletion());
117 - .getKey(),
118 - notification
119 - .getValue()
120 - .failedCompletion());
121 } 171 }
122 }).build(); 172 }).build();
173 + }
123 174
124 - for (OpenFlowSwitch sw : controller.getSwitches()) { 175 + private void createCollectors() {
125 - FlowStatsCollector fsc = new FlowStatsCollector(sw, POLL_INTERVAL); 176 + controller.getSwitches().forEach(this::createCollector);
177 + }
178 +
179 + private void createCollector(OpenFlowSwitch sw) {
180 + FlowStatsCollector fsc = new FlowStatsCollector(timer, sw, flowPollFrequency);
126 fsc.start(); 181 fsc.start();
127 collectors.put(new Dpid(sw.getId()), fsc); 182 collectors.put(new Dpid(sw.getId()), fsc);
128 } 183 }
129 184
130 - log.info("Started"); 185 + private void stopCollectors() {
186 + collectors.values().forEach(FlowStatsCollector::stop);
187 + collectors.clear();
131 } 188 }
132 189
133 - @Deactivate 190 + private void adjustRate() {
134 - public void deactivate() { 191 + DefaultLoad.setPollInterval(flowPollFrequency);
135 - providerRegistry.unregister(this); 192 + collectors.values().forEach(fsc -> fsc.adjustPollInterval(flowPollFrequency));
136 - providerService = null;
137 -
138 - log.info("Stopped");
139 } 193 }
140 194
141 @Override 195 @Override
...@@ -236,12 +290,7 @@ public class OpenFlowRuleProvider extends AbstractProvider ...@@ -236,12 +290,7 @@ public class OpenFlowRuleProvider extends AbstractProvider
236 290
237 @Override 291 @Override
238 public void switchAdded(Dpid dpid) { 292 public void switchAdded(Dpid dpid) {
239 - FlowStatsCollector fsc = new FlowStatsCollector( 293 + createCollector(controller.getSwitch(dpid));
240 - controller
241 - .getSwitch(dpid),
242 - POLL_INTERVAL);
243 - fsc.start();
244 - collectors.put(dpid, fsc);
245 } 294 }
246 295
247 @Override 296 @Override
...@@ -343,15 +392,13 @@ public class OpenFlowRuleProvider extends AbstractProvider ...@@ -343,15 +392,13 @@ public class OpenFlowRuleProvider extends AbstractProvider
343 .collect(Collectors.toList()); 392 .collect(Collectors.toList());
344 393
345 providerService.pushFlowMetrics(did, flowEntries); 394 providerService.pushFlowMetrics(did, flowEntries);
346 -
347 } 395 }
348 -
349 } 396 }
350 397
351 /** 398 /**
352 * The internal cache entry holding the original request as well as 399 * The internal cache entry holding the original request as well as
353 * accumulating the any failures along the way. 400 * accumulating the any failures along the way.
354 - * 401 + * <p/>
355 * If this entry is evicted from the cache then the entire operation is 402 * If this entry is evicted from the cache then the entire operation is
356 * considered failed. Otherwise, only the failures reported by the device 403 * considered failed. Otherwise, only the failures reported by the device
357 * will be propagated up. 404 * will be propagated up.
...@@ -400,7 +447,6 @@ public class OpenFlowRuleProvider extends AbstractProvider ...@@ -400,7 +447,6 @@ public class OpenFlowRuleProvider extends AbstractProvider
400 .unmodifiableSet(failures), 447 .unmodifiableSet(failures),
401 operation.deviceId()); 448 operation.deviceId());
402 } 449 }
403 -
404 } 450 }
405 451
406 } 452 }
......