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.IConfigLispPlugin;
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.BundleException;
122 import org.osgi.framework.InvalidSyntaxException;
123 import org.osgi.framework.ServiceReference;
124 import org.slf4j.Logger;
125 import org.slf4j.LoggerFactory;
127 import aQute.lib.osgi.Constants;
129 @RunWith(PaxExam.class)
130 public class MappingServiceIntegrationTest {
132 private IFlowMapping lms;
133 private ClusterDAOService clusterService;
134 protected static final Logger logger = LoggerFactory.getLogger(MappingServiceIntegrationTest.class);
135 private byte[] mapRequestPacket;
136 private byte[] mapRegisterPacketWithNotify;
137 private byte[] mapRegisterPacketWithoutNotify;
138 private IConfigLispPlugin configLispPlugin;
139 String lispBindAddress = "127.0.0.1";
140 String ourAddress = "127.0.0.2";
141 private LispAFIAddress locatorEid;
142 private DatagramSocket socket;
143 private byte[] mapRegisterPacketWithAuthenticationAndMapNotify;
145 public static final String ODL = "org.opendaylight.controller";
146 public static final String YANG = "org.opendaylight.yangtools";
147 public static final String JERSEY = "com.sun.jersey";
148 private static final int MAX_SERVICE_LOAD_RETRIES = 45;
149 private static final int MAX_NOTIFICATION_RETRYS = 20;
152 public void after() {
153 if (socket != null) {
156 if (connection != null) {
157 connection.disconnect();
162 public void before() throws Exception {
164 locatorEid = asIPAfiAddress("4.3.2.1");
165 socket = initSocket(socket, LispMessage.PORT_NUM);
167 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
168 // LISP(Type = 8 - Encapsulated)
169 // IP: 192.168.136.10 -> 1.2.3.4
171 // LISP(Type = 1 Map-Request
175 // Source EID not present
176 // Nonce: 0x3d8d2acd39c8d608
177 // ITR-RLOC AFI=1 Address=192.168.136.10
178 // Record 1: 1.2.3.4/32
179 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
180 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
181 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
182 + "0030 00 38 d4 31 00 00 ff 11 56 f3 7f 00 00 02 01 02 "
183 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
184 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 00 02 00 20 " //
185 + "0060 00 01 01 02 03 04"));
187 // IP: 192.168.136.10 -> 128.223.156.35
188 // UDP: 49289 -> 4342
189 // LISP(Type = 3 Map-Register, P=1, M=1
193 // AuthDataLength: 20 Data:
194 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
195 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
197 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
198 // Priority/Weight: 1/100, Multicast Priority/Weight:
202 mapRegisterPacketWithAuthenticationAndMapNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
203 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
204 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
205 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
206 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
207 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
208 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
210 // IP: 192.168.136.10 -> 128.223.156.35
211 // UDP: 49289 -> 4342
212 // LISP(Type = 3 Map-Register, P=1, M=1
215 // Key ID: 0x0000 NO AUTHENTICATION!!
216 // AuthDataLength: 00 Data:
217 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
219 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
220 // Priority/Weight: 1/100, Multicast Priority/Weight:
224 mapRegisterPacketWithNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
225 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
226 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
227 + "0030 00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
228 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
229 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
230 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
232 // IP: 192.168.136.10 -> 128.223.156.35
233 // UDP: 49289 -> 4342
234 // LISP(Type = 3 Map-Register, P=1, M=1
237 // Key ID: 0x0000 NO AUTHENTICATION!!
238 // AuthDataLength: 00 Data:
239 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
241 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
242 // Priority/Weight: 1/100, Multicast Priority/Weight:
246 mapRegisterPacketWithoutNotify = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
247 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
248 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
249 + "0030 00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
250 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
251 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
252 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
256 private BundleContext bc;
257 private HttpURLConnection connection;
258 protected static boolean notificationCalled;
260 // Configure the OSGi container
262 public Option[] config() {
263 return options(MappingServiceTestHelper.mappingServiceBundlesWithClusterDAO());
267 public void testSimpleUsage() throws Exception {
269 mapRegisterWithMapNotify();
270 mapRegisterWithMapNotifyAndMapRequest();
271 registerAndQuery__MAC();
272 mapRequestMapRegisterAndMapRequest();
273 mapRegisterWithAuthenticationWithoutConfiguringAKey();
274 mapRegisterWithoutMapNotify();
278 public void testLCAFs() throws Exception {
279 registerAndQuery__SrcDestLCAF();
280 registerAndQuery__KeyValueLCAF();
281 registerAndQuery__ListLCAF();
282 registerAndQuery__ApplicationData();
283 registerAndQuery__TrafficEngineering();
284 registerAndQuery__SegmentLCAF();
288 public void testMask() throws Exception {
289 testPasswordExactMatch();
290 testPasswordMaskMatch();
291 eidPrefixLookupIPv4();
292 eidPrefixLookupIPv6();
296 public void testNorthbound() throws Exception {
298 northboundAddMapping();
299 northboundRetrieveKey();
300 northboundRetrieveMapping();
301 northboundRetrieveSourceDestKey();
302 northboundRetrieveSourceDestMapping();
306 public void testOverWriting() throws Exception {
307 testMapRegisterDosntOverwritesOtherSubKeys();
308 testMapRegisterOverwritesSameSubkey();
309 testMapRegisterOverwritesNoSubkey();
310 testMapRegisterDoesntOverwritesNoSubkey();
314 public void testTimeOuts() throws Exception {
315 mapRequestMapRegisterAndMapRequestTestTimeout();
316 mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse();
320 public void testNonProxy() throws Throwable {
321 testSimpleNonProxy();
322 testNonProxyOtherPort();
323 testRecievingNonProxyOnXtrPort();
326 // ------------------------------- Simple Tests ---------------------------
328 public void mapRequestSimple() throws SocketTimeoutException {
330 sendPacket(mapRequestPacket);
331 ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
332 MapReply reply = MapReplySerializer.getInstance().deserialize(readBuf);
333 assertEquals(4435248268955932168L, reply.getNonce().longValue());
337 public void mapRegisterWithMapNotify() throws SocketTimeoutException {
339 sendPacket(mapRegisterPacketWithNotify);
340 MapNotify reply = receiveMapNotify();
341 assertEquals(7, reply.getNonce().longValue());
344 public void mapRegisterWithMapNotifyAndMapRequest() throws SocketTimeoutException {
346 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
348 MapReply mapReply = registerAddressAndQuery(eid, 32);
350 assertEquals(4, mapReply.getNonce().longValue());
351 assertEquals(LispAFIConvertor.toContainer(locatorEid), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
352 .getLispAddressContainer());
356 public void registerAndQuery__MAC() throws SocketTimeoutException {
358 String macAddress = "01:02:03:04:05:06";
360 MapReply reply = registerAddressAndQuery(asMacAfiAddress(macAddress));
363 LispAFIAddress addressFromNetwork = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
364 assertTrue(addressFromNetwork instanceof LispMacAddress);
365 String macAddressFromReply = ((Mac) addressFromNetwork).getMacAddress().getValue();
367 assertEquals(macAddress, macAddressFromReply);
370 public void mapRequestMapRegisterAndMapRequest() throws SocketTimeoutException {
372 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
373 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
374 mapRequestBuilder.setNonce((long) 4);
375 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
376 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
377 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
378 mapRequestBuilder.getEidRecord().add(
379 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
380 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
381 mapRequestBuilder.getItrRloc().add(
382 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
383 sendMapRequest(mapRequestBuilder.build());
384 MapReply mapReply = receiveMapReply();
385 assertEquals(4, mapReply.getNonce().longValue());
386 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
387 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
388 mapRegisterbuilder.setWantMapNotify(true);
389 mapRegisterbuilder.setNonce((long) 8);
390 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
391 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
392 etlrBuilder.setMaskLength((short) 32);
393 etlrBuilder.setRecordTtl(254);
394 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
395 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
396 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
397 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
398 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
399 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
400 sendMapRegister(mapRegisterbuilder.build());
401 MapNotify mapNotify = receiveMapNotify();
402 assertEquals(8, mapNotify.getNonce().longValue());
403 sendMapRequest(mapRequestBuilder.build());
404 mapReply = receiveMapReply();
405 assertEquals(4, mapReply.getNonce().longValue());
406 assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
407 .getLispAddressContainer());
411 public void testMapRegisterDosntOverwritesOtherSubKeys() throws SocketTimeoutException {
413 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
414 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
415 LispAFIAddress rloc1 = LispAFIConvertor.asKeyValue("subkey1", LispAFIConvertor.toPrimitive(rloc1Value));
416 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
417 LispAFIAddress rloc2 = LispAFIConvertor.asKeyValue("subkey2", LispAFIConvertor.toPrimitive(rloc2Value));
418 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
419 assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
420 assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
421 assertEquals(LispAFIConvertor.toContainer(rloc1), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(1).getLispAddressContainer());
424 public void testMapRegisterOverwritesSameSubkey() throws SocketTimeoutException {
426 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
427 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
428 LispAFIAddress rloc1 = LispAFIConvertor.asKeyValue("subkey", LispAFIConvertor.toPrimitive(rloc1Value));
429 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
430 LispAFIAddress rloc2 = LispAFIConvertor.asKeyValue("subkey", LispAFIConvertor.toPrimitive(rloc2Value));
431 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
432 assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
433 assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
436 public void testMapRegisterOverwritesNoSubkey() throws SocketTimeoutException {
438 lms.setOverwrite(true);
439 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
440 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
441 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
442 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
443 assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
444 assertEquals(LispAFIConvertor.toContainer(rloc2Value), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
445 .getLispAddressContainer());
448 public void testMapRegisterDoesntOverwritesNoSubkey() throws SocketTimeoutException {
450 lms.setOverwrite(false);
451 LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
452 LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
453 LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
454 MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
455 assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
456 LispAddressContainer rloc1ReturnValueContainer = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer();
457 LispAddressContainer rloc2ReturnValueContainer = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(1).getLispAddressContainer();
458 assertTrue((LispAFIConvertor.toContainer(rloc1Value).equals(rloc1ReturnValueContainer) && LispAFIConvertor.toContainer(rloc2Value).equals(
459 rloc2ReturnValueContainer))
460 || (LispAFIConvertor.toContainer(rloc1Value).equals(rloc2ReturnValueContainer) && LispAFIConvertor.toContainer(rloc2Value).equals(
461 rloc1ReturnValueContainer)));
464 private MapReply sendMapRegisterTwiceWithDiffrentValues(LispAFIAddress eid, LispAFIAddress rloc1, LispAFIAddress rloc2)
465 throws SocketTimeoutException {
466 MapRegister mb = createMapRegister(eid, rloc1);
467 MapNotify mapNotify = lms.handleMapRegister(mb, false);
468 MapRequest mr = createMapRequest(eid);
469 MapReply mapReply = lms.handleMapRequest(mr);
470 assertEquals(mb.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0)
471 .getLocatorRecord().get(0).getLispAddressContainer());
472 mb = createMapRegister(eid, rloc2);
473 mapNotify = lms.handleMapRegister(mb, false);
474 assertEquals(8, mapNotify.getNonce().longValue());
475 mr = createMapRequest(eid);
477 mapReply = lms.handleMapRequest(mr);
481 public void mapRegisterWithAuthenticationWithoutConfiguringAKey() throws SocketTimeoutException {
483 sendPacket(mapRegisterPacketWithAuthenticationAndMapNotify);
486 // If didn't timeout then fail:
488 } catch (SocketTimeoutException ste) {
492 public void mapRegisterWithoutMapNotify() {
494 sendPacket(mapRegisterPacketWithoutNotify);
497 // If didn't timeout then fail:
499 } catch (SocketTimeoutException ste) {
503 // --------------------- Northbound Tests ---------------------------
505 private void northboundAddKey() throws Exception {
507 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
509 String pass = "asdf";
511 URL url = createPutURL("key");
512 String authKeyJSON = createAuthKeyJSON(pass, address, mask);
513 callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
515 String retrievedKey = lms.getAuthenticationKey(LispAFIConvertor.toContainer(address), mask);
517 // Check stored password matches the one sent
518 assertEquals(pass, retrievedKey);
522 private void northboundRetrieveSourceDestKey() throws Exception {
524 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
525 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
526 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
527 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
530 LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
531 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
532 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
533 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
534 String pass = "asdf";
536 lms.addAuthenticationKey(LispAFIConvertor.toContainer(sourceDestAddress), mask1, pass);
538 // URL url = createGetKeyIPv4URL(address1, mask1);
539 URL url = createGetKeySourceDestURL(address1.getAfi(), ((LispIpv4Address) sourceDestAddress.getSrcAddress().getPrimitiveAddress())
540 .getIpv4Address().getValue(), sourceDestAddress.getSrcMaskLength(), ((LispIpv4Address) sourceDestAddress.getDstAddress()
541 .getPrimitiveAddress()).getIpv4Address().getValue(), sourceDestAddress.getDstMaskLength());
542 String reply = callURL("GET", null, "application/json", null, url);
543 JSONTokener jt = new JSONTokener(reply);
544 JSONObject json = new JSONObject(jt);
546 // test that the password matches what was we expected.
547 assertEquals(pass, json.get("key"));
551 private void northboundRetrieveKey() throws Exception {
553 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
555 String pass = "asdf";
557 lms.addAuthenticationKey(LispAFIConvertor.toContainer(address), mask, pass);
559 URL url = createGetKeyIPv4URL(address, mask);
560 String reply = callURL("GET", null, "application/json", null, url);
561 JSONTokener jt = new JSONTokener(reply);
562 JSONObject json = new JSONObject(jt);
564 // test that the password matches what was we expected.
565 assertEquals(pass, json.get("key"));
569 private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
570 return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
571 + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
574 private void northboundAddMapping() throws Exception {
576 String pass = "asdf";
577 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
579 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
581 // NB add mapping always checks the key
582 lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
584 URL url = createPutURL("mapping");
585 String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
586 callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
588 // Retrieve the RLOC from the database
589 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
590 mapRequestBuilder.setPitr(false);
591 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
592 mapRequestBuilder.getEidRecord().add(
593 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
594 MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
596 LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
599 assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
603 private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
604 String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ " + "\"proxyMapReply\" : false, "
605 + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : " + "{ " + "\"ipAddress\" : \""
606 + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "}," + "\"mapVersion\" : 0,"
607 + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ " + "\"multicastPriority\" : 1,"
608 + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\"," + "\"afi\" : "
609 + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50," + "\"rlocProbed\" : false, "
610 + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], " + "\"recordTtl\" : 100" + "} " + "], "
611 + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
616 private void northboundRetrieveMapping() throws Exception {
618 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
620 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
621 // Insert mapping in the database
622 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
623 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
624 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
625 etlr.setMaskLength((short) mask);
626 etlr.setRecordTtl(254);
627 etlr.setAuthoritative(false);
628 etlr.setAction(Action.NoAction);
629 LocatorRecordBuilder record = new LocatorRecordBuilder();
630 record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
631 record.setRouted(true);
632 record.setRlocProbed(false);
633 record.setLocalLocator(false);
634 record.setPriority((short) 1);
635 record.setWeight((short) 50);
636 record.setMulticastPriority((short) 1);
637 record.setMulticastWeight((short) 1);
638 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
639 etlr.getLocatorRecord().add(record.build());
640 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
641 mapRegister.getEidToLocatorRecord().add(etlr.build());
642 lms.handleMapRegister(mapRegister.build(), false);
644 // Get mapping using NB interface. No IID used
645 URL url = createGetMappingIPv4URL(0, eid, mask);
646 String reply = callURL("GET", null, "application/json", null, url);
647 JSONTokener jt = new JSONTokener(reply);
648 JSONObject json = new JSONObject(jt);
650 // With just one locator, locators is not a JSONArray
651 String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
653 assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
657 private void northboundRetrieveSourceDestMapping() throws Exception {
659 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
660 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
661 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
662 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
665 LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
666 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
667 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
668 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
669 LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
671 // Insert mapping in the database
672 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
673 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
674 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(sourceDestAddress));
675 etlr.setMaskLength((short) mask1);
676 etlr.setRecordTtl(254);
677 etlr.setAuthoritative(false);
678 etlr.setAction(Action.NoAction);
679 LocatorRecordBuilder record = new LocatorRecordBuilder();
680 record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
681 record.setRouted(true);
682 record.setRlocProbed(false);
683 record.setLocalLocator(false);
684 record.setPriority((short) 1);
685 record.setWeight((short) 50);
686 record.setMulticastPriority((short) 1);
687 record.setMulticastWeight((short) 1);
688 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
689 etlr.getLocatorRecord().add(record.build());
690 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
691 mapRegister.getEidToLocatorRecord().add(etlr.build());
692 lms.handleMapRegister(mapRegister.build(), false);
694 // Get mapping using NB interface. No IID used
695 URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
697 String reply = callURL("GET", null, "application/json", null, url);
698 JSONTokener jt = new JSONTokener(reply);
699 JSONObject json = new JSONObject(jt);
701 // With just one locator, locators is not a JSONArray
702 String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
704 assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
708 private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
709 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key", address.getAfi().shortValue(),
710 address.getIpv4Address().getValue(), mask);
711 URL url = new URL(restUrl);
715 private URL createGetKeySourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
716 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "key", afi, srcAddress, srcMask,
717 dstAddress, dstMask);
718 URL url = new URL(restUrl);
722 private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
723 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "mapping", afi, srcAddress,
724 srcMask, dstAddress, dstMask);
725 URL url = new URL(restUrl);
729 private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
730 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping", iid, address.getAfi()
731 .shortValue(), address.getIpv4Address().getValue(), mask);
732 URL url = new URL(restUrl);
736 private URL createPutURL(String resource) throws MalformedURLException {
738 String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
740 URL url = new URL(restUrl);
744 private String createAuthenticationString() {
745 String authString = "admin:admin";
746 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
747 String authStringEnc = new String(authEncBytes);
748 return authStringEnc;
751 private String callURL(String method, String content, String accept, String body, URL url) throws IOException, JSONException {
752 String authStringEnc = createAuthenticationString();
753 connection = (HttpURLConnection) url.openConnection();
754 connection.setRequestMethod(method);
755 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
756 if (content != null) {
757 connection.setRequestProperty("Content-Type", content);
759 if (accept != null) {
760 connection.setRequestProperty("Accept", accept);
763 // now add the request body
764 connection.setDoOutput(true);
765 OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
769 connection.connect();
771 // getting the result, first check response code
772 Integer httpResponseCode = connection.getResponseCode();
774 if (httpResponseCode > 299) {
775 logger.trace("HTTP Address: " + url);
776 logger.trace("HTTP Response Code: " + httpResponseCode);
780 InputStream is = connection.getInputStream();
781 BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
782 StringBuilder sb = new StringBuilder();
784 while ((cp = rd.read()) != -1) {
785 sb.append((char) cp);
788 connection.disconnect();
789 return (sb.toString());
792 // timePeriod - in ms
793 public void assertNoPacketReceived(int timePeriod) {
795 receivePacket(timePeriod);
796 // If didn't timeout then fail:
798 } catch (SocketTimeoutException ste) {
802 // ------------------------------- Mask Tests ---------------------------
804 public void eidPrefixLookupIPv4() throws SocketTimeoutException {
806 runPrefixTest(LispAFIConvertor.asIPAfiAddress("1.2.3.4"), 16, LispAFIConvertor.asIPAfiAddress("1.2.3.2"),
807 LispAFIConvertor.asIPAfiAddress("1.1.1.1"), (byte) 32);
810 public void eidPrefixLookupIPv6() throws SocketTimeoutException {
812 runPrefixTest(LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:6:7:8"), 64, LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:1:2:3"),
813 LispAFIConvertor.asIPv6AfiAddress("1:2:3:1:2:3:1:2"), 128);
816 private void runPrefixTest(LispAFIAddress registerEID, int registerdMask, LispAFIAddress matchedAddress, LispAFIAddress unMatchedAddress, int mask)
817 throws SocketTimeoutException {
819 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
820 mapRegister.setWantMapNotify(true);
821 mapRegister.setNonce((long) 8);
822 mapRegister.setWantMapNotify(true);
823 mapRegister.setKeyId((short) 0);
824 mapRegister.setAuthenticationData(new byte[0]);
825 mapRegister.setNonce((long) 8);
826 mapRegister.setProxyMapReply(false);
827 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
828 etlr.setRecordTtl(254);
829 etlr.setAction(Action.NoAction);
830 etlr.setAuthoritative(false);
831 etlr.setMapVersion((short) 0);
832 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(registerEID));
833 etlr.setMaskLength((short) registerdMask);
834 etlr.setRecordTtl(254);
835 LocatorRecordBuilder record = new LocatorRecordBuilder();
836 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
837 record.setLocalLocator(false);
838 record.setRlocProbed(false);
839 record.setRouted(true);
840 record.setMulticastPriority((short) 0);
841 record.setMulticastWeight((short) 0);
842 record.setPriority((short) 0);
843 record.setWeight((short) 0);
844 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
845 etlr.getLocatorRecord().add(record.build());
846 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
847 mapRegister.getEidToLocatorRecord().add(etlr.build());
848 sendMapRegister(mapRegister.build());
849 MapNotify mapNotify = receiveMapNotify();
850 assertEquals(8, mapNotify.getNonce().longValue());
851 MapRequestBuilder mapRequest = new MapRequestBuilder();
852 mapRequest.setNonce((long) 4);
853 mapRequest.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
854 mapRequest.setEidRecord(new ArrayList<EidRecord>());
855 mapRequest.setAuthoritative(false);
856 mapRequest.setMapDataPresent(false);
857 mapRequest.setPitr(false);
858 mapRequest.setProbe(false);
859 mapRequest.setSmr(false);
860 mapRequest.setSmrInvoked(false);
861 mapRequest.getEidRecord().add(
862 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(matchedAddress)).build());
863 mapRequest.setItrRloc(new ArrayList<ItrRloc>());
864 mapRequest.getItrRloc().add(
865 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ourAddress))).build());
866 sendMapRequest(mapRequest.build());
867 MapReply mapReply = receiveMapReply();
868 assertEquals(4, mapReply.getNonce().longValue());
869 assertEquals(record.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
870 mapRequest.setEidRecord(new ArrayList<EidRecord>());
871 mapRequest.getEidRecord().add(
872 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(unMatchedAddress)).build());
873 sendMapRequest(mapRequest.build());
874 mapReply = receiveMapReply();
875 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
878 // This registers an IP with a MapRegister, then adds a password via the
879 // northbound REST API
880 // and checks that the password works
881 public void testPasswordExactMatch() throws Exception {
883 String ipString = "10.0.0.1";
884 LispIpv4Address address = LispAFIConvertor.asIPAfiAddress(ipString);
886 String pass = "pass";
888 URL url = createPutURL("key");
890 String jsonAuthData = createAuthKeyJSON(pass, address, mask);
892 logger.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
893 logger.trace("Address: " + address);
895 byte[] expectedSha = new byte[] { (byte) 146, (byte) 234, (byte) 52, (byte) 247, (byte) 186, (byte) 232, (byte) 31, (byte) 249, (byte) 87,
896 (byte) 73, (byte) 234, (byte) 54, (byte) 225, (byte) 160, (byte) 129, (byte) 251, (byte) 73, (byte) 53, (byte) 196, (byte) 62 };
898 byte[] zeros = new byte[20];
900 callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
902 // build a MapRegister
903 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
904 mapRegister.setWantMapNotify(true);
905 mapRegister.setNonce((long) 8);
906 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
907 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(address));
908 etlr.setMaskLength((short) mask);
909 etlr.setRecordTtl(254);
910 LocatorRecordBuilder record = new LocatorRecordBuilder();
911 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
912 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
913 etlr.getLocatorRecord().add(record.build());
914 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
915 mapRegister.getEidToLocatorRecord().add(etlr.build());
917 mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
918 mapRegister.setAuthenticationData(zeros);
920 sendMapRegister(mapRegister.build());
921 assertNoPacketReceived(3000);
923 mapRegister.setAuthenticationData(expectedSha);
925 sendMapRegister(mapRegister.build());
927 assertMapNotifyRecieved();
930 public void testPasswordMaskMatch() throws Exception {
932 LispIpv4Address addressInRange = LispAFIConvertor.asIPAfiAddress("10.20.30.40");
933 LispIpv4Address addressOutOfRange = LispAFIConvertor.asIPAfiAddress("20.40.30.40");
934 LispIpv4Address range = LispAFIConvertor.asIPAfiAddress("10.20.30.0");
937 String pass = "pass";
939 URL url = createPutURL("key");
940 String jsonAuthData = createAuthKeyJSON(pass, range, 8);
942 callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
943 // build a MapRegister
944 MapRegisterBuilder mapRegister = new MapRegisterBuilder();
946 mapRegister.setWantMapNotify(true);
947 mapRegister.setNonce((long) 8);
948 EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
949 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressInRange));
950 etlr.setMaskLength((short) mask);
951 etlr.setRecordTtl(254);
952 LocatorRecordBuilder record = new LocatorRecordBuilder();
953 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
954 record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
955 etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
956 etlr.getLocatorRecord().add(record.build());
957 mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
958 mapRegister.getEidToLocatorRecord().add(etlr.build());
960 mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
962 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
964 sendMapRegister(mapRegister.build());
966 assertMapNotifyRecieved();
968 etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
970 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
972 sendMapRegister(mapRegister.build());
973 assertNoPacketReceived(3000);
976 private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
977 return registerAddressAndQuery(eid, -1);
980 // takes an address, packs it in a MapRegister, sends it, returns the
982 private MapReply registerAddressAndQuery(LispAFIAddress eid, int maskLength) throws SocketTimeoutException {
983 MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
984 mapRegisterBuilder.setWantMapNotify(true);
985 mapRegisterBuilder.setKeyId((short) 0);
986 mapRegisterBuilder.setAuthenticationData(new byte[0]);
987 mapRegisterBuilder.setNonce((long) 8);
988 mapRegisterBuilder.setProxyMapReply(false);
989 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
990 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
991 if (maskLength != -1) {
992 etlrBuilder.setMaskLength((short) maskLength);
994 etlrBuilder.setMaskLength((short) 0);
996 etlrBuilder.setRecordTtl(254);
997 etlrBuilder.setAction(Action.NoAction);
998 etlrBuilder.setAuthoritative(false);
999 etlrBuilder.setMapVersion((short) 0);
1000 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1001 recordBuilder.setLocalLocator(false);
1002 recordBuilder.setRlocProbed(false);
1003 recordBuilder.setRouted(true);
1004 recordBuilder.setMulticastPriority((short) 0);
1005 recordBuilder.setMulticastWeight((short) 0);
1006 recordBuilder.setPriority((short) 0);
1007 recordBuilder.setWeight((short) 0);
1008 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
1009 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1010 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1011 mapRegisterBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1012 mapRegisterBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1013 sendMapRegister(mapRegisterBuilder.build());
1014 MapNotify mapNotify = receiveMapNotify();
1015 assertEquals(8, mapNotify.getNonce().longValue());
1016 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1017 mapRequestBuilder.setNonce((long) 4);
1018 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1019 mapRequestBuilder.getEidRecord().add(
1020 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1021 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1022 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress)))
1024 mapRequestBuilder.getItrRloc().add(
1025 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1026 mapRequestBuilder.setAuthoritative(false);
1027 mapRequestBuilder.setMapDataPresent(false);
1028 mapRequestBuilder.setPitr(false);
1029 mapRequestBuilder.setProbe(false);
1030 mapRequestBuilder.setSmr(false);
1031 mapRequestBuilder.setSmrInvoked(false);
1032 sendMapRequest(mapRequestBuilder.build());
1033 return receiveMapReply();
1036 // ------------------------------- LCAF Tests ---------------------------
1038 public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1040 String ipString = "10.20.30.200";
1041 String macString = "01:02:03:04:05:06";
1042 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1043 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1044 LcafSourceDestBuilder builder = new LcafSourceDestBuilder();
1045 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1046 builder.setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode());
1047 builder.setSrcMaskLength((short) 0);
1048 builder.setDstMaskLength((short) 0);
1049 builder.setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(addrToSend1).build());
1050 builder.setDstAddress(new DstAddressBuilder().setPrimitiveAddress(addrToSend2).build());
1052 MapReply reply = registerAddressAndQuery(builder.build());
1054 LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1055 assertTrue(fromNetwork.getAddress() instanceof LcafSourceDestAddress);
1056 LcafSourceDestAddress sourceDestFromNetwork = (LcafSourceDestAddress) fromNetwork.getAddress();
1058 LispAFIAddress receivedAddr1 = (LispAFIAddress) sourceDestFromNetwork.getSrcAddress().getPrimitiveAddress();
1059 LispAFIAddress receivedAddr2 = (LispAFIAddress) sourceDestFromNetwork.getDstAddress().getPrimitiveAddress();
1061 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1062 assertTrue(receivedAddr2 instanceof LispMacAddress);
1064 LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1065 LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1067 assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1068 assertEquals(macString, receivedMAC.getMacAddress().getValue());
1072 public void registerAndQuery__KeyValueLCAF() throws SocketTimeoutException {
1074 String ipString = "10.20.30.200";
1075 String macString = "01:02:03:04:05:06";
1076 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1077 org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1078 LcafKeyValueBuilder builder = new LcafKeyValueBuilder();
1079 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1080 builder.setLcafType((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode());
1081 builder.setKey(new KeyBuilder().setPrimitiveAddress(addrToSend1).build());
1082 builder.setValue(new ValueBuilder().setPrimitiveAddress(addrToSend2).build());
1084 MapReply reply = registerAddressAndQuery(builder.build());
1086 LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1087 assertTrue(fromNetwork.getAddress() instanceof LcafKeyValueAddress);
1088 LcafKeyValueAddress keyValueFromNetwork = (LcafKeyValueAddress) fromNetwork.getAddress();
1090 LispAFIAddress receivedAddr1 = (LispAFIAddress) keyValueFromNetwork.getKey().getPrimitiveAddress();
1091 LispAFIAddress receivedAddr2 = (LispAFIAddress) keyValueFromNetwork.getValue().getPrimitiveAddress();
1093 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1094 assertTrue(receivedAddr2 instanceof LispMacAddress);
1096 LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1097 LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1099 assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1100 assertEquals(macString, receivedMAC.getMacAddress().getValue());
1103 public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1105 String macString = "01:02:03:04:05:06";
1106 String ipString = "10.20.255.30";
1107 LcafListBuilder listbuilder = new LcafListBuilder();
1108 listbuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
1109 listbuilder.setAddresses(new ArrayList<Addresses>());
1110 listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1111 listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString))).build());
1113 MapReply reply = registerAddressAndQuery(listbuilder.build());
1115 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1117 assertTrue(receivedAddress instanceof LcafListAddress);
1119 LcafListAddress listAddrFromNetwork = (LcafListAddress) receivedAddress;
1120 LispAFIAddress receivedAddr1 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(0).getPrimitiveAddress();
1121 LispAFIAddress receivedAddr2 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(1).getPrimitiveAddress();
1123 assertTrue(receivedAddr1 instanceof LispIpv4Address);
1124 assertTrue(receivedAddr2 instanceof LispMacAddress);
1126 assertEquals(macString, ((LispMacAddress) receivedAddr2).getMacAddress().getValue());
1127 assertEquals(ipString, ((LispIpv4Address) receivedAddr1).getIpv4Address().getValue());
1130 public void registerAndQuery__SegmentLCAF() throws SocketTimeoutException {
1132 String ipString = "10.20.255.30";
1134 LcafSegmentBuilder builder = new LcafSegmentBuilder();
1135 builder.setInstanceId((long) instanceId);
1136 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode());
1137 builder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1139 MapReply reply = registerAddressAndQuery(builder.build());
1141 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1142 assertTrue(receivedAddress instanceof LcafSegmentAddress);
1144 LcafSegmentAddress segmentfromNetwork = (LcafSegmentAddress) receivedAddress;
1145 LispAFIAddress addrFromSegment = (LispAFIAddress) segmentfromNetwork.getAddress().getPrimitiveAddress();
1146 assertTrue(addrFromSegment instanceof LispIpv4Address);
1147 assertEquals(ipString, ((LispIpv4Address) addrFromSegment).getIpv4Address().getValue());
1149 assertEquals(instanceId, segmentfromNetwork.getInstanceId().intValue());
1152 public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1154 String macString = "01:02:03:04:05:06";
1155 String ipString = "10.20.255.30";
1156 HopBuilder hopBuilder = new HopBuilder();
1157 hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString)));
1158 Hop hop1 = hopBuilder.build();
1159 hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString)));
1160 Hop hop2 = hopBuilder.build();
1161 HopsBuilder hb = new HopsBuilder();
1164 hb.setRLOCProbe(false);
1166 HopsBuilder hb2 = new HopsBuilder();
1168 hb2.setLookup(false);
1169 hb2.setRLOCProbe(true);
1170 hb2.setStrict(false);
1171 Hops hops1 = hb.build();
1172 Hops hops2 = hb2.build();
1173 LcafTrafficEngineeringBuilder trafficBuilder = new LcafTrafficEngineeringBuilder();
1174 trafficBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
1175 (short) LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode());
1176 trafficBuilder.setHops(new ArrayList<Hops>());
1177 trafficBuilder.getHops().add(hb.build());
1178 trafficBuilder.getHops().add(hb2.build());
1180 MapReply reply = registerAddressAndQuery(trafficBuilder.build());
1182 assertTrue(LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer()) instanceof LcafTrafficEngineeringAddress);
1184 LcafTrafficEngineeringAddress receivedAddress = (LcafTrafficEngineeringAddress) LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0)
1185 .getLispAddressContainer());
1187 ReencapHop receivedHop1 = receivedAddress.getHops().get(0);
1188 ReencapHop receivedHop2 = receivedAddress.getHops().get(1);
1190 assertEquals(true, hops1.isLookup());
1191 assertEquals(false, hops1.isRLOCProbe());
1192 assertEquals(true, hops1.isStrict());
1194 assertEquals(false, hops2.isLookup());
1195 assertEquals(true, hops2.isRLOCProbe());
1196 assertEquals(false, hops2.isStrict());
1198 assertTrue(receivedHop1.getHop().getPrimitiveAddress() instanceof LispIpv4Address);
1199 assertTrue(receivedHop2.getHop().getPrimitiveAddress() instanceof LispMacAddress);
1201 assertEquals(ipString, ((LispIpv4Address) receivedHop1.getHop().getPrimitiveAddress()).getIpv4Address().getValue());
1202 assertEquals(macString, ((LispMacAddress) receivedHop2.getHop().getPrimitiveAddress()).getMacAddress().getValue());
1205 public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1207 String ipString = "1.2.3.4";
1213 LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
1214 builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
1215 builder.setIpTos(ipTOs);
1216 builder.setProtocol(protocol);
1217 builder.setLocalPort(new PortNumber(localPort));
1218 builder.setRemotePort(new PortNumber(remotePort));
1219 builder.setAddress(new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder()
1220 .setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1222 LcafApplicationDataAddress addressToSend = builder.build();
1224 MapReply reply = registerAddressAndQuery(addressToSend);
1226 LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1228 assertTrue(receivedAddress instanceof LcafApplicationDataAddress);
1230 LcafApplicationDataAddress receivedApplicationDataAddress = (LcafApplicationDataAddress) receivedAddress;
1231 assertEquals(protocol, receivedApplicationDataAddress.getProtocol().intValue());
1232 assertEquals(ipTOs, receivedApplicationDataAddress.getIpTos().intValue());
1233 assertEquals(localPort, receivedApplicationDataAddress.getLocalPort().getValue().intValue());
1234 assertEquals(remotePort, receivedApplicationDataAddress.getRemotePort().getValue().intValue());
1236 LispIpv4Address ipAddressReceived = (LispIpv4Address) receivedApplicationDataAddress.getAddress().getPrimitiveAddress();
1237 assertEquals(ipString, ipAddressReceived.getIpv4Address().getValue());
1240 // ------------------- TimeOut Tests -----------
1242 public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1244 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1245 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1246 mapRequestBuilder.setNonce((long) 4);
1247 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1248 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1249 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1250 mapRequestBuilder.getEidRecord().add(
1251 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1252 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1253 mapRequestBuilder.getItrRloc().add(
1254 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1255 sendMapRequest(mapRequestBuilder.build());
1256 MapReply mapReply = receiveMapReply();
1257 assertEquals(4, mapReply.getNonce().longValue());
1258 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1259 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1260 mapRegisterbuilder.setWantMapNotify(true);
1261 mapRegisterbuilder.setNonce((long) 8);
1262 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1263 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1264 etlrBuilder.setMaskLength((short) 32);
1265 etlrBuilder.setRecordTtl(254);
1266 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1267 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
1268 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1269 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1270 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1271 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1272 sendMapRegister(mapRegisterbuilder.build());
1273 MapNotify mapNotify = receiveMapNotify();
1274 assertEquals(8, mapNotify.getNonce().longValue());
1275 sendMapRequest(mapRequestBuilder.build());
1276 mapReply = receiveMapReply();
1277 assertEquals(4, mapReply.getNonce().longValue());
1278 assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1279 .getLispAddressContainer());
1280 causeEntryToBeCleaned();
1281 sendMapRequest(mapRequestBuilder.build());
1282 mapReply = receiveMapReply();
1283 assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1286 public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1288 LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1289 MapRequest mapRequest = createMapRequest(eid);
1291 testTTLBeforeRegister(mapRequest);
1293 registerForTTL(eid);
1295 testTTLAfterRegister(mapRequest);
1297 causeEntryToBeCleaned();
1298 testTTLAfterClean(mapRequest);
1301 testTTLAfterAutherize(mapRequest);
1305 private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1307 sendMapRequest(mapRequest);
1308 mapReply = receiveMapReply();
1309 assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1312 private void causeEntryToBeCleaned() {
1313 clusterService.setTimeUnit(TimeUnit.NANOSECONDS);
1314 clusterService.cleanOld();
1317 private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1319 sendMapRequest(mapRequest);
1320 mapReply = receiveMapReply();
1321 assertEquals(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1322 .getLispAddressContainer());
1323 assertCorrectMapReplyTTLAndAction(mapReply, 254, Action.NoAction);
1326 private void registerForTTL(LispIpv4Address eid) throws SocketTimeoutException {
1327 MapRegister mapRegister = createMapRegister(eid);
1328 sendMapRegister(mapRegister);
1329 assertMapNotifyRecieved();
1332 private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1334 sendMapRequest(mapRequest);
1335 mapReply = receiveMapReply();
1336 assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1339 private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1341 sendMapRequest(mapRequest);
1342 mapReply = receiveMapReply();
1343 assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1346 private void assertCorrectMapReplyTTLAndAction(MapReply mapReply, int expectedTTL, Action expectedAction) {
1347 assertEquals(expectedTTL, mapReply.getEidToLocatorRecord().get(0).getRecordTtl().intValue());
1348 assertEquals(expectedAction, mapReply.getEidToLocatorRecord().get(0).getAction());
1351 private MapRegister createMapRegister(LispAFIAddress eid, LispAFIAddress rloc) {
1352 MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1353 mapRegisterbuilder.setWantMapNotify(true);
1354 mapRegisterbuilder.setNonce((long) 8);
1355 mapRegisterbuilder.setKeyId((short) 0);
1356 EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1357 etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1358 etlrBuilder.setMaskLength((short) 24);
1359 etlrBuilder.setRecordTtl(254);
1360 etlrBuilder.setAuthoritative(false);
1361 etlrBuilder.setAction(Action.NoAction);
1362 LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1363 recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
1364 etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1365 etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1366 mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1367 mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1368 MapRegister mapRegister = mapRegisterbuilder.build();
1372 private MapRegister createMapRegister(LispIpv4Address eid) {
1373 return createMapRegister(eid, asIPAfiAddress("4.3.2.1"));
1376 private MapRequest createMapRequest(LispAFIAddress eid) {
1377 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1378 mapRequestBuilder.setNonce((long) 4);
1379 mapRequestBuilder.setPitr(false);
1380 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1381 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1382 mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1383 mapRequestBuilder.getEidRecord().add(
1384 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1385 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1386 mapRequestBuilder.getItrRloc().add(
1387 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1388 MapRequest mr = mapRequestBuilder.build();
1392 public void testSimpleNonProxy() throws SocketTimeoutException, SocketException {
1394 String rloc = "127.0.0.3";
1395 int port = LispMessage.PORT_NUM;
1396 Ipv4 ipRloc = LispAFIConvertor.asIPAfiAddress(rloc);
1397 sendProxyMapRequest(rloc, port, ipRloc);
1401 public void testNonProxyOtherPort() throws SocketTimeoutException, SocketException {
1403 String rloc = "127.0.0.3";
1405 LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1406 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1407 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1409 new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1410 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1411 sendProxyMapRequest(rloc, port, adLcaf);
1415 public void testRecievingNonProxyOnXtrPort() throws SocketTimeoutException, SocketException, Throwable {
1417 lms.shouldListenOnXtrPort(true);
1418 notificationCalled = false;
1419 final String eid = "10.10.10.10";
1420 String rloc = "127.0.0.3";
1421 int port = LispMessage.XTR_PORT_NUM;
1422 LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1423 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1424 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1426 new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1427 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1428 final MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1429 ((LispMappingService) lms).registerNotificationListener(XtrRequestMapping.class, new NotificationListener<XtrRequestMapping>() {
1432 public void onNotification(XtrRequestMapping notification) {
1433 assertEquals(((LispIpv4Address) mapRequest.getEidRecord().get(0).getLispAddressContainer().getAddress()).getIpv4Address().getValue(),
1435 notificationCalled = true;
1436 logger.warn("notification arrived");
1439 sendMapRequest(mapRequest, port);
1440 for (int i = 0; i < MAX_NOTIFICATION_RETRYS; i++) {
1441 if (notificationCalled) {
1444 logger.warn("notification hasn't arrived, sleeping...");
1449 fail("Notification hasn't arrived");
1453 private void sendProxyMapRequest(String rloc, int port, LispAFIAddress adLcaf) throws SocketTimeoutException, SocketException {
1454 String eid = "10.1.0.1";
1455 MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1456 sendMapRequest(mapRequest);
1457 DatagramSocket nonProxySocket = new DatagramSocket(new InetSocketAddress(rloc, port));
1458 MapRequest recievedMapRequest = receiveMapRequest(nonProxySocket);
1459 assertEquals(mapRequest.getNonce(), recievedMapRequest.getNonce());
1460 assertEquals(mapRequest.getSourceEid(), recievedMapRequest.getSourceEid());
1461 assertEquals(mapRequest.getItrRloc(), recievedMapRequest.getItrRloc());
1462 assertEquals(mapRequest.getEidRecord(), recievedMapRequest.getEidRecord());
1463 nonProxySocket.close();
1466 private MapRequest createNonProxyMapRequest(String eid, LispAFIAddress adLcaf) throws SocketTimeoutException {
1467 MapRegister mr = createMapRegister(LispAFIConvertor.asIPAfiAddress(eid));
1468 LocatorRecord record = new LocatorRecordBuilder(mr.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)).setLispAddressContainer(
1469 LispAFIConvertor.toContainer(adLcaf)).build();
1470 mr.getEidToLocatorRecord().get(0).getLocatorRecord().set(0, record);
1471 sendMapRegister(mr);
1472 assertMapNotifyRecieved();
1473 MapRequest mapRequest = createMapRequest(LispAFIConvertor.asIPAfiAddress(eid));
1474 MapRequestBuilder builder = new MapRequestBuilder(mapRequest);
1475 builder.setPitr(true);
1476 mapRequest = builder.build();
1480 private void assertMapNotifyRecieved() throws SocketTimeoutException {
1484 private MapReply receiveMapReply() throws SocketTimeoutException {
1485 return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1488 private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
1489 return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData()));
1492 private MapNotify receiveMapNotify() throws SocketTimeoutException {
1493 return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1496 private void sendMapRequest(MapRequest mapRequest) {
1497 sendMapRequest(mapRequest, LispMessage.PORT_NUM);
1500 private void sendMapRequest(MapRequest mapRequest, int port) {
1501 sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array(), port);
1504 private void sendMapRegister(MapRegister mapRegister) {
1505 sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
1508 private void sendPacket(byte[] bytesToSend) {
1509 sendPacket(bytesToSend, LispMessage.PORT_NUM);
1512 private void sendPacket(byte[] bytesToSend, int port) {
1514 DatagramPacket packet = new DatagramPacket(bytesToSend, bytesToSend.length);
1515 initPacketAddress(packet, port);
1516 logger.trace("Sending MapRegister to LispPlugin on socket");
1517 socket.send(packet);
1518 } catch (Throwable t) {
1523 private DatagramPacket receivePacket() throws SocketTimeoutException {
1524 return receivePacket(6000);
1527 private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
1528 return receivePacket(socket, timeout);
1531 private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException {
1533 byte[] buffer = new byte[4096];
1534 DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
1535 logger.trace("Waiting for packet from socket...");
1536 receivedSocket.setSoTimeout(timeout);
1537 receivedSocket.receive(receivePacket);
1538 logger.trace("Recieved packet from socket!");
1539 return receivePacket;
1540 } catch (SocketTimeoutException ste) {
1542 } catch (Throwable t) {
1548 private void initPacketAddress(DatagramPacket packet, int port) throws UnknownHostException {
1549 packet.setAddress(InetAddress.getByName(lispBindAddress));
1550 packet.setPort(port);
1553 private DatagramSocket initSocket(DatagramSocket socket, int port) {
1555 socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
1556 } catch (SocketException e) {
1557 e.printStackTrace();
1563 private byte[] extractWSUdpByteArray(String wiresharkHex) {
1564 final int HEADER_LEN = 42;
1565 byte[] res = new byte[1000];
1566 String[] split = wiresharkHex.split(" ");
1568 for (String cur : split) {
1570 if (cur.length() == 2) {
1572 if (counter > HEADER_LEN) {
1573 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
1578 return Arrays.copyOf(res, counter - HEADER_LEN);
1581 private String stateToString(int state) {
1585 case Bundle.INSTALLED:
1587 case Bundle.RESOLVED:
1589 case Bundle.UNINSTALLED:
1590 return "UNINSTALLED";
1592 return "Not CONVERTED";
1596 @SuppressWarnings({ "rawtypes", "unchecked" })
1597 private void areWeReady() throws InvalidSyntaxException {
1599 boolean debugit = false;
1600 Bundle b[] = bc.getBundles();
1601 for (Bundle element : b) {
1602 int state = element.getState();
1603 logger.trace("Bundle[" + element.getBundleId() + "]:" + element.getSymbolicName() + ",v" + element.getVersion() + ", state:"
1604 + stateToString(state));
1605 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
1606 logger.trace("Bundle:" + element.getSymbolicName() + " state:" + stateToString(state));
1609 String host = element.getHeaders().get(Constants.FRAGMENT_HOST);
1611 logger.warn("Bundle " + element.getSymbolicName() + " is a fragment which is part of: " + host);
1612 logger.warn("Required imports are: " + element.getHeaders().get(Constants.IMPORT_PACKAGE));
1616 } catch (BundleException e) {
1617 logger.error("BundleException:", e);
1626 logger.warn(("Do some debugging because some bundle is unresolved"));
1628 // assertNotNull(broker);
1631 ServiceReference r = null;
1632 while (this.lms == null && retry < MAX_SERVICE_LOAD_RETRIES) {
1634 r = bc.getServiceReference(IFlowMapping.class.getName());
1635 // r.getPropertyKeys();
1637 this.lms = (IFlowMapping) bc.getService(r);
1641 } catch (InterruptedException e) {
1647 assertNotNull(IFlowMapping.class.getName() + " service wasn't found in bundle context ", this.lms);
1649 r = bc.getServiceReference(ILispDAO.class.getName());
1651 this.clusterService = (ClusterDAOService) bc.getService(r);
1654 assertNotNull(ILispDAO.class.getName() + " service wasn't found in bundle context ", this.clusterService);
1655 r = bc.getServiceReference(IConfigLispPlugin.class.getName());
1657 this.configLispPlugin = (IConfigLispPlugin) bc.getService(r);
1660 assertNotNull(IConfigLispPlugin.class.getName() + " service wasn't found in bundle context ", this.configLispPlugin);
1661 configLispPlugin.setLispAddress(lispBindAddress);
1663 // Uncomment this code to Know which services were actually loaded to
1666 * for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
1667 * logger.trace(sr.getBundle().getSymbolicName());
1668 * logger.trace(sr.toString()); }
1672 } catch (InterruptedException e) {
1676 private Ipv4 asIPAfiAddress(String ip) {
1677 return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1680 private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 asPrimitiveIPAfiAddress(String ip) {
1681 return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4Builder()
1682 .setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1685 private Mac asMacAfiAddress(String mac) {
1686 return new MacBuilder().setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1689 private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac asPrimitiveMacAfiAddress(String mac) {
1690 return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.MacBuilder()
1691 .setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1694 private void cleanUP() {
1697 lms.shouldListenOnXtrPort(false);
1698 socket = initSocket(socket, LispMessage.PORT_NUM);