re-activate FindBugs
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / ConnectionReadyListenerImpl.java
index 7c9f0eb6f22225e720e4cd26e375261f71a94bf9..21769465227e0caa49a333ab525d0675a28c8cad 100644 (file)
@@ -7,45 +7,70 @@
  */
 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;
-import org.opendaylight.openflowplugin.openflow.md.core.HandshakeStepWrapper;
+import org.opendaylight.openflowplugin.impl.connection.HandshakeStepWrapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * oneshot listener - once connection is ready, initiate handshake (if not already started by device)
+ * Oneshot listener - once connection is ready, initiate handshake (if not already started by device).
  */
 public class ConnectionReadyListenerImpl implements ConnectionReadyListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(ConnectionReadyListenerImpl.class);
 
-    private ConnectionContext connectionContext;
-    private HandshakeContext handshakeContext;
+    private final ConnectionContext connectionContext;
+    private final HandshakeContext handshakeContext;
 
     /**
-     * @param connectionContext
-     * @param handshakeContext
+     * Constructor.
+     *
+     * @param connectionContext - connection context
+     * @param handshakeContext - handshake context
      */
-    public ConnectionReadyListenerImpl(ConnectionContext connectionContext,
-            HandshakeContext handshakeContext) {
-                this.connectionContext = connectionContext;
-                this.handshakeContext = handshakeContext;
+    public ConnectionReadyListenerImpl(ConnectionContext connectionContext, HandshakeContext handshakeContext) {
+        this.connectionContext = connectionContext;
+        this.handshakeContext = handshakeContext;
     }
 
     @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void onConnectionReady() {
-        LOG.debug("device is connected and ready-to-use (pipeline prepared): {}",
-                connectionContext.getConnectionAdapter().getRemoteAddress());
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("device is connected and ready-to-use (pipeline prepared): {}",
+                    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.error("failed to process onConnectionReady event on device {}",
+                                connectionContext.getConnectionAdapter().getRemoteAddress(),
+                                e);
+                        connectionContext.closeConnection(false);
+                        handshakeContext.close();
+                    }
+                } else {
+                    LOG.debug("already touched by hello message from device {} after second check",
+                            connectionContext.getConnectionAdapter().getRemoteAddress());
+                }
+            }
         } else {
-            LOG.debug("already touched by hello message");
+            LOG.debug("already touched by hello message from device {} after first check",
+                    connectionContext.getConnectionAdapter().getRemoteAddress());
         }
     }