BUG-2243 Fixing invalid hello message handling 52/12952/1
authorMarian Dubai <mdubai@cisco.com>
Wed, 19 Nov 2014 15:28:07 +0000 (16:28 +0100)
committerMarian Dubai <mdubai@cisco.com>
Wed, 19 Nov 2014 15:32:11 +0000 (16:32 +0100)
When invalid message before hello message, session dropped after hello message

Change-Id: I2c92012d3ba25069693a35b76d105407d200a833
Signed-off-by: Marian Dubai <mdubai@cisco.com>
opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiator.java
opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorTest.java [new file with mode: 0644]

index a48cbbe..f7bb281 100644 (file)
@@ -10,9 +10,9 @@ package org.opendaylight.controller.netconf.impl;
 
 import com.google.common.base.Optional;
 import io.netty.channel.Channel;
+import io.netty.channel.local.LocalAddress;
 import io.netty.util.Timer;
 import io.netty.util.concurrent.Promise;
-import java.net.InetSocketAddress;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
 import org.opendaylight.controller.netconf.nettyutil.AbstractNetconfSessionNegotiator;
@@ -21,19 +21,31 @@ import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAddi
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfServerSessionNegotiator extends
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.AbstractMap;
+import java.util.Map;
+
+public class NetconfServerSessionNegotiator
+        extends
         AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession, NetconfServerSessionListener> {
 
-    static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
+    static final Logger logger = LoggerFactory
+            .getLogger(NetconfServerSessionNegotiator.class);
+    private static final String UNKNOWN = "unknown";
 
-    protected NetconfServerSessionNegotiator(NetconfServerSessionPreferences sessionPreferences,
-            Promise<NetconfServerSession> promise, Channel channel, Timer timer, NetconfServerSessionListener sessionListener,
+    protected NetconfServerSessionNegotiator(
+            NetconfServerSessionPreferences sessionPreferences,
+            Promise<NetconfServerSession> promise, Channel channel,
+            Timer timer, NetconfServerSessionListener sessionListener,
             long connectionTimeoutMillis) {
-        super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
+        super(sessionPreferences, promise, channel, timer, sessionListener,
+                connectionTimeoutMillis);
     }
 
     @Override
-    protected void handleMessage(NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
+    protected void handleMessage(NetconfHelloMessage netconfMessage)
+            throws NetconfDocumentedException {
         NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
         replaceHelloMessageInboundHandler(session);
         // Negotiation successful after all non hello messages were processed
@@ -41,21 +53,56 @@ public class NetconfServerSessionNegotiator extends
     }
 
     @Override
-    protected NetconfServerSession getSession(NetconfServerSessionListener sessionListener, Channel channel, NetconfHelloMessage message) {
-        Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message.getAdditionalHeader();
+    protected NetconfServerSession getSession(
+            NetconfServerSessionListener sessionListener, Channel channel,
+            NetconfHelloMessage message) {
+        Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message
+                .getAdditionalHeader();
 
         NetconfHelloMessageAdditionalHeader parsedHeader;
         if (additionalHeader.isPresent()) {
             parsedHeader = additionalHeader.get();
         } else {
-            InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.localAddress();
-            parsedHeader = new NetconfHelloMessageAdditionalHeader("unknown", inetSocketAddress.getHostString(), Integer.toString(inetSocketAddress.getPort()),
-                    "tcp", "client");
+
+            parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
+                    getHostName(channel.localAddress()).getValue(),
+                    getHostName(channel.localAddress()).getKey(), "tcp",
+                    "client");
+
         }
 
-        logger.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
+        logger.debug("Additional header from hello parsed as {} from {}",
+                parsedHeader, additionalHeader);
+
+        return new NetconfServerSession(sessionListener, channel,
+                getSessionPreferences().getSessionId(), parsedHeader);
+    }
+
+    /**
+     * @param socketAddress
+     *            type of socket address LocalAddress, or
+     *            InetSocketAddress, for others returns unknown
+     * @return Map<port, host > two values - port and host of socket address
+     */
+    protected static Map.Entry<String, String> getHostName(
+            SocketAddress socketAddress) {
+
+        if (socketAddress instanceof InetSocketAddress) {
+
+            InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
+
+            return new AbstractMap.SimpleImmutableEntry<>(
+                    Integer.toString(inetSocketAddress.getPort()),
+                    inetSocketAddress.getHostString());
+
+        } else if (socketAddress instanceof LocalAddress) {
+
+            return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
+                    ((LocalAddress) socketAddress).id());
+
+        }
+        return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
 
-        return new NetconfServerSession(sessionListener, channel, getSessionPreferences().getSessionId(), parsedHeader);
     }
 
 }
diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorTest.java
new file mode 100644 (file)
index 0000000..c16046c
--- /dev/null
@@ -0,0 +1,45 @@
+package org.opendaylight.controller.netconf.impl;
+
+import io.netty.channel.local.LocalAddress;
+import org.apache.sshd.common.SshdSocketAddress;
+import org.junit.Test;
+
+import java.net.InetSocketAddress;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class NetconfServerSessionNegotiatorTest {
+
+    @Test
+    public void testGetInetSocketAddress() throws Exception {
+
+        InetSocketAddress socketAddress = new InetSocketAddress(10);
+
+        assertNotNull(NetconfServerSessionNegotiator.getHostName(socketAddress));
+
+        assertEquals(socketAddress.getHostName(),
+                NetconfServerSessionNegotiator.getHostName(socketAddress)
+                        .getValue());
+
+        socketAddress = new InetSocketAddress("TestPortInet", 20);
+
+        assertEquals(socketAddress.getHostName(),
+                NetconfServerSessionNegotiator.getHostName(socketAddress)
+                        .getValue());
+
+        assertEquals(String.valueOf(socketAddress.getPort()),
+                NetconfServerSessionNegotiator.getHostName(socketAddress)
+                        .getKey());
+
+        LocalAddress localAddress = new LocalAddress("TestPortLocal");
+
+        assertEquals(String.valueOf(localAddress.id()),
+                NetconfServerSessionNegotiator.getHostName(localAddress)
+                        .getValue());
+
+        SshdSocketAddress embeddedAddress = new SshdSocketAddress(
+                "TestSshdName", 10);
+
+    }
+}
\ No newline at end of file