SONAR TD - StatisticsContextImpl, StatisticsManagerImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImpl.java
index c79f16aa56efeb1a11cc7801557ed7848739dee0..7ed60e5823d2399230256049a14690b5936be7b8 100644 (file)
@@ -9,26 +9,37 @@
 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.base.Verify;
+import com.google.common.collect.Iterators;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.Timeout;
 import io.netty.util.TimerTask;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.Future;
 import java.util.concurrent.Semaphore;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
-import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutputBuilder;
@@ -37,147 +48,140 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/**
- * Created by Martin Bobak <mbobak@cisco.com> on 1.4.2015.
- */
 public class StatisticsManagerImpl implements StatisticsManager, StatisticsManagerControlService {
 
     private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImpl.class);
-    private final RpcProviderRegistry rpcProviderRegistry;
 
-    private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
+    private static final long DEFAULT_STATS_TIMEOUT_SEC = 50L;
+    private final ConvertorExecutor convertorExecutor;
 
-    private HashedWheelTimer hashedWheelTimer;
+    private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
+    private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
 
-    private final ConcurrentHashMap<DeviceContext, StatisticsContext> contexts = new ConcurrentHashMap<>();
+    private final ConcurrentMap<DeviceInfo, StatisticsContext> contexts = new ConcurrentHashMap<>();
 
     private static final long basicTimerDelay = 3000;
     private static long currentTimerDelay = basicTimerDelay;
-    private static long maximumTimerDelay = 900000; //wait max 15 minutes for next statistics
+    private static final long maximumTimerDelay = 900000; //wait max 15 minutes for next statistics
 
     private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
-    private Semaphore workModeGuard = new Semaphore(1, true);
-    private boolean shuttingDownStatisticsPolling;
+    private final Semaphore workModeGuard = new Semaphore(1, true);
+    private boolean isStatisticsPollingEnabled;
     private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
 
+    private final HashedWheelTimer hashedWheelTimer;
+
     @Override
     public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
         deviceInitPhaseHandler = handler;
     }
 
-    public StatisticsManagerImpl(RpcProviderRegistry rpcProviderRegistry) {
-        this.rpcProviderRegistry = rpcProviderRegistry;
-        controlServiceRegistration = rpcProviderRegistry.addRpcImplementation(StatisticsManagerControlService.class, this);
-    }
-
-    public StatisticsManagerImpl(RpcProviderRegistry rpcProviderRegistry, final boolean shuttingDownStatisticsPolling) {
-        this(rpcProviderRegistry);
-        this.shuttingDownStatisticsPolling = shuttingDownStatisticsPolling;
+    public StatisticsManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
+                                 final boolean isStatisticsPollingEnabled,
+                                 final HashedWheelTimer hashedWheelTimer,
+                                 final ConvertorExecutor convertorExecutor) {
+        Preconditions.checkArgument(rpcProviderRegistry != null);
+       this.convertorExecutor = convertorExecutor;
+        this.controlServiceRegistration = Preconditions.checkNotNull(rpcProviderRegistry.addRpcImplementation(
+                StatisticsManagerControlService.class, this));
+        this.isStatisticsPollingEnabled = isStatisticsPollingEnabled;
+        this.hashedWheelTimer = hashedWheelTimer;
     }
 
     @Override
-    public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
-        LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", deviceContext.getDeviceState().getNodeId(),
-                deviceContext.getDeviceState().getRole());
-        if (deviceContext.getDeviceState().getRole() == OfpRole.BECOMESLAVE) {
-            // if slave, we dont poll for statistics and jump to rpc initialization
-            LOG.info("Skipping Statistics for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
-            deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
-            return;
-        }
+    public void onDeviceContextLevelUp(final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
 
-        if (null == hashedWheelTimer) {
-            LOG.trace("This is first device that delivered timer. Starting statistics polling immediately.");
-            hashedWheelTimer = deviceContext.getTimer();
-        }
-
-        LOG.info("Starting Statistics for master role for node:{}", deviceContext.getDeviceState().getNodeId());
+        final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceInfo, isStatisticsPollingEnabled, lifecycleService, convertorExecutor, this);
+        Verify.verify(contexts.putIfAbsent(deviceInfo, statisticsContext) == null, "StatisticsCtx still not closed for Node {}", deviceInfo.getLOGValue());
+        lifecycleService.setStatContext(statisticsContext);
+        deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+    }
 
