blob: 6c2997fef32d69f43a4da9a5eb4d5d8edb5e4d35 [file] [log] [blame]
alshabibeec3a062014-09-17 18:01:26 -07001package org.onlab.onos.provider.of.flow.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.concurrent.TimeUnit;
6
7import org.jboss.netty.util.HashedWheelTimer;
8import org.jboss.netty.util.Timeout;
9import org.jboss.netty.util.TimerTask;
10import org.onlab.onos.openflow.controller.OpenFlowSwitch;
11import org.onlab.util.Timer;
12import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
13import org.projectfloodlight.openflow.types.OFPort;
14import org.projectfloodlight.openflow.types.TableId;
15import org.slf4j.Logger;
16
17public class FlowStatsCollector implements TimerTask {
18
19 private final Logger log = getLogger(getClass());
20
21 private final HashedWheelTimer timer = Timer.getTimer();
22 private final OpenFlowSwitch sw;
23 private final int refreshInterval;
24
25 private Timeout timeout;
26
27 private boolean stopTimer = false;;
28
29 public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) {
30 this.sw = sw;
31 this.refreshInterval = refreshInterval;
32 }
33
34 @Override
35 public void run(Timeout timeout) throws Exception {
36 log.debug("Collecting stats for {}", this.sw.getStringId());
37
38 sendFlowStatistics();
39
40 if (!this.stopTimer) {
41 log.debug("Scheduling stats collection in {} seconds for {}",
42 this.refreshInterval, this.sw.getStringId());
43 timeout.getTimer().newTimeout(this, refreshInterval,
44 TimeUnit.SECONDS);
45 }
46
47
48 }
49
50 private void sendFlowStatistics() {
51 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
52 .setMatch(sw.factory().matchWildcardAll())
53 .setTableId(TableId.ALL)
54 .setOutPort(OFPort.NO_MASK)
55 .build();
56
57 this.sw.sendMsg(request);
58
59 }
60
61 public void start() {
62
63 /*
64 * Initially start polling quickly. Then drop down to configured value
65 */
66 log.info("Starting Stats collection thread for {}",
67 this.sw.getStringId());
68 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
69 }
70
71 public void stop() {
72 log.info("Stopping Stats collection thread for {}",
73 this.sw.getStringId());
74 this.stopTimer = true;
75 timeout.cancel();
76 }
77
78}