Fix logging of exception in HandshakeListenerImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / HandshakeListenerImpl.java
index 3760ff79f2ba54c1ff2cd9c234e046a705b1eec5..afac7ac95836b01fb6609b4683dd28cb1b665902 100644 (file)
@@ -14,6 +14,7 @@ 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.ConnectionStatus;
 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;
@@ -49,8 +50,10 @@ public class HandshakeListenerImpl implements HandshakeListener {
 
     @Override
     public void onHandshakeSuccessful(final GetFeaturesOutput featureOutput, final Short version) {
-        LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
-        closeHandshakeContext();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+        }
+        this.handshakeContext.close();
         connectionContext.changeStateToWorking();
         connectionContext.setFeatures(featureOutput);
         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
@@ -58,19 +61,26 @@ public class HandshakeListenerImpl implements HandshakeListener {
 
         // 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>>() {
+        Futures.addCallback(barrier, addBarrierCallback());
+    }
+
+    private FutureCallback<RpcResult<BarrierOutput>> addBarrierCallback() {
+        return 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());
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("succeeded by getting sweep barrier after post-handshake for device {}", connectionContext.getDeviceInfo().getLOGValue());
+                }
                 try {
-                    if (!deviceConnectedHandler.deviceConnected(connectionContext)) {
+                    ConnectionStatus connectionStatusResult = deviceConnectedHandler.deviceConnected(connectionContext);
+                    if (!ConnectionStatus.MAY_CONTINUE.equals(connectionStatusResult)) {
                         connectionContext.closeConnection(true);
                     }
-                    SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
+                    SessionStatistics.countEvent(connectionContext.getDeviceInfo().getLOGValue(),
                             SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
                 } catch (final Exception e) {
-                    LOG.error("ConnectionContext initial processing failed: {}", e.getMessage());
-                    SessionStatistics.countEvent(connectionContext.getNodeId().toString(),
+                    LOG.error("ConnectionContext initial processing failed for device {}", connectionContext.getDeviceInfo().getLOGValue(), e);
+                    SessionStatistics.countEvent(connectionContext.getDeviceInfo().getLOGValue(),
                             SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
                     connectionContext.closeConnection(true);
                 }
@@ -78,35 +88,28 @@ public class HandshakeListenerImpl implements HandshakeListener {
 
             @Override
             public void onFailure(final Throwable t) {
-                LOG.error("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
+                LOG.error("failed to get sweep barrier after post-handshake for device {}", connectionContext.getDeviceInfo().getLOGValue(), t);
                 connectionContext.closeConnection(false);
             }
-        });
+        };
     }
 
-    protected ListenableFuture<RpcResult<BarrierOutput>> fireBarrier(final Short version, final long xid) {
+    private 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));
+                this.connectionContext.getConnectionAdapter().barrier(barrierInput));
     }
 
     @Override
     public void onHandshakeFailure() {
-        LOG.debug("handshake failed: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
-        closeHandshakeContext();
-        connectionContext.closeConnection(false);
-    }
-
-    private void closeHandshakeContext() {
-        try {
-            handshakeContext.close();
-        } catch (final Exception e) {
-            LOG.error("Closing handshake context failed: {}", e.getMessage());
-            LOG.debug("Detail in handshake context close: {}", e);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("handshake failed: {}", this.connectionContext.getConnectionAdapter().getRemoteAddress());
         }
+        this.handshakeContext.close();
+        this.connectionContext.closeConnection(false);
     }
 
     @Override