Barrier turn on/off-add switcher value to Config-Subsystem
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpChannelInitializer.java
index b93a641ddaa3e6c4f4dc375daf58547cd9a9047f..18566eb29e086898d68fbfbfb9bbc957649b5719 100644 (file)
@@ -12,16 +12,15 @@ 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.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;
 
@@ -34,7 +33,7 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
     private static final Logger LOGGER = LoggerFactory
             .getLogger(TcpChannelInitializer.class);
     private final DefaultChannelGroup allChannels;
-    private ConnectionAdapterFactory connectionAdapterFactory;
+    private final ConnectionAdapterFactory connectionAdapterFactory;
 
     /**
      * default ctor
@@ -45,31 +44,34 @@ 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.debug("Incoming connection from (remote address): " + switchAddress.toString()
-                + ":" + remotePort + " --> :" + port);
-        if (!getSwitchConnectionHandler().accept(switchAddress)) {
-            ch.disconnect();
-            LOGGER.debug("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();
+            LOGGER.debug("Incoming connection from (remote address): {}:{} --> :{}",
+                           switchAddress.toString(), remotePort, port);
+
+            if (!getSwitchConnectionHandler().accept(switchAddress)) {
+                ch.disconnect();
+                LOGGER.debug("Incoming connection rejected");
+                return;
+            }
         }
         LOGGER.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());
+            LOGGER.debug("calling plugin: {}", getSwitchConnectionHandler());
             getSwitchConnectionHandler().onSwitchConnected(connectionFacade);
             connectionFacade.checkListeners();
             ch.pipeline().addLast(PipelineHandlers.IDLE_HANDLER.name(), new IdleHandler(getSwitchIdleTimeout(), TimeUnit.MILLISECONDS));
@@ -78,26 +80,35 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
             // If this channel is configured to support SSL it will only support SSL
             if (getTlsConfiguration() != null) {
                 tlsPresent = true;
-                SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
-                SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
+                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));
+                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, 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 (!tlsPresent) {
                 connectionFacade.fireConnectionReadyNotification();
             }
-        } catch (Exception e) {
+        } catch (final Exception e) {
             LOGGER.warn("Failed to initialize channel", e);
             ch.close();
         }
@@ -116,4 +127,4 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
     public int size() {
         return allChannels.size();
     }
-}
\ No newline at end of file
+}