blob: bd33884798047f2da164cf133be63549dc22cda5 [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;
Yuta HIGUCHI1b6f5682014-10-25 21:33:59 -070011import org.onlab.onos.openflow.controller.RoleState;
alshabibeec3a062014-09-17 18:01:26 -070012import org.onlab.util.Timer;
13import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
14import org.projectfloodlight.openflow.types.OFPort;
15import org.projectfloodlight.openflow.types.TableId;
16import org.slf4j.Logger;
17
18public class FlowStatsCollector implements TimerTask {
19
20 private final Logger log = getLogger(getClass());
21
22 private final HashedWheelTimer timer = Timer.getTimer();
23 private final OpenFlowSwitch sw;
24 private final int refreshInterval;
25
26 private Timeout timeout;
27
28 private boolean stopTimer = false;;
29
30 public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) {
31 this.sw = sw;
32 this.refreshInterval = refreshInterval;
33 }
34
35 @Override
36 public void run(Timeout timeout) throws Exception {
37 log.debug("Collecting stats for {}", this.sw.getStringId());
38
39 sendFlowStatistics();
40
41 if (!this.stopTimer) {
42 log.debug("Scheduling stats collection in {} seconds for {}",
43 this.refreshInterval, this.sw.getStringId());
44 timeout.getTimer().newTimeout(this, refreshInterval,
45 TimeUnit.SECONDS);
46 }
47
48
49 }
50
51 private void sendFlowStatistics() {
Yuta HIGUCHI1b6f5682014-10-25 21:33:59 -070052 if (log.isTraceEnabled()) {
53 log.trace("sendFlowStatistics {}:{}", sw.getStringId(), sw.getRole());
54 }
55 if (sw.getRole() != RoleState.MASTER) {
56 // Switch not master.
57 return;
58 }
alshabibeec3a062014-09-17 18:01:26 -070059 OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
60 .setMatch(sw.factory().matchWildcardAll())
61 .setTableId(TableId.ALL)
62 .setOutPort(OFPort.NO_MASK)
63 .build();
64
65 this.sw.sendMsg(request);
66
67 }
68
69 public void start() {
70
71 /*
72 * Initially start polling quickly. Then drop down to configured value
73 */
74 log.info("Starting Stats collection thread for {}",
75 this.sw.getStringId());
76 timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
77 }
78
79 public void stop() {
80 log.info("Stopping Stats collection thread for {}",
81 this.sw.getStringId());
82 this.stopTimer = true;
83 timeout.cancel();
84 }
85
86}