BUG-3774: 100k flows initial stats fail - fix
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / OpenflowProtocolListenerInitialImpl.java
index f75ee91018bb7d0951fb15c0499b09a211e923cc..585c58caec75d27eda1f3c57b83725346a61ea03 100644 (file)
@@ -72,22 +72,30 @@ public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolList
 
     @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