fixed map request yang model, changed embeded map reply to be an eid to locator record
[lispflowmapping.git] / mappingservice / integrationtest / src / test / java / org / opendaylight / lispflowmapping / integrationtest / MappingServiceIntegrationTest.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.integrationtest;
9
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;
15
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;
29 import java.net.URL;
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;
36
37 import javax.inject.Inject;
38
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;
126
127 import aQute.lib.osgi.Constants;
128
129 @RunWith(PaxExam.class)
130 public class MappingServiceIntegrationTest {
131
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;
144
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;
150
151     @After
152     public void after() {
153         if (socket != null) {
154             socket.close();
155         }
156         if (connection != null) {
157             connection.disconnect();
158         }
159     }
160
161     @Before
162     public void before() throws Exception {
163         areWeReady();
164         locatorEid = asIPAfiAddress("4.3.2.1");
165         socket = initSocket(socket, LispMessage.PORT_NUM);
166
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
170         // UDP: 56756
171         // LISP(Type = 1 Map-Request
172         // Record Count: 1
173         // ITR-RLOC count: 0
174         // Source EID AFI: 0
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"));
186
187         // IP: 192.168.136.10 -> 128.223.156.35
188         // UDP: 49289 -> 4342
189         // LISP(Type = 3 Map-Register, P=1, M=1
190         // Record Counter: 1
191         // Nonce: 0
192         // Key ID: 0x0001
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,
196         // No-Action
197         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
198         // Priority/Weight: 1/100, Multicast Priority/Weight:
199         // 255/0
200         //
201
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"));
209
210         // IP: 192.168.136.10 -> 128.223.156.35
211         // UDP: 49289 -> 4342
212         // LISP(Type = 3 Map-Register, P=1, M=1
213         // Record Counter: 1
214         // Nonce: 7
215         // Key ID: 0x0000 NO AUTHENTICATION!!
216         // AuthDataLength: 00 Data:
217         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
218         // No-Action
219         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
220         // Priority/Weight: 1/100, Multicast Priority/Weight:
221         // 255/0
222         //
223
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"));
231
232         // IP: 192.168.136.10 -> 128.223.156.35
233         // UDP: 49289 -> 4342
234         // LISP(Type = 3 Map-Register, P=1, M=1
235         // Record Counter: 1
236         // Nonce: 7
237         // Key ID: 0x0000 NO AUTHENTICATION!!
238         // AuthDataLength: 00 Data:
239         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
240         // No-Action
241         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
242         // Priority/Weight: 1/100, Multicast Priority/Weight:
243         // 255/0
244         //
245
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"));
253     }
254
255     @Inject
256     private BundleContext bc;
257     private HttpURLConnection connection;
258     protected static boolean notificationCalled;
259
260     // Configure the OSGi container
261     @Configuration
262     public Option[] config() {
263         return options(MappingServiceTestHelper.mappingServiceBundlesWithClusterDAO());
264     }
265
266     @Test
267     public void testSimpleUsage() throws Exception {
268         mapRequestSimple();
269         mapRegisterWithMapNotify();
270         mapRegisterWithMapNotifyAndMapRequest();
271         registerAndQuery__MAC();
272         mapRequestMapRegisterAndMapRequest();
273         mapRegisterWithAuthenticationWithoutConfiguringAKey();
274         mapRegisterWithoutMapNotify();
275     }
276
277     @Test
278     public void testLCAFs() throws Exception {
279         registerAndQuery__SrcDestLCAF();
280         registerAndQuery__KeyValueLCAF();
281         registerAndQuery__ListLCAF();
282         registerAndQuery__ApplicationData();
283         registerAndQuery__TrafficEngineering();
284         registerAndQuery__SegmentLCAF();
285     }
286
287     @Test
288     public void testMask() throws Exception {
289         testPasswordExactMatch();
290         testPasswordMaskMatch();
291         eidPrefixLookupIPv4();
292         eidPrefixLookupIPv6();
293     }
294
295     @Test
296     public void testNorthbound() throws Exception {
297         northboundAddKey();
298         northboundAddMapping();
299         northboundRetrieveKey();
300         northboundRetrieveMapping();
301         northboundRetrieveSourceDestKey();
302         northboundRetrieveSourceDestMapping();
303     }
304
305     @Test
306     public void testOverWriting() throws Exception {
307         testMapRegisterDosntOverwritesOtherSubKeys();
308         testMapRegisterOverwritesSameSubkey();
309         testMapRegisterOverwritesNoSubkey();
310         testMapRegisterDoesntOverwritesNoSubkey();
311     }
312
313     @Test
314     public void testTimeOuts() throws Exception {
315         mapRequestMapRegisterAndMapRequestTestTimeout();
316         mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse();
317     }
318
319     @Test
320     public void testNonProxy() throws Throwable {
321         testSimpleNonProxy();
322         testNonProxyOtherPort();
323         testRecievingNonProxyOnXtrPort();
324     }
325
326     // ------------------------------- Simple Tests ---------------------------
327
328     public void mapRequestSimple() throws SocketTimeoutException {
329         cleanUP();
330         sendPacket(mapRequestPacket);
331         ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
332         MapReply reply = MapReplySerializer.getInstance().deserialize(readBuf);
333         assertEquals(4435248268955932168L, reply.getNonce().longValue());
334
335     }
336
337     public void mapRegisterWithMapNotify() throws SocketTimeoutException {
338         cleanUP();
339         sendPacket(mapRegisterPacketWithNotify);
340         MapNotify reply = receiveMapNotify();
341         assertEquals(7, reply.getNonce().longValue());
342     }
343
344     public void mapRegisterWithMapNotifyAndMapRequest() throws SocketTimeoutException {
345         cleanUP();
346         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
347
348         MapReply mapReply = registerAddressAndQuery(eid, 32);
349
350         assertEquals(4, mapReply.getNonce().longValue());
351         assertEquals(LispAFIConvertor.toContainer(locatorEid), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
352                 .getLispAddressContainer());
353
354     }
355
356     public void registerAndQuery__MAC() throws SocketTimeoutException {
357         cleanUP();
358         String macAddress = "01:02:03:04:05:06";
359
360         MapReply reply = registerAddressAndQuery(asMacAfiAddress(macAddress));
361
362         assertTrue(true);
363         LispAFIAddress addressFromNetwork = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
364         assertTrue(addressFromNetwork instanceof LispMacAddress);
365         String macAddressFromReply = ((Mac) addressFromNetwork).getMacAddress().getValue();
366
367         assertEquals(macAddress, macAddressFromReply);
368     }
369
370     public void mapRequestMapRegisterAndMapRequest() throws SocketTimeoutException {
371         cleanUP();
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());
408
409     }
410
411     public void testMapRegisterDosntOverwritesOtherSubKeys() throws SocketTimeoutException {
412         cleanUP();
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());
422     }
423
424     public void testMapRegisterOverwritesSameSubkey() throws SocketTimeoutException {
425         cleanUP();
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());
434     }
435
436     public void testMapRegisterOverwritesNoSubkey() throws SocketTimeoutException {
437         cleanUP();
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());
446     }
447
448     public void testMapRegisterDoesntOverwritesNoSubkey() throws SocketTimeoutException {
449         cleanUP();
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)));
462     }
463
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);
476         sendMapRequest(mr);
477         mapReply = lms.handleMapRequest(mr);
478         return mapReply;
479     }
480
481     public void mapRegisterWithAuthenticationWithoutConfiguringAKey() throws SocketTimeoutException {
482         cleanUP();
483         sendPacket(mapRegisterPacketWithAuthenticationAndMapNotify);
484         try {
485             receivePacket(3000);
486             // If didn't timeout then fail:
487             fail();
488         } catch (SocketTimeoutException ste) {
489         }
490     }
491
492     public void mapRegisterWithoutMapNotify() {
493         cleanUP();
494         sendPacket(mapRegisterPacketWithoutNotify);
495         try {
496             receivePacket(3000);
497             // If didn't timeout then fail:
498             fail();
499         } catch (SocketTimeoutException ste) {
500         }
501     }
502
503     // --------------------- Northbound Tests ---------------------------
504
505     private void northboundAddKey() throws Exception {
506         cleanUP();
507         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
508         int mask = 32;
509         String pass = "asdf";
510
511         URL url = createPutURL("key");
512         String authKeyJSON = createAuthKeyJSON(pass, address, mask);
513         callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
514
515         String retrievedKey = lms.getAuthenticationKey(LispAFIConvertor.toContainer(address), mask);
516
517         // Check stored password matches the one sent
518         assertEquals(pass, retrievedKey);
519
520     }
521
522     private void northboundRetrieveSourceDestKey() throws Exception {
523         cleanUP();
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"));
528         int mask1 = 32;
529         int mask2 = 32;
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";
535
536         lms.addAuthenticationKey(LispAFIConvertor.toContainer(sourceDestAddress), mask1, pass);
537
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);
545
546         // test that the password matches what was we expected.
547         assertEquals(pass, json.get("key"));
548
549     }
550
551     private void northboundRetrieveKey() throws Exception {
552         cleanUP();
553         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
554         int mask = 32;
555         String pass = "asdf";
556
557         lms.addAuthenticationKey(LispAFIConvertor.toContainer(address), mask, pass);
558
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);
563
564         // test that the password matches what was we expected.
565         assertEquals(pass, json.get("key"));
566
567     }
568
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() + "}}";
572     }
573
574     private void northboundAddMapping() throws Exception {
575         cleanUP();
576         String pass = "asdf";
577         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
578         int mask = 32;
579         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
580
581         // NB add mapping always checks the key
582         lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
583
584         URL url = createPutURL("mapping");
585         String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
586         callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
587
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());
595
596         LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
597                 .getAddress();
598
599         assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
600
601     }
602
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 " + "} " + "}";
612
613         return jsonString;
614     }
615
616     private void northboundRetrieveMapping() throws Exception {
617         cleanUP();
618         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
619         int mask = 32;
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);
643
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);
649
650         // With just one locator, locators is not a JSONArray
651         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
652
653         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
654
655     }
656
657     private void northboundRetrieveSourceDestMapping() throws Exception {
658         cleanUP();
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"));
663         int mask1 = 32;
664         int mask2 = 32;
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");
670
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);
693
694         // Get mapping using NB interface. No IID used
695         URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
696                 mask2);
697         String reply = callURL("GET", null, "application/json", null, url);
698         JSONTokener jt = new JSONTokener(reply);
699         JSONObject json = new JSONObject(jt);
700
701         // With just one locator, locators is not a JSONArray
702         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
703
704         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
705
706     }
707
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);
712         return url;
713     }
714
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);
719         return url;
720     }
721
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);
726         return url;
727     }
728
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);
733         return url;
734     }
735
736     private URL createPutURL(String resource) throws MalformedURLException {
737
738         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
739
740         URL url = new URL(restUrl);
741         return url;
742     }
743
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;
749     }
750
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);
758         }
759         if (accept != null) {
760             connection.setRequestProperty("Accept", accept);
761         }
762         if (body != null) {
763             // now add the request body
764             connection.setDoOutput(true);
765             OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
766             wr.write(body);
767             wr.flush();
768         }
769         connection.connect();
770
771         // getting the result, first check response code
772         Integer httpResponseCode = connection.getResponseCode();
773
774         if (httpResponseCode > 299) {
775             logger.trace("HTTP Address: " + url);
776             logger.trace("HTTP Response Code: " + httpResponseCode);
777             fail();
778         }
779
780         InputStream is = connection.getInputStream();
781         BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
782         StringBuilder sb = new StringBuilder();
783         int cp;
784         while ((cp = rd.read()) != -1) {
785             sb.append((char) cp);
786         }
787         is.close();
788         connection.disconnect();
789         return (sb.toString());
790     }
791
792     // timePeriod - in ms
793     public void assertNoPacketReceived(int timePeriod) {
794         try {
795             receivePacket(timePeriod);
796             // If didn't timeout then fail:
797             fail();
798         } catch (SocketTimeoutException ste) {
799         }
800     }
801
802     // ------------------------------- Mask Tests ---------------------------
803
804     public void eidPrefixLookupIPv4() throws SocketTimeoutException {
805         cleanUP();
806         runPrefixTest(LispAFIConvertor.asIPAfiAddress("1.2.3.4"), 16, LispAFIConvertor.asIPAfiAddress("1.2.3.2"),
807                 LispAFIConvertor.asIPAfiAddress("1.1.1.1"), (byte) 32);
808     }
809
810     public void eidPrefixLookupIPv6() throws SocketTimeoutException {
811         cleanUP();
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);
814     }
815
816     private void runPrefixTest(LispAFIAddress registerEID, int registerdMask, LispAFIAddress matchedAddress, LispAFIAddress unMatchedAddress, int mask)
817             throws SocketTimeoutException {
818
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());
876     }
877
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 {
882         cleanUP();
883         String ipString = "10.0.0.1";
884         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress(ipString);
885         int mask = 32;
886         String pass = "pass";
887
888         URL url = createPutURL("key");
889
890         String jsonAuthData = createAuthKeyJSON(pass, address, mask);
891
892         logger.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
893         logger.trace("Address: " + address);
894
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 };
897
898         byte[] zeros = new byte[20];
899
900         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
901
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());
916
917         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
918         mapRegister.setAuthenticationData(zeros);
919
920         sendMapRegister(mapRegister.build());
921         assertNoPacketReceived(3000);
922
923         mapRegister.setAuthenticationData(expectedSha);
924
925         sendMapRegister(mapRegister.build());
926
927         assertMapNotifyRecieved();
928     }
929
930     public void testPasswordMaskMatch() throws Exception {
931         cleanUP();
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");
935
936         int mask = 32;
937         String pass = "pass";
938
939         URL url = createPutURL("key");
940         String jsonAuthData = createAuthKeyJSON(pass, range, 8);
941
942         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
943         // build a MapRegister
944         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
945
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());
959
960         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
961         mapRegister
962                 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
963
964         sendMapRegister(mapRegister.build());
965
966         assertMapNotifyRecieved();
967
968         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
969         mapRegister
970                 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
971
972         sendMapRegister(mapRegister.build());
973         assertNoPacketReceived(3000);
974     }
975
976     private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
977         return registerAddressAndQuery(eid, -1);
978     }
979
980     // takes an address, packs it in a MapRegister, sends it, returns the
981     // MapReply
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);
993         } else {
994             etlrBuilder.setMaskLength((short) 0);
995         }
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)))
1023                 .build());
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();
1034     }
1035
1036     // ------------------------------- LCAF Tests ---------------------------
1037
1038     public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1039         cleanUP();
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());
1051
1052         MapReply reply = registerAddressAndQuery(builder.build());
1053
1054         LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1055         assertTrue(fromNetwork.getAddress() instanceof LcafSourceDestAddress);
1056         LcafSourceDestAddress sourceDestFromNetwork = (LcafSourceDestAddress) fromNetwork.getAddress();
1057
1058         LispAFIAddress receivedAddr1 = (LispAFIAddress) sourceDestFromNetwork.getSrcAddress().getPrimitiveAddress();
1059         LispAFIAddress receivedAddr2 = (LispAFIAddress) sourceDestFromNetwork.getDstAddress().getPrimitiveAddress();
1060
1061         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1062         assertTrue(receivedAddr2 instanceof LispMacAddress);
1063
1064         LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1065         LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1066
1067         assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1068         assertEquals(macString, receivedMAC.getMacAddress().getValue());
1069     }
1070
1071     @Test
1072     public void registerAndQuery__KeyValueLCAF() throws SocketTimeoutException {
1073         cleanUP();
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());
1083
1084         MapReply reply = registerAddressAndQuery(builder.build());
1085
1086         LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1087         assertTrue(fromNetwork.getAddress() instanceof LcafKeyValueAddress);
1088         LcafKeyValueAddress keyValueFromNetwork = (LcafKeyValueAddress) fromNetwork.getAddress();
1089
1090         LispAFIAddress receivedAddr1 = (LispAFIAddress) keyValueFromNetwork.getKey().getPrimitiveAddress();
1091         LispAFIAddress receivedAddr2 = (LispAFIAddress) keyValueFromNetwork.getValue().getPrimitiveAddress();
1092
1093         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1094         assertTrue(receivedAddr2 instanceof LispMacAddress);
1095
1096         LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1097         LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1098
1099         assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1100         assertEquals(macString, receivedMAC.getMacAddress().getValue());
1101     }
1102
1103     public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1104         cleanUP();
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());
1112
1113         MapReply reply = registerAddressAndQuery(listbuilder.build());
1114
1115         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1116
1117         assertTrue(receivedAddress instanceof LcafListAddress);
1118
1119         LcafListAddress listAddrFromNetwork = (LcafListAddress) receivedAddress;
1120         LispAFIAddress receivedAddr1 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(0).getPrimitiveAddress();
1121         LispAFIAddress receivedAddr2 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(1).getPrimitiveAddress();
1122
1123         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1124         assertTrue(receivedAddr2 instanceof LispMacAddress);
1125
1126         assertEquals(macString, ((LispMacAddress) receivedAddr2).getMacAddress().getValue());
1127         assertEquals(ipString, ((LispIpv4Address) receivedAddr1).getIpv4Address().getValue());
1128     }
1129
1130     public void registerAndQuery__SegmentLCAF() throws SocketTimeoutException {
1131         cleanUP();
1132         String ipString = "10.20.255.30";
1133         int instanceId = 6;
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());
1138
1139         MapReply reply = registerAddressAndQuery(builder.build());
1140
1141         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1142         assertTrue(receivedAddress instanceof LcafSegmentAddress);
1143
1144         LcafSegmentAddress segmentfromNetwork = (LcafSegmentAddress) receivedAddress;
1145         LispAFIAddress addrFromSegment = (LispAFIAddress) segmentfromNetwork.getAddress().getPrimitiveAddress();
1146         assertTrue(addrFromSegment instanceof LispIpv4Address);
1147         assertEquals(ipString, ((LispIpv4Address) addrFromSegment).getIpv4Address().getValue());
1148
1149         assertEquals(instanceId, segmentfromNetwork.getInstanceId().intValue());
1150     }
1151
1152     public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1153         cleanUP();
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();
1162         hb.setHop(hop1);
1163         hb.setLookup(true);
1164         hb.setRLOCProbe(false);
1165         hb.setStrict(true);
1166         HopsBuilder hb2 = new HopsBuilder();
1167         hb2.setHop(hop2);
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());
1179
1180         MapReply reply = registerAddressAndQuery(trafficBuilder.build());
1181
1182         assertTrue(LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer()) instanceof LcafTrafficEngineeringAddress);
1183
1184         LcafTrafficEngineeringAddress receivedAddress = (LcafTrafficEngineeringAddress) LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0)
1185                 .getLispAddressContainer());
1186
1187         ReencapHop receivedHop1 = receivedAddress.getHops().get(0);
1188         ReencapHop receivedHop2 = receivedAddress.getHops().get(1);
1189
1190         assertEquals(true, hops1.isLookup());
1191         assertEquals(false, hops1.isRLOCProbe());
1192         assertEquals(true, hops1.isStrict());
1193
1194         assertEquals(false, hops2.isLookup());
1195         assertEquals(true, hops2.isRLOCProbe());
1196         assertEquals(false, hops2.isStrict());
1197
1198         assertTrue(receivedHop1.getHop().getPrimitiveAddress() instanceof LispIpv4Address);
1199         assertTrue(receivedHop2.getHop().getPrimitiveAddress() instanceof LispMacAddress);
1200
1201         assertEquals(ipString, ((LispIpv4Address) receivedHop1.getHop().getPrimitiveAddress()).getIpv4Address().getValue());
1202         assertEquals(macString, ((LispMacAddress) receivedHop2.getHop().getPrimitiveAddress()).getMacAddress().getValue());
1203     }
1204
1205     public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1206         cleanUP();
1207         String ipString = "1.2.3.4";
1208         short protocol = 1;
1209         int ipTOs = 2;
1210         int localPort = 3;
1211         int remotePort = 4;
1212
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());
1221
1222         LcafApplicationDataAddress addressToSend = builder.build();
1223
1224         MapReply reply = registerAddressAndQuery(addressToSend);
1225
1226         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1227
1228         assertTrue(receivedAddress instanceof LcafApplicationDataAddress);
1229
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());
1235
1236         LispIpv4Address ipAddressReceived = (LispIpv4Address) receivedApplicationDataAddress.getAddress().getPrimitiveAddress();
1237         assertEquals(ipString, ipAddressReceived.getIpv4Address().getValue());
1238     }
1239
1240     // ------------------- TimeOut Tests -----------
1241
1242     public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1243         cleanUP();
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());
1284     }
1285
1286     public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1287         cleanUP();
1288         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1289         MapRequest mapRequest = createMapRequest(eid);
1290
1291         testTTLBeforeRegister(mapRequest);
1292
1293         registerForTTL(eid);
1294
1295         testTTLAfterRegister(mapRequest);
1296
1297         causeEntryToBeCleaned();
1298         testTTLAfterClean(mapRequest);
1299
1300         northboundAddKey();
1301         testTTLAfterAutherize(mapRequest);
1302
1303     }
1304
1305     private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1306         MapReply mapReply;
1307         sendMapRequest(mapRequest);
1308         mapReply = receiveMapReply();
1309         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1310     }
1311
1312     private void causeEntryToBeCleaned() {
1313         clusterService.setTimeUnit(TimeUnit.NANOSECONDS);
1314         clusterService.cleanOld();
1315     }
1316
1317     private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1318         MapReply mapReply;
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);
1324     }
1325
1326     private void registerForTTL(LispIpv4Address eid) throws SocketTimeoutException {
1327         MapRegister mapRegister = createMapRegister(eid);
1328         sendMapRegister(mapRegister);
1329         assertMapNotifyRecieved();
1330     }
1331
1332     private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1333         MapReply mapReply;
1334         sendMapRequest(mapRequest);
1335         mapReply = receiveMapReply();
1336         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1337     }
1338
1339     private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1340         MapReply mapReply;
1341         sendMapRequest(mapRequest);
1342         mapReply = receiveMapReply();
1343         assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1344     }
1345
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());
1349     }
1350
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();
1369         return mapRegister;
1370     }
1371
1372     private MapRegister createMapRegister(LispIpv4Address eid) {
1373         return createMapRegister(eid, asIPAfiAddress("4.3.2.1"));
1374     }
1375
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();
1389         return mr;
1390     }
1391
1392     public void testSimpleNonProxy() throws SocketTimeoutException, SocketException {
1393         cleanUP();
1394         String rloc = "127.0.0.3";
1395         int port = LispMessage.PORT_NUM;
1396         Ipv4 ipRloc = LispAFIConvertor.asIPAfiAddress(rloc);
1397         sendProxyMapRequest(rloc, port, ipRloc);
1398
1399     }
1400
1401     public void testNonProxyOtherPort() throws SocketTimeoutException, SocketException {
1402         cleanUP();
1403         String rloc = "127.0.0.3";
1404         int port = 4350;
1405         LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1406                 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1407                 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1408                 .setAddress(
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);
1412
1413     }
1414
1415     public void testRecievingNonProxyOnXtrPort() throws SocketTimeoutException, SocketException, Throwable {
1416         cleanUP();
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())
1425                 .setAddress(
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>() {
1430
1431             @Override
1432             public void onNotification(XtrRequestMapping notification) {
1433                 assertEquals(((LispIpv4Address) mapRequest.getEidRecord().get(0).getLispAddressContainer().getAddress()).getIpv4Address().getValue(),
1434                         eid);
1435                 notificationCalled = true;
1436                 logger.warn("notification arrived");
1437             }
1438         });
1439         sendMapRequest(mapRequest, port);
1440         for (int i = 0; i < MAX_NOTIFICATION_RETRYS; i++) {
1441             if (notificationCalled) {
1442                 return;
1443             } else {
1444                 logger.warn("notification hasn't arrived, sleeping...");
1445                 Thread.sleep(500);
1446             }
1447         }
1448
1449         fail("Notification hasn't arrived");
1450
1451     }
1452
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();
1464     }
1465
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();
1477         return mapRequest;
1478     }
1479
1480     private void assertMapNotifyRecieved() throws SocketTimeoutException {
1481         receiveMapNotify();
1482     }
1483
1484     private MapReply receiveMapReply() throws SocketTimeoutException {
1485         return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1486     }
1487
1488     private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
1489         return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData()));
1490     }
1491
1492     private MapNotify receiveMapNotify() throws SocketTimeoutException {
1493         return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1494     }
1495
1496     private void sendMapRequest(MapRequest mapRequest) {
1497         sendMapRequest(mapRequest, LispMessage.PORT_NUM);
1498     }
1499
1500     private void sendMapRequest(MapRequest mapRequest, int port) {
1501         sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array(), port);
1502     }
1503
1504     private void sendMapRegister(MapRegister mapRegister) {
1505         sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
1506     }
1507
1508     private void sendPacket(byte[] bytesToSend) {
1509         sendPacket(bytesToSend, LispMessage.PORT_NUM);
1510     }
1511
1512     private void sendPacket(byte[] bytesToSend, int port) {
1513         try {
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) {
1519             fail();
1520         }
1521     }
1522
1523     private DatagramPacket receivePacket() throws SocketTimeoutException {
1524         return receivePacket(6000);
1525     }
1526
1527     private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
1528         return receivePacket(socket, timeout);
1529     }
1530
1531     private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException {
1532         try {
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) {
1541             throw ste;
1542         } catch (Throwable t) {
1543             fail();
1544             return null;
1545         }
1546     }
1547
1548     private void initPacketAddress(DatagramPacket packet, int port) throws UnknownHostException {
1549         packet.setAddress(InetAddress.getByName(lispBindAddress));
1550         packet.setPort(port);
1551     }
1552
1553     private DatagramSocket initSocket(DatagramSocket socket, int port) {
1554         try {
1555             socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
1556         } catch (SocketException e) {
1557             e.printStackTrace();
1558             fail();
1559         }
1560         return socket;
1561     }
1562
1563     private byte[] extractWSUdpByteArray(String wiresharkHex) {
1564         final int HEADER_LEN = 42;
1565         byte[] res = new byte[1000];
1566         String[] split = wiresharkHex.split(" ");
1567         int counter = 0;
1568         for (String cur : split) {
1569             cur = cur.trim();
1570             if (cur.length() == 2) {
1571                 ++counter;
1572                 if (counter > HEADER_LEN) {
1573                     res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
1574                 }
1575
1576             }
1577         }
1578         return Arrays.copyOf(res, counter - HEADER_LEN);
1579     }
1580
1581     private String stateToString(int state) {
1582         switch (state) {
1583         case Bundle.ACTIVE:
1584             return "ACTIVE";
1585         case Bundle.INSTALLED:
1586             return "INSTALLED";
1587         case Bundle.RESOLVED:
1588             return "RESOLVED";
1589         case Bundle.UNINSTALLED:
1590             return "UNINSTALLED";
1591         default:
1592             return "Not CONVERTED";
1593         }
1594     }
1595
1596     @SuppressWarnings({ "rawtypes", "unchecked" })
1597     private void areWeReady() throws InvalidSyntaxException {
1598         assertNotNull(bc);
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));
1607
1608                 try {
1609                     String host = element.getHeaders().get(Constants.FRAGMENT_HOST);
1610                     if (host != null) {
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));
1613                     } else {
1614                         element.start();
1615                     }
1616                 } catch (BundleException e) {
1617                     logger.error("BundleException:", e);
1618                     fail();
1619                 }
1620
1621                 debugit = true;
1622
1623             }
1624         }
1625         if (debugit) {
1626             logger.warn(("Do some debugging because some bundle is unresolved"));
1627         }
1628         // assertNotNull(broker);
1629
1630         int retry = 0;
1631         ServiceReference r = null;
1632         while (this.lms == null && retry < MAX_SERVICE_LOAD_RETRIES) {
1633
1634             r = bc.getServiceReference(IFlowMapping.class.getName());
1635             // r.getPropertyKeys();
1636             if (r != null) {
1637                 this.lms = (IFlowMapping) bc.getService(r);
1638             } else {
1639                 try {
1640                     Thread.sleep(1000);
1641                 } catch (InterruptedException e) {
1642                 }
1643             }
1644             retry += 1;
1645         }
1646
1647         assertNotNull(IFlowMapping.class.getName() + " service wasn't found in bundle context ", this.lms);
1648
1649         r = bc.getServiceReference(ILispDAO.class.getName());
1650         if (r != null) {
1651             this.clusterService = (ClusterDAOService) bc.getService(r);
1652         }
1653
1654         assertNotNull(ILispDAO.class.getName() + " service wasn't found in bundle context ", this.clusterService);
1655         r = bc.getServiceReference(IConfigLispPlugin.class.getName());
1656         if (r != null) {
1657             this.configLispPlugin = (IConfigLispPlugin) bc.getService(r);
1658         }
1659
1660         assertNotNull(IConfigLispPlugin.class.getName() + " service wasn't found in bundle context ", this.configLispPlugin);
1661         configLispPlugin.setLispAddress(lispBindAddress);
1662
1663         // Uncomment this code to Know which services were actually loaded to
1664         // BundleContext
1665         /*
1666          * for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
1667          * logger.trace(sr.getBundle().getSymbolicName());
1668          * logger.trace(sr.toString()); }
1669          */
1670         try {
1671             Thread.sleep(1000);
1672         } catch (InterruptedException e) {
1673         }
1674     }
1675
1676     private Ipv4 asIPAfiAddress(String ip) {
1677         return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1678     }
1679
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();
1683     }
1684
1685     private Mac asMacAfiAddress(String mac) {
1686         return new MacBuilder().setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1687     }
1688
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();
1692     }
1693
1694     private void cleanUP() {
1695         after();
1696         lms.clean();
1697         lms.shouldListenOnXtrPort(false);
1698         socket = initSocket(socket, LispMessage.PORT_NUM);
1699
1700     }
1701
1702 }