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