Bug 6351 - Clustering rework.
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPluginTest.java
1 /*
2  * Copyright (c) 2016 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.lispflowmapping.southbound;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13
14 import io.netty.bootstrap.Bootstrap;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.EventLoopGroup;
17 import io.netty.channel.socket.DatagramPacket;
18 import io.netty.channel.socket.nio.NioDatagramChannel;
19 import java.lang.reflect.Field;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.net.UnknownHostException;
23 import java.nio.ByteBuffer;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mockito;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
32 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
33 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv6AddressBinary;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(NioDatagramChannel.class)
47 public class LispSouthboundPluginTest {
48
49     private static NioDatagramChannel channel;
50     private static NioDatagramChannel xtrChannel;
51     private static LispSouthboundPlugin lispSouthboundPlugin;
52     private static final Bootstrap BOOTSTRAP_MOCK = Mockito.mock(Bootstrap.class);
53
54     private static final String LISP_MAP_REQUEST_PACKET_STRING =
55             "10 00 00 01 3d 8d 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 00 02 00 20 00 01 7f 00 00 01";
56     private static final String ADDRESS_1 = "0.0.0.0";
57     private static final String ADDRESS_2 = "1.1.1.1";
58     private static final ByteBuffer PACKET = parseHexString(LISP_MAP_REQUEST_PACKET_STRING);
59     private static final int PORT = 9999;
60     private static final byte[] IPV4_BYTES = new byte[]{1, 2, 3, 4};
61     private static final byte[] IPV6_BYTES = new byte[]{11, 11, 22, 22, 33, 33, 44, 44, 55, 55, 66, 66, 77, 77, 88, 88};
62     private static final IpAddressBinary IPV4_BINARY = new IpAddressBinary(new Ipv4AddressBinary(IPV4_BYTES));
63     private static final IpAddressBinary IPV6_BINARY = new IpAddressBinary(new Ipv6AddressBinary(IPV6_BYTES));
64     private static final TransportAddress TRANSPORT_ADDRESS_IPV4 = new TransportAddressBuilder()
65             .setIpAddress(IPV4_BINARY)
66             .setPort(new PortNumber(PORT)).build();
67     private static final TransportAddress TRANSPORT_ADDRESS_IPV6 = new TransportAddressBuilder()
68             .setIpAddress(IPV6_BINARY)
69             .setPort(new PortNumber(PORT)).build();
70
71     @Before
72     public void init() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
73         lispSouthboundPlugin = new LispSouthboundPlugin(
74                 Mockito.mock(DataBroker.class),
75                 Mockito.mock(NotificationPublishService.class),
76                 Mockito.mock(ClusterSingletonServiceProvider.class));
77         lispSouthboundPlugin.setBindingAddress(ADDRESS_1);
78         lispSouthboundPlugin.setMapRegisterCacheEnabled(false);
79
80         channel = PowerMockito.mock(NioDatagramChannel.class);
81         xtrChannel = PowerMockito.mock(NioDatagramChannel.class);
82         injectChannel();
83         injectXtrChannel();
84     }
85
86     /**
87      * Tests {@link LispSouthboundPlugin#handleSerializedLispBuffer} method with ipv4.
88      */
89     @Test
90     public void handleSerializedLispBufferTest_withIpv4() throws
91             NoSuchFieldException, IllegalAccessException, UnknownHostException {
92         final ArgumentCaptor<DatagramPacket> captor = ArgumentCaptor.forClass(DatagramPacket.class);
93         final InetAddress address = InetAddress.getByAddress(IPV4_BINARY.getIpv4AddressBinary().getValue());
94         final InetSocketAddress inetSocketAddress = new InetSocketAddress(address, PORT);
95
96         // Ensures that NPE is not thrown.
97         Mockito.when(channel.write(Mockito.any())).thenReturn(Mockito.mock(ChannelFuture.class));
98
99         lispSouthboundPlugin.handleSerializedLispBuffer(TRANSPORT_ADDRESS_IPV4, PACKET, MessageType.MapRequest);
100         Mockito.verify(channel).write(captor.capture());
101         Mockito.verify(channel).flush();
102
103         final DatagramPacket result = captor.getValue();
104         assertArrayEquals(PACKET.array(), result.content().array());
105         assertEquals(inetSocketAddress, result.recipient());
106     }
107
108     /**
109      * Tests {@link LispSouthboundPlugin#handleSerializedLispBuffer} method with ipv6.
110      */
111     @Test
112     public void handleSerializedLispBufferTest_withIpv6() throws
113             NoSuchFieldException, IllegalAccessException, UnknownHostException {
114         final ArgumentCaptor<DatagramPacket> captor = ArgumentCaptor.forClass(DatagramPacket.class);
115         final InetAddress address = InetAddress.getByAddress(IPV6_BINARY.getIpv6AddressBinary().getValue());
116         final InetSocketAddress inetSocketAddress = new InetSocketAddress(address, PORT);
117
118         // Ensures that NPE is not thrown.
119         Mockito.when(channel.write(Mockito.any())).thenReturn(Mockito.mock(ChannelFuture.class));
120
121         lispSouthboundPlugin.handleSerializedLispBuffer(TRANSPORT_ADDRESS_IPV6, PACKET, MessageType.MapRequest);
122         Mockito.verify(channel).write(captor.capture());
123         Mockito.verify(channel).flush();
124
125         final DatagramPacket result = captor.getValue();
126         assertArrayEquals(PACKET.array(), result.content().array());
127         assertEquals(inetSocketAddress, result.recipient());
128     }
129
130     /**
131      * Tests {@link LispSouthboundPlugin#setLispAddress} method - binding address has changed.
132      */
133     @Test
134     public void setLispAddressTest_withEqualAddress() throws NoSuchFieldException, IllegalAccessException {
135         injectField("bootstrap", BOOTSTRAP_MOCK);
136         lispSouthboundPlugin.setLispAddress(ADDRESS_2);
137
138         Mockito.verify(BOOTSTRAP_MOCK).bind(ADDRESS_2, LispMessage.PORT_NUM);
139         Mockito.verify(channel).close();
140     }
141
142     /**
143      * Tests {@link LispSouthboundPlugin#setLispAddress} method - binding address has not changed.
144      */
145     @Test
146     public void setLispAddressTest_withChangedAddress() throws NoSuchFieldException, IllegalAccessException {
147         injectField("bootstrap", BOOTSTRAP_MOCK);
148         lispSouthboundPlugin.setLispAddress(ADDRESS_1);
149
150         Mockito.verifyZeroInteractions(BOOTSTRAP_MOCK);
151         Mockito.verifyZeroInteractions(channel);
152     }
153
154     /**
155      * Tests {@link LispSouthboundPlugin#shouldListenOnXtrPort} method, shouldListenOnXtrPort == true.
156      */
157     @Test
158     public void shouldListenOnXtrPortTest_true() throws NoSuchFieldException, IllegalAccessException {
159         lispSouthboundPlugin.shouldListenOnXtrPort(true);
160
161         Mockito.verify(xtrChannel).close();
162     }
163
164     /**
165      * Tests {@link LispSouthboundPlugin#shouldListenOnXtrPort} method, shouldListenOnXtrPort == false.
166      */
167     @Test
168     public void shouldListenOnXtrPortTest_false() throws NoSuchFieldException, IllegalAccessException {
169         lispSouthboundPlugin.shouldListenOnXtrPort(false);
170
171         Mockito.verifyZeroInteractions(xtrChannel);
172     }
173
174     /**
175      * Tests {@link LispSouthboundPlugin#setXtrPort} method.
176      */
177     @Test
178     public void setXtrPortTest() throws NoSuchFieldException, IllegalAccessException {
179         lispSouthboundPlugin.shouldListenOnXtrPort(true);
180         lispSouthboundPlugin.setXtrPort(PORT);
181
182         Mockito.verify(xtrChannel, Mockito.times(2)).close();
183         assertEquals(PORT, (int) LispSouthboundPluginTest.<Integer>getField("xtrPort"));
184     }
185
186     /**
187      * Tests {@link LispSouthboundPlugin#close} method.
188      */
189     @Test
190     @SuppressWarnings("unchecked")
191     public void closeTest() throws Exception {
192         EventLoopGroup elgMock = Mockito.mock(EventLoopGroup.class);
193         LispSouthboundPluginTest.injectField("eventLoopGroup", elgMock);
194
195         LispSouthboundHandler handlerMock = Mockito.mock(LispSouthboundHandler.class);
196         LispSouthboundPluginTest.injectField("lispSouthboundHandler", handlerMock);
197         Mockito.when(channel.close()).thenReturn(Mockito.mock(ChannelFuture.class));
198
199         lispSouthboundPlugin.close();
200
201         Mockito.verify(channel).close();
202         Mockito.verify(elgMock).shutdownGracefully();
203         Mockito.verify(handlerMock).close();
204         assertNull(getField("lispSouthboundHandler"));
205         assertNull(getField("lispXtrSouthboundHandler"));
206         assertNull(getField("channel"));
207     }
208
209     private static void injectChannel() throws NoSuchFieldException, IllegalAccessException {
210         final Field channelField = LispSouthboundPlugin.class.getDeclaredField("channel");
211         channelField.setAccessible(true);
212         channelField.set(lispSouthboundPlugin, channel);
213     }
214
215     private static void injectXtrChannel() throws NoSuchFieldException, IllegalAccessException {
216         final Field xtrChannelField = LispSouthboundPlugin.class.getDeclaredField("xtrChannel");
217         xtrChannelField.setAccessible(true);
218         xtrChannelField.set(lispSouthboundPlugin, xtrChannel);
219     }
220
221     private static ByteBuffer parseHexString(String packet) {
222         final String[] tokens = packet.split("\\s+");
223         final ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
224         for (String token : tokens) {
225             buffer.put((byte) Integer.parseInt(token, 16));
226         }
227
228         return buffer;
229     }
230
231     private static <T> void injectField(String fieldName, T obj) throws NoSuchFieldException, IllegalAccessException {
232         Field field = LispSouthboundPlugin.class.getDeclaredField(fieldName);
233         field.setAccessible(true);
234         field.set(lispSouthboundPlugin, obj);
235     }
236
237     @SuppressWarnings("unchecked")
238     private static <T> T getField(String fieldName) throws NoSuchFieldException, IllegalAccessException {
239         Field field = LispSouthboundPlugin.class.getDeclaredField(fieldName);
240         field.setAccessible(true);
241
242         return (T) field.get(lispSouthboundPlugin);
243     }
244 }