2 * Copyright (c) 2014 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.client;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyString;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
21 import com.google.common.collect.ImmutableSet;
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelHandler;
24 import io.netty.channel.ChannelHandlerContext;
25 import io.netty.channel.ChannelInboundHandlerAdapter;
26 import io.netty.channel.ChannelPipeline;
27 import io.netty.channel.ChannelProgressivePromise;
28 import io.netty.channel.ChannelPromise;
29 import io.netty.channel.EventLoop;
30 import io.netty.handler.codec.MessageToByteEncoder;
31 import io.netty.handler.ssl.SslHandler;
32 import io.netty.util.HashedWheelTimer;
33 import io.netty.util.Timer;
34 import io.netty.util.concurrent.GenericFutureListener;
35 import io.netty.util.concurrent.Promise;
36 import java.io.InputStream;
37 import java.util.Optional;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.internal.util.collections.Sets;
42 import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
43 import org.opendaylight.netconf.api.NetconfDocumentedException;
44 import org.opendaylight.netconf.api.NetconfMessage;
45 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
46 import org.opendaylight.netconf.api.xml.XmlUtil;
47 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
48 import org.opendaylight.netconf.nettyutil.handler.NetconfEXIToMessageDecoder;
49 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
50 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
51 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
52 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
53 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
54 import org.opendaylight.netconf.util.test.XmlFileLoader;
55 import org.w3c.dom.Document;
57 public class NetconfClientSessionNegotiatorTest {
59 private NetconfHelloMessage helloMessage;
60 private ChannelPipeline pipeline;
61 private ChannelPromise future;
62 private Channel channel;
63 private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
66 public void setUp() throws Exception {
67 helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.empty());
68 pipeline = mockChannelPipeline();
69 future = mockChannelFuture();
70 channel = mockChannel();
74 private static ChannelHandler mockChannelHandler() {
75 ChannelHandler handler = mock(ChannelHandler.class);
79 private Channel mockChannel() {
80 Channel ret = mock(Channel.class);
81 ChannelHandler channelHandler = mockChannelHandler();
82 doReturn("").when(ret).toString();
83 doReturn(future).when(ret).newPromise();
84 doReturn(future).when(ret).close();
85 doReturn(future).when(ret).writeAndFlush(any());
86 doReturn(future).when(ret).writeAndFlush(any(), any());
87 doReturn(true).when(ret).isOpen();
88 doReturn(pipeline).when(ret).pipeline();
89 doReturn("").when(pipeline).toString();
90 doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
91 doReturn(channelHandler).when(pipeline).remove(anyString());
95 private static ChannelPromise mockChannelFuture() {
96 ChannelPromise future = mock(ChannelPromise.class);
97 doReturn(future).when(future).addListener(any(GenericFutureListener.class));
101 private static ChannelPipeline mockChannelPipeline() {
102 ChannelPipeline pipeline = mock(ChannelPipeline.class);
103 ChannelHandler handler = mock(ChannelHandler.class);
104 doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
105 doReturn(null).when(pipeline).get(SslHandler.class);
106 doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
107 doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
109 NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
110 doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(),
111 any(NetconfXMLToMessageDecoder.class));
112 doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(),
113 any(NetconfClientSession.class));
114 doReturn(null).when(pipeline).replace(anyString(), anyString(),
115 any(MessageToByteEncoder.class));
116 doReturn(null).when(pipeline).replace(anyString(), anyString(),
117 any(NetconfEXIToMessageDecoder.class));
121 private void mockEventLoop() {
122 final EventLoop eventLoop = mock(EventLoop.class);
123 doReturn(eventLoop).when(channel).eventLoop();
124 doAnswer(invocation -> {
125 invocation.<Runnable>getArgument(0).run();
127 }).when(eventLoop).execute(any(Runnable.class));
130 private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
131 final Promise<NetconfClientSession> promise,
132 final NetconfMessage startExi) {
133 ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
134 NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
135 doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
138 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
139 Timer timer = new HashedWheelTimer();
140 return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
143 private static NetconfHelloMessage createHelloMsg(final String name) throws Exception {
144 final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
145 final Document doc = XmlUtil.readXmlToDocument(stream);
147 return new NetconfHelloMessage(doc);
150 private static Set<String> createCapabilities(final String name) throws Exception {
151 NetconfHelloMessage hello = createHelloMsg(name);
153 return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(hello.getDocument()));
157 public void testNetconfClientSessionNegotiator() throws NetconfDocumentedException {
158 Promise<NetconfClientSession> promise = mock(Promise.class);
159 doReturn(promise).when(promise).setSuccess(any());
160 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
162 negotiator.channelActive(null);
163 Set<String> caps = Sets.newSet("a", "b");
164 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
165 negotiator.handleMessage(helloServerMessage);
166 verify(promise).setSuccess(any());
170 public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
171 Promise promise = mock(Promise.class);
172 doReturn(false).when(promise).isDone();
173 doReturn(promise).when(promise).setSuccess(any());
174 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
175 Set<String> caps = Sets.newSet("a", "b");
176 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
178 negotiator.handleMessage(helloServerMessage);
179 negotiator.channelActive(null);
181 verify(promise).setSuccess(any());
186 public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
187 Promise<NetconfClientSession> promise = mock(Promise.class);
188 NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id");
189 doReturn(promise).when(promise).setSuccess(any());
190 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
192 negotiator.channelActive(null);
193 Set<String> caps = Sets.newSet("exi:1.0");
194 NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
196 doAnswer(invocationOnMock -> {
197 channelInboundHandlerAdapter = invocationOnMock.getArgument(2);
199 }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
201 ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
202 doReturn(pipeline).when(handlerContext).pipeline();
203 negotiator.handleMessage(message);
204 Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
205 channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
207 verify(promise).setSuccess(any());
209 // two calls for exiMessage, 2 for hello message
210 verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
214 public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
215 Promise promise = mock(Promise.class);
216 doReturn(promise).when(promise).setSuccess(any());
217 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
218 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
220 Set<String> set = createCapabilities("/helloMessage3.xml");
222 final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
223 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
225 //helloMessage2 and helloMessage3 are the same with different order
226 final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
227 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
228 final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
229 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
231 assertEquals(cachedS3, set);
232 assertNotEquals(cachedS1, set);
233 assertEquals(cachedS2, set);
234 assertEquals(cachedS3, cachedS2);
235 assertNotEquals(cachedS3, cachedS1);
236 assertNotEquals(cachedS2, cachedS1);
237 assertTrue(cachedS2 == cachedS3);