Do not use string concat
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpChannelInitializer.java
index d84126b88b45e849b7a59292a6fae5a93d425712..8282f078e7b38b8738b24a62709411659490e437 100644 (file)
@@ -19,9 +19,9 @@ 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;
 
@@ -45,26 +45,29 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
 
     /**
      * Testing Constructor
-     * 
+     *
      */
     protected TcpChannelInitializer( DefaultChannelGroup channelGroup, 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) {
+            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;
+            }
         }
-        LOGGER.info("Incoming connection accepted - building pipeline");
+        LOGGER.debug("Incoming connection accepted - building pipeline");
         allChannels.add(ch);
         ConnectionFacade connectionFacade = null;
         connectionFacade = connectionAdapterFactory.createConnectionFacade(ch, null);
@@ -73,16 +76,19 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
             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) {
+                tlsPresent = true;
                 SslContextFactory sslFactory = new SslContextFactory(getTlsConfiguration());
                 SSLEngine engine = sslFactory.getServerContext().createSSLEngine();
                 engine.setNeedClientAuth(true);
                 engine.setUseClientMode(false);
                 ch.pipeline().addLast(PipelineHandlers.SSL_HANDLER.name(), new SslHandler(engine));
             }
-            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();
             ofDecoder.setDeserializationFactory(getDeserializationFactory());
@@ -91,11 +97,11 @@ public class TcpChannelInitializer extends ProtocolChannelInitializer<SocketChan
             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);
+            LOGGER.warn("Failed to initialize channel", e);
             ch.close();
         }
     }