Integrate MRI projects for Neon
[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
9 package org.opendaylight.netconf.client;
10
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;
21
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;
39 import java.util.Set;
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;
57
58 public class NetconfClientSessionNegotiatorTest {
59
60     private NetconfHelloMessage helloMessage;
61     private ChannelPipeline pipeline;
62     private ChannelPromise future;
63     private Channel channel;
64     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
65
66     @Before
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();
72         mockEventLoop();
73     }
74
75     private static ChannelHandler mockChannelHandler() {
76         ChannelHandler handler = mock(ChannelHandler.class);
77         return handler;
78     }
79
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());
93         return ret;
94     }
95
96     private static ChannelPromise mockChannelFuture() {
97         ChannelPromise future = mock(ChannelPromise.class);
98         doReturn(future).when(future).addListener(any(GenericFutureListener.class));
99         return future;
100     }
101
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));
109
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));
119         return pipeline;
120     }
121
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();
127             return null;
128         }).when(eventLoop).execute(any(Runnable.class));
129     }
130
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));
137
138         long timeout = 10L;
139         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
140         Timer timer = new HashedWheelTimer();
141         return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
142     }
143
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);
147
148         return new NetconfHelloMessage(doc);
149     }
150
151     private static Set<String> createCapabilities(final String name) throws Exception {
152         NetconfHelloMessage hello = createHelloMsg(name);
153
154         return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(hello.getDocument()));
155     }
156
157     @Test
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);
162
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());
168     }
169
170     @Test
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);
178
179         negotiator.handleMessage(helloServerMessage);
180         negotiator.channelActive(null);
181
182         verify(promise).setSuccess(any());
183     }
184
185
186     @Test
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);
192
193         negotiator.channelActive(null);
194         Set<String> caps = Sets.newSet("exi:1.0");
195         NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
196
197         doAnswer(invocationOnMock -> {
198             channelInboundHandlerAdapter = invocationOnMock.getArgument(2);
199             return null;
200         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
201
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));
207
208         verify(promise).setSuccess(any());
209
210         // two calls for exiMessage, 2 for hello message
211         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
212     }
213
214     @Test
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);
220
221         Set<String> set = createCapabilities("/helloMessage3.xml");
222
223         final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
224                 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
225
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();
231
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);
239     }
240 }