2 * Copyright (c) 2016 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.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.timeout;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
20 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
22 import com.google.common.base.Optional;
23 import io.netty.buffer.ByteBuf;
24 import io.netty.buffer.Unpooled;
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelInboundHandlerAdapter;
27 import io.netty.channel.ChannelOutboundHandler;
28 import io.netty.channel.ChannelOutboundHandlerAdapter;
29 import io.netty.channel.embedded.EmbeddedChannel;
30 import io.netty.handler.ssl.SslHandler;
31 import io.netty.util.HashedWheelTimer;
32 import io.netty.util.Timer;
33 import io.netty.util.concurrent.Future;
34 import io.netty.util.concurrent.Promise;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.opendaylight.controller.config.util.xml.XmlUtil;
45 import org.opendaylight.netconf.api.NetconfDocumentedException;
46 import org.opendaylight.netconf.api.NetconfSessionListener;
47 import org.opendaylight.netconf.api.NetconfSessionPreferences;
48 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
49 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
50 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
51 import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
52 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
53 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
54 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
55 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
56 import org.opendaylight.netconf.util.messages.FramingMechanism;
58 public class AbstractNetconfSessionNegotiatorTest {
61 private NetconfSessionListener<TestingNetconfSession> listener;
63 private Promise<TestingNetconfSession> promise;
65 private SslHandler sslHandler;
66 private EmbeddedChannel channel;
67 private AbstractNetconfSessionNegotiator negotiator;
68 private NetconfHelloMessage hello;
69 private NetconfHelloMessage helloBase11;
70 private NetconfXMLToHelloMessageDecoder xmlToHello;
71 private NetconfSessionPreferences prefs;
74 public void setUp() throws Exception {
75 MockitoAnnotations.initMocks(this);
76 channel = new EmbeddedChannel();
77 xmlToHello = new NetconfXMLToHelloMessageDecoder();
78 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, new ChannelInboundHandlerAdapter());
79 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, xmlToHello);
80 channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
81 channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
82 hello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.absent());
83 helloBase11 = NetconfHelloMessage.createClientHello(Collections.singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.absent());
84 prefs = new NetconfSessionPreferences(helloBase11);
85 doReturn(promise).when(promise).setFailure(any());
86 doReturn(promise).when(promise).setSuccess(any());
87 negotiator = new TestSessionNegotiator(prefs, promise, channel, new HashedWheelTimer(), listener, 100L);
91 public void testStartNegotiation() throws Exception {
92 negotiator.startNegotiation();
93 Assert.assertEquals(helloBase11, channel.readOutbound());
97 public void testStartNegotiationSsl() throws Exception {
98 doReturn(true).when(sslHandler).isSharable();
99 doNothing().when(sslHandler).handlerAdded(any());
100 doNothing().when(sslHandler).write(any(), any(), any());
101 final Future<EmbeddedChannel> handshakeFuture = channel.eventLoop().newSucceededFuture(channel);
102 doReturn(handshakeFuture).when(sslHandler).handshakeFuture();
103 channel.pipeline().addLast(sslHandler);
104 negotiator.startNegotiation();
105 verify(sslHandler, timeout(1000)).write(any(), eq(helloBase11), any());
110 public void testStartNegotiationNotEstablished() throws Exception {
111 final ChannelOutboundHandler closedDetector = Mockito.spy(new ChannelOutboundHandlerAdapter());
112 channel.pipeline().addLast("closedDetector", closedDetector);
113 doReturn(false).when(promise).isDone();
114 doReturn(false).when(promise).isCancelled();
115 negotiator.startNegotiation();
116 verify(closedDetector, timeout(2000)).close(any(), any());
120 public void testGetSessionPreferences() throws Exception {
121 Assert.assertEquals(prefs, negotiator.getSessionPreferences());
125 public void testGetSessionForHelloMessage() throws Exception {
126 negotiator.startNegotiation();
127 final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(hello);
128 Assert.assertNotNull(session);
129 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfEOMAggregator);
130 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof EOMFramingMechanismEncoder);
134 public void testGetSessionForHelloMessageBase11() throws Exception {
135 negotiator.startNegotiation();
136 final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloBase11);
137 Assert.assertNotNull(session);
138 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
139 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder);
143 public void testReplaceHelloMessageInboundHandler() throws Exception {
144 final List<Object> out = new ArrayList<>();
145 final byte[] msg = "<rpc/>".getBytes();
146 final ByteBuf msgBuf = Unpooled.wrappedBuffer(msg);
147 final ByteBuf helloBuf = Unpooled.wrappedBuffer(XmlUtil.toString(hello.getDocument()).getBytes());
148 negotiator.startNegotiation();
149 xmlToHello.decode(null, helloBuf, out);
150 xmlToHello.decode(null, msgBuf, out);
151 final AbstractNetconfSession session = mock(AbstractNetconfSession.class);
152 doNothing().when(session).handleMessage(any());
153 negotiator.replaceHelloMessageInboundHandler(session);
154 verify(session, times(1)).handleMessage(any());
158 public void testNegotiationFail() throws Exception {
159 negotiator.startNegotiation();
160 final RuntimeException cause = new RuntimeException("failure cause");
161 channel.pipeline().fireExceptionCaught(cause);
162 verify(promise).setFailure(cause);
165 private static class TestSessionNegotiator extends
166 AbstractNetconfSessionNegotiator<NetconfSessionPreferences,
167 TestingNetconfSession, NetconfSessionListener<TestingNetconfSession>> {
170 TestSessionNegotiator(final NetconfSessionPreferences sessionPreferences,
171 final Promise<TestingNetconfSession> promise, final Channel channel,
173 final NetconfSessionListener<TestingNetconfSession> sessionListener,
174 final long connectionTimeoutMillis) {
175 super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
179 protected TestingNetconfSession getSession(final NetconfSessionListener sessionListener, final Channel channel,
180 final NetconfHelloMessage message) throws NetconfDocumentedException {
181 return new TestingNetconfSession(sessionListener, channel, 0L);
185 protected void handleMessage(final NetconfHelloMessage netconfHelloMessage) throws Exception {