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;
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;
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);
statListForCollecting.add(MultipartType.OFPMPQUEUE);
}
collectingStatType = ImmutableList.<MultipartType>copyOf(statListForCollecting);
+ itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext);
}
@Override
return settableStatResultFuture;
}
- private ListenableFuture<Boolean> choiseStat(final MultipartType multipartType) {
+ private ListenableFuture<Boolean> chooseStat(final MultipartType multipartType) {
switch (multipartType) {
case OFPMPFLOW:
return collectFlowStatistics(multipartType);
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) {
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) {
*
* @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()) {
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) {
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;
+ }
}