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