-        final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceContext);
-        deviceContext.addDeviceContextClosedHandler(this);
-        final ListenableFuture<Boolean> weHaveDynamicData = statisticsContext.gatherDynamicData();
-        Futures.addCallback(weHaveDynamicData, new FutureCallback<Boolean>() {
-            @Override
-            public void onSuccess(final Boolean statisticsGathered) {
-                if (statisticsGathered) {
-                    //there are some statistics on device worth gathering
-                    contexts.put(deviceContext, statisticsContext);
-                    final TimeCounter timeCounter = new TimeCounter();
-                    scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
-                    LOG.trace("Device dynamic info collecting done. Going to announce raise to next level.");
-                    deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
-                    deviceContext.getDeviceState().setDeviceSynchronized(true);
-                } else {
-                    final String deviceAdress = deviceContext.getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress().toString();
-                    try {
-                        deviceContext.close();
-                    } catch (Exception e) {
-                        LOG.info("Statistics for device {} could not be gathered. Closing its device context.", deviceAdress);
-                    }
-                }
-            }
+    @VisibleForTesting
+    void pollStatistics(final DeviceState deviceState,
+                        final StatisticsContext statisticsContext,
+                        final TimeCounter timeCounter,
+                        final DeviceInfo deviceInfo) {
 
-            @Override
-            public void onFailure(final Throwable throwable) {
-                LOG.warn("Statistics manager was not able to collect dynamic info for device.", deviceContext.getDeviceState().getNodeId(), throwable);
-                try {
-                    deviceContext.close();
-                } catch (Exception e) {
-                    LOG.warn("Error closing device context.", e);
-                }
+        if (!statisticsContext.isSchedulingEnabled()) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
             }
-        });
-    }
+            return;
+        }
 
-    private void pollStatistics(final DeviceContext deviceContext,
-                                final StatisticsContext statisticsContext,
-                                final TimeCounter timeCounter) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("POLLING ALL STATISTICS for device: {}", deviceInfo.getNodeId());
+        }
         timeCounter.markStart();
-        ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
+        final ListenableFuture<Boolean> deviceStatisticsCollectionFuture = statisticsContext.gatherDynamicData();
         Futures.addCallback(deviceStatisticsCollectionFuture, new FutureCallback<Boolean>() {
             @Override
             public void onSuccess(final Boolean o) {
                 timeCounter.addTimeMark();
                 calculateTimerDelay(timeCounter);
-                scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
+                scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
             }
 
             @Override
-            public void onFailure(final Throwable throwable) {
+            public void onFailure(@Nonnull 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(timeCounter);
-                    scheduleNextPolling(deviceContext, statisticsContext, timeCounter);
+                LOG.warn("Statistics gathering for single node {} was not successful: ", deviceInfo.getLOGValue(), throwable.getMessage());
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Gathering for node {} failure: ", deviceInfo.getLOGValue(), throwable);
+                }
+                calculateTimerDelay(timeCounter);
+                if (throwable instanceof CancellationException) {
+                    /* This often happens when something wrong with akka or DS, so closing connection will help to restart device **/
+                    contexts.get(deviceInfo).getLifecycleService().closeConnection();
+                } else {
+                    if (throwable instanceof IllegalStateException) {
+                        stopScheduling(deviceInfo);
+                    } else {
+                        scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
+                    }
                 }
             }
         });
+
+        final long averageTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
+        final long statsTimeoutSec = averageTime > 0 ? 3 * averageTime : DEFAULT_STATS_TIMEOUT_SEC;
+        final TimerTask timerTask = timeout -> {
+            if (!deviceStatisticsCollectionFuture.isDone()) {
+                LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceInfo.getLOGValue(), statsTimeoutSec);
+                deviceStatisticsCollectionFuture.cancel(true);
+            }
+        };
+
+        hashedWheelTimer.newTimeout(timerTask, statsTimeoutSec, TimeUnit.SECONDS);
     }
 
