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.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
28 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
29 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4BinaryBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequest;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
44 @RunWith(MockitoJUnitRunner.class)
45 public class LispXtrSouthboundHandlerTest {
47 private static final String IPV4_STRING_1 = "1.2.3.4";
48 private static final String IPV4_STRING_2 = "127.0.0.1";
49 private static final String IPV4_STRING_PREFIX = "/32";
51 private static final long NONCE = 4435248268955932168L;
52 private static final int HEADER_LENGTH = 74;
53 private static final int LISP_MAP_REQUEST_PACKET_LENGTH = 32;
54 private static final int LISP_MAP_REPLY_PACKET_LENGTH = 40;
55 private static final int PORT = 9999;
58 * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
59 * LISP(Type = 8 - Encapsulated)
60 * IP: 192.168.136.10 -> 153.16.254.1
62 * LISP Type = Map-Request (1)
65 * Nonce: 0x3d8d2acd39c8d608
68 * ITR-RLOC AFI=1 Address=192.168.136.10
69 * Record 1: 127.0.0.1/32
71 private static final String MAP_REQUEST_PACKET_STRING =
72 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
73 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
74 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
75 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
76 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
77 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 "
78 + "0060 00 02 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
80 private static final String MAP_REQUEST_PACKET_STRING_MALFORMED =
81 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
82 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
83 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
84 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
85 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
86 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 00 00 00 "
87 + "0060 00 00 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
90 * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
91 * LISP(Type = 8 - Encapsulated)
92 * IP: 192.168.136.10 -> 153.16.254.1
94 * LISP Type = Map-Reply (2)
96 * Nonce: 0x3d8d2acd39c8d608
98 * Source EID Mask Length: 32
100 * Record TTL: 32-bit Max value
102 * Locator Record 1: 127.0.0.1/32
104 private static final String MAP_REPLY_PACKET_STRING =
105 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
106 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
107 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
108 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
109 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 28 00 00 01 3d 8d "
110 + "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 "
111 + "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
112 + "0070 fe fe 0d e3 70 40";
114 private LispSouthboundPlugin lispSbPluginMock;
115 private LispXtrSouthboundHandler handler;
118 public void initTest() {
119 lispSbPluginMock = Mockito.mock(LispSouthboundPlugin.class);
120 handler = new LispXtrSouthboundHandler(lispSbPluginMock);
124 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request.
127 public void handlePacketTest_withMapRequest() throws InterruptedException {
128 final ArgumentCaptor<XtrRequestMapping> captor = ArgumentCaptor.forClass(XtrRequestMapping.class);
131 final MapRequest expectedRequest = getDefaultMapRequestBuilder().build();
133 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
134 LISP_MAP_REQUEST_PACKET_LENGTH));
135 Mockito.verify(lispSbPluginMock).sendNotificationIfPossible(captor.capture());
137 assertEquals(expectedRequest, captor.getValue().getMapRequest());
141 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, no Itr Rlocs.
143 @Test(expected = LispMalformedPacketException.class)
144 public void handlePacketTest__withMapRequest_withNoItrRloc() throws InterruptedException {
145 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING_MALFORMED, HEADER_LENGTH,
146 LISP_MAP_REQUEST_PACKET_LENGTH));
150 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply.
153 public void handlePacketTest_withMapReply() throws InterruptedException {
154 ArgumentCaptor<XtrReplyMapping> captor = ArgumentCaptor.forClass(XtrReplyMapping.class);
155 handler.handlePacket(extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH,
156 LISP_MAP_REPLY_PACKET_LENGTH));
158 Mockito.verify(lispSbPluginMock).sendNotificationIfPossible(captor.capture());
159 assertNotNull(captor.getValue().getMapReply());
163 * Tests {@link LispXtrSouthboundHandler#channelReadComplete} method.
166 public void channelReadCompleteTest() throws Exception {
167 ChannelHandlerContext ctxMock = Mockito.mock(ChannelHandlerContext.class);
168 handler.channelReadComplete(ctxMock);
170 Mockito.verify(ctxMock).flush();
174 * Following test is executed for coverage-increase purpose only.
177 public void otherTest() throws Exception {
179 // This map-notification packet is not valid! Don't use it anywhere else.
180 String mapNotificationPacket =
181 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
182 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
183 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
184 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
185 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 40 00 00 01 3d 8d "
186 + "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 "
187 + "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
188 + "0070 fe fe 0d e3 70 40";
190 handler.exceptionCaught(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Throwable.class));
191 handler.handlePacket(extractLispPacket(mapNotificationPacket, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
194 private static DatagramPacket extractLispPacket(String packetString, int headerLength, int lispPacketLength) {
195 final String[] tokens = packetString.split("\\s+");
196 ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
198 for (String token : tokens) {
199 if (token.length() == 2) {
200 buffer.put((byte) Integer.parseInt(token, 16));
204 byte[] result = Arrays.copyOfRange(buffer.array(),
205 headerLength, headerLength + lispPacketLength);
206 final InetAddress inetAddress = null;
207 final InetSocketAddress recipient = new InetSocketAddress(PORT);
208 final InetSocketAddress sender = new InetSocketAddress(inetAddress, PORT);
209 return new DatagramPacket(Unpooled.copiedBuffer(result), recipient, sender);
212 private static MapRequestBuilder getDefaultMapRequestBuilder() {
213 final ItrRloc itrRloc = new ItrRlocBuilder()
214 .setRloc(new RlocBuilder()
215 .setAddressType(Ipv4BinaryAfi.class)
216 .setAddress(new Ipv4BinaryBuilder()
217 .setIpv4Binary(new Ipv4AddressBinary(new byte[]{127, 0, 0, 2})).build())
221 final EidItem eidItem = new EidItemBuilder()
222 .setEid(LispAddressUtil.asIpv4PrefixBinaryEid(IPV4_STRING_2 + IPV4_STRING_PREFIX)).build();
224 return new MapRequestBuilder()
225 .setItrRloc(Lists.newArrayList(itrRloc))
226 .setEidItem(Lists.newArrayList(eidItem))
228 .setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(IPV4_STRING_1)).build())
229 .setAuthoritative(false)
230 .setMapDataPresent(false)
234 .setSmrInvoked(false);