* </p>
* Created by Martin Bobak <mbobak@cisco.com> on 25.2.2015.
*/
-public interface ConnectionContext extends AutoCloseable {
+public interface ConnectionContext {
/**
* distinguished connection states
* @return
*/
OutboundQueue getOutboundQueueProvider();
+
/**
* Method sets reference to OFJava outbound queue provider.
- *
*/
- void setOutboundQueueProvider(OutboundQueueProvider outboundQueueProvider);
+ void setOutboundQueueProvider(OutboundQueueProvider outboundQueueProvider);
/**
* Method returns current connection state.
*/
CONNECTION_STATE getConnectionState();
- /**
- * Method sets connection state of current context.
- *
- * @param connectionState
- */
- void setConnectionState(CONNECTION_STATE connectionState);
-
/**
* @param featuresReply as received from device during handshake
*/
*/
void setDeviceDisconnectedHandler(DeviceDisconnectedHandler deviceDisconnectedHandler);
+ void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration);
+
/**
- * Method provides propagates info about closed connection to handler for handling closing connections.
+ * actively drop associated connection
+ *
+ * @param propagate true if event need to be propagated to higher contexts (device, stats, rpc..)
+ * or false if invoked from higher context
+ * @see ConnectionAdapter#disconnect()
*/
- void propagateClosingConnection();
+ void closeConnection(boolean propagate);
- void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration);
+ /**
+ * cleanup context upon connection closed event (by device)
+ */
+ void onConnectionClosed();
+
+ /**
+ * change internal state to {@link ConnectionContext.CONNECTION_STATE#HANDSHAKING}
+ */
+ void changeStateToHandshaking();
- @Override
- void close();
+ /**
+ * change internal state to {@link ConnectionContext.CONNECTION_STATE#TIMEOUTING}
+ */
+ void changeStateToTimeouting();
+
+ /**
+ * change internal state to {@link ConnectionContext.CONNECTION_STATE#WORKING}
+ */
+ void changeStateToWorking();
}
MessageSpy getMessageSpy();
- void setDeviceDisconnectedHandler(DeviceDisconnectedHandler deviceDisconnectedHandler);
-
/**
* Method sets reference to handler used for cleanup after device context about to be closed.
*/
package org.opendaylight.openflowplugin.api.openflow.rpc;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
import org.opendaylight.yangtools.yang.binding.RpcService;
/**
* <p>
* Created by Martin Bobak <mbobak@cisco.com> on 25.2.2015.
*/
-public interface RpcContext extends RequestContextStack, AutoCloseable, DeviceDisconnectedHandler {
+public interface RpcContext extends RequestContextStack, AutoCloseable, DeviceContextClosedHandler {
<S extends RpcService> void registerRpcServiceImplementation(Class<S> serviceClass, S serviceInstance);
}
/**
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- *
+ * <p/>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.openflowplugin.impl.connection;
+import java.math.BigInteger;
+import java.net.InetSocketAddress;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
this.nodeId = nodeId;
}
- @Override
- public void setConnectionState(final CONNECTION_STATE connectionState) {
- this.connectionState = connectionState;
- }
-
@Override
public FeaturesReply getFeatures() {
return featuresReply;
this.deviceDisconnectedHandler = deviceDisconnectedHandler;
}
- @Override
- public void propagateClosingConnection() {
- if (null != deviceDisconnectedHandler) {
- LOG.trace("Populating connection closed event.");
- this.deviceDisconnectedHandler.onDeviceDisconnected(this);
- }
- }
-
@Override
public void setFeatures(final FeaturesReply featuresReply) {
this.featuresReply = featuresReply;
}
@Override
- public void close() {
+ public void closeConnection(boolean propagate) {
+ final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
+ LOG.debug("Actively closing connection: {}, datapathId:{}.",
+ connectionAdapter.getRemoteAddress(), datapathId);
+ connectionState = ConnectionContext.CONNECTION_STATE.RIP;
+
+ unregisterOutboundQueue();
if (getConnectionAdapter().isAlive()) {
- setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
getConnectionAdapter().disconnect();
}
+
+ if (propagate) {
+ propagateDeviceDisconnectedEvent();
+ }
+ }
+
+ @Override
+ public void onConnectionClosed() {
+ connectionState = ConnectionContext.CONNECTION_STATE.RIP;
+
+ final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
+ final Short auxiliaryId;
+ if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
+ auxiliaryId = getFeatures().getAuxiliaryId();
+ } else {
+ auxiliaryId = 0;
+ }
+
+ LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
+ remoteAddress,
+ auxiliaryId,
+ getConnectionState());
+
+ unregisterOutboundQueue();
+
+ propagateDeviceDisconnectedEvent();
+ }
+
+ private void propagateDeviceDisconnectedEvent() {
+ if (null != deviceDisconnectedHandler) {
+ final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
+ LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
+ connectionAdapter.getRemoteAddress(), datapathId);
+ deviceDisconnectedHandler.onDeviceDisconnected(this);
+ }
+ }
+
+ @Override
+ public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
+ this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
+ }
+
+ private void unregisterOutboundQueue() {
if (outboundQueueHandlerRegistration != null) {
outboundQueueHandlerRegistration.close();
outboundQueueHandlerRegistration = null;
}
@Override
- public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
- this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
+ public void changeStateToHandshaking() {
+ connectionState = CONNECTION_STATE.HANDSHAKING;
+ }
+
+ @Override
+ public void changeStateToTimeouting() {
+ connectionState = CONNECTION_STATE.TIMEOUTING;
+ }
+
+ @Override
+ public void changeStateToWorking() {
+ connectionState = CONNECTION_STATE.WORKING;
}
}
/**
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- *
+ * <p/>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
connectionAdapter.setMessageListener(ofMessageListener);
- final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext, handshakeContext);
+ final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext);
connectionAdapter.setSystemListener(systemListener);
LOG.trace("connection ballet finished");
* @return
*/
private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
- final HandshakeListener handshakeListener) {
+ final HandshakeListener handshakeListener) {
HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
ConnectionConductor.versionOrder.get(0),
ConnectionConductor.versionOrder);
* @return parameter dedicated to hello message content
*/
public boolean isBitmapNegotiationEnabled() {
- return bitmapNegotiationEnabled ;
+ return bitmapNegotiationEnabled;
}
HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
null, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
handshakeContext.getHandshakePool().execute(handshakeStepWrapper);
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.HANDSHAKING);
+ connectionContext.changeStateToHandshaking();
} else {
LOG.debug("already touched by hello message");
}
/**
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- *
+ * <p/>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
@Override
public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version) {
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
+ LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+ closeHandshakeContext();
+ connectionContext.changeStateToWorking();
connectionContext.setFeatures(featureOutput);
connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
deviceConnectedHandler.deviceConnected(connectionContext);
@Override
public void onHandshakeFailure() {
- LOG.info("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
+ LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+ closeHandshakeContext();
+ connectionContext.closeConnection(false);
+ }
+
+ private void closeHandshakeContext() {
try {
handshakeContext.close();
} catch (Exception e) {
public void onHelloMessage(final HelloMessage hello) {
LOG.debug("processing HELLO.xid: {}", hello.getXid());
if (connectionContext.getConnectionState() == null) {
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.HANDSHAKING);
+ connectionContext.changeStateToHandshaking();
}
if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
package org.opendaylight.openflowplugin.impl.connection.listener;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.JdkFutureAdapters;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.base.Preconditions;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
-import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import javax.annotation.Nonnull;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
-import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
import org.opendaylight.openflowplugin.api.openflow.device.Xid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
private final ConnectionContext connectionContext;
- HandshakeContext handshakeContext;
private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
@VisibleForTesting
static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
- public SystemNotificationsListenerImpl(final ConnectionContext connectionContext,
- final HandshakeContext handshakeContext) {
- this.connectionContext = connectionContext;
- this.handshakeContext = handshakeContext;
+ public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext) {
+ this.connectionContext = Preconditions.checkNotNull(connectionContext);
}
@Override
public void onDisconnectEvent(final DisconnectEvent notification) {
- disconnect();
+ connectionContext.onConnectionClosed();
}
@Override
LOG.debug(
"first idle state occured, node={}|auxId={}",
remoteAddress, features.getAuxiliaryId());
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.TIMEOUTING);
+ connectionContext.changeStateToTimeouting();
EchoInputBuilder builder = new EchoInputBuilder();
builder.setVersion(features.getVersion());
Xid xid = new Xid(0L);
try {
RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(MAX_ECHO_REPLY_TIMEOUT, TimeUnit.MILLISECONDS);
if (echoReplyValue.isSuccessful()) {
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
+ connectionContext.changeStateToWorking();
shouldBeDisconnected = false;
} else {
for (RpcError replyError : echoReplyValue
}
}
if (shouldBeDisconnected) {
- disconnect();
+ connectionContext.closeConnection(true);
}
}
}).start();
}
-
- private void disconnect() {
- final ConnectionAdapter connectionAdapter = connectionContext.getConnectionAdapter();
- short auxId = -1;
- if (null != connectionContext.getFeatures() && null != connectionContext.getFeatures().getAuxiliaryId()) {
- auxId = connectionContext.getFeatures().getAuxiliaryId();
- }
- final Short auxiliaryId = auxId;
- final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
-
- LOG.trace("disconnecting: node={}|auxId={}|connection state = {}",
- remoteAddress,
- auxiliaryId,
- connectionContext.getConnectionState());
-
- ListenableFuture<Boolean> result = null;
- if (connectionAdapter.isAlive()) {
- result = JdkFutureAdapters.listenInPoolThread(connectionAdapter.disconnect());
- } else {
- LOG.debug("connection already disconnected");
- result = Futures.immediateFuture(true);
- }
- connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
- Futures.addCallback(result, new FutureCallback<Boolean>() {
- @Override
- public void onSuccess(final Boolean aBoolean) {
- LOG.debug("Connection node={}|auxId={}|connection state = {}, closed successfully:{}.",
- remoteAddress,
- auxiliaryId,
- connectionContext.getConnectionState(),
- aBoolean);
- }
-
- @Override
- public void onFailure(final Throwable throwable) {
- LOG.debug("Connection node={}|auxId={}|connection state = {} close failed.",
- remoteAddress,
- auxiliaryId,
- connectionContext.getConnectionState());
- }
- });
-
- connectionContext.propagateClosingConnection();
- try {
- handshakeContext.close();
- } catch (Exception e) {
- LOG.debug("Closing of handshake context wasn't successfull. {}", e);
- }
- }
}
import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
import org.opendaylight.openflowplugin.api.openflow.device.Xid;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
private final PacketInRateLimiter packetInLimiter;
private final MessageSpy messageSpy;
private NotificationPublishService notificationPublishService;
- private DeviceDisconnectedHandler deviceDisconnectedHandler;
private NotificationService notificationService;
private final OutboundQueue outboundQueueProvider;
private Timeout barrierTaskTimeout;
@Override
public void close() {
+ LOG.debug("closing deviceContext: {}, nodeId:{}",
+ getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress(),
+ getDeviceState().getNodeId());
+
+ tearDown();
+
+ primaryConnectionContext.closeConnection(false);
+ }
+
+ private void tearDown() {
deviceState.setValid(false);
+ for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) {
+ connectionContext.closeConnection(false);
+ }
+
deviceGroupRegistry.close();
deviceFlowRegistry.close();
deviceMeterRegistry.close();
- primaryConnectionContext.close();
- for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) {
- connectionContext.close();
- }
for (final DeviceContextClosedHandler deviceContextClosedHandler : closeHandlers) {
deviceContextClosedHandler.onDeviceContextClosed(this);
public void onDeviceDisconnected(final ConnectionContext connectionContext) {
if (getPrimaryConnectionContext().equals(connectionContext)) {
try {
- close();
+ tearDown();
} catch (final Exception e) {
LOG.trace("Error closing device context.");
}
- if (null != deviceDisconnectedHandler) {
- deviceDisconnectedHandler.onDeviceDisconnected(connectionContext);
- }
} else {
+ LOG.debug("auxiliary connection dropped: {}, nodeId:{}",
+ connectionContext.getConnectionAdapter().getRemoteAddress(),
+ getDeviceState().getNodeId());
final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
auxiliaryConnectionContexts.remove(connectionDistinguisher);
}
return messageSpy;
}
- @Override
- public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
- this.deviceDisconnectedHandler = deviceDisconnectedHandler;
- }
-
@Override
public void addDeviceContextClosedHandler(final DeviceContextClosedHandler deviceContextClosedHandler) {
closeHandlers.add(deviceContextClosedHandler);
public void onSuccess(final RpcResult<List<MultipartReply>> rpcResult) {
final List<MultipartReply> result = rpcResult.getResult();
if (result != null) {
- LOG.info("Static node {} info: {} collected", nodeII.toString(), type);
+ LOG.info("Static node {} info: {} collected", deviceContext.getDeviceState().getNodeId(), type);
translateAndWriteReply(type, deviceContext, nodeII, result);
} else {
final Iterator<RpcError> rpcErrorIterator = rpcResult.getErrors().iterator();
if (!transactionChainManager.attemptToRegisterHandler(readyForNewTransactionChainHandler)) {
if (TransactionChainManager.TransactionChainManagerStatus.WORKING.equals(transactionChainManager.getTransactionChainManagerStatus())) {
LOG.info("There already exists one handler for connection described as {}. Connection is working will not try again.", nodeId);
- connectionContext.close();
+ connectionContext.closeConnection(false);
} else {
LOG.info("There already exists one handler for connection described as {}. Transaction chain manager is in state {}. Will try again.",
nodeId,
import java.util.concurrent.Semaphore;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
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.RequestContext;
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
}
@Override
- public void onDeviceDisconnected(final ConnectionContext connectionContext) {
+ public void onDeviceContextClosed(DeviceContext deviceContext) {
for (RoutedRpcRegistration<?> registration : rpcRegistrations) {
registration.close();
}
@Override
public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
final RpcContext rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry, deviceContext, maxRequestsQuota);
- deviceContext.setDeviceDisconnectedHandler(rpcContext);
+ deviceContext.addDeviceContextClosedHandler(rpcContext);
MdSalRegistratorUtils.registerServices(rpcContext, deviceContext);
// finish device initialization cycle back to DeviceManager
deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
+++ /dev/null
-/**
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.openflowplugin.impl.connection;
-
-import static org.junit.Assert.fail;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
-
-/**
- * openflowplugin-impl
- * org.opendaylight.openflowplugin.impl.connection
- * <p/>
- * test of {@link ConnectionContextImpl} - lightweight version, using basic ways (TDD)
- *
- * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
- * <p/>
- * Created: Mar 26, 2015
- */
-@RunWith(MockitoJUnitRunner.class)
-public class ConnectionContextImplTest {
-
-
- @Mock
- private ConnectionAdapter conAdapter;
-
- @Before
- public void initialization() {
- // place for mocking method's general behavior for ConnectorAdapter
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#ConnectionContextImpl(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
- */
- @Test
- @Ignore
- public void testConnectionContextImpl() {
- fail("Not yet implemented");
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getConnectionAdapter()}.
- */
- @Test
- @Ignore
- public void testGetConnectionAdapter() {
- fail("Not yet implemented");
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getConnectionState()}.
- */
- @Test
- @Ignore
- public void testGetConnectionState() {
- fail("Not yet implemented");
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getNodeId()}.
- */
- @Test
- @Ignore
- public void testGetNodeId() {
- fail("Not yet implemented");
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#setConnectionState(org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext.CONNECTION_STATE)}.
- */
- @Test
- @Ignore
- public void testSetConnectionState() {
- fail("Not yet implemented");
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#getFeatures()}.
- */
- @Test
- @Ignore
- public void testGetFeatures() {
- fail("Not yet implemented");
- }
-
- /**
- * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl#setFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply)}.
- */
- @Test
- @Ignore
- public void testSetFeatures() {
- fail("Not yet implemented");
- }
-}
package org.opendaylight.openflowplugin.impl.connection.listener;
-import static org.junit.Assert.*;
-
-import org.opendaylight.openflowplugin.impl.connection.HandshakeContextImpl;
-
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.TimeUnit;
-import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import java.net.InetSocketAddress;
-import java.util.List;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
@Mock
private FeaturesReply features;
private ConnectionContext connectionContext;
- @Captor
- private ArgumentCaptor<ConnectionContext.CONNECTION_STATE> connectionStateArgumentCaptor;
private SystemNotificationsListenerImpl systemNotificationsListener;
private ConnectionContextImpl connectionContextGolem;
@Before
public void setUp() {
connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
- connectionContextGolem.setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
+ connectionContextGolem.changeStateToWorking();
Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
Mockito.when(connectionContext.getFeatures()).thenReturn(features);
- ThreadPoolLoggingExecutor threadPoolLoggingExecutor = new ThreadPoolLoggingExecutor(2000, 2000, 0L, TimeUnit.MILLISECONDS,
- new ArrayBlockingQueue<Runnable>(20), "OFHandshake-test identifier");
-
- systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext,
- new HandshakeContextImpl(threadPoolLoggingExecutor, null));
+ systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext);
}
@After
DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
systemNotificationsListener.onDisconnectEvent(disconnectNotification);
- verifyCommonInvocations();
- Mockito.verify(connectionAdapter).disconnect();
- Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
- Mockito.verify(connectionContext).propagateClosingConnection();
- assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+ verifyCommonInvocationsSubSet();
+ Mockito.verify(connectionContext).onConnectionClosed();
}
/**
DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
systemNotificationsListener.onDisconnectEvent(disconnectNotification);
- verifyCommonInvocations();
- Mockito.verify(connectionAdapter).disconnect();
- Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
- Mockito.verify(connectionContext).propagateClosingConnection();
- assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+ verifyCommonInvocationsSubSet();
+ Mockito.verify(connectionContext).onConnectionClosed();
}
/**
*/
@Test
public void testOnDisconnectEvent3() throws Exception {
- connectionContextGolem.setConnectionState(ConnectionContext.CONNECTION_STATE.TIMEOUTING);
+ connectionContextGolem.changeStateToTimeouting();
Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
systemNotificationsListener.onDisconnectEvent(disconnectNotification);
- verifyCommonInvocations();
- Mockito.verify(connectionAdapter).disconnect();
- Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
- Mockito.verify(connectionContext).propagateClosingConnection();
- assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+ verifyCommonInvocationsSubSet();
+ Mockito.verify(connectionContext).onConnectionClosed();
}
/**
*/
@Test
public void testOnDisconnectEvent4() throws Exception {
- connectionContextGolem.setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
+// TODO: solve connectionContextGolem.chansetConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
Mockito.when(connectionAdapter.isAlive()).thenReturn(false);
DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
systemNotificationsListener.onDisconnectEvent(disconnectNotification);
- verifyCommonInvocations();
- Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
- Mockito.verify(connectionContext).setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
- Mockito.verify(connectionContext).propagateClosingConnection();
- assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+ verifyCommonInvocationsSubSet();
+ Mockito.verify(connectionContext).onConnectionClosed();
}
/**
EchoOutput echoReplyVal = new EchoOutputBuilder().build();
echoReply.set(RpcResultBuilder.success(echoReplyVal).build());
- Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).times(2))
- .setConnectionState(connectionStateArgumentCaptor.capture());
- List<ConnectionContext.CONNECTION_STATE> allStates = connectionStateArgumentCaptor.getAllValues();
- Assert.assertEquals(2, allStates.size());
- Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, allStates.get(0));
- Assert.assertEquals(ConnectionContext.CONNECTION_STATE.WORKING, allStates.get(1));
-
verifyCommonInvocations();
Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
- Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT)).setConnectionState(ConnectionContext.CONNECTION_STATE.WORKING);
Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
- assertFalse(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+ Mockito.verify(connectionContext).changeStateToTimeouting();
+ Mockito.verify(connectionContext).changeStateToWorking();
}
/**
verifyCommonInvocations();
Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
- Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).times(2))
- .setConnectionState(connectionStateArgumentCaptor.capture());
- List<ConnectionContext.CONNECTION_STATE> allStates = connectionStateArgumentCaptor.getAllValues();
- Assert.assertEquals(2, allStates.size());
- Assert.assertEquals(ConnectionContext.CONNECTION_STATE.TIMEOUTING, allStates.get(0));
- Assert.assertEquals(ConnectionContext.CONNECTION_STATE.RIP, allStates.get(1));
-
Mockito.verify(connectionAdapter).disconnect();
- Mockito.verify(connectionContext).propagateClosingConnection();
- assertTrue(systemNotificationsListener.handshakeContext.getHandshakePool().isTerminated());
+ Mockito.verify(connectionContext).changeStateToTimeouting();
+ Mockito.verify(connectionContext).closeConnection(true);
}
private void verifyCommonInvocations() {
+ verifyCommonInvocationsSubSet();
+ Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
+ }
+
+ private void verifyCommonInvocationsSubSet() {
Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();
- Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
}
}
\ No newline at end of file