2 * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.nettyutil;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
14 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
16 import com.google.common.base.Optional;
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelInboundHandlerAdapter;
19 import io.netty.channel.embedded.EmbeddedChannel;
20 import io.netty.util.HashedWheelTimer;
21 import io.netty.util.Timer;
22 import io.netty.util.concurrent.Promise;
23 import java.util.Collections;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.netconf.api.NetconfDocumentedException;
30 import org.opendaylight.netconf.api.NetconfSessionListener;
31 import org.opendaylight.netconf.api.NetconfSessionPreferences;
32 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
33 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
34 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
35 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
36 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
37 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
38 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
39 import org.opendaylight.netconf.util.messages.FramingMechanism;
40 import org.opendaylight.netconf.util.test.XmlFileLoader;
41 import org.w3c.dom.Document;
43 public class Netconf539Test {
45 private NetconfSessionListener<TestingNetconfSession> listener;
47 private Promise<TestingNetconfSession> promise;
49 private EmbeddedChannel channel;
50 private AbstractNetconfSessionNegotiator negotiator;
51 private NetconfSessionPreferences prefs;
54 public void setUp() throws Exception {
55 MockitoAnnotations.initMocks(this);
56 channel = new EmbeddedChannel();
57 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
58 new ChannelInboundHandlerAdapter());
59 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
60 new NetconfXMLToHelloMessageDecoder());
61 channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
62 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
63 channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
64 final NetconfHelloMessage serverHello = NetconfHelloMessage.createClientHello(Collections
65 .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.absent());
66 doReturn(promise).when(promise).setFailure(any());
67 doReturn(promise).when(promise).setSuccess(any());
68 negotiator = new TestSessionNegotiator(new NetconfSessionPreferences(serverHello), promise, channel,
69 new HashedWheelTimer(), listener, 100L);
73 public void testGetSessionForHelloMessageDefaultNs() throws Exception {
74 testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml");
78 public void testGetSessionForHelloMessageNsPrefix() throws Exception {
79 testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml");
82 private void testGetSessionForHelloMessage(final String fileName) throws Exception {
83 final Document helloDocument = XmlFileLoader.xmlFileToDocument(fileName);
84 negotiator.startNegotiation();
85 final NetconfHelloMessage helloMessage = new NetconfHelloMessage(helloDocument);
86 final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage);
87 Assert.assertNotNull(session);
88 Assert.assertTrue("NetconfChunkAggregator was not installed in the Netconf pipeline",
89 channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
90 Assert.assertTrue("ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline",
91 channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder);
94 private static class TestSessionNegotiator extends
95 AbstractNetconfSessionNegotiator<NetconfSessionPreferences,
96 TestingNetconfSession, NetconfSessionListener<TestingNetconfSession>> {
99 TestSessionNegotiator(final NetconfSessionPreferences sessionPreferences,
100 final Promise<TestingNetconfSession> promise, final Channel channel,
102 final NetconfSessionListener<TestingNetconfSession> sessionListener,
103 final long connectionTimeoutMillis) {
104 super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
108 protected TestingNetconfSession getSession(final NetconfSessionListener sessionListener, final Channel channel,
109 final NetconfHelloMessage message)
110 throws NetconfDocumentedException {
111 return new TestingNetconfSession(sessionListener, channel, 0L);
115 protected void handleMessage(final NetconfHelloMessage netconfHelloMessage) throws Exception {