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