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
8 package org.opendaylight.netconf.nettyutil;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
13 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
15 import io.netty.channel.ChannelInboundHandlerAdapter;
16 import io.netty.channel.embedded.EmbeddedChannel;
17 import io.netty.util.HashedWheelTimer;
18 import io.netty.util.concurrent.Promise;
19 import java.util.Collections;
20 import java.util.Optional;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.netconf.api.NetconfSessionListener;
27 import org.opendaylight.netconf.api.NetconfSessionPreferences;
28 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
29 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
30 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
31 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
32 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
33 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
34 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
35 import org.opendaylight.netconf.util.messages.FramingMechanism;
36 import org.opendaylight.netconf.util.test.XmlFileLoader;
37 import org.w3c.dom.Document;
39 public class Netconf539Test {
41 private NetconfSessionListener<TestingNetconfSession> listener;
43 private Promise<TestingNetconfSession> promise;
45 private EmbeddedChannel channel;
46 private AbstractNetconfSessionNegotiator negotiator;
47 private NetconfSessionPreferences prefs;
50 public void setUp() throws Exception {
51 MockitoAnnotations.initMocks(this);
52 channel = new EmbeddedChannel();
53 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
54 new ChannelInboundHandlerAdapter());
55 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER,
56 new NetconfXMLToHelloMessageDecoder());
57 channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
58 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
59 channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
60 final NetconfHelloMessage serverHello = NetconfHelloMessage.createClientHello(Collections
61 .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.empty());
62 doReturn(promise).when(promise).setFailure(any());
63 doReturn(promise).when(promise).setSuccess(any());
64 negotiator = new TestSessionNegotiator(new NetconfSessionPreferences(serverHello), promise, channel,
65 new HashedWheelTimer(), listener, 100L);
69 public void testGetSessionForHelloMessageDefaultNs() throws Exception {
70 testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml");
74 public void testGetSessionForHelloMessageNsPrefix() throws Exception {
75 testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml");
78 private void testGetSessionForHelloMessage(final String fileName) throws Exception {
79 final Document helloDocument = XmlFileLoader.xmlFileToDocument(fileName);
80 negotiator.startNegotiation();
81 final NetconfHelloMessage helloMessage = new NetconfHelloMessage(helloDocument);
82 final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage);
83 Assert.assertNotNull(session);
84 Assert.assertTrue("NetconfChunkAggregator was not installed in the Netconf pipeline",
85 channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
86 Assert.assertTrue("ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline",
87 channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder);