Move NetconfMessage into netconf.api.messages
[netconf.git] / protocol / 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 package org.opendaylight.netconf.client;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyString;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20
21 import com.google.common.collect.ImmutableSet;
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelHandler;
24 import io.netty.channel.ChannelHandlerContext;
25 import io.netty.channel.ChannelInboundHandlerAdapter;
26 import io.netty.channel.ChannelPipeline;
27 import io.netty.channel.ChannelProgressivePromise;
28 import io.netty.channel.ChannelPromise;
29 import io.netty.channel.EventLoop;
30 import io.netty.handler.codec.MessageToByteEncoder;
31 import io.netty.handler.ssl.SslHandler;
32 import io.netty.util.HashedWheelTimer;
33 import io.netty.util.Timer;
34 import io.netty.util.concurrent.GenericFutureListener;
35 import io.netty.util.concurrent.Promise;
36 import java.io.InputStream;
37 import java.util.Optional;
38 import java.util.Set;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.opendaylight.netconf.api.messages.HelloMessage;
42 import org.opendaylight.netconf.api.messages.NetconfMessage;
43 import org.opendaylight.netconf.api.xml.XmlUtil;
44 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
45 import org.opendaylight.netconf.nettyutil.handler.NetconfEXIToMessageDecoder;
46 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
47 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
48 import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
49 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
50 import org.opendaylight.netconf.test.util.XmlFileLoader;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53 import org.w3c.dom.Document;
54
55 public class NetconfClientSessionNegotiatorTest {
56     private HelloMessage helloMessage;
57     private ChannelPipeline pipeline;
58     private ChannelPromise future;
59     private Channel channel;
60     private ChannelInboundHandlerAdapter channelInboundHandlerAdapter;
61
62     @Before
63     public void setUp() {
64         helloMessage = HelloMessage.createClientHello(Set.of("exi:1.0"), Optional.empty());
65         pipeline = mockChannelPipeline();
66         future = mockChannelFuture();
67         channel = mockChannel();
68         mockEventLoop();
69     }
70
71     private static ChannelHandler mockChannelHandler() {
72         ChannelHandler handler = mock(ChannelHandler.class);
73         return handler;
74     }
75
76     private Channel mockChannel() {
77         Channel ret = mock(Channel.class);
78         ChannelHandler channelHandler = mockChannelHandler();
79         doReturn("").when(ret).toString();
80         doReturn(future).when(ret).newPromise();
81         doReturn(future).when(ret).close();
82         doReturn(future).when(ret).writeAndFlush(any());
83         doReturn(future).when(ret).writeAndFlush(any(), any());
84         doReturn(true).when(ret).isOpen();
85         doReturn(pipeline).when(ret).pipeline();
86         doReturn("").when(pipeline).toString();
87         doReturn(pipeline).when(pipeline).remove(any(ChannelHandler.class));
88         doReturn(channelHandler).when(pipeline).remove(anyString());
89         return ret;
90     }
91
92     private static ChannelPromise mockChannelFuture() {
93         ChannelPromise future = mock(ChannelPromise.class);
94         doReturn(future).when(future).addListener(any(GenericFutureListener.class));
95         return future;
96     }
97
98     private static ChannelPipeline mockChannelPipeline() {
99         ChannelPipeline pipeline = mock(ChannelPipeline.class);
100         ChannelHandler handler = mock(ChannelHandler.class);
101         doReturn(pipeline).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
102         doReturn(null).when(pipeline).get(SslHandler.class);
103         doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
104         doReturn(handler).when(pipeline).replace(anyString(), anyString(), any(ChunkedFramingMechanismEncoder.class));
105
106         NetconfXMLToHelloMessageDecoder messageDecoder = new NetconfXMLToHelloMessageDecoder();
107         doReturn(messageDecoder).when(pipeline).replace(anyString(), anyString(),
108             any(NetconfXMLToMessageDecoder.class));
109         doReturn(pipeline).when(pipeline).replace(any(ChannelHandler.class), anyString(),
110             any(NetconfClientSession.class));
111         doReturn(null).when(pipeline).replace(anyString(), anyString(),
112             any(MessageToByteEncoder.class));
113         doReturn(null).when(pipeline).replace(anyString(), anyString(),
114             any(NetconfEXIToMessageDecoder.class));
115         return pipeline;
116     }
117
118     private void mockEventLoop() {
119         final EventLoop eventLoop = mock(EventLoop.class);
120         doReturn(eventLoop).when(channel).eventLoop();
121         doAnswer(invocation -> {
122             invocation.<Runnable>getArgument(0).run();
123             return null;
124         }).when(eventLoop).execute(any(Runnable.class));
125     }
126
127     private NetconfClientSessionNegotiator createNetconfClientSessionNegotiator(
128             final Promise<NetconfClientSession> promise,
129             final NetconfStartExiMessage startExi) {
130         ChannelProgressivePromise progressivePromise = mock(ChannelProgressivePromise.class);
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(helloMessage, startExi, promise, channel, timer, sessionListener,
137             timeout, 16384);
138     }
139
140     private static HelloMessage createHelloMsg(final String name) throws Exception {
141         final InputStream stream = NetconfClientSessionNegotiatorTest.class.getResourceAsStream(name);
142         return new HelloMessage(XmlUtil.readXmlToDocument(stream));
143     }
144
145     private static Set<String> createCapabilities(final String name) throws Exception {
146         return ImmutableSet.copyOf(NetconfMessageUtil.extractCapabilitiesFromHello(createHelloMsg(name).getDocument()));
147     }
148
149     @Test
150     public void testNetconfClientSessionNegotiator() throws Exception {
151         Promise<NetconfClientSession> promise = mock(Promise.class);
152         doReturn(promise).when(promise).setSuccess(any());
153         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
154
155         negotiator.channelActive(null);
156         doReturn(null).when(future).cause();
157         negotiator.handleMessage(HelloMessage.createServerHello(Set.of("a", "b"), new SessionIdType(Uint32.TEN)));
158         verify(promise).setSuccess(any());
159     }
160
161     @Test
162     public void testNegotiatorWhenChannelActiveHappenAfterHandleMessage() throws Exception {
163         Promise<NetconfClientSession> promise = mock(Promise.class);
164         doReturn(false).when(promise).isDone();
165         doReturn(promise).when(promise).setSuccess(any());
166         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
167
168         doReturn(null).when(future).cause();
169         negotiator.handleMessage(HelloMessage.createServerHello(Set.of("a", "b"), new SessionIdType(Uint32.TEN)));
170         negotiator.channelActive(null);
171         verify(promise).setSuccess(any());
172     }
173
174     @Test
175     public void testNetconfClientSessionNegotiatorWithEXI() throws Exception {
176         Promise<NetconfClientSession> promise = mock(Promise.class);
177         NetconfStartExiMessage exiMessage = NetconfStartExiMessage.create(EXIParameters.empty(), "msg-id");
178         doReturn(promise).when(promise).setSuccess(any());
179         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, exiMessage);
180
181         doReturn(null).when(future).cause();
182         negotiator.channelActive(null);
183
184         doAnswer(invocationOnMock -> {
185             channelInboundHandlerAdapter = invocationOnMock.getArgument(2);
186             return null;
187         }).when(pipeline).addAfter(anyString(), anyString(), any(ChannelHandler.class));
188
189         ChannelHandlerContext handlerContext = mock(ChannelHandlerContext.class);
190         doReturn(pipeline).when(handlerContext).pipeline();
191         negotiator.handleMessage(HelloMessage.createServerHello(Set.of("exi:1.0"), new SessionIdType(Uint32.TEN)));
192         Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
193         channelInboundHandlerAdapter.channelRead(handlerContext, new NetconfMessage(expectedResult));
194
195         verify(promise).setSuccess(any());
196
197         // two calls for exiMessage, 2 for hello message
198         verify(pipeline, times(4)).replace(anyString(), anyString(), any(ChannelHandler.class));
199     }
200
201     @Test
202     public void testNetconfClientSessionNegotiatorGetCached() throws Exception {
203         Promise<NetconfClientSession> promise = mock(Promise.class);
204         doReturn(promise).when(promise).setSuccess(any());
205         NetconfClientSessionListener sessionListener = mock(NetconfClientSessionListener.class);
206         NetconfClientSessionNegotiator negotiator = createNetconfClientSessionNegotiator(promise, null);
207
208         Set<String> set = createCapabilities("/helloMessage3.xml");
209
210         final Set<String> cachedS1 = (Set<String>) negotiator.getSession(sessionListener, channel,
211                 createHelloMsg("/helloMessage1.xml")).getServerCapabilities();
212
213         //helloMessage2 and helloMessage3 are the same with different order
214         final Set<String> cachedS2 = (Set<String>) negotiator.getSession(sessionListener, channel,
215                 createHelloMsg("/helloMessage2.xml")).getServerCapabilities();
216         final Set<String> cachedS3 = (Set<String>) negotiator.getSession(sessionListener, channel,
217                 createHelloMsg("/helloMessage3.xml")).getServerCapabilities();
218
219         assertEquals(cachedS3, set);
220         assertNotEquals(cachedS1, set);
221         assertEquals(cachedS2, set);
222         assertEquals(cachedS3, cachedS2);
223         assertNotEquals(cachedS3, cachedS1);
224         assertNotEquals(cachedS2, cachedS1);
225         assertTrue(cachedS2 == cachedS3);
226     }
227 }