2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.client;
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;
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.ChannelFuture;
27 import io.netty.channel.ChannelHandler;
28 import io.netty.channel.ChannelHandlerContext;
29 import io.netty.channel.ChannelInboundHandlerAdapter;
30 import io.netty.channel.ChannelPipeline;
31 import io.netty.channel.ChannelProgressivePromise;
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;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.internal.util.collections.Sets;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.stubbing.Answer;
45 import org.opendaylight.controller.config.util.xml.XmlUtil;
46 import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
47 import org.opendaylight.netconf.api.NetconfMessage;
48 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
49 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
50 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
51 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
52 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
53 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
54 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
55 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
56 import org.opendaylight.netconf.util.test.XmlFileLoader;
57 import org.w3c.dom.Document;
59 public class NetconfClientSessionNegotiatorTest {
61 private NetconfHelloMessage helloMessage;
62 private ChannelPipeline pipeline;
63 private ChannelFuture future;
64 private Channel channel;
65 private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
68 public void setUp() throws Exception {
69 helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional
70 .<NetconfHelloMessageAdditionalHeader>absent());
71 pipeline = mockChannelPipeline();
72 future = mockChannelFuture();
73 channel = mockChannel();
77 private static ChannelHandler mockChannelHandler() {
78 ChannelHandler handler = mock(ChannelHandler.class);
82 private Channel mockChannel() {
83 Channel ret = mock(Channel.class);
84 ChannelHandler channelHandler = mockChannelHandler();
85 doReturn("").when(ret).toString();
86 doReturn(future).when(ret).close();
87 doReturn(future).when(ret).writeAndFlush(anyObject());
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());
96 private static ChannelFuture mockChannelFuture() {
97 ChannelFuture future = mock(ChannelFuture.class);
98 doReturn(future).when(future).addListener(any(GenericFutureListener.class));
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));
110 NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
111 doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder
113 doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession
118 private void mockEventLoop() {
119 final EventLoop eventLoop = mock(EventLoop.class);
120 doReturn(eventLoop).when(channel).eventLoop();
121 doAnswer(new Answer<Void>() {
123 public Void answer(InvocationOnMock invocation) throws Throwable {
124 final Object[] args = invocation.getArguments();
125 final Runnable runnable = (Runnable) args[0];
129 }).when(eventLoop).execute(any(Runnable.class));
132 private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
133 final Promise<NetconfClientSession> promise,
134 final NetconfMessage startExi) {
135 ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
136 NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
137 doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
140 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
141 Timer timer = new HashedWheelTimer();
142 return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
145 private NetconfHelloMessage createHelloMsg(final String name) throws Exception {
146 final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
147 final Document doc = XmlUtil.readXmlToDocument(stream);
149 return new NetconfHelloMessage(doc);
152 private Set<String> createCapabilities(String name) throws Exception {
153 NetconfHelloMessage hello = createHelloMsg(name);
155 return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(hello.getDocument()));
159 public void testNetconfClientSessionNegotiator() throws Exception {
160 Promise<NetconfClientSession> promise = mock(Promise.class);
161 doReturn(promise).when(promise).setSuccess(anyObject());
162 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
164 negotiator.channelActive(null);
165 Set<String> caps = Sets.newSet("a", "b");
166 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
167 negotiator.handleMessage(helloServerMessage);
168 verify(promise).setSuccess(anyObject());
172 public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
173 Promise promise = mock(Promise.class);
174 doReturn(false).when(promise).isDone();
175 doReturn(promise).when(promise).setSuccess(anyObject());
176 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
177 Set<String> caps = Sets.newSet("a", "b");
178 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
180 negotiator.handleMessage(helloServerMessage);
181 negotiator.channelActive(null);
183 verify(promise).setSuccess(anyObject());
188 public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
189 Promise<NetconfClientSession> promise = mock(Promise.class);
190 NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id");
191 doReturn(promise).when(promise).setSuccess(anyObject());
192 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
194 negotiator.channelActive(null);
195 Set<String> caps = Sets.newSet("exi:1.0");
196 NetconfHelloMessage message = NetconfHelloMessage.createServerHello(caps, 10);
198 doAnswer(new Answer<Object>() {
200 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
201 channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
204 }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
206 ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
207 doReturn(pipeline).when(handlerContext).pipeline();
208 negotiator.handleMessage(message);
209 Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
210 channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
212 verify(promise).setSuccess(anyObject());
214 // two calls for exiMessage, 2 for hello message
215 verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
219 public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
220 Promise promise = mock(Promise.class);
221 doReturn(promise).when(promise).setSuccess(anyObject());
222 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
223 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
225 Set<String> set = createCapabilities("/helloMessage3.xml");
227 final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
228 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
230 //helloMessage2 and helloMessage3 are the same with different order
231 final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
232 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
233 final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
234 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
236 assertEquals(cachedS3, set);
237 assertNotEquals(cachedS1, set);
238 assertEquals(cachedS2, set);
239 assertEquals(cachedS3, cachedS2);
240 assertNotEquals(cachedS3, cachedS1);
241 assertNotEquals(cachedS2, cachedS1);
242 assertTrue(cachedS2 == cachedS3);