Merge "update nagasena dependencies for IT"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImpl.java
index 8b5848c4a1dfe3eb890ae3113289f361df681aba..301d0061fbd97c226ea5ce413489da571eca6589 100644 (file)
@@ -8,12 +8,15 @@
 
 package org.opendaylight.openflowplugin.impl.statistics;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
+import io.netty.util.Timeout;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
@@ -24,9 +27,12 @@ import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
+import org.opendaylight.openflowplugin.impl.rpc.listener.ItemLifecycleListenerImpl;
 import org.opendaylight.openflowplugin.impl.services.RequestContextUtil;
+import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringOnTheFlyService;
 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
 import org.slf4j.Logger;
@@ -39,19 +45,25 @@ public class StatisticsContextImpl implements StatisticsContext {
 
     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImpl.class);
     private static final String CONNECTION_CLOSED = "Connection closed.";
+
+    private final ItemLifecycleListener itemLifeCycleListener;
     private final Collection<RequestContext<?>> requestContexts = new HashSet<>();
     private final DeviceContext deviceContext;
     private final DeviceState devState;
     private final ListenableFuture<Boolean> emptyFuture;
     private final List<MultipartType> collectingStatType;
 
-    private final StatisticsGatheringService statisticsGatheringService;
+    private StatisticsGatheringService statisticsGatheringService;
+    private StatisticsGatheringOnTheFlyService statisticsGatheringOnTheFlyService;
+    private Timeout pollTimeout;
 
     public StatisticsContextImpl(@CheckForNull final DeviceContext deviceContext) {
         this.deviceContext = Preconditions.checkNotNull(deviceContext);
         devState = Preconditions.checkNotNull(deviceContext.getDeviceState());
         emptyFuture = Futures.immediateFuture(new Boolean(false));
         statisticsGatheringService = new StatisticsGatheringService(this, deviceContext);
+        statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext);
+
         final List<MultipartType> statListForCollecting = new ArrayList<>();
         if (devState.isTableStatisticsAvailable()) {
             statListForCollecting.add(MultipartType.OFPMPTABLE);
@@ -74,6 +86,7 @@ public class StatisticsContextImpl implements StatisticsContext {
             statListForCollecting.add(MultipartType.OFPMPQUEUE);
         }
         collectingStatType = ImmutableList.<MultipartType>copyOf(statListForCollecting);
+        itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext);
     }
 
     @Override
@@ -88,7 +101,7 @@ public class StatisticsContextImpl implements StatisticsContext {
         return settableStatResultFuture;
     }
 
-    private ListenableFuture<Boolean> choiseStat(final MultipartType multipartType) {
+    private ListenableFuture<Boolean> chooseStat(final MultipartType multipartType) {
         switch (multipartType) {
             case OFPMPFLOW:
                 return collectFlowStatistics(multipartType);
@@ -129,6 +142,19 @@ public class StatisticsContextImpl implements StatisticsContext {
         for (final RequestContext<?> requestContext : requestContexts) {
             RequestContextUtil.closeRequestContextWithRpcError(requestContext, CONNECTION_CLOSED);
         }
+        if (null != pollTimeout && !pollTimeout.isExpired()) {
+            pollTimeout.cancel();
+        }
+    }
+
+    @Override
+    public void setPollTimeout(Timeout pollTimeout) {
+        this.pollTimeout = pollTimeout;
+    }
+
+    @Override
+    public Optional<Timeout> getPollTimeout() {
+        return Optional.fromNullable(pollTimeout);
     }
 
     void statChainFuture(final Iterator<MultipartType> iterator, final SettableFuture<Boolean> resultFuture) {
@@ -136,7 +162,7 @@ public class StatisticsContextImpl implements StatisticsContext {
             resultFuture.set(Boolean.TRUE);
             return;
         }
-        final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = choiseStat(iterator.next());
+        final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = chooseStat(iterator.next());
         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
             @Override
             public void onSuccess(final Boolean result) {
@@ -155,7 +181,8 @@ public class StatisticsContextImpl implements StatisticsContext {
      *
      * @return
      */
-    private ListenableFuture<Boolean> deviceConnectionCheck() {
+    @VisibleForTesting
+    ListenableFuture<Boolean> deviceConnectionCheck() {
         if (!ConnectionContext.CONNECTION_STATE.WORKING.equals(deviceContext.getPrimaryConnectionContext().getConnectionState())) {
             ListenableFuture<Boolean> resultingFuture = SettableFuture.create();
             switch (deviceContext.getPrimaryConnectionContext().getConnectionState()) {
@@ -175,7 +202,7 @@ public class StatisticsContextImpl implements StatisticsContext {
 
     private ListenableFuture<Boolean> collectFlowStatistics(final MultipartType multipartType) {
         return devState.isFlowStatisticsAvailable() ? StatisticsGatheringUtils.gatherStatistics(
-                statisticsGatheringService, deviceContext, /*MultipartType.OFPMPFLOW*/ multipartType) : emptyFuture;
+                statisticsGatheringOnTheFlyService, deviceContext, /*MultipartType.OFPMPFLOW*/ multipartType) : emptyFuture;
     }
 
     private ListenableFuture<Boolean> collectTableStatistics(final MultipartType multipartType) {
@@ -212,4 +239,20 @@ public class StatisticsContextImpl implements StatisticsContext {
         return devState.isMetersAvailable() ? StatisticsGatheringUtils.gatherStatistics(
                 statisticsGatheringService, deviceContext, /*MultipartType.OFPMPMETER*/ multipartType) : emptyFuture;
     }
+
+    @VisibleForTesting
+    protected void setStatisticsGatheringService(StatisticsGatheringService statisticsGatheringService) {
+        this.statisticsGatheringService = statisticsGatheringService;
+    }
+
+    @VisibleForTesting
+    protected void setStatisticsGatheringOnTheFlyService(StatisticsGatheringOnTheFlyService
+                                                             statisticsGatheringOnTheFlyService) {
+        this.statisticsGatheringOnTheFlyService = statisticsGatheringOnTheFlyService;
+    }
+
+    @Override
+    public ItemLifecycleListener getItemLifeCycleListener() {
+        return itemLifeCycleListener;
+    }
 }