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.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
-import javax.annotation.CheckForNull;
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.OFPContext;
+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.LifecycleConductor;
import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
private StatisticsWorkMode workMode = StatisticsWorkMode.COLLECTALL;
private final Semaphore workModeGuard = new Semaphore(1, true);
- private boolean shuttingDownStatisticsPolling;
+ private boolean isStatisticsPollingEnabled;
private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> controlServiceRegistration;
- private final LifecycleConductor conductor;
+ private final HashedWheelTimer hashedWheelTimer;
@Override
public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
deviceInitPhaseHandler = handler;
}
- public StatisticsManagerImpl(@CheckForNull final RpcProviderRegistry rpcProviderRegistry,
- final boolean shuttingDownStatisticsPolling,
- final LifecycleConductor lifecycleConductor,
+ public StatisticsManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
+ final boolean isStatisticsPollingEnabled,
+ final HashedWheelTimer hashedWheelTimer,
final ConvertorExecutor convertorExecutor) {
- this.convertorExecutor = convertorExecutor;
Preconditions.checkArgument(rpcProviderRegistry != null);
+ this.convertorExecutor = convertorExecutor;
this.controlServiceRegistration = Preconditions.checkNotNull(rpcProviderRegistry.addRpcImplementation(
StatisticsManagerControlService.class, this));
- this.shuttingDownStatisticsPolling = shuttingDownStatisticsPolling;
- this.conductor = lifecycleConductor;
+ this.isStatisticsPollingEnabled = isStatisticsPollingEnabled;
+ this.hashedWheelTimer = hashedWheelTimer;
}
@Override
- public void onDeviceContextLevelUp(final DeviceInfo deviceInfo, LifecycleService lifecycleService) throws Exception {
+ public void onDeviceContextLevelUp(final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
- final StatisticsContext statisticsContext = new StatisticsContextImpl(deviceInfo, shuttingDownStatisticsPolling, conductor, convertorExecutor);
- Verify.verify(contexts.putIfAbsent(deviceInfo, statisticsContext) == null, "StatisticsCtx still not closed for Node {}", deviceInfo.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 DeviceInfo deviceInfo) {
if (!statisticsContext.isSchedulingEnabled()) {
- LOG.debug("Disabling statistics scheduling for device: {}", deviceInfo.getNodeId());
- return;
- }
-
- if (!deviceState.isValid()) {
- LOG.debug("Session is not valid for device: {}", deviceInfo.getNodeId());
- return;
- }
-
- if (!deviceState.isStatisticsPollingEnabled()) {
- LOG.debug("Statistics polling is currently disabled for device: {}", deviceInfo.getNodeId());
- scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
+ LOG.debug("Disabled statistics scheduling for device: {}", deviceInfo.getNodeId().getValue());
return;
}
@Override
public void onFailure(@Nonnull final Throwable throwable) {
timeCounter.addTimeMark();
- LOG.warn("Statistics gathering for single node was not successful: {}", throwable.getMessage());
- LOG.trace("Statistics gathering for single node was not successful.. ", throwable);
+ LOG.warn("Statistics gathering for single node was not successful: {}", 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 **/
- conductor.closeConnection(deviceInfo);
+ contexts.get(deviceInfo).getLifecycleService().closeConnection();
} else {
scheduleNextPolling(deviceState, deviceInfo, statisticsContext, timeCounter);
}
});
final long averageTime = TimeUnit.MILLISECONDS.toSeconds(timeCounter.getAverageTimeBetweenMarks());
- final long STATS_TIMEOUT_SEC = averageTime > 0 ? 3 * averageTime : DEFAULT_STATS_TIMEOUT_SEC;
+ 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.getNodeId(), STATS_TIMEOUT_SEC);
+ LOG.info("Statistics collection for node {} still in progress even after {} secs", deviceInfo.getLOGValue(), statsTimeoutSec);
deviceStatisticsCollectionFuture.cancel(true);
}
};
- conductor.newTimeout(timerTask, STATS_TIMEOUT_SEC, TimeUnit.SECONDS);
+ hashedWheelTimer.newTimeout(timerTask, statsTimeoutSec, TimeUnit.SECONDS);
}
private void scheduleNextPolling(final DeviceState deviceState,
final StatisticsContext statisticsContext,
final TimeCounter timeCounter) {
LOG.debug("SCHEDULING NEXT STATISTICS POLLING for device: {}", deviceInfo.getNodeId());
- if (!shuttingDownStatisticsPolling) {
- final Timeout pollTimeout = conductor.newTimeout(timeout -> pollStatistics(deviceState, statisticsContext, timeCounter, deviceInfo), currentTimerDelay, TimeUnit.MILLISECONDS);
+ if (!isStatisticsPollingEnabled) {
+ final Timeout pollTimeout = hashedWheelTimer.newTimeout(
+ timeout -> pollStatistics(
+ deviceState,
+ statisticsContext,
+ timeCounter,
+ deviceInfo),
+ currentTimerDelay,
+ TimeUnit.MILLISECONDS);
statisticsContext.setPollTimeout(pollTimeout);
}
}
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 device: {}", deviceInfo.getNodeId());
+ LOG.debug("Removing device context from stack. No more statistics gathering for device: {}", deviceInfo.getLOGValue());
statisticsContext.close();
}
deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
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<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(conductor.getDeviceContext(entry.getKey()).getDeviceState(), entry.getKey(), entry.getValue(), new TimeCounter());
- for (final ItemLifeCycleSource lifeCycleSource : conductor.getDeviceContext(entry.getKey()).getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
+ scheduleNextPolling(deviceContext.getDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
+ for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
lifeCycleSource.setItemLifecycleListener(null);
}
break;
case FULLYDISABLED:
- final Optional<Timeout> pollTimeout = entry.getValue().getPollTimeout();
+ final Optional<Timeout> pollTimeout = statisticsContext.getPollTimeout();
if (pollTimeout.isPresent()) {
pollTimeout.get().cancel();
}
- for (final ItemLifeCycleSource lifeCycleSource : conductor.getDeviceContext(entry.getKey()).getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
- lifeCycleSource.setItemLifecycleListener(entry.getValue().getItemLifeCycleListener());
+ for (final ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
+ lifeCycleSource.setItemLifecycleListener(statisticsContext.getItemLifeCycleListener());
}
break;
default:
@Override
public void startScheduling(final DeviceInfo deviceInfo) {
- if (shuttingDownStatisticsPolling) {
+ if (isStatisticsPollingEnabled) {
LOG.info("Statistics are shut down for device: {}", deviceInfo.getNodeId());
return;
}
LOG.info("Scheduling statistics poll for device: {}", deviceInfo.getNodeId());
statisticsContext.setSchedulingEnabled(true);
- scheduleNextPolling(conductor.getDeviceContext(deviceInfo).getDeviceState(), deviceInfo, statisticsContext, new TimeCounter());
+ final DeviceState deviceState = contexts.get(deviceInfo).getLifecycleService().getDeviceContext().getDeviceState();
+ scheduleNextPolling(deviceState, deviceInfo, statisticsContext, new TimeCounter());
}
@Override
this.deviceTerminPhaseHandler = handler;
}
- @Override
- public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
- return (T) contexts.get(deviceInfo);
- }
}