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; |
Yuta HIGUCHI | 1b6f568 | 2014-10-25 21:33:59 -0700 | [diff] [blame] | 11 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 12 | import org.onlab.util.Timer; |
| 13 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 14 | import org.projectfloodlight.openflow.types.OFPort; |
| 15 | import org.projectfloodlight.openflow.types.TableId; |
| 16 | import org.slf4j.Logger; |
| 17 | |
| 18 | public 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 HIGUCHI | 1b6f568 | 2014-10-25 21:33:59 -0700 | [diff] [blame] | 52 | 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 | } |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 59 | 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 | } |