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