From 9d9e45067bab572cbaf79587ed2b8bc20ec39d7a Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Wed, 25 Apr 2018 09:32:57 +0200 Subject: [PATCH] NETCONF-539: use netconf namespace in base:1.1 capability check Use Document.getElementsByTagNameNSgetElementsByTagNameNS with urn:ietf:params:xml:ns:netconf:base:1.0 namespace instead of getElementsByTagName to support clients using namespace prefixes in the hello message. Change-Id: I333bb4522aca566b74db3e4411be1c90ec419aec Signed-off-by: Marek Gradzki --- netconf/netconf-netty-util/pom.xml | 8 ++ .../AbstractNetconfSessionNegotiator.java | 4 +- .../netconf/nettyutil/Netconf539Test.java | 119 ++++++++++++++++++ .../resources/netconf539/client_hello_1.1.xml | 13 ++ .../netconf539/client_hello_1.1_ns.xml | 13 ++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/Netconf539Test.java create mode 100644 netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1.xml create mode 100644 netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1_ns.xml diff --git a/netconf/netconf-netty-util/pom.xml b/netconf/netconf-netty-util/pom.xml index d3dca9abdf..eb338a6749 100644 --- a/netconf/netconf-netty-util/pom.xml +++ b/netconf/netconf-netty-util/pom.xml @@ -85,6 +85,14 @@ aaa-encrypt-service 0.8.0-SNAPSHOT + + + + ${project.groupId} + netconf-util + test-jar + test + diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java index 30b8114604..4135896b34 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java @@ -248,7 +248,9 @@ public abstract class AbstractNetconfSessionNegotiator

listener; + @Mock + private Promise promise; + + private EmbeddedChannel channel; + private AbstractNetconfSessionNegotiator negotiator; + private NetconfSessionPreferences prefs; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + channel = new EmbeddedChannel(); + channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, + new ChannelInboundHandlerAdapter()); + channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, + new NetconfXMLToHelloMessageDecoder()); + channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, + FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM)); + channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator()); + final NetconfHelloMessage serverHello = NetconfHelloMessage.createClientHello(Collections + .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.absent()); + doReturn(promise).when(promise).setFailure(any()); + doReturn(promise).when(promise).setSuccess(any()); + negotiator = new TestSessionNegotiator(new NetconfSessionPreferences(serverHello), promise, channel, + new HashedWheelTimer(), listener, 100L); + } + + @Test + public void testGetSessionForHelloMessageDefaultNs() throws Exception { + testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml"); + } + + @Test + public void testGetSessionForHelloMessageNsPrefix() throws Exception { + testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml"); + } + + private void testGetSessionForHelloMessage(final String fileName) throws Exception { + final Document helloDocument = XmlFileLoader.xmlFileToDocument(fileName); + negotiator.startNegotiation(); + final NetconfHelloMessage helloMessage = new NetconfHelloMessage(helloDocument); + final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage); + Assert.assertNotNull(session); + Assert.assertTrue("NetconfChunkAggregator was not installed in the Netconf pipeline", + channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator); + Assert.assertTrue("ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline", + channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder); + } + + private static class TestSessionNegotiator extends + AbstractNetconfSessionNegotiator> { + + + TestSessionNegotiator(final NetconfSessionPreferences sessionPreferences, + final Promise promise, final Channel channel, + final Timer timer, + final NetconfSessionListener sessionListener, + final long connectionTimeoutMillis) { + super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis); + } + + @Override + protected TestingNetconfSession getSession(final NetconfSessionListener sessionListener, final Channel channel, + final NetconfHelloMessage message) + throws NetconfDocumentedException { + return new TestingNetconfSession(sessionListener, channel, 0L); + } + + @Override + protected void handleMessage(final NetconfHelloMessage netconfHelloMessage) throws Exception { + + } + } +} \ No newline at end of file diff --git a/netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1.xml b/netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1.xml new file mode 100644 index 0000000000..8c43f33b30 --- /dev/null +++ b/netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1.xml @@ -0,0 +1,13 @@ + + + + + urn:ietf:params:netconf:base:1.1 + + \ No newline at end of file diff --git a/netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1_ns.xml b/netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1_ns.xml new file mode 100644 index 0000000000..8a2d34743a --- /dev/null +++ b/netconf/netconf-netty-util/src/test/resources/netconf539/client_hello_1.1_ns.xml @@ -0,0 +1,13 @@ + + + + + urn:ietf:params:netconf:base:1.1 + + \ No newline at end of file -- 2.36.6