2 * Copyright (c) 2014 Contextream, 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.integrationtest;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.ops4j.pax.exam.CoreOptions.options;
16 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.io.OutputStreamWriter;
21 import java.net.DatagramPacket;
22 import java.net.DatagramSocket;
23 import java.net.HttpURLConnection;
24 import java.net.InetAddress;
25 import java.net.InetSocketAddress;
26 import java.net.MalformedURLException;
27 import java.net.SocketException;
28 import java.net.SocketTimeoutException;
30 import java.net.UnknownHostException;
31 import java.nio.ByteBuffer;
32 import java.nio.charset.Charset;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.concurrent.TimeUnit;
37 import javax.inject.Inject;
39 import org.apache.commons.codec.binary.Base64;
40 import org.codehaus.jettison.json.JSONException;
41 import org.codehaus.jettison.json.JSONObject;
42 import org.codehaus.jettison.json.JSONTokener;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.opendaylight.controller.sal.binding.api.NotificationListener;
48 import org.opendaylight.lispflowmapping.clusterdao.ClusterDAOService;
49 import org.opendaylight.lispflowmapping.implementation.LispMappingService;
50 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
51 import org.opendaylight.lispflowmapping.implementation.serializer.MapNotifySerializer;
52 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
53 import org.opendaylight.lispflowmapping.implementation.serializer.MapReplySerializer;
54 import org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer;
55 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
56 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
57 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
58 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
59 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
60 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
61 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.EidToLocatorRecord.Action;
62 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafApplicationDataAddress;
63 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafKeyValueAddress;
64 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafListAddress;
65 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafSegmentAddress;
66 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafSourceDestAddress;
67 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafTrafficEngineeringAddress;
68 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
69 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispIpv4Address;
70 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispMacAddress;
71 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
72 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
73 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
74 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
75 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.ReencapHop;
76 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.XtrRequestMapping;
77 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
78 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
79 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
80 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
81 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafkeyvalueaddress.KeyBuilder;
82 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafkeyvalueaddress.ValueBuilder;
83 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.Addresses;
84 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.AddressesBuilder;
85 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafsegmentaddress.AddressBuilder;
86 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafsourcedestaddress.DstAddressBuilder;
87 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafsourcedestaddress.SrcAddressBuilder;
88 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaftrafficengineeringaddress.Hops;
89 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaftrafficengineeringaddress.HopsBuilder;
90 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
91 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4;
92 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
93 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationData;
94 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationDataBuilder;
95 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafKeyValueBuilder;
96 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafListBuilder;
97 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSegmentBuilder;
98 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSourceDest;
99 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSourceDestBuilder;
100 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafTrafficEngineeringBuilder;
101 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Mac;
102 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.MacBuilder;
103 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.NoBuilder;
104 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
105 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
106 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapregisternotification.MapRegisterBuilder;
107 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
108 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRlocBuilder;
109 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
110 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
111 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.reencaphop.Hop;
112 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.reencaphop.HopBuilder;
113 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
114 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
115 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
116 import org.ops4j.pax.exam.Configuration;
117 import org.ops4j.pax.exam.Option;
118 import org.ops4j.pax.exam.junit.PaxExam;
119 import org.osgi.framework.Bundle;
120 import org.osgi.framework.BundleContext;
121 import org.osgi.framework.InvalidSyntaxException;
122 import org.osgi.framework.ServiceReference;
123 import org.slf4j.Logger;
124 import org.slf4j.LoggerFactory;
126 @RunWith(PaxExam.class)
127 public class MappingServiceIntegrationTest {
129 private IFlowMapping lms;
130 private ClusterDAOService clusterService;
131 protected static final Logger LOG = LoggerFactory.getLogger(MappingServiceIntegrationTest.class);
132 private byte[] mapRequestPacket;
133 private byte[] mapRegisterPacketWithNotify;
134 private byte[] mapRegisterPacketWithoutNotify;
135 private IConfigLispSouthboundPlugin configLispPlugin;
136 String lispBindAddress = "127.0.0.1";
137 String ourAddress = "127.0.0.2";
138 private LispAFIAddress locatorEid;
139 private DatagramSocket socket;
140 private byte[] mapRegisterPacketWithAuthenticationAndMapNotify;
142 public static final String ODL = "org.opendaylight.controller";
143 public static final String YANG = "org.opendaylight.yangtools";
144 public static final String JERSEY = "com.sun.jersey";
145 private static final int MAX_SERVICE_LOAD_RETRIES = 45;
146 private static final int MAX_NOTIFICATION_RETRYS = 20;
149 public void after() {
150 if (socket != null) {
153 if (connection != null) {
154 connection.disconnect();
159 public void before() throws Exception {
161 locatorEid = asIPAfiAddress("4.3.2.1");
162 socket = initSocket(socket, LispMessage.PORT_NUM);
164 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
165 // LISP(Type = 8 - Encapsulated)
166 // IP: 192.168.136.10 -> 153.16.254.1
168 // LISP(Type = 1 Map-Request
172 // Source EID not present
173 // Nonce: 0x3d8d2acd39c8d608
174 // ITR-RLOC AFI=1 Address=192.168.136.10
175 // Record 1: 153.16.254.1/32
176 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
177 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
178 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
179 + "0030 00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
180 + "0040 fe 01 dd b4 10 f6 00 28 ef 3a 10 00 00 01 3d 8d "
181 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 00 02 00 20 " //
182 + "0060 00 01 99 10 fe 01"));
184 // IP: 192.168.136.10 -> 128.223.156.35
185 // UDP: 49289 -> 4342
186 // LISP(Type = 3 Map-Register, P=1, M=1
190 // AuthDataLength: 20 Data:
191 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
192 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
194 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
195 // Priority/Weight: 1/100, Multicast Priority/Weight:
199 mapRegisterPacketWithAuthenticationAndMapNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
200 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
201 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
202 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
203 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
204 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
205 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
207 // IP: 192.168.136.10 -> 128.223.156.35
208 // UDP: 49289 -> 4342
209 // LISP(Type = 3 Map-Register, P=1, M=1
212 // Key ID: 0x0000 NO AUTHENTICATION!!
213 // AuthDataLength: 00 Data:
214 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
216 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
217 // Priority/Weight: 1/100, Multicast Priority/Weight:
221 mapRegisterPacketWithNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
222 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
223 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
224 + "0030 00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
225 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
226 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
227 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
229 // IP: 192.168.136.10 -> 128.223.156.35
230 // UDP: 49289 -> 4342
231 // LISP(Type = 3 Map-Register, P=1, M=1
234 // Key ID: 0x0000 NO AUTHENTICATION!!
235 // AuthDataLength: 00 Data:
236 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
238 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
239 // Priority/Weight: 1/100, Multicast Priority/Weight:
243 mapRegisterPacketWithoutNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
244 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
245 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
246 + "0030 00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
247 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
248 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
249 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
253 private BundleContext bc;
254 private HttpURLConnection connection;
255 protected static boolean notificationCalled;
257 // Configure the OSGi container
259 public Option[] config() {
260 return options(MappingServiceTestHelper.mappingServiceBundlesWithClusterDAO());
264 public void testSimpleUsage() throws Exception {
266 mapRegisterWithMapNotify();
267 mapRegisterWithMapNotifyAndMapRequest();
268 registerAndQuery__MAC();
269 mapRequestMapRegisterAndMapRequest();
270 mapRegisterWithAuthenticationWithoutConfiguringAKey();
271 mapRegisterWithoutMapNotify();
275 public void testLCAFs() throws Exception {
276 registerAndQuery__SrcDestLCAF();
277 registerAndQuery__KeyValueLCAF();
278 registerAndQuery__ListLCAF();
279 registerAndQuery__ApplicationData();
280 registerAndQuery__TrafficEngineering();
281 registerAndQuery__SegmentLCAF();
285 public void testMask() throws Exception {
286 testPasswordExactMatch();
287 testPasswordMaskMatch();
288 eidPrefixLookupIPv4();
289 eidPrefixLookupIPv6();
293 public void testNorthbound() throws Exception {
295 northboundAddMapping();
296 northboundRetrieveKey();
297 northboundRetrieveMapping();
298 northboundRetrieveSourceDestKey();
299 northboundRetrieveSourceDestMapping();
303 public void testOverWriting() throws Exception {
304 testMapRegisterDosntOverwritesOtherSubKeys();
305 testMapRegisterOverwritesSameSubkey();
306 testMapRegisterOverwritesNoSubkey();
307 testMapRegisterDoesntOverwritesNoSubkey();
311 public void testTimeOuts() throws Exception {
312 mapRequestMapRegisterAndMapRequestTestTimeout();
313 mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse();
317 public void testNonProxy() throws Throwable {
318 testSimpleNonProxy();
319 testNonProxyOtherPort();
320 testRecievingNonProxyOnXtrPort();
324 public void testSmr() throws Exception {
325 registerQueryRegisterWithSmr();
328 // ------------------------------- Simple Tests ---------------------------
330 public void mapRequestSimple() throws SocketTimeoutException {
333 // This Map-Request is sent from a source port different from 4342
334 // We close and bind the socket on the correct port
335 if (socket != null) {
338 socket = initSocket(socket, 56756);
340 sendPacket(mapRequestPacket);
341 ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
342 MapReply reply = MapReplySerializer.getInstance().deserialize(readBuf);
343 assertEquals(4435248268955932168L, reply.getNonce().longValue());
347 public void mapRegisterWithMapNotify() throws SocketTimeoutException {
349 sendPacket(mapRegisterPacketWithNotify);
350 MapNotify reply = receiveMapNotify();
351 assertEquals(7, reply.getNonce().longValue());
354 public void mapRegisterWithMapNotifyAndMapRequest() throws SocketTimeoutException {
356 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
358 MapReply mapReply = registerAddressAndQuery(eid, 32);
360 assertEquals(4, mapReply.getNonce().longValue());
361 assertEquals(LispAFIConvertor.toContainer(locatorEid), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
362 .getLispAddressContainer());
366 public void registerAndQuery__MAC() throws SocketTimeoutException {
368 String macAddress = "01:02:03:04:05:06";
370 MapReply reply = registerAddressAndQuery(asMacAfiAddress(macAddress));
373 LispAFIAddress addressFromNetwork = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
374 assertTrue(addressFromNetwork instanceof LispMacAddress);
375 String macAddressFromReply = ((Mac) addressFromNetwork).getMacAddress().getValue();
377 assertEquals(macAddress, macAddressFromReply);
380 public void mapRequestMapRegisterAndMapRequest() throws SocketTimeoutException {
382 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
383 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
384 mapRequestBuilder.setNonce((long) 4);
385 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
386 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
387 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
388 mapRequestBuilder.getEidRecord().add(
389 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
390 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
391 mapRequestBuilder.getItrRloc().add(
392 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
393 sendMapRequest(mapRequestBuilder.build());
394 MapReply mapReply = receiveMapReply();
395 assertEquals(4, mapReply.getNonce().longValue());
396 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
397 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
398 mapRegisterbuilder.setWantMapNotify(true);
399 mapRegisterbuilder.setNonce((long) 8);
400 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
401 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
402 etlrBuilder.setMaskLength((short) 32);
403 etlrBuilder.setRecordTtl(254);
404 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
405 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
406 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
407 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
408 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
409 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
410 sendMapRegister(mapRegisterbuilder.build());
411 MapNotify mapNotify = receiveMapNotify();
412 assertEquals(8, mapNotify.getNonce().longValue());
413 sendMapRequest(mapRequestBuilder.build());
414 mapReply = receiveMapReply();
415 assertEquals(4, mapReply.getNonce().longValue());
416 assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
417 .getLispAddressContainer());
421 public void testMapRegisterDosntOverwritesOtherSubKeys() throws SocketTimeoutException {
423 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
424 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
425 LispAFIAddress rloc1 = LispAFIConvertor.asKeyValue("subkey1", LispAFIConvertor.toPrimitive(rloc1Value));
426 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
427 LispAFIAddress rloc2 = LispAFIConvertor.asKeyValue("subkey2", LispAFIConvertor.toPrimitive(rloc2Value));
428 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
429 assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
430 assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
431 assertEquals(LispAFIConvertor.toContainer(rloc1), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(1).getLispAddressContainer());
434 public void testMapRegisterOverwritesSameSubkey() throws SocketTimeoutException {
436 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
437 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
438 LispAFIAddress rloc1 = LispAFIConvertor.asKeyValue("subkey", LispAFIConvertor.toPrimitive(rloc1Value));
439 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
440 LispAFIAddress rloc2 = LispAFIConvertor.asKeyValue("subkey", LispAFIConvertor.toPrimitive(rloc2Value));
441 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
442 assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
443 assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
446 public void testMapRegisterOverwritesNoSubkey() throws SocketTimeoutException {
448 lms.setOverwrite(true);
449 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
450 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
451 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
452 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
453 assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
454 assertEquals(LispAFIConvertor.toContainer(rloc2Value), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
455 .getLispAddressContainer());
458 public void testMapRegisterDoesntOverwritesNoSubkey() throws SocketTimeoutException {
460 lms.setOverwrite(false);
461 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
462 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
463 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
464 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
465 assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
466 LispAddressContainer rloc1ReturnValueContainer = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer();
467 LispAddressContainer rloc2ReturnValueContainer = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(1).getLispAddressContainer();
468 assertTrue((LispAFIConvertor.toContainer(rloc1Value).equals(rloc1ReturnValueContainer) && LispAFIConvertor.toContainer(rloc2Value).equals(
469 rloc2ReturnValueContainer))
470 || (LispAFIConvertor.toContainer(rloc1Value).equals(rloc2ReturnValueContainer) && LispAFIConvertor.toContainer(rloc2Value).equals(
471 rloc1ReturnValueContainer)));
474 private MapReply sendMapRegisterTwiceWithDiffrentValues(LispAFIAddress eid, LispAFIAddress rloc1, LispAFIAddress rloc2)
475 throws SocketTimeoutException {
476 MapRegister mb = createMapRegister(eid, rloc1);
477 MapNotify mapNotify = lms.handleMapRegister(mb, false);
478 MapRequest mr = createMapRequest(eid);
479 MapReply mapReply = lms.handleMapRequest(mr);
480 assertEquals(mb.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0)
481 .getLocatorRecord().get(0).getLispAddressContainer());
482 mb = createMapRegister(eid, rloc2);
483 mapNotify = lms.handleMapRegister(mb, false);
484 assertEquals(8, mapNotify.getNonce().longValue());
485 mr = createMapRequest(eid);
487 mapReply = lms.handleMapRequest(mr);
491 public void mapRegisterWithAuthenticationWithoutConfiguringAKey() throws SocketTimeoutException {
493 sendPacket(mapRegisterPacketWithAuthenticationAndMapNotify);
496 // If didn't timeout then fail:
498 } catch (SocketTimeoutException ste) {
502 public void mapRegisterWithoutMapNotify() {
504 sendPacket(mapRegisterPacketWithoutNotify);
507 // If didn't timeout then fail:
509 } catch (SocketTimeoutException ste) {
513 public void registerQueryRegisterWithSmr() throws SocketTimeoutException {
515 lms.setShouldUseSmr(true);
517 sendPacket(mapRegisterPacketWithNotify);
520 sendPacket(mapRequestPacket);
523 } catch (InterruptedException e) {
526 mapRegisterPacketWithoutNotify[mapRegisterPacketWithoutNotify.length - 1] += 1;
527 sendPacket(mapRegisterPacketWithoutNotify);
529 ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
530 MapRequest smr = MapRequestSerializer.getInstance().deserialize(readBuf);
531 assertTrue(smr.isSmr());
532 LispAddressContainer smrEid = smr.getEidRecord().get(0).getLispAddressContainer();
533 assertTrue(LispAFIConvertor.toContainer(asIPAfiAddress("153.16.254.1")).equals(smrEid));
536 // --------------------- Northbound Tests ---------------------------
538 private void northboundAddKey() throws Exception {
540 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
542 String pass = "asdf";
544 URL url = createPutURL("key");
545 String authKeyJSON = createAuthKeyJSON(pass, address, mask);
546 callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
548 String retrievedKey = lms.getAuthenticationKey(LispAFIConvertor.toContainer(address), mask);
550 // Check stored password matches the one sent
551 assertEquals(pass, retrievedKey);
555 private void northboundRetrieveSourceDestKey() throws Exception {
557 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
558 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
559 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
560 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
563 LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
564 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
565 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
566 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
567 String pass = "asdf";
569 lms.addAuthenticationKey(LispAFIConvertor.toContainer(sourceDestAddress), mask1, pass);
571 // URL url = createGetKeyIPv4URL(address1, mask1);
572 URL url = createGetKeySourceDestURL(address1.getAfi(), ((LispIpv4Address) sourceDestAddress.getSrcAddress().getPrimitiveAddress())
573 .getIpv4Address().getValue(), sourceDestAddress.getSrcMaskLength(), ((LispIpv4Address) sourceDestAddress.getDstAddress()
574 .getPrimitiveAddress()).getIpv4Address().getValue(), sourceDestAddress.getDstMaskLength());
575 String reply = callURL("GET", null, "application/json", null, url);
576 JSONTokener jt = new JSONTokener(reply);
577 JSONObject json = new JSONObject(jt);
579 // test that the password matches what was we expected.
580 assertEquals(pass, json.get("key"));
584 private void northboundRetrieveKey() throws Exception {
586 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
588 String pass = "asdf";
590 lms.addAuthenticationKey(LispAFIConvertor.toContainer(address), mask, pass);
592 URL url = createGetKeyIPv4URL(address, mask);
593 String reply = callURL("GET", null, "application/json", null, url);
594 JSONTokener jt = new JSONTokener(reply);
595 JSONObject json = new JSONObject(jt);
597 // test that the password matches what was we expected.
598 assertEquals(pass, json.get("key"));
602 private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
603 return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
604 + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
607 private void northboundAddMapping() throws Exception {
609 String pass = "asdf";
610 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
612 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
614 // NB add mapping always checks the key
615 lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
617 URL url = createPutURL("mapping");
618 String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
619 callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
621 // Retrieve the RLOC from the database
622 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
623 mapRequestBuilder.setPitr(false);
624 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
625 mapRequestBuilder.getEidRecord().add(
626 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
627 MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
629 LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
632 assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
636 private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
637 String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ " + "\"proxyMapReply\" : false, "
638 + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : " + "{ " + "\"ipAddress\" : \""
639 + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "}," + "\"mapVersion\" : 0,"
640 + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ " + "\"multicastPriority\" : 1,"
641 + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\"," + "\"afi\" : "
642 + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50," + "\"rlocProbed\" : false, "
643 + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], " + "\"recordTtl\" : 100" + "} " + "], "
644 + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
649 private void northboundRetrieveMapping() throws Exception {
651 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
653 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
654 // Insert mapping in the database
655 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
656 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
657 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
658 etlr.setMaskLength((short) mask);
659 etlr.setRecordTtl(254);
660 etlr.setAuthoritative(false);
661 etlr.setAction(Action.NoAction);
662 LocatorRecordBuilder record = new LocatorRecordBuilder();
663 record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
664 record.setRouted(true);
665 record.setRlocProbed(false);
666 record.setLocalLocator(false);
667 record.setPriority((short) 1);
668 record.setWeight((short) 50);
669 record.setMulticastPriority((short) 1);
670 record.setMulticastWeight((short) 1);
671 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
672 etlr.getLocatorRecord().add(record.build());
673 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
674 mapRegister.getEidToLocatorRecord().add(etlr.build());
675 lms.handleMapRegister(mapRegister.build(), false);
677 // Get mapping using NB interface. No IID used
678 URL url = createGetMappingIPv4URL(0, eid, mask);
679 String reply = callURL("GET", null, "application/json", null, url);
680 JSONTokener jt = new JSONTokener(reply);
681 JSONObject json = new JSONObject(jt);
683 // With just one locator, locators is not a JSONArray
684 String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
686 assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
690 private void northboundRetrieveSourceDestMapping() throws Exception {
692 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
693 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
694 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
695 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
698 LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
699 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
700 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
701 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
702 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
704 // Insert mapping in the database
705 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
706 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
707 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(sourceDestAddress));
708 etlr.setMaskLength((short) mask1);
709 etlr.setRecordTtl(254);
710 etlr.setAuthoritative(false);
711 etlr.setAction(Action.NoAction);
712 LocatorRecordBuilder record = new LocatorRecordBuilder();
713 record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
714 record.setRouted(true);
715 record.setRlocProbed(false);
716 record.setLocalLocator(false);
717 record.setPriority((short) 1);
718 record.setWeight((short) 50);
719 record.setMulticastPriority((short) 1);
720 record.setMulticastWeight((short) 1);
721 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
722 etlr.getLocatorRecord().add(record.build());
723 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
724 mapRegister.getEidToLocatorRecord().add(etlr.build());
725 lms.handleMapRegister(mapRegister.build(), false);
727 // Get mapping using NB interface. No IID used
728 URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
730 String reply = callURL("GET", null, "application/json", null, url);
731 JSONTokener jt = new JSONTokener(reply);
732 JSONObject json = new JSONObject(jt);
734 // With just one locator, locators is not a JSONArray
735 String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
737 assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
741 private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
742 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key", address.getAfi().shortValue(),
743 address.getIpv4Address().getValue(), mask);
744 URL url = new URL(restUrl);
748 private URL createGetKeySourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
749 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "key", afi, srcAddress, srcMask,
750 dstAddress, dstMask);
751 URL url = new URL(restUrl);
755 private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
756 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "mapping", afi, srcAddress,
757 srcMask, dstAddress, dstMask);
758 URL url = new URL(restUrl);
762 private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
763 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping", iid, address.getAfi()
764 .shortValue(), address.getIpv4Address().getValue(), mask);
765 URL url = new URL(restUrl);
769 private URL createPutURL(String resource) throws MalformedURLException {
771 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
773 URL url = new URL(restUrl);
777 private String createAuthenticationString() {
778 String authString = "admin:admin";
779 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
780 String authStringEnc = new String(authEncBytes);
781 return authStringEnc;
784 private String callURL(String method, String content, String accept, String body, URL url) throws IOException, JSONException {
785 String authStringEnc = createAuthenticationString();
786 connection = (HttpURLConnection) url.openConnection();
787 connection.setRequestMethod(method);
788 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
789 if (content != null) {
790 connection.setRequestProperty("Content-Type", content);
792 if (accept != null) {
793 connection.setRequestProperty("Accept", accept);
796 // now add the request body
797 connection.setDoOutput(true);
798 OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
802 connection.connect();
804 // getting the result, first check response code
805 Integer httpResponseCode = connection.getResponseCode();
807 if (httpResponseCode > 299) {
808 LOG.trace("HTTP Address: " + url);
809 LOG.trace("HTTP Response Code: " + httpResponseCode);
813 InputStream is = connection.getInputStream();
814 BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
815 StringBuilder sb = new StringBuilder();
817 while ((cp = rd.read()) != -1) {
818 sb.append((char) cp);
821 connection.disconnect();
822 return (sb.toString());
825 // timePeriod - in ms
826 public void assertNoPacketReceived(int timePeriod) {
828 receivePacket(timePeriod);
829 // If didn't timeout then fail:
831 } catch (SocketTimeoutException ste) {
835 // ------------------------------- Mask Tests ---------------------------
837 public void eidPrefixLookupIPv4() throws SocketTimeoutException {
839 runPrefixTest(LispAFIConvertor.asIPAfiAddress("1.2.3.4"), 16, LispAFIConvertor.asIPAfiAddress("1.2.3.2"),
840 LispAFIConvertor.asIPAfiAddress("1.1.1.1"), (byte) 32);
843 public void eidPrefixLookupIPv6() throws SocketTimeoutException {
845 runPrefixTest(LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:6:7:8"), 64, LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:1:2:3"),
846 LispAFIConvertor.asIPv6AfiAddress("1:2:3:1:2:3:1:2"), 128);
849 private void runPrefixTest(LispAFIAddress registerEID, int registerdMask, LispAFIAddress matchedAddress, LispAFIAddress unMatchedAddress, int mask)
850 throws SocketTimeoutException {
852 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
853 mapRegister.setWantMapNotify(true);
854 mapRegister.setNonce((long) 8);
855 mapRegister.setWantMapNotify(true);
856 mapRegister.setKeyId((short) 0);
857 mapRegister.setAuthenticationData(new byte[0]);
858 mapRegister.setNonce((long) 8);
859 mapRegister.setProxyMapReply(false);
860 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
861 etlr.setRecordTtl(254);
862 etlr.setAction(Action.NoAction);
863 etlr.setAuthoritative(false);
864 etlr.setMapVersion((short) 0);
865 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(registerEID));
866 etlr.setMaskLength((short) registerdMask);
867 etlr.setRecordTtl(254);
868 LocatorRecordBuilder record = new LocatorRecordBuilder();
869 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
870 record.setLocalLocator(false);
871 record.setRlocProbed(false);
872 record.setRouted(true);
873 record.setMulticastPriority((short) 0);
874 record.setMulticastWeight((short) 0);
875 record.setPriority((short) 0);
876 record.setWeight((short) 0);
877 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
878 etlr.getLocatorRecord().add(record.build());
879 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
880 mapRegister.getEidToLocatorRecord().add(etlr.build());
881 sendMapRegister(mapRegister.build());
882 MapNotify mapNotify = receiveMapNotify();
883 assertEquals(8, mapNotify.getNonce().longValue());
884 MapRequestBuilder mapRequest = new MapRequestBuilder();
885 mapRequest.setNonce((long) 4);
886 mapRequest.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
887 mapRequest.setEidRecord(new ArrayList<EidRecord>());
888 mapRequest.setAuthoritative(false);
889 mapRequest.setMapDataPresent(false);
890 mapRequest.setPitr(false);
891 mapRequest.setProbe(false);
892 mapRequest.setSmr(false);
893 mapRequest.setSmrInvoked(false);
894 mapRequest.getEidRecord().add(
895 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(matchedAddress)).build());
896 mapRequest.setItrRloc(new ArrayList<ItrRloc>());
897 mapRequest.getItrRloc().add(
898 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ourAddress))).build());
899 sendMapRequest(mapRequest.build());
900 MapReply mapReply = receiveMapReply();
901 assertEquals(4, mapReply.getNonce().longValue());
902 assertEquals(record.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
903 mapRequest.setEidRecord(new ArrayList<EidRecord>());
904 mapRequest.getEidRecord().add(
905 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(unMatchedAddress)).build());
906 sendMapRequest(mapRequest.build());
907 mapReply = receiveMapReply();
908 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
911 // This registers an IP with a MapRegister, then adds a password via the
912 // northbound REST API
913 // and checks that the password works
914 public void testPasswordExactMatch() throws Exception {
916 String ipString = "10.0.0.1";
917 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress(ipString);
919 String pass = "pass";
921 URL url = createPutURL("key");
923 String jsonAuthData = createAuthKeyJSON(pass, address, mask);
925 LOG.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
926 LOG.trace("Address: " + address);
928 byte[] expectedSha = new byte[] { (byte) 146, (byte) 234, (byte) 52, (byte) 247, (byte) 186, (byte) 232, (byte) 31, (byte) 249, (byte) 87,
929 (byte) 73, (byte) 234, (byte) 54, (byte) 225, (byte) 160, (byte) 129, (byte) 251, (byte) 73, (byte) 53, (byte) 196, (byte) 62 };
931 byte[] zeros = new byte[20];
933 callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
935 // build a MapRegister
936 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
937 mapRegister.setWantMapNotify(true);
938 mapRegister.setNonce((long) 8);
939 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
940 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(address));
941 etlr.setMaskLength((short) mask);
942 etlr.setRecordTtl(254);
943 LocatorRecordBuilder record = new LocatorRecordBuilder();
944 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
945 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
946 etlr.getLocatorRecord().add(record.build());
947 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
948 mapRegister.getEidToLocatorRecord().add(etlr.build());
950 mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
951 mapRegister.setAuthenticationData(zeros);
953 sendMapRegister(mapRegister.build());
954 assertNoPacketReceived(3000);
956 mapRegister.setAuthenticationData(expectedSha);
958 sendMapRegister(mapRegister.build());
960 assertMapNotifyRecieved();
963 public void testPasswordMaskMatch() throws Exception {
965 LispIpv4Address addressInRange = LispAFIConvertor.asIPAfiAddress("10.20.30.40");
966 LispIpv4Address addressOutOfRange = LispAFIConvertor.asIPAfiAddress("20.40.30.40");
967 LispIpv4Address range = LispAFIConvertor.asIPAfiAddress("10.20.30.0");
970 String pass = "pass";
972 URL url = createPutURL("key");
973 String jsonAuthData = createAuthKeyJSON(pass, range, 8);
975 callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
976 // build a MapRegister
977 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
979 mapRegister.setWantMapNotify(true);
980 mapRegister.setNonce((long) 8);
981 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
982 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressInRange));
983 etlr.setMaskLength((short) mask);
984 etlr.setRecordTtl(254);
985 LocatorRecordBuilder record = new LocatorRecordBuilder();
986 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
987 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
988 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
989 etlr.getLocatorRecord().add(record.build());
990 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
991 mapRegister.getEidToLocatorRecord().add(etlr.build());
993 mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
995 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
997 sendMapRegister(mapRegister.build());
999 assertMapNotifyRecieved();
1001 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
1003 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
1005 sendMapRegister(mapRegister.build());
1006 assertNoPacketReceived(3000);
1009 private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
1010 return registerAddressAndQuery(eid, -1);
1013 // takes an address, packs it in a MapRegister, sends it, returns the
1015 private MapReply registerAddressAndQuery(LispAFIAddress eid, int maskLength) throws SocketTimeoutException {
1016 MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
1017 mapRegisterBuilder.setWantMapNotify(true);
1018 mapRegisterBuilder.setKeyId((short) 0);
1019 mapRegisterBuilder.setAuthenticationData(new byte[0]);
1020 mapRegisterBuilder.setNonce((long) 8);
1021 mapRegisterBuilder.setProxyMapReply(false);
1022 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1023 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1024 if (maskLength != -1) {
1025 etlrBuilder.setMaskLength((short) maskLength);
1027 etlrBuilder.setMaskLength((short) 0);
1029 etlrBuilder.setRecordTtl(254);
1030 etlrBuilder.setAction(Action.NoAction);
1031 etlrBuilder.setAuthoritative(false);
1032 etlrBuilder.setMapVersion((short) 0);
1033 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1034 recordBuilder.setLocalLocator(false);
1035 recordBuilder.setRlocProbed(false);
1036 recordBuilder.setRouted(true);
1037 recordBuilder.setMulticastPriority((short) 0);
1038 recordBuilder.setMulticastWeight((short) 0);
1039 recordBuilder.setPriority((short) 0);
1040 recordBuilder.setWeight((short) 0);
1041 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
1042 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1043 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1044 mapRegisterBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1045 mapRegisterBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1046 sendMapRegister(mapRegisterBuilder.build());
1047 MapNotify mapNotify = receiveMapNotify();
1048 assertEquals(8, mapNotify.getNonce().longValue());
1049 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1050 mapRequestBuilder.setNonce((long) 4);
1051 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1052 mapRequestBuilder.getEidRecord().add(
1053 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1054 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1055 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress)))
1057 mapRequestBuilder.getItrRloc().add(
1058 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1059 mapRequestBuilder.setAuthoritative(false);
1060 mapRequestBuilder.setMapDataPresent(false);
1061 mapRequestBuilder.setPitr(false);
1062 mapRequestBuilder.setProbe(false);
1063 mapRequestBuilder.setSmr(false);
1064 mapRequestBuilder.setSmrInvoked(false);
1065 sendMapRequest(mapRequestBuilder.build());
1066 return receiveMapReply();
1069 // ------------------------------- LCAF Tests ---------------------------
1071 public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1073 String ipString = "10.20.30.200";
1074 String macString = "01:02:03:04:05:06";
1075 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1076 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1077 LcafSourceDestBuilder builder = new LcafSourceDestBuilder();
1078 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1079 builder.setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode());
1080 builder.setSrcMaskLength((short) 0);
1081 builder.setDstMaskLength((short) 0);
1082 builder.setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(addrToSend1).build());
1083 builder.setDstAddress(new DstAddressBuilder().setPrimitiveAddress(addrToSend2).build());
1085 MapReply reply = registerAddressAndQuery(builder.build());
1087 LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1088 assertTrue(fromNetwork.getAddress() instanceof LcafSourceDestAddress);
1089 LcafSourceDestAddress sourceDestFromNetwork = (LcafSourceDestAddress) fromNetwork.getAddress();
1091 LispAFIAddress receivedAddr1 = (LispAFIAddress) sourceDestFromNetwork.getSrcAddress().getPrimitiveAddress();
1092 LispAFIAddress receivedAddr2 = (LispAFIAddress) sourceDestFromNetwork.getDstAddress().getPrimitiveAddress();
1094 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1095 assertTrue(receivedAddr2 instanceof LispMacAddress);
1097 LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1098 LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1100 assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1101 assertEquals(macString, receivedMAC.getMacAddress().getValue());
1105 public void registerAndQuery__KeyValueLCAF() throws SocketTimeoutException {
1107 String ipString = "10.20.30.200";
1108 String macString = "01:02:03:04:05:06";
1109 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1110 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1111 LcafKeyValueBuilder builder = new LcafKeyValueBuilder();
1112 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1113 builder.setLcafType((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode());
1114 builder.setKey(new KeyBuilder().setPrimitiveAddress(addrToSend1).build());
1115 builder.setValue(new ValueBuilder().setPrimitiveAddress(addrToSend2).build());
1117 MapReply reply = registerAddressAndQuery(builder.build());
1119 LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1120 assertTrue(fromNetwork.getAddress() instanceof LcafKeyValueAddress);
1121 LcafKeyValueAddress keyValueFromNetwork = (LcafKeyValueAddress) fromNetwork.getAddress();
1123 LispAFIAddress receivedAddr1 = (LispAFIAddress) keyValueFromNetwork.getKey().getPrimitiveAddress();
1124 LispAFIAddress receivedAddr2 = (LispAFIAddress) keyValueFromNetwork.getValue().getPrimitiveAddress();
1126 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1127 assertTrue(receivedAddr2 instanceof LispMacAddress);
1129 LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1130 LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1132 assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1133 assertEquals(macString, receivedMAC.getMacAddress().getValue());
1136 public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1138 String macString = "01:02:03:04:05:06";
1139 String ipString = "10.20.255.30";
1140 LcafListBuilder listbuilder = new LcafListBuilder();
1141 listbuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
1142 listbuilder.setAddresses(new ArrayList<Addresses>());
1143 listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1144 listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString))).build());
1146 MapReply reply = registerAddressAndQuery(listbuilder.build());
1148 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1150 assertTrue(receivedAddress instanceof LcafListAddress);
1152 LcafListAddress listAddrFromNetwork = (LcafListAddress) receivedAddress;
1153 LispAFIAddress receivedAddr1 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(0).getPrimitiveAddress();
1154 LispAFIAddress receivedAddr2 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(1).getPrimitiveAddress();
1156 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1157 assertTrue(receivedAddr2 instanceof LispMacAddress);
1159 assertEquals(macString, ((LispMacAddress) receivedAddr2).getMacAddress().getValue());
1160 assertEquals(ipString, ((LispIpv4Address) receivedAddr1).getIpv4Address().getValue());
1163 public void registerAndQuery__SegmentLCAF() throws SocketTimeoutException {
1165 String ipString = "10.20.255.30";
1167 LcafSegmentBuilder builder = new LcafSegmentBuilder();
1168 builder.setInstanceId((long) instanceId);
1169 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode());
1170 builder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1172 MapReply reply = registerAddressAndQuery(builder.build());
1174 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1175 assertTrue(receivedAddress instanceof LcafSegmentAddress);
1177 LcafSegmentAddress segmentfromNetwork = (LcafSegmentAddress) receivedAddress;
1178 LispAFIAddress addrFromSegment = (LispAFIAddress) segmentfromNetwork.getAddress().getPrimitiveAddress();
1179 assertTrue(addrFromSegment instanceof LispIpv4Address);
1180 assertEquals(ipString, ((LispIpv4Address) addrFromSegment).getIpv4Address().getValue());
1182 assertEquals(instanceId, segmentfromNetwork.getInstanceId().intValue());
1185 public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1187 String macString = "01:02:03:04:05:06";
1188 String ipString = "10.20.255.30";
1189 HopBuilder hopBuilder = new HopBuilder();
1190 hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString)));
1191 Hop hop1 = hopBuilder.build();
1192 hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString)));
1193 Hop hop2 = hopBuilder.build();
1194 HopsBuilder hb = new HopsBuilder();
1197 hb.setRLOCProbe(false);
1199 HopsBuilder hb2 = new HopsBuilder();
1201 hb2.setLookup(false);
1202 hb2.setRLOCProbe(true);
1203 hb2.setStrict(false);
1204 Hops hops1 = hb.build();
1205 Hops hops2 = hb2.build();
1206 LcafTrafficEngineeringBuilder trafficBuilder = new LcafTrafficEngineeringBuilder();
1207 trafficBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
1208 (short) LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode());
1209 trafficBuilder.setHops(new ArrayList<Hops>());
1210 trafficBuilder.getHops().add(hb.build());
1211 trafficBuilder.getHops().add(hb2.build());
1213 MapReply reply = registerAddressAndQuery(trafficBuilder.build());
1215 assertTrue(LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer()) instanceof LcafTrafficEngineeringAddress);
1217 LcafTrafficEngineeringAddress receivedAddress = (LcafTrafficEngineeringAddress) LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0)
1218 .getLispAddressContainer());
1220 ReencapHop receivedHop1 = receivedAddress.getHops().get(0);
1221 ReencapHop receivedHop2 = receivedAddress.getHops().get(1);
1223 assertEquals(true, hops1.isLookup());
1224 assertEquals(false, hops1.isRLOCProbe());
1225 assertEquals(true, hops1.isStrict());
1227 assertEquals(false, hops2.isLookup());
1228 assertEquals(true, hops2.isRLOCProbe());
1229 assertEquals(false, hops2.isStrict());
1231 assertTrue(receivedHop1.getHop().getPrimitiveAddress() instanceof LispIpv4Address);
1232 assertTrue(receivedHop2.getHop().getPrimitiveAddress() instanceof LispMacAddress);
1234 assertEquals(ipString, ((LispIpv4Address) receivedHop1.getHop().getPrimitiveAddress()).getIpv4Address().getValue());
1235 assertEquals(macString, ((LispMacAddress) receivedHop2.getHop().getPrimitiveAddress()).getMacAddress().getValue());
1238 public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1240 String ipString = "1.2.3.4";
1246 LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
1247 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
1248 builder.setIpTos(ipTOs);
1249 builder.setProtocol(protocol);
1250 builder.setLocalPort(new PortNumber(localPort));
1251 builder.setRemotePort(new PortNumber(remotePort));
1252 builder.setAddress(new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder()
1253 .setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1255 LcafApplicationDataAddress addressToSend = builder.build();
1257 MapReply reply = registerAddressAndQuery(addressToSend);
1259 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1261 assertTrue(receivedAddress instanceof LcafApplicationDataAddress);
1263 LcafApplicationDataAddress receivedApplicationDataAddress = (LcafApplicationDataAddress) receivedAddress;
1264 assertEquals(protocol, receivedApplicationDataAddress.getProtocol().intValue());
1265 assertEquals(ipTOs, receivedApplicationDataAddress.getIpTos().intValue());
1266 assertEquals(localPort, receivedApplicationDataAddress.getLocalPort().getValue().intValue());
1267 assertEquals(remotePort, receivedApplicationDataAddress.getRemotePort().getValue().intValue());
1269 LispIpv4Address ipAddressReceived = (LispIpv4Address) receivedApplicationDataAddress.getAddress().getPrimitiveAddress();
1270 assertEquals(ipString, ipAddressReceived.getIpv4Address().getValue());
1273 // ------------------- TimeOut Tests -----------
1275 public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1277 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1278 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1279 mapRequestBuilder.setNonce((long) 4);
1280 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1281 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1282 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1283 mapRequestBuilder.getEidRecord().add(
1284 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1285 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1286 mapRequestBuilder.getItrRloc().add(
1287 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1288 sendMapRequest(mapRequestBuilder.build());
1289 MapReply mapReply = receiveMapReply();
1290 assertEquals(4, mapReply.getNonce().longValue());
1291 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1292 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1293 mapRegisterbuilder.setWantMapNotify(true);
1294 mapRegisterbuilder.setNonce((long) 8);
1295 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1296 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1297 etlrBuilder.setMaskLength((short) 32);
1298 etlrBuilder.setRecordTtl(254);
1299 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1300 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
1301 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1302 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1303 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1304 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1305 sendMapRegister(mapRegisterbuilder.build());
1306 MapNotify mapNotify = receiveMapNotify();
1307 assertEquals(8, mapNotify.getNonce().longValue());
1308 sendMapRequest(mapRequestBuilder.build());
1309 mapReply = receiveMapReply();
1310 assertEquals(4, mapReply.getNonce().longValue());
1311 assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1312 .getLispAddressContainer());
1313 causeEntryToBeCleaned();
1314 sendMapRequest(mapRequestBuilder.build());
1315 mapReply = receiveMapReply();
1316 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1319 public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1321 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1322 MapRequest mapRequest = createMapRequest(eid);
1324 testTTLBeforeRegister(mapRequest);
1326 registerForTTL(eid);
1328 testTTLAfterRegister(mapRequest);
1330 causeEntryToBeCleaned();
1331 testTTLAfterClean(mapRequest);
1334 testTTLAfterAutherize(mapRequest);
1338 private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1340 sendMapRequest(mapRequest);
1341 mapReply = receiveMapReply();
1342 assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1345 private void causeEntryToBeCleaned() {
1346 clusterService.setTimeUnit(TimeUnit.NANOSECONDS);
1347 clusterService.cleanOld();
1350 private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1352 sendMapRequest(mapRequest);
1353 mapReply = receiveMapReply();
1354 assertEquals(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1355 .getLispAddressContainer());
1356 assertCorrectMapReplyTTLAndAction(mapReply, 254, Action.NoAction);
1359 private void registerForTTL(LispIpv4Address eid) throws SocketTimeoutException {
1360 MapRegister mapRegister = createMapRegister(eid);
1361 sendMapRegister(mapRegister);
1362 assertMapNotifyRecieved();
1365 private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1367 sendMapRequest(mapRequest);
1368 mapReply = receiveMapReply();
1369 assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1372 private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1374 sendMapRequest(mapRequest);
1375 mapReply = receiveMapReply();
1376 assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1379 private void assertCorrectMapReplyTTLAndAction(MapReply mapReply, int expectedTTL, Action expectedAction) {
1380 assertEquals(expectedTTL, mapReply.getEidToLocatorRecord().get(0).getRecordTtl().intValue());
1381 assertEquals(expectedAction, mapReply.getEidToLocatorRecord().get(0).getAction());
1384 private MapRegister createMapRegister(LispAFIAddress eid, LispAFIAddress rloc) {
1385 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1386 mapRegisterbuilder.setWantMapNotify(true);
1387 mapRegisterbuilder.setNonce((long) 8);
1388 mapRegisterbuilder.setKeyId((short) 0);
1389 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1390 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1391 etlrBuilder.setMaskLength((short) 24);
1392 etlrBuilder.setRecordTtl(254);
1393 etlrBuilder.setAuthoritative(false);
1394 etlrBuilder.setAction(Action.NoAction);
1395 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1396 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
1397 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1398 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1399 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1400 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1401 MapRegister mapRegister = mapRegisterbuilder.build();
1405 private MapRegister createMapRegister(LispIpv4Address eid) {
1406 return createMapRegister(eid, asIPAfiAddress("4.3.2.1"));
1409 private MapRequest createMapRequest(LispAFIAddress eid) {
1410 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1411 mapRequestBuilder.setNonce((long) 4);
1412 mapRequestBuilder.setPitr(false);
1413 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1414 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1415 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1416 mapRequestBuilder.getEidRecord().add(
1417 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1418 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1419 mapRequestBuilder.getItrRloc().add(
1420 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1421 MapRequest mr = mapRequestBuilder.build();
1425 public void testSimpleNonProxy() throws SocketTimeoutException, SocketException {
1427 String rloc = "127.0.0.3";
1428 int port = LispMessage.PORT_NUM;
1429 Ipv4 ipRloc = LispAFIConvertor.asIPAfiAddress(rloc);
1430 sendProxyMapRequest(rloc, port, ipRloc);
1434 public void testNonProxyOtherPort() throws SocketTimeoutException, SocketException {
1436 String rloc = "127.0.0.3";
1438 LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1439 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1440 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1442 new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1443 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1444 sendProxyMapRequest(rloc, port, adLcaf);
1448 public void testRecievingNonProxyOnXtrPort() throws SocketTimeoutException, SocketException, Throwable {
1450 configLispPlugin.shouldListenOnXtrPort(true);
1451 notificationCalled = false;
1452 final String eid = "10.10.10.10";
1453 String rloc = "127.0.0.3";
1454 int port = LispMessage.XTR_PORT_NUM;
1455 LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1456 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1457 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1459 new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1460 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1461 final MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1462 ((LispMappingService) lms).registerNotificationListener(XtrRequestMapping.class, new NotificationListener<XtrRequestMapping>() {
1465 public void onNotification(XtrRequestMapping notification) {
1466 assertEquals(((LispIpv4Address) mapRequest.getEidRecord().get(0).getLispAddressContainer().getAddress()).getIpv4Address().getValue(),
1468 notificationCalled = true;
1469 LOG.warn("notification arrived");
1472 sendMapRequest(mapRequest, port);
1473 for (int i = 0; i < MAX_NOTIFICATION_RETRYS; i++) {
1474 if (notificationCalled) {
1477 LOG.warn("notification hasn't arrived, sleeping...");
1482 fail("Notification hasn't arrived");
1486 private void sendProxyMapRequest(String rloc, int port, LispAFIAddress adLcaf) throws SocketTimeoutException, SocketException {
1487 String eid = "10.1.0.1";
1488 MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1489 sendMapRequest(mapRequest);
1490 DatagramSocket nonProxySocket = new DatagramSocket(new InetSocketAddress(rloc, port));
1491 MapRequest recievedMapRequest = receiveMapRequest(nonProxySocket);
1492 assertEquals(mapRequest.getNonce(), recievedMapRequest.getNonce());
1493 assertEquals(mapRequest.getSourceEid(), recievedMapRequest.getSourceEid());
1494 assertEquals(mapRequest.getItrRloc(), recievedMapRequest.getItrRloc());
1495 assertEquals(mapRequest.getEidRecord(), recievedMapRequest.getEidRecord());
1496 nonProxySocket.close();
1499 private MapRequest createNonProxyMapRequest(String eid, LispAFIAddress adLcaf) throws SocketTimeoutException {
1500 MapRegister mr = createMapRegister(LispAFIConvertor.asIPAfiAddress(eid));
1501 LocatorRecord record = new LocatorRecordBuilder(mr.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)).setLispAddressContainer(
1502 LispAFIConvertor.toContainer(adLcaf)).build();
1503 mr.getEidToLocatorRecord().get(0).getLocatorRecord().set(0, record);
1504 sendMapRegister(mr);
1505 assertMapNotifyRecieved();
1506 MapRequest mapRequest = createMapRequest(LispAFIConvertor.asIPAfiAddress(eid));
1507 MapRequestBuilder builder = new MapRequestBuilder(mapRequest);
1508 builder.setPitr(true);
1509 mapRequest = builder.build();
1513 private void assertMapNotifyRecieved() throws SocketTimeoutException {
1517 private MapReply receiveMapReply() throws SocketTimeoutException {
1518 return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1521 private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
1522 return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData()));
1525 private MapNotify receiveMapNotify() throws SocketTimeoutException {
1526 return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1529 private void sendMapRequest(MapRequest mapRequest) {
1530 sendMapRequest(mapRequest, LispMessage.PORT_NUM);
1533 private void sendMapRequest(MapRequest mapRequest, int port) {
1534 sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array(), port);
1537 private void sendMapRegister(MapRegister mapRegister) {
1538 sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
1541 private void sendPacket(byte[] bytesToSend) {
1542 sendPacket(bytesToSend, LispMessage.PORT_NUM);
1545 private void sendPacket(byte[] bytesToSend, int port) {
1547 DatagramPacket packet = new DatagramPacket(bytesToSend, bytesToSend.length);
1548 initPacketAddress(packet, port);
1549 LOG.trace("Sending packet to LispPlugin on socket, port {}", port);
1550 socket.send(packet);
1551 } catch (Throwable t) {
1556 private DatagramPacket receivePacket() throws SocketTimeoutException {
1557 return receivePacket(6000);
1560 private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
1561 return receivePacket(socket, timeout);
1564 private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException {
1566 byte[] buffer = new byte[4096];
1567 DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
1568 LOG.trace("Waiting for packet from socket...");
1569 receivedSocket.setSoTimeout(timeout);
1570 receivedSocket.receive(receivePacket);
1571 LOG.trace("Recieved packet from socket!");
1572 return receivePacket;
1573 } catch (SocketTimeoutException ste) {
1575 } catch (Throwable t) {
1581 private void initPacketAddress(DatagramPacket packet, int port) throws UnknownHostException {
1582 packet.setAddress(InetAddress.getByName(lispBindAddress));
1583 packet.setPort(port);
1586 private DatagramSocket initSocket(DatagramSocket socket, int port) {
1588 socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
1589 } catch (SocketException e) {
1590 e.printStackTrace();
1596 private byte[] extractWSUdpByteArray(String wiresharkHex) {
1597 final int HEADER_LEN = 42;
1598 byte[] res = new byte[1000];
1599 String[] split = wiresharkHex.split(" ");
1601 for (String cur : split) {
1603 if (cur.length() == 2) {
1605 if (counter > HEADER_LEN) {
1606 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
1611 return Arrays.copyOf(res, counter - HEADER_LEN);
1614 private String stateToString(int state) {
1618 case Bundle.INSTALLED:
1620 case Bundle.RESOLVED:
1622 case Bundle.UNINSTALLED:
1623 return "UNINSTALLED";
1625 return "Not CONVERTED";
1629 @SuppressWarnings({ "rawtypes", "unchecked" })
1630 private void areWeReady() throws InvalidSyntaxException {
1632 boolean debugit = false;
1633 Bundle b[] = bc.getBundles();
1634 for (Bundle element : b) {
1635 int state = element.getState();
1636 LOG.trace("Bundle[" + element.getBundleId() + "]:" + element.getSymbolicName() + ",v" + element.getVersion() + ", state:"
1637 + stateToString(state));
1638 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
1639 LOG.trace("Bundle:" + element.getSymbolicName() + " state:" + stateToString(state));
1642 // String host = element.getHeaders().get("FRAGMENT-HOST");
1643 // if (host != null) {
1644 // LOG.warn("Bundle " + element.getSymbolicName() +
1645 // " is a fragment which is part of: " + host);
1646 // LOG.warn("Required imports are: " +
1647 // element.getHeaders().get("IMPORT-PACKAGE"));
1651 // } catch (BundleException e) {
1652 // LOG.error("BundleException:", e);
1661 LOG.warn(("Do some debugging because some bundle is unresolved"));
1663 // assertNotNull(broker);
1666 ServiceReference r = null;
1667 while (this.lms == null && retry < MAX_SERVICE_LOAD_RETRIES) {
1669 r = bc.getServiceReference(IFlowMapping.class.getName());
1670 // r.getPropertyKeys();
1672 this.lms = (IFlowMapping) bc.getService(r);
1676 } catch (InterruptedException e) {
1682 assertNotNull(IFlowMapping.class.getName() + " service wasn't found in bundle context ", this.lms);
1684 r = bc.getServiceReference(ILispDAO.class.getName());
1686 this.clusterService = (ClusterDAOService) bc.getService(r);
1689 assertNotNull(ILispDAO.class.getName() + " service wasn't found in bundle context ", this.clusterService);
1690 r = bc.getServiceReference(IConfigLispSouthboundPlugin.class.getName());
1692 this.configLispPlugin = (IConfigLispSouthboundPlugin) bc.getService(r);
1695 assertNotNull(IConfigLispSouthboundPlugin.class.getName() + " service wasn't found in bundle context ", this.configLispPlugin);
1696 configLispPlugin.setLispAddress(lispBindAddress);
1698 // Uncomment this code to Know which services were actually loaded to
1701 * for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
1702 * LOG.trace(sr.getBundle().getSymbolicName());
1703 * LOG.trace(sr.toString()); }
1707 } catch (InterruptedException e) {
1711 private Ipv4 asIPAfiAddress(String ip) {
1712 return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1715 private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 asPrimitiveIPAfiAddress(String ip) {
1716 return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4Builder()
1717 .setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1720 private Mac asMacAfiAddress(String mac) {
1721 return new MacBuilder().setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1724 private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac asPrimitiveMacAfiAddress(String mac) {
1725 return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.MacBuilder()
1726 .setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1729 private void cleanUP() {
1732 configLispPlugin.shouldListenOnXtrPort(false);
1733 socket = initSocket(socket, LispMessage.PORT_NUM);