schedule next poll only after previous is done
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImpl.java
index ac1b5874eea3b07a5010bd868d1f9ac2dff98d56..ebcb995d30309b0f13a6968b266713a29a2db2e1 100644 (file)
@@ -16,6 +16,7 @@ import io.netty.util.Timeout;
 import io.netty.util.TimerTask;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
@@ -64,7 +65,7 @@ public class StatisticsManagerImpl implements StatisticsManager {
                 if (statisticsGathered.booleanValue()) {
                     //there are some statistics on device worth gathering
                     contexts.put(deviceContext, statisticsContext);
-                    pollStatistics(statisticsContext);
+                    pollStatistics(deviceContext, statisticsContext);
                 }
                 LOG.trace("Device dynamic info collecting done. Going to announce raise to next level.");
                 deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
@@ -83,33 +84,38 @@ public class StatisticsManagerImpl implements StatisticsManager {
         });
     }
 
-    private void pollStatistics(final StatisticsContext statisticsContext) {
-        try {
-            timeCounter.markStart();
-            ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
-            Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
-                @Override
-                public void onSuccess(final Boolean o) {
-                    timeCounter.addTimeMark();
+    private void pollStatistics(final DeviceContext deviceContext, final StatisticsContext statisticsContext) {
+        timeCounter.markStart();
+        ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
+        Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
+            @Override
+            public void onSuccess(final Boolean o) {
+                timeCounter.addTimeMark();
+                calculateTimerDelay();
+                scheduleNextPolling(deviceContext, statisticsContext);
+            }
+
+            @Override
+            public void onFailure(final Throwable throwable) {
+                timeCounter.addTimeMark();
+                LOG.info("Statistics gathering for single node was not successful: {}", throwable.getMessage());
+                LOG.debug("Statistics gathering for single node was not successful.. ", throwable);
+                if (ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
+                    calculateTimerDelay();
+                    scheduleNextPolling(deviceContext, statisticsContext);
                 }
+            }
+        });
+    }
 
+    private void scheduleNextPolling(final DeviceContext deviceContext, final StatisticsContext statisticsContext) {
+        if (null != hashedWheelTimer) {
+            hashedWheelTimer.newTimeout(new TimerTask() {
                 @Override
-                public void onFailure(final Throwable throwable) {
-                    timeCounter.addTimeMark();
-                    LOG.info("Statistics gathering for single node was not successful: {}", throwable.getMessage());
-                    LOG.debug("Statistics gathering for single node was not successful.. ", throwable);
+                public void run(final Timeout timeout) throws Exception {
+                    pollStatistics(deviceContext, statisticsContext);
                 }
-            });
-        } finally {
-            calculateTimerDelay();
-            if (null != hashedWheelTimer) {
-                hashedWheelTimer.newTimeout(new TimerTask() {
-                    @Override
-                    public void run(final Timeout timeout) throws Exception {
-                        pollStatistics(statisticsContext);
-                    }
-                }, currentTimerDelay, TimeUnit.MILLISECONDS);
-            }
+            }, currentTimerDelay, TimeUnit.MILLISECONDS);
         }
     }