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.InetSocketAddress;
18 import java.nio.ByteBuffer;
19 import java.util.Arrays;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
29 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
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
34 .address.Ipv4BinaryBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
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.maprequest.ItrRloc;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequest;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
46 @RunWith(MockitoJUnitRunner.class)
47 public class LispXtrSouthboundHandlerTest {
49 @Mock(name = "notificationPublishService") private static NotificationPublishService notificationPublishServiceMock;
50 @InjectMocks private static LispXtrSouthboundHandler handler;
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";
120 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request.
122 * @throws InterruptedException
125 public void handlePacketTest_withMapRequest() throws InterruptedException {
126 final ArgumentCaptor<XtrRequestMapping> captor = ArgumentCaptor.forClass(XtrRequestMapping.class);
129 final MapRequest expectedRequest = getDefaultMapRequestBuilder().build();
131 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
132 LISP_MAP_REQUEST_PACKET_LENGTH));
133 Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
135 assertEquals(expectedRequest, captor.getValue().getMapRequest());
139 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, null NotificationPublishService.
141 * @throws InterruptedException
144 public void handlePacketTest_withMapRequest_withNullNotifPublishService() throws InterruptedException {
145 final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
146 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
147 LISP_MAP_REQUEST_PACKET_LENGTH));
148 Mockito.verifyZeroInteractions(notificationPublishServiceMock);
152 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, no Itr Rlocs.
154 * @throws InterruptedException
156 @Test(expected = LispMalformedPacketException.class)
157 public void handlePacketTest__withMapRequest_withNoItrRloc() throws InterruptedException {
158 handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING_MALFORMED, HEADER_LENGTH,
159 LISP_MAP_REQUEST_PACKET_LENGTH));
163 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply.
165 * @throws InterruptedException
168 public void handlePacketTest_withMapReply() throws InterruptedException {
169 ArgumentCaptor<XtrReplyMapping> captor = ArgumentCaptor.forClass(XtrReplyMapping.class);
170 handler.handlePacket(extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH,
171 LISP_MAP_REPLY_PACKET_LENGTH));
173 Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
174 assertNotNull(captor.getValue().getMapReply());
178 * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply over channelRead0 method.
183 public void handlePacketTest_withMapReply_withNullNotifPublishService() throws Exception {
184 final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
185 handler.channelRead0(Mockito.mock(ChannelHandlerContext.class),
186 extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
188 Mockito.verifyZeroInteractions(notificationPublishServiceMock);
192 * Tests {@link LispXtrSouthboundHandler#channelReadComplete} method.
197 public void channelReadCompleteTest() throws Exception {
198 ChannelHandlerContext ctxMock = Mockito.mock(ChannelHandlerContext.class);
199 handler.channelReadComplete(ctxMock);
201 Mockito.verify(ctxMock).flush();
205 * Following test is executed for coverage-increase purpose only.
208 public void otherTest() throws Exception {
210 // This map-notification packet is not valid! Don't use it anywhere else.
211 String mapNotificationPacket =
212 "0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " +
213 "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " +
214 "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 " +
215 "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 " +
216 "0040 fe 01 dd b4 10 f6 00 24 ef 3a 40 00 00 01 3d 8d " +
217 "0050 2a cd 39 c8 d6 08 ff ff ff ff 01 20 10 00 00 00 " +
218 "0060 00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe " +
219 "0070 fe fe 0d e3 70 40";
221 handler.exceptionCaught(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Throwable.class));
222 handler.setNotificationProvider(notificationPublishServiceMock);
223 handler.handlePacket(extractLispPacket(mapNotificationPacket, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
226 private static DatagramPacket extractLispPacket(String packetString, int headerLength, int lispPacketLength) {
227 final String[] tokens = packetString.split("\\s+");
228 ByteBuffer buffer = ByteBuffer.allocate(tokens.length);
230 for (String token : tokens) {
231 if (token.length() == 2) {
232 buffer.put((byte) Integer.parseInt(token, 16));
236 byte[] result = Arrays.copyOfRange(buffer.array(),
237 headerLength, headerLength + lispPacketLength);
238 final InetSocketAddress address = new InetSocketAddress(PORT);
239 return new DatagramPacket(Unpooled.copiedBuffer(result), address);
242 private static MapRequestBuilder getDefaultMapRequestBuilder() {
243 final ItrRloc itrRloc = new ItrRlocBuilder()
244 .setRloc(new RlocBuilder()
245 .setAddressType(Ipv4BinaryAfi.class)
246 .setAddress(new Ipv4BinaryBuilder()
247 .setIpv4Binary(new Ipv4AddressBinary(new byte[]{127, 0, 0, 2})).build())
251 final EidItem eidItem = new EidItemBuilder()
252 .setEid(LispAddressUtil.toEid(new Ipv4Prefix(IPV4_STRING_2 + IPV4_STRING_PREFIX), null)).build();
254 return new MapRequestBuilder()
255 .setItrRloc(Lists.newArrayList(itrRloc))
256 .setEidItem(Lists.newArrayList(eidItem))
258 .setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(IPV4_STRING_1)).build())
259 .setAuthoritative(false)
260 .setMapDataPresent(false)
264 .setSmrInvoked(false);