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