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.controller.netconf.client;
11 import com.google.common.base.Optional;
12 import io.netty.channel.*;
13 import io.netty.handler.ssl.SslHandler;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.concurrent.GenericFutureListener;
16 import io.netty.util.concurrent.Promise;
17 import org.apache.mina.handler.demux.ExceptionHandler;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.mockito.internal.util.collections.Sets;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24 import org.opendaylight.controller.netconf.api.NetconfClientSessionPreferences;
25 import org.opendaylight.controller.netconf.api.NetconfMessage;
26 import io.netty.util.Timer;
27 import org.opendaylight.controller.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
28 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
29 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
30 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
31 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
32 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
33 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
34 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
35 import org.openexi.proc.common.EXIOptions;
36 import org.w3c.dom.Document;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.anyObject;
40 import static org.mockito.Matchers.anyString;
41 import static org.mockito.Mockito.*;
43 public class NetconfClientSessionNegotiatorTest {
45 private NetconfHelloMessage helloMessage;
46 private ChannelPipeline pipeline;
47 private ChannelFuture future;
48 private Channel channel;
49 private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
52 public void setUp() throws Exception {
53 helloMessage = NetconfHelloMessage.createClientHello(Sets.newSet("exi:1.0"), Optional.<NetconfHelloMessageAdditionalHeader>absent());
54 pipeline = mockChannelPipeline();
55 future = mockChannelFuture();
56 channel = mockChannel();
57 System.out.println("setup done");
60 private ChannelHandler mockChannelHandler() {
61 ChannelHandler handler = mock(ChannelHandler.class);
65 private Channel mockChannel() {
66 Channel channel = mock(Channel.class);
67 ChannelHandler channelHandler = mockChannelHandler();
68 doReturn("").when(channel).toString();
69 doReturn(future).when(channel).close();
70 doReturn(future).when(channel).writeAndFlush(anyObject());
71 doReturn(true).when(channel).isOpen();
72 doReturn(pipeline).when(channel).pipeline();
73 doReturn("").when(pipeline).toString();
74 doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
75 doReturn(channelHandler).when(pipeline).remove(anyString());
79 private ChannelFuture mockChannelFuture() {
80 ChannelFuture future = mock(ChannelFuture.class);
81 doReturn(future).when(future).addListener(any(GenericFutureListener.class));
85 private ChannelPipeline mockChannelPipeline() {
86 ChannelPipeline pipeline = mock(ChannelPipeline.class);
87 ChannelHandler handler = mock(ChannelHandler.class);
88 doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
89 doReturn(null).when(pipeline).get(SslHandler.class);
90 doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
91 doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
93 NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
94 doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(), any(NetconfXMLToMessageDecoder.class));
95 doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(), any(NetconfClientSession.class));
99 private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(Promise promise,
100 NetconfMessage startExi) {
101 ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
102 NetconfClientSessionPreferences preferences = new NetconfClientSessionPreferences(helloMessage, startExi);
103 doReturn(progressivePromise).when(promise).setFailure(any(Throwable.class));
106 NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
107 Timer timer = new HashedWheelTimer();
108 return new NetconfClientSessionNegotiator(preferences, promise, channel, timer, sessionListener, timeout);
112 public void testNetconfClientSessionNegotiator() throws Exception {
113 Promise promise = mock(Promise.class);
114 doReturn(promise).when(promise).setSuccess(anyObject());
115 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
117 negotiator.channelActive(null);
118 Set caps = Sets.newSet("a", "b");
119 NetconfHelloMessage helloServerMessage = NetconfHelloMessage.createServerHello(caps, 10);
120 negotiator.handleMessage(helloServerMessage);
121 verify(promise).setSuccess(anyObject());
125 public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
126 Promise promise = mock(Promise.class);
127 EXIOptions exiOptions = new EXIOptions();
128 NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(exiOptions, "msg-id");
129 doReturn(promise).when(promise).setSuccess(anyObject());
130 NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
132 negotiator.channelActive(null);
133 Set caps = Sets.newSet("exi:1.0");
134 NetconfHelloMessage helloMessage = NetconfHelloMessage.createServerHello(caps, 10);
136 doAnswer(new Answer() {
138 public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
139 channelInboundHandlerAdapter = ((ChannelInboundHandlerAdapter) invocationOnMock.getArguments()[2]);
142 }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
144 ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
145 doReturn(pipeline).when(handlerContext).pipeline();
146 negotiator.handleMessage(helloMessage);
147 Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
148 channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
150 verify(promise).setSuccess(anyObject());
152 // two calls for exiMessage, 2 for hello message
153 verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));