2 * Copyright (c) 2016 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
8 package org.opendaylight.lispflowmapping.southbound.lisp;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.Unpooled;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.socket.DatagramPacket;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import java.nio.ByteBuffer;
20 import java.util.Arrays;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
29 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
30 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4BinaryBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequest;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
45 @RunWith(MockitoJUnitRunner.class)
46 public class LispXtrSouthboundHandlerTest {
48 @Mock(name = "notificationPublishService") private static NotificationPublishService notificationPublishServiceMock;
49 @InjectMocks private static LispXtrSouthboundHandler handler;
51 private static final String IPV4_STRING_1 = "1.2.3.4";
52 private static final String IPV4_STRING_2 = "127.0.0.1";
53 private static final String IPV4_STRING_PREFIX = "/32";
55 private static final long NONCE = 4435248268955932168L;
56 private static final int HEADER_LENGTH = 74;
57 private static final int LISP_MAP_REQUEST_PACKET_LENGTH = 32;
58 private static final int LISP_MAP_REPLY_PACKET_LENGTH = 40;
59 private static final int PORT = 9999;
62 * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
63 * LISP(Type = 8 - Encapsulated)
64 * IP: 192.168.136.10 -> 153.16.254.1
66 * LISP Type = Map-Request (1)
69 * Nonce: 0x3d8d2acd39c8d608
72 * ITR-RLOC AFI=1 Address=192.168.136.10
73 * Record 1: 127.0.0.1/32
75 private static final String MAP_REQUEST_PACKET_STRING =
76 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
77 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
78 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
79 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
80 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
81 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 "
82 + "0060 00 02 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
84 private static final String MAP_REQUEST_PACKET_STRING_MALFORMED =
85 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
86 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
87 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
88 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
89 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
90 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 00 00 00 "
91 + "0060 00 00 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
94 * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
95 * LISP(Type = 8 - Encapsulated)
96 * IP: 192.168.136.10 -> 153.16.254.1
98 * LISP Type = Map-Reply (2)
100 * Nonce: 0x3d8d2acd39c8d608
102 * Source EID Mask Length: 32
104 * Record TTL: 32-bit Max value
106 * Locator Record 1: 127.0.0.1/32
108 private static final String MAP_REPLY_PACKET_STRING =
109 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
110 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
111 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
112 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
113 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 28 00 00 01 3d 8d "
114 + "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 "
115 + "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
116 + "0070 fe fe 0d e3 70 40";
119 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request.
122 public void handlePacketTest_withMapRequest() throws InterruptedException {
123 final ArgumentCaptor<XtrRequestMapping> captor = ArgumentCaptor.forClass(XtrRequestMapping.class);
126 final MapRequest expectedRequest = getDefaultMapRequestBuilder().build();
128 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
129 LISP_MAP_REQUEST_PACKET_LENGTH));
130 Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
132 assertEquals(expectedRequest, captor.getValue().getMapRequest());
136 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, null NotificationPublishService.
139 public void handlePacketTest_withMapRequest_withNullNotifPublishService() throws InterruptedException {
140 final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
141 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
142 LISP_MAP_REQUEST_PACKET_LENGTH));
143 Mockito.verifyZeroInteractions(notificationPublishServiceMock);
147 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, no Itr Rlocs.
149 @Test(expected = LispMalformedPacketException.class)
150 public void handlePacketTest__withMapRequest_withNoItrRloc() throws InterruptedException {
151 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING_MALFORMED, HEADER_LENGTH,
152 LISP_MAP_REQUEST_PACKET_LENGTH));
156 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply.
159 public void handlePacketTest_withMapReply() throws InterruptedException {
160 ArgumentCaptor<XtrReplyMapping> captor = ArgumentCaptor.forClass(XtrReplyMapping.class);
161 handler.handlePacket(extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH,
162 LISP_MAP_REPLY_PACKET_LENGTH));
164 Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
165 assertNotNull(captor.getValue().getMapReply());
169 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply over channelRead0 method.
172 public void handlePacketTest_withMapReply_withNullNotifPublishService() throws Exception {
173 final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
174 handler.channelRead0(Mockito.mock(ChannelHandlerContext.class),
175 extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
177 Mockito.verifyZeroInteractions(notificationPublishServiceMock);
181 * Tests {@link LispXtrSouthboundHandler#channelReadComplete} method.
184 public void channelReadCompleteTest() throws Exception {
185 ChannelHandlerContext ctxMock = Mockito.mock(ChannelHandlerContext.class);
186 handler.channelReadComplete(ctxMock);
188 Mockito.verify(ctxMock).flush();
192 * Following test is executed for coverage-increase purpose only.
195 public void otherTest() throws Exception {
197 // This map-notification packet is not valid! Don't use it anywhere else.
198 String mapNotificationPacket =
199 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
200 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
201 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
202 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
203 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 40 00 00 01 3d 8d "
204 + "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 "
205 + "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
206 + "0070 fe fe 0d e3 70 40";
208 handler.exceptionCaught(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Throwable.class));
209 handler.setNotificationProvider(notificationPublishServiceMock);
210 handler.handlePacket(extractLispPacket(mapNotificationPacket, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
213 private static DatagramPacket extractLispPacket(String packetString, int headerLength, int lispPacketLength) {
214 final String[] tokens = packetString.split("\\s+");
215 ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
217 for (String token : tokens) {
218 if (token.length() == 2) {
219 buffer.put((byte) Integer.parseInt(token, 16));
223 byte[] result = Arrays.copyOfRange(buffer.array(),
224 headerLength, headerLength + lispPacketLength);
225 final InetAddress inetAddress = null;
226 final InetSocketAddress recipient = new InetSocketAddress(PORT);
227 final InetSocketAddress sender = new InetSocketAddress(inetAddress, PORT);
228 return new DatagramPacket(Unpooled.copiedBuffer(result), recipient, sender);
231 private static MapRequestBuilder getDefaultMapRequestBuilder() {
232 final ItrRloc itrRloc = new ItrRlocBuilder()
233 .setRloc(new RlocBuilder()
234 .setAddressType(Ipv4BinaryAfi.class)
235 .setAddress(new Ipv4BinaryBuilder()
236 .setIpv4Binary(new Ipv4AddressBinary(new byte[]{127, 0, 0, 2})).build())
240 final EidItem eidItem = new EidItemBuilder()
241 .setEid(LispAddressUtil.asIpv4PrefixBinaryEid(IPV4_STRING_2 + IPV4_STRING_PREFIX)).build();
243 return new MapRequestBuilder()
244 .setItrRloc(Lists.newArrayList(itrRloc))
245 .setEidItem(Lists.newArrayList(eidItem))
247 .setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(IPV4_STRING_1)).build())
248 .setAuthoritative(false)
249 .setMapDataPresent(false)
253 .setSmrInvoked(false);