Fixed netty & checkstyle failures
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpChannelInitializer.java
index d84126b88b45e849b7a59292a6fae5a93d425712..376978d1c4e392eb9069fb50ba50422c7c1b807d 100644 (file)
@@ -12,16 +12,16 @@ import io.netty.channel.Channel;
 import io.netty.channel.group.DefaultChannelGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.handler.ssl.SslHandler;
-
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GenericFutureListener;
 import java.net.InetAddress;
 import java.util.Iterator;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
-
 import javax.net.ssl.SSLEngine;
-
-import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterFactory;
-import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterFactoryImpl;
-import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionFacade;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
+import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -31,10 +31,10 @@ import org.slf4j.LoggerFactory;
  */
 public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChannel> {
 
-    private static final Logger LOGGER = LoggerFactory
+    private static final Logger LOG = LoggerFactory
             .getLogger(TcpChannelInitializer.class);
     private final DefaultChannelGroup allChannels;
-    private ConnectionAdapterFactory connectionAdapterFactory;
+    private final ConnectionAdapterFactory connectionAdapterFactory;
 
     /**
      * default ctor
@@ -45,57 +45,79 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
 
     /**
      * Testing Constructor
-     * 
+     *
      */
-    protected TcpChannelInitializer( DefaultChannelGroup channelGroup, ConnectionAdapterFactory connAdaptorFactory ) {
+    protected TcpChannelInitializer( final DefaultChannelGroup channelGroup, final ConnectionAdapterFactory connAdaptorFactory ) {
        allChannels = channelGroup ;
        connectionAdapterFactory = connAdaptorFactory ;
     }
-    
+
     @Override
     protected void initChannel(final SocketChannel ch) {
-        InetAddress switchAddress = ch.remoteAddress().getAddress();
-        int port = ch.localAddress().getPort();
-        int remotePort = ch.remoteAddress().getPort();
-        LOGGER.info("Incoming connection from (remote address): " + switchAddress.toString()
-                + ":" + remotePort + " --> :" + port);
-        if (!getSwitchConnectionHandler().accept(switchAddress)) {
-            ch.disconnect();
-            LOGGER.info("Incoming connection rejected");
-            return;
+        if (ch.remoteAddress() != null) {
+            final InetAddress switchAddress = ch.remoteAddress().getAddress();
+            final int port = ch.localAddress().getPort();
+            final int remotePort = ch.remoteAddress().getPort();
+            LOG.debug("Incoming connection from (remote address): {}:{} --> :{}",
+                           switchAddress.toString(), remotePort, port);
+
+            if (!getSwitchConnectionHandler().accept(switchAddress)) {
+                ch.disconnect();
+                LOG.debug("Incoming connection rejected");
+                return;
+            }
         }
-        LOGGER.info("Incoming connection accepted - building pipeline");
+        LOG.debug("Incoming connection accepted - building pipeline");
         allChannels.add(ch);
         ConnectionFacade connectionFacade = null;
-        connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null);
+        connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null, useBarrier());
         try {
-            LOGGER.debug("calling plugin: " + getSwitchConnectionHandler());
+            LOG.debug("calling plugin: {}", getSwitchConnectionHandler());
             getSwitchConnectionHandler().onSwitchConnected(connectionFacade);
             connectionFacade.checkListeners();
             ch.pipeline().addLast(PipelineHandlers.IDLE_HANDLER.name(), new IdleHandler(getSwitchIdleTimeout(), TimeUnit.MILLISECONDS));
-            
+            boolean tlsPresent = false;
+
             // If this channel is configured to support SSL it will only support SSL
             if (getTlsConfiguration() != null) {
-                SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
-                SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
+                tlsPresent = true;
+                final SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
+                final SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
                 engine.setNeedClientAuth(true);
                 engine.setUseClientMode(false);
-                ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), new SslHandler(engine));
+                List<String> suitesList = getTlsConfiguration().getCipherSuites();
+                if (suitesList != null && !suitesList.isEmpty()) {
+                    LOG.debug("Requested Cipher Suites are: {}", suitesList);
+                    String[] suites = suitesList.toArray(new String[suitesList.size()]);
+                    engine.setEnabledCipherSuites(suites);
+                    LOG.debug("Cipher suites enabled in SSLEngine are: {}", engine.getEnabledCipherSuites().toString());
+                }
+                final SslHandler ssl = new SslHandler(engine);
+                final Future<Channel> handshakeFuture = ssl.handshakeFuture();
+                final ConnectionFacade finalConnectionFacade = connectionFacade;
+                handshakeFuture.addListener(new GenericFutureListener<Future<? super Channel>>() {
+                    @Override
+                    public void operationComplete(final Future<? super Channel> future) throws Exception {
+                        finalConnectionFacade.fireConnectionReadyNotification();
+                    }
+                });
+                ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), ssl);
             }
-            ch.pipeline().addLast(PipelineHandlers.OF_FRAME_DECODER.name(), new OFFrameDecoder(connectionFacade));
+            ch.pipeline().addLast(PipelineHandlers.OF_FRAME_DECODER.name(),
+                    new OFFrameDecoder(connectionFacade, tlsPresent));
             ch.pipeline().addLast(PipelineHandlers.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
-            OFDecoder ofDecoder = new OFDecoder();
+            final OFDecoder ofDecoder = new OFDecoder();
             ofDecoder.setDeserializationFactory(getDeserializationFactory());
             ch.pipeline().addLast(PipelineHandlers.OF_DECODER.name(), ofDecoder);
-            OFEncoder ofEncoder = new OFEncoder();
+            final OFEncoder ofEncoder = new OFEncoder();
             ofEncoder.setSerializationFactory(getSerializationFactory());
             ch.pipeline().addLast(PipelineHandlers.OF_ENCODER.name(), ofEncoder);
             ch.pipeline().addLast(PipelineHandlers.DELEGATING_INBOUND_HANDLER.name(), new DelegatingInboundHandler(connectionFacade));
-            if (getTlsConfiguration() == null) {
+            if (!tlsPresent) {
                 connectionFacade.fireConnectionReadyNotification();
             }
-        } catch (Exception e) {
-            LOGGER.error("Failed to initialize channel", e);
+        } catch (final Exception e) {
+            LOG.warn("Failed to initialize channel", e);
             ch.close();
         }
     }
@@ -113,4 +135,4 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
     public int size() {
         return allChannels.size();
     }
-}
\ No newline at end of file
+}