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.junitBundles;
15 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
16 import static org.ops4j.pax.exam.CoreOptions.options;
17 import static org.ops4j.pax.exam.CoreOptions.systemPackages;
18 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.net.DatagramPacket;
26 import java.net.DatagramSocket;
27 import java.net.HttpURLConnection;
28 import java.net.InetAddress;
29 import java.net.InetSocketAddress;
30 import java.net.MalformedURLException;
31 import java.net.SocketException;
32 import java.net.SocketTimeoutException;
34 import java.net.UnknownHostException;
35 import java.nio.ByteBuffer;
36 import java.nio.charset.Charset;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.concurrent.TimeUnit;
41 import javax.inject.Inject;
43 import org.apache.commons.codec.binary.Base64;
44 import org.codehaus.jettison.json.JSONException;
45 import org.codehaus.jettison.json.JSONObject;
46 import org.codehaus.jettison.json.JSONTokener;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.opendaylight.controller.test.sal.binding.it.TestHelper;
52 import org.opendaylight.lispflowmapping.implementation.LispMappingService;
53 import org.opendaylight.lispflowmapping.implementation.dao.ClusterDAOService;
54 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
55 import org.opendaylight.lispflowmapping.implementation.serializer.MapNotifySerializer;
56 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
57 import org.opendaylight.lispflowmapping.implementation.serializer.MapReplySerializer;
58 import org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer;
59 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
60 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
61 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
62 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
63 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
64 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispPlugin;
65 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafApplicationDataAddress;
66 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafListAddress;
67 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafSegmentAddress;
68 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafSourceDestAddress;
69 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafTrafficEngineeringAddress;
70 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
71 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispIpv4Address;
72 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispMacAddress;
73 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
74 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
75 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
76 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
77 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.ReencapHop;
78 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
79 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
80 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
81 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord.Action;
82 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
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.LispAddressContainerBuilder;
92 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4;
93 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
94 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv6;
95 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv6Builder;
96 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationDataBuilder;
97 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafListBuilder;
98 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSegmentBuilder;
99 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSourceDest;
100 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSourceDestBuilder;
101 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafTrafficEngineeringBuilder;
102 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Mac;
103 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.MacBuilder;
104 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.NoBuilder;
105 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
106 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
107 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapregisternotification.MapRegisterBuilder;
108 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
109 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRlocBuilder;
110 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
111 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
112 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.reencaphop.Hop;
113 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.reencaphop.HopBuilder;
114 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
115 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
116 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
117 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
118 import org.ops4j.pax.exam.Configuration;
119 import org.ops4j.pax.exam.Option;
120 import org.ops4j.pax.exam.junit.PaxExam;
121 import org.ops4j.pax.exam.util.Filter;
122 import org.ops4j.pax.exam.util.PathUtils;
123 import org.osgi.framework.Bundle;
124 import org.osgi.framework.BundleContext;
125 import org.osgi.framework.BundleException;
126 import org.osgi.framework.InvalidSyntaxException;
127 import org.osgi.framework.ServiceReference;
128 import org.slf4j.Logger;
129 import org.slf4j.LoggerFactory;
131 import aQute.lib.osgi.Constants;
133 @RunWith(PaxExam.class)
134 public class MappingServiceIntegrationTest {
136 private IFlowMapping lms;
137 private ClusterDAOService clusterService;
138 protected static final Logger logger = LoggerFactory.getLogger(MappingServiceIntegrationTest.class);
139 private byte[] mapRequestPacket;
140 private byte[] mapRegisterPacketWithNotify;
141 private byte[] mapRegisterPacketWithoutNotify;
142 private IConfigLispPlugin configLispPlugin;
143 int lispPortNumber = LispMessage.PORT_NUM;
144 String lispBindAddress = "127.0.0.1";
145 String ourAddress = "127.0.0.2";
146 private LispAFIAddress locatorEid;
147 private DatagramSocket socket;
148 private byte[] mapRegisterPacketWithAuthenticationAndMapNotify;
150 public static final String ODL = "org.opendaylight.controller";
151 public static final String YANG = "org.opendaylight.yangtools";
152 public static final String JERSEY = "com.sun.jersey";
153 private static final String DEBUG_PORT = "8005";
154 private static final int MAX_SERVICE_LOAD_RETRIES = 45;
157 public void after() {
158 if (socket != null) {
161 if (connection != null) {
162 connection.disconnect();
167 public void before() throws Exception {
169 locatorEid = asIPAfiAddress("4.3.2.1");
170 socket = initSocket(socket);
172 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
173 // LISP(Type = 8 - Encapsulated)
174 // IP: 192.168.136.10 -> 1.2.3.4
176 // LISP(Type = 1 Map-Request
180 // Source EID not present
181 // Nonce: 0x3d8d2acd39c8d608
182 // ITR-RLOC AFI=1 Address=192.168.136.10
183 // Record 1: 1.2.3.4/32
184 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
185 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
186 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
187 + "0030 00 38 d4 31 00 00 ff 11 56 f3 7f 00 00 02 01 02 "
188 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
189 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 00 02 00 20 " //
190 + "0060 00 01 01 02 03 04"));
192 // IP: 192.168.136.10 -> 128.223.156.35
193 // UDP: 49289 -> 4342
194 // LISP(Type = 3 Map-Register, P=1, M=1
198 // AuthDataLength: 20 Data:
199 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
200 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
202 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
203 // Priority/Weight: 1/100, Multicast Priority/Weight:
207 mapRegisterPacketWithAuthenticationAndMapNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
208 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
209 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
210 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
211 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
212 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
213 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
215 // IP: 192.168.136.10 -> 128.223.156.35
216 // UDP: 49289 -> 4342
217 // LISP(Type = 3 Map-Register, P=1, M=1
220 // Key ID: 0x0000 NO AUTHENTICATION!!
221 // AuthDataLength: 00 Data:
222 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
224 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
225 // Priority/Weight: 1/100, Multicast Priority/Weight:
229 mapRegisterPacketWithNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
230 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
231 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
232 + "0030 00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
233 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
234 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
235 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
237 // IP: 192.168.136.10 -> 128.223.156.35
238 // UDP: 49289 -> 4342
239 // LISP(Type = 3 Map-Register, P=1, M=1
242 // Key ID: 0x0000 NO AUTHENTICATION!!
243 // AuthDataLength: 00 Data:
244 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
246 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
247 // Priority/Weight: 1/100, Multicast Priority/Weight:
251 mapRegisterPacketWithoutNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
252 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
253 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
254 + "0030 00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
255 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
256 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
257 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
261 private BundleContext bc;
262 private HttpURLConnection connection;
264 // Configure the OSGi container
266 public Option[] config() {
267 return options(MappingServiceTestHelper.mappingServiceBundles());
271 public void testSimpleUsage() throws Exception {
273 mapRegisterWithMapNotify();
274 mapRegisterWithMapNotifyAndMapRequest();
275 registerAndQuery__MAC();
276 mapRequestMapRegisterAndMapRequest();
277 mapRegisterWithAuthenticationWithoutConfiguringAKey();
278 mapRegisterWithoutMapNotify();
282 public void testLCAFs() throws Exception {
283 registerAndQuery__SrcDestLCAF();
284 registerAndQuery__ListLCAF();
285 registerAndQuery__SrcDestLCAF();
286 registerAndQuery__ApplicationData();
287 registerAndQuery__TrafficEngineering();
291 public void testMask() throws Exception {
292 testPasswordExactMatch();
293 testPasswordMaskMatch();
294 eidPrefixLookupIPv4();
295 eidPrefixLookupIPv6();
299 public void testNorthbound() throws Exception {
301 northboundAddMapping();
302 northboundRetrieveKey();
303 northboundRetrieveMapping();
304 northboundRetrieveSourceDestKey();
305 northboundRetrieveSourceDestMapping();
309 public void testTimeOuts() throws Exception {
310 mapRequestMapRegisterAndMapRequestTestTimeout();
311 mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse();
315 public void testOverWriting() throws Exception {
316 testMapRegisterDosntOverwrites();
317 testMapRegisterOverwrites();
320 // ------------------------------- Simple Tests ---------------------------
322 public void mapRequestSimple() throws SocketTimeoutException {
324 sendPacket(mapRequestPacket);
325 ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
326 MapReply reply = MapReplySerializer.getInstance().deserialize(readBuf);
327 assertEquals(4435248268955932168L, reply.getNonce().longValue());
331 public void mapRegisterWithMapNotify() throws SocketTimeoutException {
333 sendPacket(mapRegisterPacketWithNotify);
334 MapNotify reply = receiveMapNotify();
335 assertEquals(7, reply.getNonce().longValue());
338 public void mapRegisterWithMapNotifyAndMapRequest() throws SocketTimeoutException {
340 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
342 MapReply mapReply = registerAddressAndQuery(eid, 32);
344 assertEquals(4, mapReply.getNonce().longValue());
345 assertEquals(LispAFIConvertor.toContainer(locatorEid), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
346 .getLispAddressContainer());
350 public void registerAndQuery__MAC() throws SocketTimeoutException {
352 String macAddress = "01:02:03:04:05:06";
354 MapReply reply = registerAddressAndQuery(asMacAfiAddress(macAddress));
357 LispAFIAddress addressFromNetwork = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
358 assertTrue(addressFromNetwork instanceof LispMacAddress);
359 String macAddressFromReply = ((Mac) addressFromNetwork).getMacAddress().getValue();
361 assertEquals(macAddress, macAddressFromReply);
364 public void mapRequestMapRegisterAndMapRequest() throws SocketTimeoutException {
366 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
367 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
368 mapRequestBuilder.setNonce((long) 4);
369 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
370 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
371 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
372 mapRequestBuilder.getEidRecord().add(
373 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
374 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
375 mapRequestBuilder.getItrRloc().add(
376 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
377 sendMapRequest(mapRequestBuilder.build());
378 MapReply mapReply = receiveMapReply();
379 assertEquals(4, mapReply.getNonce().longValue());
380 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
381 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
382 mapRegisterbuilder.setWantMapNotify(true);
383 mapRegisterbuilder.setNonce((long) 8);
384 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
385 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
386 etlrBuilder.setMaskLength((short) 32);
387 etlrBuilder.setRecordTtl(254);
388 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
389 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
390 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
391 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
392 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
393 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
394 sendMapRegister(mapRegisterbuilder.build());
395 MapNotify mapNotify = receiveMapNotify();
396 assertEquals(8, mapNotify.getNonce().longValue());
397 sendMapRequest(mapRequestBuilder.build());
398 mapReply = receiveMapReply();
399 assertEquals(4, mapReply.getNonce().longValue());
400 assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
401 .getLispAddressContainer());
405 public void testMapRegisterDosntOverwrites() throws SocketTimeoutException {
407 LispIpv4Address eid = asIPAfiAddress("1.2.3.4");
408 LispIpv4Address rloc1 = asIPAfiAddress("4.3.2.1");
409 Ipv4 rloc2 = asIPAfiAddress("4.3.2.2");
410 MapReply mapReply = sendMapRegisterTwice(eid, rloc1, rloc2);
411 assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
412 assertEquals(LispAFIConvertor.toContainer(rloc1), mapReply.getEidToLocatorRecord().get(0)
413 .getLocatorRecord().get(0).getLispAddressContainer());
414 assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0)
415 .getLocatorRecord().get(1).getLispAddressContainer());
418 public void testMapRegisterOverwrites() throws SocketTimeoutException {
420 this.lms.setShouldOverwriteRlocs(true);
421 LispIpv4Address eid = asIPAfiAddress("1.2.3.4");
422 LispIpv4Address rloc1 = asIPAfiAddress("4.3.2.1");
423 Ipv4 rloc2 = asIPAfiAddress("4.3.2.2");
424 MapReply mapReply = sendMapRegisterTwice(eid, rloc1, rloc2);
425 assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
426 assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0)
427 .getLocatorRecord().get(0).getLispAddressContainer());
430 private MapReply sendMapRegisterTwice(LispIpv4Address eid, LispIpv4Address rloc1, Ipv4 rloc2) throws SocketTimeoutException {
431 MapRegister mb = createMapRegister(eid, rloc1);
433 MapNotify mapNotify = receiveMapNotify();
434 MapRequest mr = createMapRequest(eid);
436 MapReply mapReply = receiveMapReply();
437 assertEquals(mb.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0)
438 .getLocatorRecord().get(0).getLispAddressContainer());
439 MapRegister mb2 = createMapRegister(eid, rloc2);
440 sendMapRegister(mb2);
441 mapNotify = receiveMapNotify();
442 assertEquals(8, mapNotify.getNonce().longValue());
443 mr = createMapRequest(eid);
445 mapReply = receiveMapReply();
449 public void mapRegisterWithAuthenticationWithoutConfiguringAKey() throws SocketTimeoutException {
451 sendPacket(mapRegisterPacketWithAuthenticationAndMapNotify);
454 // If didn't timeout then fail:
456 } catch (SocketTimeoutException ste) {
460 public void mapRegisterWithoutMapNotify() {
462 sendPacket(mapRegisterPacketWithoutNotify);
465 // If didn't timeout then fail:
467 } catch (SocketTimeoutException ste) {
471 // --------------------- Northbound Tests ---------------------------
473 private void northboundAddKey() throws Exception {
475 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
477 String pass = "asdf";
479 URL url = createPutURL("key");
480 String authKeyJSON = createAuthKeyJSON(pass, address, mask);
481 callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
483 String retrievedKey = lms.getAuthenticationKey(LispAFIConvertor.toContainer(address), mask);
485 // Check stored password matches the one sent
486 assertEquals(pass, retrievedKey);
490 private void northboundRetrieveSourceDestKey() throws Exception {
492 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
493 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
494 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
495 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
498 LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
499 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
500 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
501 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
502 String pass = "asdf";
504 lms.addAuthenticationKey(LispAFIConvertor.toContainer(sourceDestAddress), mask1, pass);
506 // URL url = createGetKeyIPv4URL(address1, mask1);
507 URL url = createGetKeySourceDestURL(address1.getAfi(), ((LispIpv4Address) sourceDestAddress.getSrcAddress().getPrimitiveAddress())
508 .getIpv4Address().getValue(), sourceDestAddress.getSrcMaskLength(), ((LispIpv4Address) sourceDestAddress.getDstAddress()
509 .getPrimitiveAddress()).getIpv4Address().getValue(), sourceDestAddress.getDstMaskLength());
510 String reply = callURL("GET", null, "application/json", null, url);
511 JSONTokener jt = new JSONTokener(reply);
512 JSONObject json = new JSONObject(jt);
514 // test that the password matches what was we expected.
515 assertEquals(pass, json.get("key"));
519 private void northboundRetrieveKey() throws Exception {
521 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
523 String pass = "asdf";
525 lms.addAuthenticationKey(LispAFIConvertor.toContainer(address), mask, pass);
527 URL url = createGetKeyIPv4URL(address, mask);
528 String reply = callURL("GET", null, "application/json", null, url);
529 JSONTokener jt = new JSONTokener(reply);
530 JSONObject json = new JSONObject(jt);
532 // test that the password matches what was we expected.
533 assertEquals(pass, json.get("key"));
537 private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
538 return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
539 + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
542 private void northboundAddMapping() throws Exception {
544 String pass = "asdf";
545 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
547 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
549 // NB add mapping always checks the key
550 lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
552 URL url = createPutURL("mapping");
553 String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
554 callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
556 // Retrieve the RLOC from the database
557 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
558 mapRequestBuilder.setPitr(false);
559 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
560 mapRequestBuilder.getEidRecord().add(
561 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
562 MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
564 LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
567 assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
571 private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
572 String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ " + "\"proxyMapReply\" : false, "
573 + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : " + "{ " + "\"ipAddress\" : \""
574 + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "}," + "\"mapVersion\" : 0,"
575 + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ " + "\"multicastPriority\" : 1,"
576 + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\"," + "\"afi\" : "
577 + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50," + "\"rlocProbed\" : false, "
578 + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], " + "\"recordTtl\" : 100" + "} " + "], "
579 + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
584 private void northboundRetrieveMapping() throws Exception {
586 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
588 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
589 // Insert mapping in the database
590 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
591 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
592 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
593 etlr.setMaskLength((short) mask);
594 etlr.setRecordTtl(254);
595 etlr.setAuthoritative(false);
596 etlr.setAction(Action.NoAction);
597 LocatorRecordBuilder record = new LocatorRecordBuilder();
598 record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
599 record.setRouted(true);
600 record.setRlocProbed(false);
601 record.setLocalLocator(false);
602 record.setPriority((short) 1);
603 record.setWeight((short) 50);
604 record.setMulticastPriority((short) 1);
605 record.setMulticastWeight((short) 1);
606 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
607 etlr.getLocatorRecord().add(record.build());
608 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
609 mapRegister.getEidToLocatorRecord().add(etlr.build());
610 lms.handleMapRegister(mapRegister.build());
612 // Get mapping using NB interface. No IID used
613 URL url = createGetMappingIPv4URL(0, eid, mask);
614 String reply = callURL("GET", null, "application/json", null, url);
615 JSONTokener jt = new JSONTokener(reply);
616 JSONObject json = new JSONObject(jt);
618 // With just one locator, locators is not a JSONArray
619 String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
621 assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
625 private void northboundRetrieveSourceDestMapping() throws Exception {
627 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
628 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
629 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
630 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
633 LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
634 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
635 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
636 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
637 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
639 // Insert mapping in the database
640 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
641 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
642 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(sourceDestAddress));
643 etlr.setMaskLength((short) mask1);
644 etlr.setRecordTtl(254);
645 etlr.setAuthoritative(false);
646 etlr.setAction(Action.NoAction);
647 LocatorRecordBuilder record = new LocatorRecordBuilder();
648 record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
649 record.setRouted(true);
650 record.setRlocProbed(false);
651 record.setLocalLocator(false);
652 record.setPriority((short) 1);
653 record.setWeight((short) 50);
654 record.setMulticastPriority((short) 1);
655 record.setMulticastWeight((short) 1);
656 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
657 etlr.getLocatorRecord().add(record.build());
658 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
659 mapRegister.getEidToLocatorRecord().add(etlr.build());
660 lms.handleMapRegister(mapRegister.build());
662 // Get mapping using NB interface. No IID used
663 URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
665 String reply = callURL("GET", null, "application/json", null, url);
666 JSONTokener jt = new JSONTokener(reply);
667 JSONObject json = new JSONObject(jt);
669 // With just one locator, locators is not a JSONArray
670 String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
672 assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
676 private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
677 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key", address.getAfi().shortValue(),
678 address.getIpv4Address().getValue(), mask);
679 URL url = new URL(restUrl);
683 private URL createGetKeySourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
684 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "key", afi, srcAddress, srcMask,
685 dstAddress, dstMask);
686 URL url = new URL(restUrl);
690 private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
691 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "mapping", afi, srcAddress,
692 srcMask, dstAddress, dstMask);
693 URL url = new URL(restUrl);
697 private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
698 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping", iid, address.getAfi()
699 .shortValue(), address.getIpv4Address().getValue(), mask);
700 URL url = new URL(restUrl);
704 private URL createPutURL(String resource) throws MalformedURLException {
706 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
708 URL url = new URL(restUrl);
712 private String createAuthenticationString() {
713 String authString = "admin:admin";
714 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
715 String authStringEnc = new String(authEncBytes);
716 return authStringEnc;
719 private String callURL(String method, String content, String accept, String body, URL url) throws IOException, JSONException {
720 String authStringEnc = createAuthenticationString();
721 connection = (HttpURLConnection) url.openConnection();
722 connection.setRequestMethod(method);
723 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
724 if (content != null) {
725 connection.setRequestProperty("Content-Type", content);
727 if (accept != null) {
728 connection.setRequestProperty("Accept", accept);
731 // now add the request body
732 connection.setDoOutput(true);
733 OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
737 connection.connect();
739 // getting the result, first check response code
740 Integer httpResponseCode = connection.getResponseCode();
742 if (httpResponseCode > 299) {
743 logger.trace("HTTP Address: " + url);
744 logger.trace("HTTP Response Code: " + httpResponseCode);
748 InputStream is = connection.getInputStream();
749 BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
750 StringBuilder sb = new StringBuilder();
752 while ((cp = rd.read()) != -1) {
753 sb.append((char) cp);
756 connection.disconnect();
757 return (sb.toString());
760 // timePeriod - in ms
761 public void assertNoPacketReceived(int timePeriod) {
763 receivePacket(timePeriod);
764 // If didn't timeout then fail:
766 } catch (SocketTimeoutException ste) {
770 // ------------------------------- Mask Tests ---------------------------
772 public void eidPrefixLookupIPv4() throws SocketTimeoutException {
774 runPrefixTest(LispAFIConvertor.asIPAfiAddress("1.2.3.4"), 16, LispAFIConvertor.asIPAfiAddress("1.2.3.2"),
775 LispAFIConvertor.asIPAfiAddress("1.1.1.1"), (byte) 32);
778 public void eidPrefixLookupIPv6() throws SocketTimeoutException {
780 runPrefixTest(LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:6:7:8"), 64, LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:1:2:3"),
781 LispAFIConvertor.asIPv6AfiAddress("1:2:3:1:2:3:1:2"), 128);
784 private void runPrefixTest(LispAFIAddress registerEID, int registerdMask, LispAFIAddress matchedAddress, LispAFIAddress unMatchedAddress, int mask)
785 throws SocketTimeoutException {
787 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
788 mapRegister.setWantMapNotify(true);
789 mapRegister.setNonce((long) 8);
790 mapRegister.setWantMapNotify(true);
791 mapRegister.setKeyId((short) 0);
792 mapRegister.setAuthenticationData(new byte[0]);
793 mapRegister.setNonce((long) 8);
794 mapRegister.setProxyMapReply(false);
795 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
796 etlr.setRecordTtl(254);
797 etlr.setAction(Action.NoAction);
798 etlr.setAuthoritative(false);
799 etlr.setMapVersion((short) 0);
800 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(registerEID));
801 etlr.setMaskLength((short) registerdMask);
802 etlr.setRecordTtl(254);
803 LocatorRecordBuilder record = new LocatorRecordBuilder();
804 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
805 record.setLocalLocator(false);
806 record.setRlocProbed(false);
807 record.setRouted(true);
808 record.setMulticastPriority((short) 0);
809 record.setMulticastWeight((short) 0);
810 record.setPriority((short) 0);
811 record.setWeight((short) 0);
812 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
813 etlr.getLocatorRecord().add(record.build());
814 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
815 mapRegister.getEidToLocatorRecord().add(etlr.build());
816 sendMapRegister(mapRegister.build());
817 MapNotify mapNotify = receiveMapNotify();
818 assertEquals(8, mapNotify.getNonce().longValue());
819 MapRequestBuilder mapRequest = new MapRequestBuilder();
820 mapRequest.setNonce((long) 4);
821 mapRequest.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
822 mapRequest.setEidRecord(new ArrayList<EidRecord>());
823 mapRequest.setAuthoritative(false);
824 mapRequest.setMapDataPresent(false);
825 mapRequest.setPitr(false);
826 mapRequest.setProbe(false);
827 mapRequest.setSmr(false);
828 mapRequest.setSmrInvoked(false);
829 mapRequest.getEidRecord().add(
830 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(matchedAddress)).build());
831 mapRequest.setItrRloc(new ArrayList<ItrRloc>());
832 mapRequest.getItrRloc().add(
833 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ourAddress))).build());
834 sendMapRequest(mapRequest.build());
835 MapReply mapReply = receiveMapReply();
836 assertEquals(4, mapReply.getNonce().longValue());
837 assertEquals(record.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
838 mapRequest.setEidRecord(new ArrayList<EidRecord>());
839 mapRequest.getEidRecord().add(
840 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(unMatchedAddress)).build());
841 sendMapRequest(mapRequest.build());
842 mapReply = receiveMapReply();
843 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
846 // This registers an IP with a MapRegister, then adds a password via the
847 // northbound REST API
848 // and checks that the password works
849 public void testPasswordExactMatch() throws Exception {
851 String ipString = "10.0.0.1";
852 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress(ipString);
854 String pass = "pass";
856 URL url = createPutURL("key");
858 String jsonAuthData = createAuthKeyJSON(pass, address, mask);
860 logger.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
861 logger.trace("Address: " + address);
863 byte[] expectedSha = new byte[] { (byte) 146, (byte) 234, (byte) 52, (byte) 247, (byte) 186, (byte) 232, (byte) 31, (byte) 249, (byte) 87,
864 (byte) 73, (byte) 234, (byte) 54, (byte) 225, (byte) 160, (byte) 129, (byte) 251, (byte) 73, (byte) 53, (byte) 196, (byte) 62 };
866 byte[] zeros = new byte[20];
868 callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
870 // build a MapRegister
871 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
872 mapRegister.setWantMapNotify(true);
873 mapRegister.setNonce((long) 8);
874 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
875 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(address));
876 etlr.setMaskLength((short) mask);
877 etlr.setRecordTtl(254);
878 LocatorRecordBuilder record = new LocatorRecordBuilder();
879 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
880 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
881 etlr.getLocatorRecord().add(record.build());
882 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
883 mapRegister.getEidToLocatorRecord().add(etlr.build());
885 mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
886 mapRegister.setAuthenticationData(zeros);
888 sendMapRegister(mapRegister.build());
889 assertNoPacketReceived(3000);
891 mapRegister.setAuthenticationData(expectedSha);
893 sendMapRegister(mapRegister.build());
895 // this will fail if no MapNotify arrives for 6 seconds
896 MapNotify notify = receiveMapNotify();
899 public void testPasswordMaskMatch() throws Exception {
901 LispIpv4Address addressInRange = LispAFIConvertor.asIPAfiAddress("10.20.30.40");
902 LispIpv4Address addressOutOfRange = LispAFIConvertor.asIPAfiAddress("20.40.30.40");
903 LispIpv4Address range = LispAFIConvertor.asIPAfiAddress("10.20.30.0");
906 String pass = "pass";
908 URL url = createPutURL("key");
909 String jsonAuthData = createAuthKeyJSON(pass, range, 8);
911 callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
912 // build a MapRegister
913 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
915 mapRegister.setWantMapNotify(true);
916 mapRegister.setNonce((long) 8);
917 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
918 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressInRange));
919 etlr.setMaskLength((short) mask);
920 etlr.setRecordTtl(254);
921 LocatorRecordBuilder record = new LocatorRecordBuilder();
922 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
923 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
924 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
925 etlr.getLocatorRecord().add(record.build());
926 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
927 mapRegister.getEidToLocatorRecord().add(etlr.build());
929 mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
931 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
933 sendMapRegister(mapRegister.build());
935 // this will fail if no MapNotify arrives for 6 seconds
936 MapNotify notify = receiveMapNotify();
938 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
940 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
942 sendMapRegister(mapRegister.build());
943 assertNoPacketReceived(3000);
946 private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
947 return registerAddressAndQuery(eid, -1);
950 // takes an address, packs it in a MapRegister, sends it, returns the
952 private MapReply registerAddressAndQuery(LispAFIAddress eid, int maskLength) throws SocketTimeoutException {
953 MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
954 mapRegisterBuilder.setWantMapNotify(true);
955 mapRegisterBuilder.setKeyId((short) 0);
956 mapRegisterBuilder.setAuthenticationData(new byte[0]);
957 mapRegisterBuilder.setNonce((long) 8);
958 mapRegisterBuilder.setProxyMapReply(false);
959 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
960 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
961 if (maskLength != -1) {
962 etlrBuilder.setMaskLength((short) maskLength);
964 etlrBuilder.setMaskLength((short) 0);
966 etlrBuilder.setRecordTtl(254);
967 etlrBuilder.setAction(Action.NoAction);
968 etlrBuilder.setAuthoritative(false);
969 etlrBuilder.setMapVersion((short) 0);
970 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
971 recordBuilder.setLocalLocator(false);
972 recordBuilder.setRlocProbed(false);
973 recordBuilder.setRouted(true);
974 recordBuilder.setMulticastPriority((short) 0);
975 recordBuilder.setMulticastWeight((short) 0);
976 recordBuilder.setPriority((short) 0);
977 recordBuilder.setWeight((short) 0);
978 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
979 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
980 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
981 mapRegisterBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
982 mapRegisterBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
983 sendMapRegister(mapRegisterBuilder.build());
984 MapNotify mapNotify = receiveMapNotify();
985 assertEquals(8, mapNotify.getNonce().longValue());
986 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
987 mapRequestBuilder.setNonce((long) 4);
988 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
989 mapRequestBuilder.getEidRecord().add(
990 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
991 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
992 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress)))
994 mapRequestBuilder.getItrRloc().add(
995 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
996 mapRequestBuilder.setAuthoritative(false);
997 mapRequestBuilder.setMapDataPresent(false);
998 mapRequestBuilder.setPitr(false);
999 mapRequestBuilder.setProbe(false);
1000 mapRequestBuilder.setSmr(false);
1001 mapRequestBuilder.setSmrInvoked(false);
1002 sendMapRequest(mapRequestBuilder.build());
1003 return receiveMapReply();
1006 // ------------------------------- LCAF Tests ---------------------------
1008 public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1010 String ipString = "10.20.30.200";
1011 String macString = "01:02:03:04:05:06";
1012 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1013 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1014 LcafSourceDestBuilder builder = new LcafSourceDestBuilder();
1015 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1016 builder.setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode());
1017 builder.setSrcMaskLength((short) 0);
1018 builder.setDstMaskLength((short) 0);
1019 builder.setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(addrToSend1).build());
1020 builder.setDstAddress(new DstAddressBuilder().setPrimitiveAddress(addrToSend2).build());
1022 MapReply reply = registerAddressAndQuery(builder.build());
1024 LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1025 assertTrue(fromNetwork.getAddress() instanceof LcafSourceDestAddress);
1026 LcafSourceDestAddress sourceDestFromNetwork = (LcafSourceDestAddress) fromNetwork.getAddress();
1028 LispAFIAddress receivedAddr1 = (LispAFIAddress) sourceDestFromNetwork.getSrcAddress().getPrimitiveAddress();
1029 LispAFIAddress receivedAddr2 = (LispAFIAddress) sourceDestFromNetwork.getDstAddress().getPrimitiveAddress();
1031 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1032 assertTrue(receivedAddr2 instanceof LispMacAddress);
1034 LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1035 LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1037 assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1038 assertEquals(macString, receivedMAC.getMacAddress().getValue());
1041 public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1043 String macString = "01:02:03:04:05:06";
1044 String ipString = "10.20.255.30";
1045 LcafListBuilder listbuilder = new LcafListBuilder();
1046 listbuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
1047 listbuilder.setAddresses(new ArrayList<Addresses>());
1048 listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1049 listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString))).build());
1051 MapReply reply = registerAddressAndQuery(listbuilder.build());
1053 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1055 assertTrue(receivedAddress instanceof LcafListAddress);
1057 LcafListAddress listAddrFromNetwork = (LcafListAddress) receivedAddress;
1058 LispAFIAddress receivedAddr1 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(0).getPrimitiveAddress();
1059 LispAFIAddress receivedAddr2 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(1).getPrimitiveAddress();
1061 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1062 assertTrue(receivedAddr2 instanceof LispMacAddress);
1064 assertEquals(macString, ((LispMacAddress) receivedAddr2).getMacAddress().getValue());
1065 assertEquals(ipString, ((LispIpv4Address) receivedAddr1).getIpv4Address().getValue());
1068 public void registerAndQuerySegmentLCAF() throws SocketTimeoutException {
1070 String ipString = "10.20.255.30";
1072 LcafSegmentBuilder builder = new LcafSegmentBuilder();
1073 builder.setInstanceId((long) instanceId);
1074 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode());
1075 builder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1077 MapReply reply = registerAddressAndQuery(builder.build());
1079 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1080 assertTrue(receivedAddress instanceof LcafSegmentAddress);
1082 LcafSegmentAddress segmentfromNetwork = (LcafSegmentAddress) receivedAddress;
1083 LispAFIAddress addrFromSegment = (LispAFIAddress) segmentfromNetwork.getAddress().getPrimitiveAddress();
1084 assertTrue(addrFromSegment instanceof LispIpv4Address);
1085 assertEquals(ipString, ((LispIpv4Address) addrFromSegment).getIpv4Address().getValue());
1087 assertEquals(instanceId, segmentfromNetwork.getInstanceId().intValue());
1090 public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1092 String macString = "01:02:03:04:05:06";
1093 String ipString = "10.20.255.30";
1094 HopBuilder hopBuilder = new HopBuilder();
1095 hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString)));
1096 Hop hop1 = hopBuilder.build();
1097 hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString)));
1098 Hop hop2 = hopBuilder.build();
1099 HopsBuilder hb = new HopsBuilder();
1102 hb.setRLOCProbe(false);
1104 HopsBuilder hb2 = new HopsBuilder();
1106 hb2.setLookup(false);
1107 hb2.setRLOCProbe(true);
1108 hb2.setStrict(false);
1109 Hops hops1 = hb.build();
1110 Hops hops2 = hb2.build();
1111 LcafTrafficEngineeringBuilder trafficBuilder = new LcafTrafficEngineeringBuilder();
1112 trafficBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
1113 (short) LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode());
1114 trafficBuilder.setHops(new ArrayList<Hops>());
1115 trafficBuilder.getHops().add(hb.build());
1116 trafficBuilder.getHops().add(hb2.build());
1118 MapReply reply = registerAddressAndQuery(trafficBuilder.build());
1120 assertTrue(LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer()) instanceof LcafTrafficEngineeringAddress);
1122 LcafTrafficEngineeringAddress receivedAddress = (LcafTrafficEngineeringAddress) LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0)
1123 .getLispAddressContainer());
1125 ReencapHop receivedHop1 = receivedAddress.getHops().get(0);
1126 ReencapHop receivedHop2 = receivedAddress.getHops().get(1);
1128 assertEquals(true, hops1.isLookup());
1129 assertEquals(false, hops1.isRLOCProbe());
1130 assertEquals(true, hops1.isStrict());
1132 assertEquals(false, hops2.isLookup());
1133 assertEquals(true, hops2.isRLOCProbe());
1134 assertEquals(false, hops2.isStrict());
1136 assertTrue(receivedHop1.getHop().getPrimitiveAddress() instanceof LispIpv4Address);
1137 assertTrue(receivedHop2.getHop().getPrimitiveAddress() instanceof LispMacAddress);
1139 assertEquals(ipString, ((LispIpv4Address) receivedHop1.getHop().getPrimitiveAddress()).getIpv4Address().getValue());
1140 assertEquals(macString, ((LispMacAddress) receivedHop2.getHop().getPrimitiveAddress()).getMacAddress().getValue());
1143 public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1145 String ipString = "1.2.3.4";
1151 LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
1152 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
1153 builder.setIpTos(ipTOs);
1154 builder.setProtocol(protocol);
1155 builder.setLocalPort(new PortNumber(localPort));
1156 builder.setRemotePort(new PortNumber(remotePort));
1157 builder.setAddress(new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder()
1158 .setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1160 LcafApplicationDataAddress addressToSend = builder.build();
1162 MapReply reply = registerAddressAndQuery(addressToSend);
1164 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1166 assertTrue(receivedAddress instanceof LcafApplicationDataAddress);
1168 LcafApplicationDataAddress receivedApplicationDataAddress = (LcafApplicationDataAddress) receivedAddress;
1169 assertEquals(protocol, receivedApplicationDataAddress.getProtocol().intValue());
1170 assertEquals(ipTOs, receivedApplicationDataAddress.getIpTos().intValue());
1171 assertEquals(localPort, receivedApplicationDataAddress.getLocalPort().getValue().intValue());
1172 assertEquals(remotePort, receivedApplicationDataAddress.getRemotePort().getValue().intValue());
1174 LispIpv4Address ipAddressReceived = (LispIpv4Address) receivedApplicationDataAddress.getAddress().getPrimitiveAddress();
1175 assertEquals(ipString, ipAddressReceived.getIpv4Address().getValue());
1178 // ------------------- TimeOut Tests -----------
1180 public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1182 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1183 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1184 mapRequestBuilder.setNonce((long) 4);
1185 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1186 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1187 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1188 mapRequestBuilder.getEidRecord().add(
1189 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1190 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1191 mapRequestBuilder.getItrRloc().add(
1192 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1193 sendMapRequest(mapRequestBuilder.build());
1194 MapReply mapReply = receiveMapReply();
1195 assertEquals(4, mapReply.getNonce().longValue());
1196 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1197 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1198 mapRegisterbuilder.setWantMapNotify(true);
1199 mapRegisterbuilder.setNonce((long) 8);
1200 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1201 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1202 etlrBuilder.setMaskLength((short) 32);
1203 etlrBuilder.setRecordTtl(254);
1204 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1205 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
1206 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1207 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1208 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1209 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1210 sendMapRegister(mapRegisterbuilder.build());
1211 MapNotify mapNotify = receiveMapNotify();
1212 assertEquals(8, mapNotify.getNonce().longValue());
1213 sendMapRequest(mapRequestBuilder.build());
1214 mapReply = receiveMapReply();
1215 assertEquals(4, mapReply.getNonce().longValue());
1216 assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1217 .getLispAddressContainer());
1218 causeEntryToBeCleaned();
1219 sendMapRequest(mapRequestBuilder.build());
1220 mapReply = receiveMapReply();
1221 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1224 public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1226 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1227 MapRequest mapRequest = createMapRequest(eid);
1229 testTTLBeforeRegister(mapRequest);
1231 registerForTTL(eid);
1233 testTTLAfterRegister(mapRequest);
1235 causeEntryToBeCleaned();
1236 testTTLAfterClean(mapRequest);
1239 testTTLAfterAutherize(mapRequest);
1243 private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1245 sendMapRequest(mapRequest);
1246 mapReply = receiveMapReply();
1247 assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1250 private void causeEntryToBeCleaned() {
1251 clusterService.setTimeUnit(TimeUnit.NANOSECONDS);
1252 clusterService.cleanOld();
1255 private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1257 sendMapRequest(mapRequest);
1258 mapReply = receiveMapReply();
1259 assertEquals(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1260 .getLispAddressContainer());
1261 assertCorrectMapReplyTTLAndAction(mapReply, 254, Action.NoAction);
1264 private void registerForTTL(LispIpv4Address eid) throws SocketTimeoutException {
1265 MapRegister mapRegister = createMapRegister(eid);
1266 sendMapRegister(mapRegister);
1270 private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1272 sendMapRequest(mapRequest);
1273 mapReply = receiveMapReply();
1274 assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1277 private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1279 sendMapRequest(mapRequest);
1280 mapReply = receiveMapReply();
1281 assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1284 private void assertCorrectMapReplyTTLAndAction(MapReply mapReply, int expectedTTL, Action expectedAction) {
1285 assertEquals(expectedTTL, mapReply.getEidToLocatorRecord().get(0).getRecordTtl().intValue());
1286 assertEquals(expectedAction, mapReply.getEidToLocatorRecord().get(0).getAction());
1289 private MapRegister createMapRegister(LispIpv4Address eid, LispIpv4Address rloc) {
1290 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1291 mapRegisterbuilder.setWantMapNotify(true);
1292 mapRegisterbuilder.setNonce((long) 8);
1293 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1294 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1295 etlrBuilder.setMaskLength((short) 24);
1296 etlrBuilder.setRecordTtl(254);
1297 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1298 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
1299 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1300 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1301 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1302 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1303 MapRegister mapRegister = mapRegisterbuilder.build();
1307 private MapRegister createMapRegister(LispIpv4Address eid) {
1308 return createMapRegister(eid, asIPAfiAddress("4.3.2.1"));
1311 private MapRequest createMapRequest(LispIpv4Address eid) {
1312 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1313 mapRequestBuilder.setNonce((long) 4);
1314 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1315 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1316 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1317 mapRequestBuilder.getEidRecord().add(
1318 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1319 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1320 mapRequestBuilder.getItrRloc().add(
1321 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1322 MapRequest mr = mapRequestBuilder.build();
1327 public void testNonProxy() throws SocketTimeoutException, SocketException {
1329 String eid = "10.0.0.1";
1330 String rloc = "127.0.0.3";
1331 MapRegister mr = createMapRegister(LispAFIConvertor.asIPAfiAddress(eid));
1332 LocatorRecord record = new LocatorRecordBuilder(mr.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)).setLispAddressContainer(
1333 LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(rloc))).build();
1334 mr.getEidToLocatorRecord().get(0).getLocatorRecord().set(0, record);
1335 sendMapRegister(mr);
1337 MapRequest mapRequest = createMapRequest(LispAFIConvertor.asIPAfiAddress(eid));
1338 MapRequestBuilder builder = new MapRequestBuilder(mapRequest);
1339 builder.setPitr(true);
1340 mapRequest = builder.build();
1341 sendMapRequest(mapRequest);
1342 socket = new DatagramSocket(new InetSocketAddress(rloc, LispMessage.PORT_NUM));
1343 MapRequest recievedMapRequest = receiveMapRequest();
1344 assertEquals(mapRequest.getNonce(), recievedMapRequest.getNonce());
1345 assertEquals(mapRequest.getSourceEid(), recievedMapRequest.getSourceEid());
1346 assertEquals(mapRequest.getItrRloc(), recievedMapRequest.getItrRloc());
1347 assertEquals(mapRequest.getEidRecord(), recievedMapRequest.getEidRecord());
1351 private MapReply receiveMapReply() throws SocketTimeoutException {
1352 return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1355 private MapRequest receiveMapRequest() throws SocketTimeoutException {
1356 return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(10000).getData()));
1359 private MapNotify receiveMapNotify() throws SocketTimeoutException {
1360 return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1363 private void sendMapRequest(MapRequest mapRequest) {
1364 sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array());
1367 private void sendMapRegister(MapRegister mapRegister) {
1368 sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
1371 private void sendPacket(byte[] bytesToSend) {
1373 DatagramPacket packet = new DatagramPacket(bytesToSend, bytesToSend.length);
1374 initPacketAddress(packet);
1375 logger.trace("Sending MapRegister to LispPlugin on socket");
1376 socket.send(packet);
1377 } catch (Throwable t) {
1382 private DatagramPacket receivePacket() throws SocketTimeoutException {
1383 return receivePacket(6000);
1386 private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
1388 byte[] buffer = new byte[4096];
1389 DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
1390 logger.trace("Waiting for packet from socket...");
1391 socket.setSoTimeout(timeout);
1392 socket.receive(receivePacket);
1393 logger.trace("Recieved packet from socket!");
1394 return receivePacket;
1395 } catch (SocketTimeoutException ste) {
1397 } catch (Throwable t) {
1403 private void initPacketAddress(DatagramPacket packet) throws UnknownHostException {
1404 packet.setAddress(InetAddress.getByName(lispBindAddress));
1405 packet.setPort(lispPortNumber);
1408 private DatagramSocket initSocket(DatagramSocket socket) {
1410 socket = new DatagramSocket(new InetSocketAddress(ourAddress, LispMessage.PORT_NUM));
1411 } catch (SocketException e) {
1417 private byte[] extractWSUdpByteArray(String wiresharkHex) {
1418 final int HEADER_LEN = 42;
1419 byte[] res = new byte[1000];
1420 String[] split = wiresharkHex.split(" ");
1422 for (String cur : split) {
1424 if (cur.length() == 2) {
1426 if (counter > HEADER_LEN) {
1427 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
1432 return Arrays.copyOf(res, counter - HEADER_LEN);
1435 private String stateToString(int state) {
1439 case Bundle.INSTALLED:
1441 case Bundle.RESOLVED:
1443 case Bundle.UNINSTALLED:
1444 return "UNINSTALLED";
1446 return "Not CONVERTED";
1450 private void areWeReady() throws InvalidSyntaxException {
1452 boolean debugit = false;
1453 Bundle b[] = bc.getBundles();
1454 for (Bundle element : b) {
1455 int state = element.getState();
1456 logger.trace("Bundle[" + element.getBundleId() + "]:" + element.getSymbolicName() + ",v" + element.getVersion() + ", state:"
1457 + stateToString(state));
1458 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
1459 logger.trace("Bundle:" + element.getSymbolicName() + " state:" + stateToString(state));
1462 String host = element.getHeaders().get(Constants.FRAGMENT_HOST);
1464 logger.warn("Bundle " + element.getSymbolicName() + " is a fragment which is part of: " + host);
1465 logger.warn("Required imports are: " + element.getHeaders().get(Constants.IMPORT_PACKAGE));
1469 } catch (BundleException e) {
1470 logger.error("BundleException:", e);
1479 logger.warn(("Do some debugging because some bundle is unresolved"));
1481 // assertNotNull(broker);
1484 ServiceReference r = null;
1485 while (this.lms == null && retry < MAX_SERVICE_LOAD_RETRIES) {
1487 r = bc.getServiceReference(IFlowMapping.class.getName());
1488 // r.getPropertyKeys();
1490 this.lms = (IFlowMapping) bc.getService(r);
1494 } catch (InterruptedException e) {
1500 assertNotNull(IFlowMapping.class.getName() + " service wasn't found in bundle context ", this.lms);
1502 r = bc.getServiceReference(ILispDAO.class.getName());
1504 this.clusterService = (ClusterDAOService) bc.getService(r);
1507 assertNotNull(ILispDAO.class.getName() + " service wasn't found in bundle context ", this.clusterService);
1508 r = bc.getServiceReference(IConfigLispPlugin.class.getName());
1510 this.configLispPlugin = (IConfigLispPlugin) bc.getService(r);
1513 assertNotNull(IConfigLispPlugin.class.getName() + " service wasn't found in bundle context ", this.configLispPlugin);
1514 configLispPlugin.setLispAddress(lispBindAddress);
1516 // Uncomment this code to Know which services were actually loaded to
1519 * for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
1520 * logger.trace(sr.getBundle().getSymbolicName());
1521 * logger.trace(sr.toString()); }
1525 } catch (InterruptedException e) {
1529 private LispAddressContainer getIPContainer(String ip) {
1530 return new LispAddressContainerBuilder().setAddress(asIPAfiAddress(ip)).build();
1533 private Ipv4 asIPAfiAddress(String ip) {
1534 return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1537 private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 asPrimitiveIPAfiAddress(String ip) {
1538 return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4Builder()
1539 .setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1542 private Mac asMacAfiAddress(String mac) {
1543 return new MacBuilder().setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1546 private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac asPrimitiveMacAfiAddress(String mac) {
1547 return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.MacBuilder()
1548 .setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1551 private void cleanUP() {
1554 lms.setShouldOverwriteRlocs(false);
1555 socket = initSocket(socket);