Teach netconf-client abount maximum incoming chunk size
[netconf.git] / netconf / netconf-client / src / test / java / org / opendaylight / netconf / client / NetconfClientSessionNegotiatorTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.client;
9
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;
20
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;
38 import java.util.Set;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.internal.util.collections.Sets;
42 import org.opendaylight.netconf.api.NetconfDocumentedException;
43 import org.opendaylight.netconf.api.NetconfMessage;
44 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
45 import org.opendaylight.netconf.api.xml.XmlUtil;
46 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
47 import org.opendaylight.netconf.nettyutil.handler.NetconfEXIToMessageDecoder;
48 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
49 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
50 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
51 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
52 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
53 import org.opendaylight.netconf.util.test.XmlFileLoader;
54 import org.w3c.dom.Document;
55
56 public class NetconfClientSessionNegotiatorTest {
57     private NetconfHelloMessage helloMessage;
58     private ChannelPipeline pipeline;
59     private ChannelPromise future;
60     private Channel channel;
61     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
62
63     @Before
64     public void setUp() throws Exception {
65         helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.empty());
66         pipeline = mockChannelPipeline();
67         future = mockChannelFuture();
68         channel = mockChannel();
69         mockEventLoop();
70     }
71
72     private static ChannelHandler mockChannelHandler() {
73         ChannelHandler handler = mock(ChannelHandler.class);
74         return handler;
75     }
76
77     private Channel mockChannel() {
78         Channel ret = mock(Channel.class);
79         ChannelHandler channelHandler = mockChannelHandler();
80         doReturn("").when(ret).toString();
81         doReturn(future).when(ret).newPromise();
82         doReturn(future).when(ret).close();
83         doReturn(future).when(ret).writeAndFlush(any());
84         doReturn(future).when(ret).writeAndFlush(any(), any());
85         doReturn(true).when(ret).isOpen();
86         doReturn(pipeline).when(ret).pipeline();
87         doReturn("").when(pipeline).toString();
88         doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
89         doReturn(channelHandler).when(pipeline).remove(anyString());
90         return ret;
91     }
92
93     private static ChannelPromise mockChannelFuture() {
94         ChannelPromise future = mock(ChannelPromise.class);
95         doReturn(future).when(future).addListener(any(GenericFutureListener.class));
96         return future;
97     }
98
99     private static ChannelPipeline mockChannelPipeline() {
100         ChannelPipeline pipeline = mock(ChannelPipeline.class);
101         ChannelHandler handler = mock(ChannelHandler.class);
102         doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
103         doReturn(null).when(pipeline).get(SslHandler.class);
104         doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
105         doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
106
107         NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
108         doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(),
109             any(NetconfXMLToMessageDecoder.class));
110         doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(),
111             any(NetconfClientSession.class));
112         doReturn(null).when(pipeline).replace(anyString(), anyString(),
113             any(MessageToByteEncoder.class));
114         doReturn(null).when(pipeline).replace(anyString(), anyString(),
115             any(NetconfEXIToMessageDecoder.class));
116         return pipeline;
117     }
118
119     private void mockEventLoop() {
120         final EventLoop eventLoop = mock(EventLoop.class);
121         doReturn(eventLoop).when(channel).eventLoop();
122         doAnswer(invocation -> {
123             invocation.<Runnable>getArgument(0).run();
124             return null;
125         }).when(eventLoop).execute(any(Runnable.class));
126     }
127
128     private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
129             final Promise<NetconfClientSession> promise,
130             final NetconfStartExiMessage startExi) {
131         ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
132         doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
133
134         long timeout = 10L;
135         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
136         Timer timer = new HashedWheelTimer();
137         return new NetconfClientSessionNegotiator(helloMessage, startExi, promise, channel, timer, sessionListener,
138             timeout, 16384);
139     }
140
141     private static NetconfHelloMessage createHelloMsg(final String name) throws Exception {
142         final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
143         final Document doc = XmlUtil.readXmlToDocument(stream);
144
145         return new NetconfHelloMessage(doc);
146     }
147
148     private static Set<String> createCapabilities(final String name) throws Exception {
149         NetconfHelloMessage hello = createHelloMsg(name);
150
151         return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(hello.getDocument()));
152     }
153
154     @Test
155     public void testNetconfClientSessionNegotiator() throws NetconfDocumentedException {
156         Promise<NetconfClientSession> promise = mock(Promise.class);
157         doReturn(promise).when(promise).setSuccess(any());
158         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
159
160         negotiator.channelActive(null);
161         Set<String> caps = Sets.newSet("a", "b");
162         NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
163         negotiator.handleMessage(helloServerMessage);
164         verify(promise).setSuccess(any());
165     }
166
167     @Test
168     public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
169         Promise promise = mock(Promise.class);
170         doReturn(false).when(promise).isDone();
171         doReturn(promise).when(promise).setSuccess(any());
172         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
173         Set<String> caps = Sets.newSet("a", "b");
174         NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
175
176         negotiator.handleMessage(helloServerMessage);
177         negotiator.channelActive(null);
178
179         verify(promise).setSuccess(any());
180     }
181
182     @Test
183     public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
184         Promise<NetconfClientSession> promise = mock(Promise.class);
185         NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id");
186         doReturn(promise).when(promise).setSuccess(any());
187         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
188
189         negotiator.channelActive(null);
190         Set<String> caps = Sets.newSet("exi:1.0");
191         NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
192
193         doAnswer(invocationOnMock -> {
194             channelInboundHandlerAdapter = invocationOnMock.getArgument(2);
195             return null;
196         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
197
198         ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
199         doReturn(pipeline).when(handlerContext).pipeline();
200         negotiator.handleMessage(message);
201         Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
202         channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
203
204         verify(promise).setSuccess(any());
205
206         // two calls for exiMessage, 2 for hello message
207         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
208     }
209
210     @Test
211     public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
212         Promise promise = mock(Promise.class);
213         doReturn(promise).when(promise).setSuccess(any());
214         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
215         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
216
217         Set<String> set = createCapabilities("/helloMessage3.xml");
218
219         final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
220                 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
221
222         //helloMessage2 and helloMessage3 are the same with different order
223         final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
224                 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
225         final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
226                 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
227
228         assertEquals(cachedS3, set);
229         assertNotEquals(cachedS1, set);
230         assertEquals(cachedS2, set);
231         assertEquals(cachedS3, cachedS2);
232         assertNotEquals(cachedS3, cachedS1);
233         assertNotEquals(cachedS2, cachedS1);
234         assertTrue(cachedS2 == cachedS3);
235     }
236 }