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.LispAddressStringifier;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
29 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
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.container.Eid;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequest;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
49 @RunWith(MockitoJUnitRunner.class)
50 public class LispXtrSouthboundHandlerTest {
52 private static final String IPV4_STRING_1 = "1.2.3.4";
53 private static final String IPV4_STRING_2 = "127.0.0.1";
54 private static final String IPV4_STRING_PREFIX = "/32";
56 private static final long NONCE = 4435248268955932168L;
57 private static final int HEADER_LENGTH = 74;
58 private static final int LISP_MAP_REQUEST_PACKET_LENGTH = 32;
59 private static final int LISP_MAP_REPLY_PACKET_LENGTH = 40;
60 private static final int PORT = 9999;
63 * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
64 * LISP(Type = 8 - Encapsulated)
65 * IP: 192.168.136.10 -> 153.16.254.1
67 * LISP Type = Map-Request (1)
70 * Nonce: 0x3d8d2acd39c8d608
73 * ITR-RLOC AFI=1 Address=192.168.136.10
74 * Record 1: 127.0.0.1/32
76 private static final String MAP_REQUEST_PACKET_STRING =
77 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
78 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
79 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
80 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
81 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
82 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 "
83 + "0060 00 02 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
85 private static final String MAP_REQUEST_PACKET_STRING_MALFORMED =
86 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
87 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
88 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
89 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
90 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
91 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 00 00 00 "
92 + "0060 00 00 00 20 00 01 7f 00 00 01 ac 4a 06 7d";
95 * SRC: 127.0.0.1:58560 to 127.0.0.1:4342
96 * LISP(Type = 8 - Encapsulated)
97 * IP: 192.168.136.10 -> 153.16.254.1
99 * LISP Type = Map-Reply (2)
101 * Nonce: 0x3d8d2acd39c8d608
103 * Source EID Mask Length: 32
105 * Record TTL: 32-bit Max value
107 * Locator Record 1: 127.0.0.1/32
109 private static final String MAP_REPLY_PACKET_STRING =
110 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
111 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
112 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
113 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
114 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 28 00 00 01 3d 8d "
115 + "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 "
116 + "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
117 + "0070 fe fe 0d e3 70 40";
119 private LispSouthboundPlugin lispSbPluginMock;
120 private LispXtrSouthboundHandler handler;
123 public void initTest() {
124 lispSbPluginMock = Mockito.mock(LispSouthboundPlugin.class);
125 handler = new LispXtrSouthboundHandler(lispSbPluginMock);
129 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request.
132 public void handlePacketTest_withMapRequest() throws InterruptedException {
133 final ArgumentCaptor<XtrRequestMapping> captor = ArgumentCaptor.forClass(XtrRequestMapping.class);
136 final MapRequest expectedRequest = getDefaultMapRequestBuilder().build();
138 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
139 LISP_MAP_REQUEST_PACKET_LENGTH));
140 Mockito.verify(lispSbPluginMock).sendNotificationIfPossible(captor.capture());
142 assertEquals(expectedRequest, captor.getValue().getMapRequest());
146 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, no Itr Rlocs.
148 @Test(expected = LispMalformedPacketException.class)
149 public void handlePacketTest__withMapRequest_withNoItrRloc() throws InterruptedException {
150 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING_MALFORMED, HEADER_LENGTH,
151 LISP_MAP_REQUEST_PACKET_LENGTH));
155 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply.
158 public void handlePacketTest_withMapReply() throws InterruptedException {
159 ArgumentCaptor<XtrReplyMapping> captor = ArgumentCaptor.forClass(XtrReplyMapping.class);
160 handler.handlePacket(extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH,
161 LISP_MAP_REPLY_PACKET_LENGTH));
163 Mockito.verify(lispSbPluginMock).sendNotificationIfPossible(captor.capture());
164 assertNotNull(captor.getValue().getMapReply());
168 * Tests {@link LispXtrSouthboundHandler#channelReadComplete} method.
171 public void channelReadCompleteTest() throws Exception {
172 ChannelHandlerContext ctxMock = Mockito.mock(ChannelHandlerContext.class);
173 handler.channelReadComplete(ctxMock);
175 Mockito.verify(ctxMock).flush();
179 * Following test is executed for coverage-increase purpose only.
182 public void otherTest() throws Exception {
184 // This map-notification packet is not valid! Don't use it anywhere else.
185 String mapNotificationPacket =
186 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 "
187 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
188 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
189 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
190 + "0040 fe 01 dd b4 10 f6 00 24 ef 3a 40 00 00 01 3d 8d "
191 + "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 "
192 + "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
193 + "0070 fe fe 0d e3 70 40";
195 handler.exceptionCaught(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Throwable.class));
196 handler.handlePacket(extractLispPacket(mapNotificationPacket, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
199 private static DatagramPacket extractLispPacket(String packetString, int headerLength, int lispPacketLength) {
200 final String[] tokens = packetString.split("\\s+");
201 ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
203 for (String token : tokens) {
204 if (token.length() == 2) {
205 buffer.put((byte) Integer.parseInt(token, 16));
209 byte[] result = Arrays.copyOfRange(buffer.array(),
210 headerLength, headerLength + lispPacketLength);
211 final InetAddress inetAddress = null;
212 final InetSocketAddress recipient = new InetSocketAddress(PORT);
213 final InetSocketAddress sender = new InetSocketAddress(inetAddress, PORT);
214 return new DatagramPacket(Unpooled.copiedBuffer(result), recipient, sender);
217 private static MapRequestBuilder getDefaultMapRequestBuilder() {
218 final Rloc rloc = new RlocBuilder()
219 .setAddressType(Ipv4BinaryAfi.class)
220 .setAddress(new Ipv4BinaryBuilder()
221 .setIpv4Binary(new Ipv4AddressBinary(new byte[]{127, 0, 0, 2})).build())
223 final ItrRloc itrRloc = new ItrRlocBuilder()
224 .withKey(new ItrRlocKey(LispAddressStringifier.getString(rloc)))
225 .setRloc(rloc).build();
227 final Eid eid = LispAddressUtil.asIpv4PrefixBinaryEid(IPV4_STRING_2 + IPV4_STRING_PREFIX);
228 final EidItem eidItem = new EidItemBuilder()
229 .withKey(new EidItemKey(IPV4_STRING_2 + IPV4_STRING_PREFIX))
230 .setEid(eid).build();
232 return new MapRequestBuilder()
233 .setItrRloc(Lists.newArrayList(itrRloc))
234 .setEidItem(Lists.newArrayList(eidItem))
236 .setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(IPV4_STRING_1)).build())
237 .setAuthoritative(false)
238 .setMapDataPresent(false)
242 .setSmrInvoked(false);