Fix checkstyle warnings for impl/connection package and OpenFlowPluginProviderImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / HandshakeListenerImpl.java
index c8f0600edf866903a2f4bfc45545e7c8e304d53e..9ec64066e0c3655593c69a4e31f0c047661b6d05 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;
@@ -27,80 +28,95 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/**
- *
- */
 public class HandshakeListenerImpl implements HandshakeListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(HandshakeListenerImpl.class);
 
-    private ConnectionContext connectionContext;
-    private DeviceConnectedHandler deviceConnectedHandler;
+    private final ConnectionContext connectionContext;
+    private final DeviceConnectedHandler deviceConnectedHandler;
     private HandshakeContext handshakeContext;
 
     /**
-     * @param connectionContext
-     * @param deviceConnectedHandler
+     * Constructor.
+     *
+     * @param connectionContext - connection context
+     * @param deviceConnectedHandler - device connected handler
      */
-    public HandshakeListenerImpl(ConnectionContext connectionContext, DeviceConnectedHandler deviceConnectedHandler) {
+    public HandshakeListenerImpl(final ConnectionContext connectionContext,
+                                 final DeviceConnectedHandler deviceConnectedHandler) {
         this.connectionContext = connectionContext;
         this.deviceConnectedHandler = deviceConnectedHandler;
     }
 
     @Override
-    public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version) {
-        LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
-        closeHandshakeContext();
+    public void onHandshakeSuccessful(final GetFeaturesOutput featureOutput, final Short version) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("handshake succeeded: {}", connectionContext.getConnectionAdapter().getRemoteAddress());
+        }
+        this.handshakeContext.close();
         connectionContext.changeStateToWorking();
         connectionContext.setFeatures(featureOutput);
         connectionContext.setNodeId(InventoryDataServiceUtil.nodeIdFromDatapathId(featureOutput.getDatapathId()));
+        connectionContext.handshakeSuccessful();
 
         // 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
+            @SuppressWarnings("checkstyle:IllegalCatch")
             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);
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("succeeded by getting sweep barrier after post-handshake for device {}",
+                            connectionContext.getDeviceInfo());
+                }
+                try {
+                    ConnectionStatus connectionStatusResult = deviceConnectedHandler.deviceConnected(connectionContext);
+                    if (connectionStatusResult != ConnectionStatus.MAY_CONTINUE) {
+                        connectionContext.closeConnection(false);
+                    }
+                    SessionStatistics.countEvent(connectionContext.getDeviceInfo().toString(),
+                            SessionStatistics.ConnectionStatus.CONNECTION_CREATED);
+                } catch (final Exception e) {
+                    LOG.warn("initial processing failed for device {}", connectionContext.getDeviceInfo(), e);
+                    SessionStatistics.countEvent(connectionContext.getDeviceInfo().toString(),
+                            SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
+                    connectionContext.closeConnection(true);
+                }
             }
 
             @Override
-            public void onFailure(final Throwable t) {
-                LOG.info("failed to get sweep barrier after posthandshake for device {}", connectionContext.getNodeId());
+            public void onFailure(final Throwable throwable) {
+                LOG.warn("failed to get sweep barrier after post-handshake for device {}",
+                        connectionContext.getDeviceInfo(), throwable);
                 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 (Exception e) {
-            LOG.warn("Closing handshake context failed: {}", e.getMessage());
-            LOG.debug("Detail in hanshake context close:", e);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("handshake failed: {}", this.connectionContext.getConnectionAdapter().getRemoteAddress());
         }
+        this.handshakeContext.close();
+        this.connectionContext.closeConnection(false);
     }
 
     @Override
-    public void setHandshakeContext(HandshakeContext handshakeContext) {
+    public void setHandshakeContext(final HandshakeContext handshakeContext) {
         this.handshakeContext = handshakeContext;
     }
 }