alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.provider.of.flow.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.concurrent.TimeUnit; |
| 6 | |
| 7 | import org.jboss.netty.util.HashedWheelTimer; |
| 8 | import org.jboss.netty.util.Timeout; |
| 9 | import org.jboss.netty.util.TimerTask; |
| 10 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 11 | import org.onlab.util.Timer; |
| 12 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 13 | import org.projectfloodlight.openflow.types.OFPort; |
| 14 | import org.projectfloodlight.openflow.types.TableId; |
| 15 | import org.slf4j.Logger; |
| 16 | |
| 17 | public 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 | } |