Merge "fix failure during connecting device when channelActive happens later than...
[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 junit.framework.Assert.assertEquals;
12 import static junit.framework.Assert.assertTrue;
13 import static org.junit.Assert.assertNotEquals;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyObject;
16 import static org.mockito.Matchers.anyString;
17 import static org.mockito.Mockito.doAnswer;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22
23 import com.google.common.base.Optional;
24 import com.google.common.collect.ImmutableSet;
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelHandler;
27 import io.netty.channel.ChannelHandlerContext;
28 import io.netty.channel.ChannelInboundHandlerAdapter;
29 import io.netty.channel.ChannelPipeline;
30 import io.netty.channel.ChannelProgressivePromise;
31 import io.netty.channel.ChannelPromise;
32 import io.netty.channel.EventLoop;
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.NetconfXMLToHelloMessageDecoder;
50 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
51 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
52 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
53 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
54 import org.opendaylight.netconf.util.test.XmlFileLoader;
55 import org.w3c.dom.Document;
56
57 public class NetconfClientSessionNegotiatorTest {
58
59     private NetconfHelloMessage helloMessage;
60     private ChannelPipeline pipeline;
61     private ChannelPromise future;
62     private Channel channel;
63     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
64
65     @Before
66     public void setUp() throws Exception {
67         helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.absent());
68         pipeline = mockChannelPipeline();
69         future = mockChannelFuture();
70         channel = mockChannel();
71         mockEventLoop();
72     }
73
74     private static ChannelHandler mockChannelHandler() {
75         ChannelHandler handler = mock(ChannelHandler.class);
76         return handler;
77     }
78
79     private Channel mockChannel() {
80         Channel ret = mock(Channel.class);
81         ChannelHandler channelHandler = mockChannelHandler();
82         doReturn("").when(ret).toString();
83         doReturn(future).when(ret).newPromise();
84         doReturn(future).when(ret).close();
85         doReturn(future).when(ret).writeAndFlush(anyObject());
86         doReturn(future).when(ret).writeAndFlush(anyObject(), anyObject());
87         doReturn(true).when(ret).isOpen();
88         doReturn(pipeline).when(ret).pipeline();
89         doReturn("").when(pipeline).toString();
90         doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
91         doReturn(channelHandler).when(pipeline).remove(anyString());
92         return ret;
93     }
94
95     private static ChannelPromise mockChannelFuture() {
96         ChannelPromise future = mock(ChannelPromise.class);
97         doReturn(future).when(future).addListener(any(GenericFutureListener.class));
98         return future;
99     }
100
101     private static ChannelPipeline mockChannelPipeline() {
102         ChannelPipeline pipeline = mock(ChannelPipeline.class);
103         ChannelHandler handler = mock(ChannelHandler.class);
104         doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
105         doReturn(null).when(pipeline).get(SslHandler.class);
106         doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
107         doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
108
109         NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
110         doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder
111                 .class));
112         doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession
113                 .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.getArgumentAt(0, Runnable.class).run();
122             return null;
123         }).when(eventLoop).execute(any(Runnable.class));
124     }
125
126     private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
127             final Promise<NetconfClientSession> promise,
128             final NetconfMessage startExi) {
129         ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
130         NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
131         doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
132
133         long timeout = 10L;
134         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
135         Timer timer = new HashedWheelTimer();
136         return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
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 NetconfDocumentedException {
154         Promise<NetconfClientSession> promise = mock(Promise.class);
155         doReturn(promise).when(promise).setSuccess(anyObject());
156         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
157
158         negotiator.channelActive(null);
159         Set<String> caps = Sets.newSet("a", "b");
160         NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
161         negotiator.handleMessage(helloServerMessage);
162         verify(promise).setSuccess(anyObject());
163     }
164
165     @Test
166     public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
167         Promise promise = mock(Promise.class);
168         doReturn(false).when(promise).isDone();
169         doReturn(promise).when(promise).setSuccess(anyObject());
170         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
171         Set<String> caps = Sets.newSet("a", "b");
172         NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
173
174         negotiator.handleMessage(helloServerMessage);
175         negotiator.channelActive(null);
176
177         verify(promise).setSuccess(anyObject());
178     }
179
180
181     @Test
182     public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
183         Promise<NetconfClientSession> promise = mock(Promise.class);
184         NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id");
185         doReturn(promise).when(promise).setSuccess(anyObject());
186         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
187
188         negotiator.channelActive(null);
189         Set<String> caps = Sets.newSet("exi:1.0");
190         NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
191
192         doAnswer(invocationOnMock -> {
193             channelInboundHandlerAdapter = (ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2];
194             return null;
195         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
196
197         ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
198         doReturn(pipeline).when(handlerContext).pipeline();
199         negotiator.handleMessage(message);
200         Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
201         channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
202
203         verify(promise).setSuccess(anyObject());
204
205         // two calls for exiMessage, 2 for hello message
206         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
207     }
208
209     @Test
210     public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
211         Promise promise = mock(Promise.class);
212         doReturn(promise).when(promise).setSuccess(anyObject());
213         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
214         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
215
216         Set<String> set = createCapabilities("/helloMessage3.xml");
217
218         final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
219                 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
220
221         //helloMessage2 and helloMessage3 are the same with different order
222         final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
223                 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
224         final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
225                 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
226
227         assertEquals(cachedS3, set);
228         assertNotEquals(cachedS1, set);
229         assertEquals(cachedS2, set);
230         assertEquals(cachedS3, cachedS2);
231         assertNotEquals(cachedS3, cachedS1);
232         assertNotEquals(cachedS2, cachedS1);
233         assertTrue(cachedS2 == cachedS3);
234     }
235 }