public class ConnectionContextImpl implements ConnectionContext {
private final ConnectionAdapter connectionAdapter;
- private CONNECTION_STATE connectionState;
+ private volatile CONNECTION_STATE connectionState;
private FeaturesReply featuresReply;
private NodeId nodeId;
private DeviceDisconnectedHandler deviceDisconnectedHandler;
}
@Override
- public void changeStateToHandshaking() {
+ public synchronized void changeStateToHandshaking() {
connectionState = CONNECTION_STATE.HANDSHAKING;
}
@Override
- public void changeStateToTimeouting() {
+ public synchronized void changeStateToTimeouting() {
connectionState = CONNECTION_STATE.TIMEOUTING;
}
@Override
- public void changeStateToWorking() {
+ public synchronized void changeStateToWorking() {
connectionState = CONNECTION_STATE.WORKING;
}
*/
package org.opendaylight.openflowplugin.impl.connection.listener;
+import java.util.concurrent.Future;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
connectionContext.getConnectionAdapter().getRemoteAddress());
if (connectionContext.getConnectionState() == null) {
- HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
- null, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
- handshakeContext.getHandshakePool().execute(handshakeStepWrapper);
- connectionContext.changeStateToHandshaking();
+ synchronized (connectionContext) {
+ if (connectionContext.getConnectionState() == null) {
+ connectionContext.changeStateToHandshaking();
+ HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
+ null, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
+ final Future<?> handshakeResult = handshakeContext.getHandshakePool().submit(handshakeStepWrapper);
+
+ try {
+ // as we run not in netty thread, need to remain in sync lock until initial handshake step processed
+ handshakeResult.get();
+ } catch (Exception e) {
+ LOG.warn("failed to process onConnectionReady event on device {}",
+ connectionContext.getConnectionAdapter().getRemoteAddress(),
+ e);
+ connectionContext.closeConnection(false);
+ try {
+ handshakeContext.close();
+ } catch (Exception e1) {
+ LOG.info("failed to close handshake context for device {}",
+ connectionContext.getConnectionAdapter().getRemoteAddress(),
+ e1
+ );
+ }
+ }
+ } else {
+ LOG.debug("already touched by hello message from device {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+ }
+ }
} else {
- LOG.debug("already touched by hello message");
+ LOG.debug("already touched by hello message from device {}", connectionContext.getConnectionAdapter().getRemoteAddress());
}
}
package org.opendaylight.openflowplugin.impl.connection.listener;
+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 javax.annotation.Nullable;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+import org.opendaylight.yangtools.yang.common.RpcResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
connectionContext.changeStateToWorking();
connectionContext.setFeatures(featureOutput);
connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
- deviceConnectedHandler.deviceConnected(connectionContext);
- SessionStatistics.countEvent(connectionContext.getNodeId().toString(), SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
+
+ // fire barrier in order to sweep all handshake and posthandshake messages before continue
+ final ListenableFuture<RpcResult<BarrierOutput>> barrier = fireBarrier(version, 0L);
+ Futures.addCallback(barrier, new FutureCallback<RpcResult<BarrierOutput>>() {
+ @Override
+ public void onSuccess(@Nullable final RpcResult<BarrierOutput> result) {
+ LOG.debug("succeeded by getting sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
+ deviceConnectedHandler.deviceConnected(connectionContext);
+ SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
+ SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
+ }
+
+ @Override
+ public void onFailure(final Throwable t) {
+ LOG.info("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
+ connectionContext.closeConnection(false);
+ }
+ });
+ }
+
+ protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
+ final BarrierInput barrierInput = new BarrierInputBuilder()
+ .setXid(xid)
+ .setVersion(version)
+ .build();
+ return JdkFutureAdapters.listenInPoolThread(
+ connectionContext.getConnectionAdapter().barrier(barrierInput));
}
@Override
@Override
public void onHelloMessage(final HelloMessage hello) {
- LOG.debug("processing HELLO.xid: {}", hello.getXid());
- if (connectionContext.getConnectionState() == null) {
- connectionContext.changeStateToHandshaking();
- }
-
- if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
- final HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
- hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
- //handshakeContext.getHandshakePool().submit(handshakeStepWrapper);
- // use up netty thread
- handshakeStepWrapper.run();
+ LOG.debug("processing HELLO.xid: {} from device {}", hello.getXid(), connectionContext.getConnectionAdapter().getRemoteAddress());
+ final ConnectionContext.CONNECTION_STATE connectionState = connectionContext.getConnectionState();
+ if (connectionState == null
+ || ConnectionContext.CONNECTION_STATE.HANDSHAKING.equals(connectionState)) {
+ synchronized (connectionContext) {
+ if (connectionContext.getConnectionState() == null) {
+ // got here before connection ready notification
+ connectionContext.changeStateToHandshaking();
+ }
+
+ if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
+ final HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
+ hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
+ // use up netty thread
+ handshakeStepWrapper.run();
+ } else {
+ LOG.debug("already out of handshake phase but still received hello message from device {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+ }
+ }
} else {
//TODO: consider disconnecting of bad behaving device
LOG.warn("Hello message received outside handshake phase: ", hello);
+ LOG.debug("already touched by onConnectionReady event from device {} (or finished handshake)", connectionContext.getConnectionAdapter().getRemoteAddress());
}
-
}
@Override
import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
Mockito.when(connection.isAlive()).thenReturn(true);
+ Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
+ .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
}
/**
import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
/**
* Test for {@link HandshakeListenerImpl}.
@Before
public void setUp() throws Exception {
+ Mockito.when(connectionAdapter.barrier(Matchers.<BarrierInput>any()))
+ .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
connectionContextSpy = Mockito.spy(new ConnectionContextImpl(connectionAdapter));
Mockito.when(connectionContextSpy.getConnectionAdapter()).thenReturn(connectionAdapter);
Mockito.when(features.getDatapathId()).thenReturn(BigInteger.TEN);