0b9bba0f6f76d9abc61bdbf33220bfe4efdfb66e
[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.controller.config.util.xml.XmlUtil;
44 import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
45 import org.opendaylight.netconf.api.NetconfDocumentedException;
46 import org.opendaylight.netconf.api.NetconfMessage;
47 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
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 testNetconfClientSessionNegotiatorWithEXI() throws Exception {
167         Promise<NetconfClientSession> promise = mock(Promise.class);
168         NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id");
169         doReturn(promise).when(promise).setSuccess(anyObject());
170         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
171
172         negotiator.channelActive(null);
173         Set<String> caps = Sets.newSet("exi:1.0");
174         NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
175
176         doAnswer(invocationOnMock -> {
177             channelInboundHandlerAdapter = (ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2];
178             return null;
179         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
180
181         ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
182         doReturn(pipeline).when(handlerContext).pipeline();
183         negotiator.handleMessage(message);
184         Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
185         channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
186
187         verify(promise).setSuccess(anyObject());
188
189         // two calls for exiMessage, 2 for hello message
190         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
191     }
192
193     @Test
194     public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
195         Promise promise = mock(Promise.class);
196         doReturn(promise).when(promise).setSuccess(anyObject());
197         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
198         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
199
200         Set<String> set = createCapabilities("/helloMessage3.xml");
201
202         final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
203                 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
204
205         //helloMessage2 and helloMessage3 are the same with different order
206         final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
207                 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
208         final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
209                 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
210
211         assertEquals(cachedS3, set);
212         assertNotEquals(cachedS1, set);
213         assertEquals(cachedS2, set);
214         assertEquals(cachedS3, cachedS2);
215         assertNotEquals(cachedS3, cachedS1);
216         assertNotEquals(cachedS2, cachedS1);
217         assertTrue(cachedS2 == cachedS3);
218     }
219 }