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.concurrent.GenericFutureListener;
33 import io.netty.util.concurrent.Promise;
34 import java.io.InputStream;
35 import java.util.Optional;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.opendaylight.netconf.api.messages.HelloMessage;
40 import org.opendaylight.netconf.api.messages.NetconfMessage;
41 import org.opendaylight.netconf.api.messages.RpcMessage;
42 import org.opendaylight.netconf.api.xml.XmlUtil;
43 import org.opendaylight.netconf.common.impl.DefaultNetconfTimer;
44 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
45 import org.opendaylight.netconf.nettyutil.handler.NetconfEXIToMessageDecoder;
46 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
47 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
48 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
49 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessageProvider;
50 import org.opendaylight.netconf.test.util.XmlFileLoader;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53 import org.w3c.dom.Document;
55 public class NetconfClientSessionNegotiatorTest {
56 private HelloMessage helloMessage;
57 private ChannelPipeline pipeline;
58 private ChannelPromise future;
59 private Channel channel;
60 private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
64 helloMessage = HelloMessage.createClientHello(Set.of("exi:1.0"), Optional.empty());
65 pipeline = mockChannelPipeline();
66 future = mockChannelFuture();
67 channel = mockChannel();
71 private static ChannelHandler mockChannelHandler() {
72 ChannelHandler handler = mock(ChannelHandler.class);
76 private Channel mockChannel() {
77 Channel ret = mock(Channel.class);
78 ChannelHandler channelHandler = mockChannelHandler();
79 doReturn("").when(ret).toString();
80 doReturn(future).when(ret).newPromise();
81 doReturn(future).when(ret).close();
82 doReturn(future).when(ret).writeAndFlush(any());
83 doReturn(future).when(ret).writeAndFlush(any(), any());
84 doReturn(true).when(ret).isOpen();
85 doReturn(pipeline).when(ret).pipeline();
86 doReturn("").when(pipeline).toString();
87 doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
88 doReturn(channelHandler).when(pipeline).remove(anyString());
92 private static ChannelPromise mockChannelFuture() {
93 ChannelPromise future = mock(ChannelPromise.class);
94 doReturn(future).when(future).addListener(any(GenericFutureListener.class));
98 private static ChannelPipeline mockChannelPipeline() {
99 ChannelPipeline pipeline = mock(ChannelPipeline.class);
100 ChannelHandler handler = mock(ChannelHandler.class);
101 doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
102 doReturn(null).when(pipeline).get(SslHandler.class);
103 doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
104 doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
106 NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
107 doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(),
108 any(NetconfXMLToMessageDecoder.class));
109 doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(),
110 any(NetconfClientSession.class));
111 doReturn(null).when(pipeline).replace(anyString(), anyString(),
112 any(MessageToByteEncoder.class));
113 doReturn(null).when(pipeline).replace(anyString(), anyString(),
114 any(NetconfEXIToMessageDecoder.class));
118 private void mockEventLoop() {
119 final EventLoop eventLoop = mock(EventLoop.class);
120 doReturn(eventLoop).when(channel).eventLoop();
121 doAnswer(invocation -> {
122 invocation.<Runnable>getArgument(0).run();
124 }).when(eventLoop).execute(any(Runnable.class));
127 private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
128 final Promise<NetconfClientSession> promise,
129 final RpcMessage startExi) {
130 ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
131 doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
134 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
135 var timer = new DefaultNetconfTimer();
136 return new NetconfClientSessionNegotiator(helloMessage, startExi, promise, channel, timer, sessionListener,
140 private static HelloMessage createHelloMsg(final String name) throws Exception {
141 final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
142 return new HelloMessage(XmlUtil.readXmlToDocument(stream));
145 private static Set<String> createCapabilities(final String name) throws Exception {
146 return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(createHelloMsg(name).getDocument()));
150 public void testNetconfClientSessionNegotiator() throws Exception {
151 Promise<NetconfClientSession> promise = mock(Promise.class);
152 doReturn(promise).when(promise).setSuccess(any());
153 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
155 negotiator.channelActive(null);
156 doReturn(null).when(future).cause();
157 negotiator.handleMessage(HelloMessage.createServerHello(Set.of("a", "b"), new SessionIdType(Uint32.TEN)));
158 verify(promise).setSuccess(any());
162 public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
163 Promise<NetconfClientSession> promise = mock(Promise.class);
164 doReturn(false).when(promise).isDone();
165 doReturn(promise).when(promise).setSuccess(any());
166 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
168 doReturn(null).when(future).cause();
169 negotiator.handleMessage(HelloMessage.createServerHello(Set.of("a", "b"), new SessionIdType(Uint32.TEN)));
170 negotiator.channelActive(null);
171 verify(promise).setSuccess(any());
175 public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
176 Promise<NetconfClientSession> promise = mock(Promise.class);
177 RpcMessage exiMessage = NetconfStartExiMessageProvider.create(EXIParameters.empty(), "msg-id");
178 doReturn(promise).when(promise).setSuccess(any());
179 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
181 doReturn(null).when(future).cause();
182 negotiator.channelActive(null);
184 doAnswer(invocationOnMock -> {
185 channelInboundHandlerAdapter = invocationOnMock.getArgument(2);
187 }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
189 ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
190 doReturn(pipeline).when(handlerContext).pipeline();
191 negotiator.handleMessage(HelloMessage.createServerHello(Set.of("exi:1.0"), new SessionIdType(Uint32.TEN)));
192 Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
193 channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
195 verify(promise).setSuccess(any());
197 // two calls for exiMessage, 2 for hello message
198 verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
202 public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
203 Promise<NetconfClientSession> promise = mock(Promise.class);
204 doReturn(promise).when(promise).setSuccess(any());
205 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
206 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
208 Set<String> set = createCapabilities("/helloMessage3.xml");
210 final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
211 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
213 //helloMessage2 and helloMessage3 are the same with different order
214 final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
215 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
216 final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
217 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
219 assertEquals(cachedS3, set);
220 assertNotEquals(cachedS1, set);
221 assertEquals(cachedS2, set);
222 assertEquals(cachedS3, cachedS2);
223 assertNotEquals(cachedS3, cachedS1);
224 assertNotEquals(cachedS2, cachedS1);
225 assertTrue(cachedS2 == cachedS3);