-    private void scheduleNextPolling(final DeviceContext deviceContext,
+    private void scheduleNextPolling(final DeviceState deviceState,
+                                     final DeviceInfo deviceInfo,
                                      final StatisticsContext statisticsContext,
                                      final TimeCounter timeCounter) {
-        if (null != hashedWheelTimer) {
-            if (!shuttingDownStatisticsPolling) {
-                Timeout pollTimeout = hashedWheelTimer.newTimeout(new TimerTask() {
-                    @Override
-                    public void run(final Timeout timeout) throws Exception {
-                        pollStatistics(deviceContext, statisticsContext, timeCounter);
-                    }
-                }, currentTimerDelay, TimeUnit.MILLISECONDS);
-                statisticsContext.setPollTimeout(pollTimeout);
-            }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
+        }
+        if (!isStatisticsPollingEnabled) {
+            final Timeout pollTimeout = hashedWheelTimer.newTimeout(
+                    timeout -> pollStatistics(
+                            deviceState,
+                            statisticsContext,
+                            timeCounter,
+                            deviceInfo),
+                    currentTimerDelay,
+                    TimeUnit.MILLISECONDS);
+            statisticsContext.setPollTimeout(pollTimeout);
         }
     }
 
     @VisibleForTesting
-    protected void calculateTimerDelay(final TimeCounter timeCounter) {
-        long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
+    void calculateTimerDelay(final TimeCounter timeCounter) {
+        final long averageStatisticsGatheringTime = timeCounter.getAverageTimeBetweenMarks();
         if (averageStatisticsGatheringTime > currentTimerDelay) {
             currentTimerDelay *= 2;
             if (currentTimerDelay > maximumTimerDelay) {
@@ -193,26 +197,23 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
     }
 
     @VisibleForTesting
-    protected static long getCurrentTimerDelay() {
+    static long getCurrentTimerDelay() {
         return currentTimerDelay;
     }
 
     @Override
-    public void onDeviceContextClosed(final DeviceContext deviceContext) {
-        StatisticsContext statisticsContext = contexts.remove(deviceContext);
+    public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
+        final StatisticsContext statisticsContext = contexts.remove(deviceInfo);
         if (null != statisticsContext) {
-            LOG.trace("Removing device context from stack. No more statistics gathering for node {}", deviceContext.getDeviceState().getNodeId());
-            try {
-                statisticsContext.close();
-            } catch (Exception e) {
-                LOG.debug("Error closing statistic context for node {}.", deviceContext.getDeviceState().getNodeId());
-            }
+            LOG.debug("Removing device context from stack. No more statistics gathering for device: {}", deviceInfo.getLOGValue());
+            statisticsContext.close();
         }
+        deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
     }
 
     @Override
     public Future<RpcResult<GetStatisticsWorkModeOutput>> getStatisticsWorkMode() {
-        GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
+        final GetStatisticsWorkModeOutputBuilder smModeOutputBld = new GetStatisticsWorkModeOutputBuilder();
         smModeOutputBld.setMode(workMode);
         return RpcResultBuilder.success(smModeOutputBld.build()).buildFuture();
     }
@@ -224,15 +225,16 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
         if (workModeGuard.tryAcquire()) {
             final StatisticsWorkMode targetWorkMode = input.getMode();
             if (!workMode.equals(targetWorkMode)) {
-                shuttingDownStatisticsPolling = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
+                isStatisticsPollingEnabled = StatisticsWorkMode.FULLYDISABLED.equals(targetWorkMode);
                 // iterate through stats-ctx: propagate mode
-                for (Map.Entry<DeviceContext, StatisticsContext> contextEntry : contexts.entrySet()) {
-                    final DeviceContext deviceContext = contextEntry.getKey();
-                    final StatisticsContext statisticsContext = contextEntry.getValue();
+                for (Map.Entry<DeviceInfo, StatisticsContext> entry : contexts.entrySet()) {
+                    final DeviceInfo deviceInfo = entry.getKey();
+                    final StatisticsContext statisticsContext = entry.getValue();
+                    final DeviceContext deviceContext = statisticsContext.getLifecycleService().getDeviceContext();
                     switch (targetWorkMode) {
                         case COLLECTALL:
-                            scheduleNextPolling(deviceContext, statisticsContext, new TimeCounter());
-                            for (ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
+                            scheduleNextPolling(deviceContext.getDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
+                            for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
                                 lifeCycleSource.setItemLifecycleListener(null);
                             }
                             break;
@@ -241,12 +243,12 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
                             if (pollTimeout.isPresent()) {
                                 pollTimeout.get().cancel();
                             }
-                            for (ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
+                            for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
                                 lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
                             }
                             break;
                         default:
-                            LOG.warn("statistics work mode not supported: {}", targetWorkMode);
+                            LOG.warn("Statistics work mode not supported: {}", targetWorkMode);
                     }
                 }
                 workMode = targetWorkMode;
@@ -261,11 +263,62 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
         return result;
     }
 
+    @Override
+    public void startScheduling(final DeviceInfo deviceInfo) {
+        if (isStatisticsPollingEnabled) {
+            LOG.info("Statistics are shutdown for device: {}", deviceInfo.getNodeId());
+            return;
+        }
+
+        final StatisticsContext statisticsContext = contexts.get(deviceInfo);
+
+        if (statisticsContext == null) {
+            LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
+            return;
+        }
+
+        if (statisticsContext.isSchedulingEnabled()) {
+            LOG.debug("Statistics scheduling is already enabled for device: {}", deviceInfo.getNodeId());
+            return;
+        }
+
+        LOG.info("Scheduling statistics poll for device: {}", deviceInfo.getNodeId());
+
+        statisticsContext.setSchedulingEnabled(true);
+        final DeviceState deviceState = contexts.get(deviceInfo).getLifecycleService().getDeviceContext().getDeviceState();
+        scheduleNextPolling(deviceState, deviceInfo, statisticsContext, new TimeCounter());
+    }
+
+    @Override
+    public void stopScheduling(final DeviceInfo deviceInfo) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Stopping statistics scheduling for device: {}", deviceInfo.getNodeId());
+        }
+        final StatisticsContext statisticsContext = contexts.get(deviceInfo);
+
+        if (statisticsContext == null) {
+            LOG.warn("Statistics context not found for device: {}", deviceInfo.getNodeId());
+            return;
+        }
+
+        statisticsContext.setSchedulingEnabled(false);
+    }
+
     @Override
     public void close() {
         if (controlServiceRegistration != null) {
             controlServiceRegistration.close();
             controlServiceRegistration = null;
         }
+        for (final Iterator<StatisticsContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
+                iterator.hasNext();) {
+            iterator.next().close();
+        }
     }
+
+    @Override
+    public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
+        this.deviceTerminPhaseHandler = handler;
+    }
+
 }