OpenFlowRuleProvider is now configurable with respect to flowPollFrequency.

Change-Id: I3a559a9cd65df1ae56d80017696452788fc08d91
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowStatsCollector.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowStatsCollector.java
index 62c1b3f..c4c81af 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowStatsCollector.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowStatsCollector.java
@@ -15,87 +15,86 @@
  */
 package org.onosproject.provider.of.flow.impl;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
-import java.util.concurrent.TimeUnit;
-
-import org.jboss.netty.util.HashedWheelTimer;
-import org.jboss.netty.util.Timeout;
-import org.jboss.netty.util.TimerTask;
+import org.onlab.util.SharedExecutors;
 import org.onosproject.openflow.controller.OpenFlowSwitch;
 import org.onosproject.openflow.controller.RoleState;
-import org.onlab.util.Timer;
 import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest;
 import org.projectfloodlight.openflow.types.OFPort;
 import org.projectfloodlight.openflow.types.TableId;
 import org.slf4j.Logger;
 
-public class FlowStatsCollector implements TimerTask {
+import java.util.Timer;
+import java.util.TimerTask;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Collects flow statistics for the specified switch.
+ */
+class FlowStatsCollector {
 
     private final Logger log = getLogger(getClass());
 
-    private final HashedWheelTimer timer = Timer.getTimer();
+    public static final int SECONDS = 1000;
+
     private final OpenFlowSwitch sw;
-    private final int refreshInterval;
+    private Timer timer;
+    private TimerTask task;
 
-    private Timeout timeout;
+    private int pollInterval;
 
-    private boolean stopTimer = false;;
-
-    public FlowStatsCollector(OpenFlowSwitch sw, int refreshInterval) {
+    /**
+     * Creates a new collector for the given switch and poll frequency.
+     *
+     * @param timer        timer to use for scheduling
+     * @param sw           switch to pull
+     * @param pollInterval poll frequency in seconds
+     */
+    FlowStatsCollector(Timer timer, OpenFlowSwitch sw, int pollInterval) {
+        this.timer = timer;
         this.sw = sw;
-        this.refreshInterval = refreshInterval;
+        this.pollInterval = pollInterval;
     }
 
-    @Override
-    public void run(Timeout timeout) throws Exception {
-        log.trace("Collecting stats for {}", this.sw.getStringId());
+    /**
+     * Adjusts poll frequency.
+     *
+     * @param pollInterval poll frequency in seconds
+     */
+    synchronized void adjustPollInterval(int pollInterval) {
+        this.pollInterval = pollInterval;
+        task.cancel();
+        task = new InternalTimerTask();
+        timer.scheduleAtFixedRate(task, pollInterval * SECONDS, pollInterval * 1000);
+    }
 
-        sendFlowStatistics();
-
-        if (!this.stopTimer) {
-            log.trace("Scheduling stats collection in {} seconds for {}",
-                    this.refreshInterval, this.sw.getStringId());
-            timeout.getTimer().newTimeout(this, refreshInterval,
-                    TimeUnit.SECONDS);
+    private class InternalTimerTask extends TimerTask {
+        @Override
+        public void run() {
+            if (sw.getRole() == RoleState.MASTER) {
+                log.trace("Collecting stats for {}", sw.getStringId());
+                OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
+                        .setMatch(sw.factory().matchWildcardAll())
+                        .setTableId(TableId.ALL)
+                        .setOutPort(OFPort.NO_MASK)
+                        .build();
+                sw.sendMsg(request);
+            }
         }
-
-
     }
 
-    private void sendFlowStatistics() {
-        if (log.isTraceEnabled()) {
-            log.trace("sendFlowStatistics {}:{}", sw.getStringId(), sw.getRole());
-        }
-        if (sw.getRole() != RoleState.MASTER) {
-            // Switch not master.
-            return;
-        }
-        OFFlowStatsRequest request = sw.factory().buildFlowStatsRequest()
-                .setMatch(sw.factory().matchWildcardAll())
-                .setTableId(TableId.ALL)
-                .setOutPort(OFPort.NO_MASK)
-                .build();
-
-        this.sw.sendMsg(request);
-
+    public synchronized void start() {
+        // Initially start polling quickly. Then drop down to configured value
+        log.debug("Starting Stats collection thread for {}", sw.getStringId());
+        task = new InternalTimerTask();
+        SharedExecutors.getTimer().scheduleAtFixedRate(task, 1 * SECONDS,
+                                                       pollInterval * SECONDS);
     }
 
-    public void start() {
-
-        /*
-         * Initially start polling quickly. Then drop down to configured value
-         */
-        log.info("Starting Stats collection thread for {}",
-                this.sw.getStringId());
-        timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
-    }
-
-    public void stop() {
-        log.info("Stopping Stats collection thread for {}",
-                this.sw.getStringId());
-        this.stopTimer = true;
-        timeout.cancel();
+    public synchronized void stop() {
+        log.debug("Stopping Stats collection thread for {}", sw.getStringId());
+        task.cancel();
+        task = null;
     }
 
 }