8c1bce1c648dfd7bf94cee3555f955ea86851aca
[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.IConfigLispSouthboundPlugin;
61 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.EidToLocatorRecord.Action;
62 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafApplicationDataAddress;
63 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafKeyValueAddress;
64 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafListAddress;
65 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafSegmentAddress;
66 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafSourceDestAddress;
67 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafTrafficEngineeringAddress;
68 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
69 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispIpv4Address;
70 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispMacAddress;
71 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
72 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
73 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
74 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
75 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.ReencapHop;
76 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.XtrRequestMapping;
77 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
78 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
79 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
80 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
81 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafkeyvalueaddress.KeyBuilder;
82 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafkeyvalueaddress.ValueBuilder;
83 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.Addresses;
84 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaflistaddress.AddressesBuilder;
85 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafsegmentaddress.AddressBuilder;
86 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafsourcedestaddress.DstAddressBuilder;
87 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafsourcedestaddress.SrcAddressBuilder;
88 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaftrafficengineeringaddress.Hops;
89 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcaftrafficengineeringaddress.HopsBuilder;
90 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
91 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4;
92 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
93 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationData;
94 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationDataBuilder;
95 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafKeyValueBuilder;
96 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafListBuilder;
97 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSegmentBuilder;
98 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSourceDest;
99 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafSourceDestBuilder;
100 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafTrafficEngineeringBuilder;
101 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Mac;
102 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.MacBuilder;
103 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.NoBuilder;
104 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
105 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
106 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapregisternotification.MapRegisterBuilder;
107 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
108 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRlocBuilder;
109 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
110 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
111 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.reencaphop.Hop;
112 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.reencaphop.HopBuilder;
113 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
114 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
115 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
116 import org.ops4j.pax.exam.Configuration;
117 import org.ops4j.pax.exam.Option;
118 import org.ops4j.pax.exam.junit.PaxExam;
119 import org.osgi.framework.Bundle;
120 import org.osgi.framework.BundleContext;
121 import org.osgi.framework.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 @RunWith(PaxExam.class)
128 public class MappingServiceIntegrationTest {
129
130     private IFlowMapping lms;
131     private ClusterDAOService clusterService;
132     protected static final Logger logger = LoggerFactory.getLogger(MappingServiceIntegrationTest.class);
133     private byte[] mapRequestPacket;
134     private byte[] mapRegisterPacketWithNotify;
135     private byte[] mapRegisterPacketWithoutNotify;
136     private IConfigLispSouthboundPlugin configLispPlugin;
137     String lispBindAddress = "127.0.0.1";
138     String ourAddress = "127.0.0.2";
139     private LispAFIAddress locatorEid;
140     private DatagramSocket socket;
141     private byte[] mapRegisterPacketWithAuthenticationAndMapNotify;
142
143     public static final String ODL = "org.opendaylight.controller";
144     public static final String YANG = "org.opendaylight.yangtools";
145     public static final String JERSEY = "com.sun.jersey";
146     private static final int MAX_SERVICE_LOAD_RETRIES = 45;
147     private static final int MAX_NOTIFICATION_RETRYS = 20;
148
149     @After
150     public void after() {
151         if (socket != null) {
152             socket.close();
153         }
154         if (connection != null) {
155             connection.disconnect();
156         }
157     }
158
159     @Before
160     public void before() throws Exception {
161         areWeReady();
162         locatorEid = asIPAfiAddress("4.3.2.1");
163         socket = initSocket(socket, LispMessage.PORT_NUM);
164
165         // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
166         // LISP(Type = 8 - Encapsulated)
167         // IP: 192.168.136.10 -> 153.16.254.1
168         // UDP: 56756
169         // LISP(Type = 1 Map-Request
170         // Record Count: 1
171         // ITR-RLOC count: 0
172         // Source EID AFI: 0
173         // Source EID not present
174         // Nonce: 0x3d8d2acd39c8d608
175         // ITR-RLOC AFI=1 Address=192.168.136.10
176         // Record 1: 153.16.254.1/32
177         mapRequestPacket = extractWSUdpByteArray(new String("0000   00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
178                 + "0010   00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
179                 + "0020   00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
180                 + "0030   00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
181                 + "0040   fe 01 dd b4 10 f6 00 28 ef 3a 10 00 00 01 3d 8d "
182                 + "0050   2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 00 02 00 20 " //
183                 + "0060   00 01 99 10 fe 01"));
184
185         // IP: 192.168.136.10 -> 128.223.156.35
186         // UDP: 49289 -> 4342
187         // LISP(Type = 3 Map-Register, P=1, M=1
188         // Record Counter: 1
189         // Nonce: 0
190         // Key ID: 0x0001
191         // AuthDataLength: 20 Data:
192         // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
193         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
194         // No-Action
195         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
196         // Priority/Weight: 1/100, Multicast Priority/Weight:
197         // 255/0
198         //
199
200         mapRegisterPacketWithAuthenticationAndMapNotify = extractWSUdpByteArray(new String("0000   00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
201                 + "0010   00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
202                 + "0020   9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
203                 + "0030   00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
204                 + "0040   71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
205                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
206                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
207
208         // IP: 192.168.136.10 -> 128.223.156.35
209         // UDP: 49289 -> 4342
210         // LISP(Type = 3 Map-Register, P=1, M=1
211         // Record Counter: 1
212         // Nonce: 7
213         // Key ID: 0x0000 NO AUTHENTICATION!!
214         // AuthDataLength: 00 Data:
215         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
216         // No-Action
217         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
218         // Priority/Weight: 1/100, Multicast Priority/Weight:
219         // 255/0
220         //
221
222         mapRegisterPacketWithNotify = extractWSUdpByteArray(new String("0000   00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
223                 + "0010   00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
224                 + "0020   9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
225                 + "0030   00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
226                 + "0040   71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
227                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
228                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
229
230         // IP: 192.168.136.10 -> 128.223.156.35
231         // UDP: 49289 -> 4342
232         // LISP(Type = 3 Map-Register, P=1, M=1
233         // Record Counter: 1
234         // Nonce: 7
235         // Key ID: 0x0000 NO AUTHENTICATION!!
236         // AuthDataLength: 00 Data:
237         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
238         // No-Action
239         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
240         // Priority/Weight: 1/100, Multicast Priority/Weight:
241         // 255/0
242         //
243
244         mapRegisterPacketWithoutNotify = extractWSUdpByteArray(new String("0000   00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
245                 + "0010   00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
246                 + "0020   9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
247                 + "0030   00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
248                 + "0040   71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
249                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
250                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
251     }
252
253     @Inject
254     private BundleContext bc;
255     private HttpURLConnection connection;
256     protected static boolean notificationCalled;
257
258     // Configure the OSGi container
259     @Configuration
260     public Option[] config() {
261         return options(MappingServiceTestHelper.mappingServiceBundlesWithClusterDAO());
262     }
263
264     @Test
265     public void testSimpleUsage() throws Exception {
266         mapRequestSimple();
267         mapRegisterWithMapNotify();
268         mapRegisterWithMapNotifyAndMapRequest();
269         registerAndQuery__MAC();
270         mapRequestMapRegisterAndMapRequest();
271         mapRegisterWithAuthenticationWithoutConfiguringAKey();
272         mapRegisterWithoutMapNotify();
273     }
274
275     @Test
276     public void testLCAFs() throws Exception {
277         registerAndQuery__SrcDestLCAF();
278         registerAndQuery__KeyValueLCAF();
279         registerAndQuery__ListLCAF();
280         registerAndQuery__ApplicationData();
281         registerAndQuery__TrafficEngineering();
282         registerAndQuery__SegmentLCAF();
283     }
284
285     @Test
286     public void testMask() throws Exception {
287         testPasswordExactMatch();
288         testPasswordMaskMatch();
289         eidPrefixLookupIPv4();
290         eidPrefixLookupIPv6();
291     }
292
293     @Test
294     public void testNorthbound() throws Exception {
295         northboundAddKey();
296         northboundAddMapping();
297         northboundRetrieveKey();
298         northboundRetrieveMapping();
299         northboundRetrieveSourceDestKey();
300         northboundRetrieveSourceDestMapping();
301     }
302
303     @Test
304     public void testOverWriting() throws Exception {
305         testMapRegisterDosntOverwritesOtherSubKeys();
306         testMapRegisterOverwritesSameSubkey();
307         testMapRegisterOverwritesNoSubkey();
308         testMapRegisterDoesntOverwritesNoSubkey();
309     }
310
311     @Test
312     public void testTimeOuts() throws Exception {
313         mapRequestMapRegisterAndMapRequestTestTimeout();
314         mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse();
315     }
316
317     @Test
318     public void testNonProxy() throws Throwable {
319         testSimpleNonProxy();
320         testNonProxyOtherPort();
321         testRecievingNonProxyOnXtrPort();
322     }
323
324     @Test
325     public void testSmr() throws Exception {
326         registerQueryRegisterWithSmr();
327     }
328
329     // ------------------------------- Simple Tests ---------------------------
330
331     public void mapRequestSimple() throws SocketTimeoutException {
332         cleanUP();
333
334         // This Map-Request is sent from a source port different from 4342
335         // We close and bind the socket on the correct port
336         if (socket != null) {
337             socket.close();
338         }
339         socket = initSocket(socket, 56756);
340
341         sendPacket(mapRequestPacket);
342         ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
343         MapReply reply = MapReplySerializer.getInstance().deserialize(readBuf);
344         assertEquals(4435248268955932168L, reply.getNonce().longValue());
345
346     }
347
348     public void mapRegisterWithMapNotify() throws SocketTimeoutException {
349         cleanUP();
350         sendPacket(mapRegisterPacketWithNotify);
351         MapNotify reply = receiveMapNotify();
352         assertEquals(7, reply.getNonce().longValue());
353     }
354
355     public void mapRegisterWithMapNotifyAndMapRequest() throws SocketTimeoutException {
356         cleanUP();
357         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
358
359         MapReply mapReply = registerAddressAndQuery(eid, 32);
360
361         assertEquals(4, mapReply.getNonce().longValue());
362         assertEquals(LispAFIConvertor.toContainer(locatorEid), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
363                 .getLispAddressContainer());
364
365     }
366
367     public void registerAndQuery__MAC() throws SocketTimeoutException {
368         cleanUP();
369         String macAddress = "01:02:03:04:05:06";
370
371         MapReply reply = registerAddressAndQuery(asMacAfiAddress(macAddress));
372
373         assertTrue(true);
374         LispAFIAddress addressFromNetwork = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
375         assertTrue(addressFromNetwork instanceof LispMacAddress);
376         String macAddressFromReply = ((Mac) addressFromNetwork).getMacAddress().getValue();
377
378         assertEquals(macAddress, macAddressFromReply);
379     }
380
381     public void mapRequestMapRegisterAndMapRequest() throws SocketTimeoutException {
382         cleanUP();
383         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
384         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
385         mapRequestBuilder.setNonce((long) 4);
386         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
387                 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
388         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
389         mapRequestBuilder.getEidRecord().add(
390                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
391         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
392         mapRequestBuilder.getItrRloc().add(
393                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
394         sendMapRequest(mapRequestBuilder.build());
395         MapReply mapReply = receiveMapReply();
396         assertEquals(4, mapReply.getNonce().longValue());
397         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
398         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
399         mapRegisterbuilder.setWantMapNotify(true);
400         mapRegisterbuilder.setNonce((long) 8);
401         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
402         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
403         etlrBuilder.setMaskLength((short) 32);
404         etlrBuilder.setRecordTtl(254);
405         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
406         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
407         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
408         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
409         mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
410         mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
411         sendMapRegister(mapRegisterbuilder.build());
412         MapNotify mapNotify = receiveMapNotify();
413         assertEquals(8, mapNotify.getNonce().longValue());
414         sendMapRequest(mapRequestBuilder.build());
415         mapReply = receiveMapReply();
416         assertEquals(4, mapReply.getNonce().longValue());
417         assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
418                 .getLispAddressContainer());
419
420     }
421
422     public void testMapRegisterDosntOverwritesOtherSubKeys() throws SocketTimeoutException {
423         cleanUP();
424         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
425         LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
426         LispAFIAddress rloc1 = LispAFIConvertor.asKeyValue("subkey1", LispAFIConvertor.toPrimitive(rloc1Value));
427         LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
428         LispAFIAddress rloc2 = LispAFIConvertor.asKeyValue("subkey2", LispAFIConvertor.toPrimitive(rloc2Value));
429         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
430         assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
431         assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
432         assertEquals(LispAFIConvertor.toContainer(rloc1), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(1).getLispAddressContainer());
433     }
434
435     public void testMapRegisterOverwritesSameSubkey() throws SocketTimeoutException {
436         cleanUP();
437         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
438         LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
439         LispAFIAddress rloc1 = LispAFIConvertor.asKeyValue("subkey", LispAFIConvertor.toPrimitive(rloc1Value));
440         LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
441         LispAFIAddress rloc2 = LispAFIConvertor.asKeyValue("subkey", LispAFIConvertor.toPrimitive(rloc2Value));
442         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
443         assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
444         assertEquals(LispAFIConvertor.toContainer(rloc2), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
445     }
446
447     public void testMapRegisterOverwritesNoSubkey() throws SocketTimeoutException {
448         cleanUP();
449         lms.setOverwrite(true);
450         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
451         LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
452         LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
453         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
454         assertEquals(1, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
455         assertEquals(LispAFIConvertor.toContainer(rloc2Value), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
456                 .getLispAddressContainer());
457     }
458
459     public void testMapRegisterDoesntOverwritesNoSubkey() throws SocketTimeoutException {
460         cleanUP();
461         lms.setOverwrite(false);
462         LispAFIAddress eid = asIPAfiAddress("1.2.3.4");
463         LispAFIAddress rloc1Value = asIPAfiAddress("4.3.2.1");
464         LispAFIAddress rloc2Value = asIPAfiAddress("4.3.2.2");
465         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
466         assertEquals(2, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
467         LispAddressContainer rloc1ReturnValueContainer = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer();
468         LispAddressContainer rloc2ReturnValueContainer = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(1).getLispAddressContainer();
469         assertTrue((LispAFIConvertor.toContainer(rloc1Value).equals(rloc1ReturnValueContainer) && LispAFIConvertor.toContainer(rloc2Value).equals(
470                 rloc2ReturnValueContainer))
471                 || (LispAFIConvertor.toContainer(rloc1Value).equals(rloc2ReturnValueContainer) && LispAFIConvertor.toContainer(rloc2Value).equals(
472                         rloc1ReturnValueContainer)));
473     }
474
475     private MapReply sendMapRegisterTwiceWithDiffrentValues(LispAFIAddress eid, LispAFIAddress rloc1, LispAFIAddress rloc2)
476             throws SocketTimeoutException {
477         MapRegister mb = createMapRegister(eid, rloc1);
478         MapNotify mapNotify = lms.handleMapRegister(mb, false);
479         MapRequest mr = createMapRequest(eid);
480         MapReply mapReply = lms.handleMapRequest(mr);
481         assertEquals(mb.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0)
482                 .getLocatorRecord().get(0).getLispAddressContainer());
483         mb = createMapRegister(eid, rloc2);
484         mapNotify = lms.handleMapRegister(mb, false);
485         assertEquals(8, mapNotify.getNonce().longValue());
486         mr = createMapRequest(eid);
487         sendMapRequest(mr);
488         mapReply = lms.handleMapRequest(mr);
489         return mapReply;
490     }
491
492     public void mapRegisterWithAuthenticationWithoutConfiguringAKey() throws SocketTimeoutException {
493         cleanUP();
494         sendPacket(mapRegisterPacketWithAuthenticationAndMapNotify);
495         try {
496             receivePacket(3000);
497             // If didn't timeout then fail:
498             fail();
499         } catch (SocketTimeoutException ste) {
500         }
501     }
502
503     public void mapRegisterWithoutMapNotify() {
504         cleanUP();
505         sendPacket(mapRegisterPacketWithoutNotify);
506         try {
507             receivePacket(3000);
508             // If didn't timeout then fail:
509             fail();
510         } catch (SocketTimeoutException ste) {
511         }
512     }
513
514     public void registerQueryRegisterWithSmr() throws SocketTimeoutException {
515         cleanUP();
516         lms.setShouldUseSmr(true);
517
518         sendPacket(mapRegisterPacketWithoutNotify);
519         sendPacket(mapRequestPacket);
520         mapRegisterPacketWithoutNotify[mapRegisterPacketWithoutNotify.length - 1] += 1;
521         sendPacket(mapRegisterPacketWithoutNotify);
522
523         ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
524         MapRequest smr = MapRequestSerializer.getInstance().deserialize(readBuf);
525         assertTrue(smr.isSmr());
526         LispAddressContainer smrEid = smr.getEidRecord().get(0).getLispAddressContainer();
527         assertTrue(LispAFIConvertor.toContainer(asIPAfiAddress("153.16.254.1")).equals(smrEid));
528     }
529
530     // --------------------- Northbound Tests ---------------------------
531
532     private void northboundAddKey() throws Exception {
533         cleanUP();
534         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
535         int mask = 32;
536         String pass = "asdf";
537
538         URL url = createPutURL("key");
539         String authKeyJSON = createAuthKeyJSON(pass, address, mask);
540         callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
541
542         String retrievedKey = lms.getAuthenticationKey(LispAFIConvertor.toContainer(address), mask);
543
544         // Check stored password matches the one sent
545         assertEquals(pass, retrievedKey);
546
547     }
548
549     private void northboundRetrieveSourceDestKey() throws Exception {
550         cleanUP();
551         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
552                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
553         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
554                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
555         int mask1 = 32;
556         int mask2 = 32;
557         LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
558                 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
559                 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
560                 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
561         String pass = "asdf";
562
563         lms.addAuthenticationKey(LispAFIConvertor.toContainer(sourceDestAddress), mask1, pass);
564
565         // URL url = createGetKeyIPv4URL(address1, mask1);
566         URL url = createGetKeySourceDestURL(address1.getAfi(), ((LispIpv4Address) sourceDestAddress.getSrcAddress().getPrimitiveAddress())
567                 .getIpv4Address().getValue(), sourceDestAddress.getSrcMaskLength(), ((LispIpv4Address) sourceDestAddress.getDstAddress()
568                 .getPrimitiveAddress()).getIpv4Address().getValue(), sourceDestAddress.getDstMaskLength());
569         String reply = callURL("GET", null, "application/json", null, url);
570         JSONTokener jt = new JSONTokener(reply);
571         JSONObject json = new JSONObject(jt);
572
573         // test that the password matches what was we expected.
574         assertEquals(pass, json.get("key"));
575
576     }
577
578     private void northboundRetrieveKey() throws Exception {
579         cleanUP();
580         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
581         int mask = 32;
582         String pass = "asdf";
583
584         lms.addAuthenticationKey(LispAFIConvertor.toContainer(address), mask, pass);
585
586         URL url = createGetKeyIPv4URL(address, mask);
587         String reply = callURL("GET", null, "application/json", null, url);
588         JSONTokener jt = new JSONTokener(reply);
589         JSONObject json = new JSONObject(jt);
590
591         // test that the password matches what was we expected.
592         assertEquals(pass, json.get("key"));
593
594     }
595
596     private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
597         return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
598                 + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
599     }
600
601     private void northboundAddMapping() throws Exception {
602         cleanUP();
603         String pass = "asdf";
604         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
605         int mask = 32;
606         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
607
608         // NB add mapping always checks the key
609         lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
610
611         URL url = createPutURL("mapping");
612         String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
613         callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
614
615         // Retrieve the RLOC from the database
616         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
617         mapRequestBuilder.setPitr(false);
618         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
619         mapRequestBuilder.getEidRecord().add(
620                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
621         MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
622
623         LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
624                 .getAddress();
625
626         assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
627
628     }
629
630     private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
631         String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ " + "\"proxyMapReply\" : false, "
632                 + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : " + "{ " + "\"ipAddress\" : \""
633                 + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "}," + "\"mapVersion\" : 0,"
634                 + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ " + "\"multicastPriority\" : 1,"
635                 + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\"," + "\"afi\" : "
636                 + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50," + "\"rlocProbed\" : false, "
637                 + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], " + "\"recordTtl\" : 100" + "} " + "], "
638                 + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
639
640         return jsonString;
641     }
642
643     private void northboundRetrieveMapping() throws Exception {
644         cleanUP();
645         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
646         int mask = 32;
647         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
648         // Insert mapping in the database
649         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
650         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
651         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
652         etlr.setMaskLength((short) mask);
653         etlr.setRecordTtl(254);
654         etlr.setAuthoritative(false);
655         etlr.setAction(Action.NoAction);
656         LocatorRecordBuilder record = new LocatorRecordBuilder();
657         record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
658         record.setRouted(true);
659         record.setRlocProbed(false);
660         record.setLocalLocator(false);
661         record.setPriority((short) 1);
662         record.setWeight((short) 50);
663         record.setMulticastPriority((short) 1);
664         record.setMulticastWeight((short) 1);
665         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
666         etlr.getLocatorRecord().add(record.build());
667         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
668         mapRegister.getEidToLocatorRecord().add(etlr.build());
669         lms.handleMapRegister(mapRegister.build(), false);
670
671         // Get mapping using NB interface. No IID used
672         URL url = createGetMappingIPv4URL(0, eid, mask);
673         String reply = callURL("GET", null, "application/json", null, url);
674         JSONTokener jt = new JSONTokener(reply);
675         JSONObject json = new JSONObject(jt);
676
677         // With just one locator, locators is not a JSONArray
678         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
679
680         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
681
682     }
683
684     private void northboundRetrieveSourceDestMapping() throws Exception {
685         cleanUP();
686         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
687                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
688         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
689                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
690         int mask1 = 32;
691         int mask2 = 32;
692         LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
693                 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
694                 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
695                 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
696         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
697
698         // Insert mapping in the database
699         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
700         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
701         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(sourceDestAddress));
702         etlr.setMaskLength((short) mask1);
703         etlr.setRecordTtl(254);
704         etlr.setAuthoritative(false);
705         etlr.setAction(Action.NoAction);
706         LocatorRecordBuilder record = new LocatorRecordBuilder();
707         record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
708         record.setRouted(true);
709         record.setRlocProbed(false);
710         record.setLocalLocator(false);
711         record.setPriority((short) 1);
712         record.setWeight((short) 50);
713         record.setMulticastPriority((short) 1);
714         record.setMulticastWeight((short) 1);
715         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
716         etlr.getLocatorRecord().add(record.build());
717         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
718         mapRegister.getEidToLocatorRecord().add(etlr.build());
719         lms.handleMapRegister(mapRegister.build(), false);
720
721         // Get mapping using NB interface. No IID used
722         URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
723                 mask2);
724         String reply = callURL("GET", null, "application/json", null, url);
725         JSONTokener jt = new JSONTokener(reply);
726         JSONObject json = new JSONObject(jt);
727
728         // With just one locator, locators is not a JSONArray
729         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
730
731         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
732
733     }
734
735     private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
736         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key", address.getAfi().shortValue(),
737                 address.getIpv4Address().getValue(), mask);
738         URL url = new URL(restUrl);
739         return url;
740     }
741
742     private URL createGetKeySourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
743         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "key", afi, srcAddress, srcMask,
744                 dstAddress, dstMask);
745         URL url = new URL(restUrl);
746         return url;
747     }
748
749     private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
750         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "mapping", afi, srcAddress,
751                 srcMask, dstAddress, dstMask);
752         URL url = new URL(restUrl);
753         return url;
754     }
755
756     private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
757         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping", iid, address.getAfi()
758                 .shortValue(), address.getIpv4Address().getValue(), mask);
759         URL url = new URL(restUrl);
760         return url;
761     }
762
763     private URL createPutURL(String resource) throws MalformedURLException {
764
765         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
766
767         URL url = new URL(restUrl);
768         return url;
769     }
770
771     private String createAuthenticationString() {
772         String authString = "admin:admin";
773         byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
774         String authStringEnc = new String(authEncBytes);
775         return authStringEnc;
776     }
777
778     private String callURL(String method, String content, String accept, String body, URL url) throws IOException, JSONException {
779         String authStringEnc = createAuthenticationString();
780         connection = (HttpURLConnection) url.openConnection();
781         connection.setRequestMethod(method);
782         connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
783         if (content != null) {
784             connection.setRequestProperty("Content-Type", content);
785         }
786         if (accept != null) {
787             connection.setRequestProperty("Accept", accept);
788         }
789         if (body != null) {
790             // now add the request body
791             connection.setDoOutput(true);
792             OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
793             wr.write(body);
794             wr.flush();
795         }
796         connection.connect();
797
798         // getting the result, first check response code
799         Integer httpResponseCode = connection.getResponseCode();
800
801         if (httpResponseCode > 299) {
802             logger.trace("HTTP Address: " + url);
803             logger.trace("HTTP Response Code: " + httpResponseCode);
804             fail();
805         }
806
807         InputStream is = connection.getInputStream();
808         BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
809         StringBuilder sb = new StringBuilder();
810         int cp;
811         while ((cp = rd.read()) != -1) {
812             sb.append((char) cp);
813         }
814         is.close();
815         connection.disconnect();
816         return (sb.toString());
817     }
818
819     // timePeriod - in ms
820     public void assertNoPacketReceived(int timePeriod) {
821         try {
822             receivePacket(timePeriod);
823             // If didn't timeout then fail:
824             fail();
825         } catch (SocketTimeoutException ste) {
826         }
827     }
828
829     // ------------------------------- Mask Tests ---------------------------
830
831     public void eidPrefixLookupIPv4() throws SocketTimeoutException {
832         cleanUP();
833         runPrefixTest(LispAFIConvertor.asIPAfiAddress("1.2.3.4"), 16, LispAFIConvertor.asIPAfiAddress("1.2.3.2"),
834                 LispAFIConvertor.asIPAfiAddress("1.1.1.1"), (byte) 32);
835     }
836
837     public void eidPrefixLookupIPv6() throws SocketTimeoutException {
838         cleanUP();
839         runPrefixTest(LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:6:7:8"), 64, LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:1:2:3"),
840                 LispAFIConvertor.asIPv6AfiAddress("1:2:3:1:2:3:1:2"), 128);
841     }
842
843     private void runPrefixTest(LispAFIAddress registerEID, int registerdMask, LispAFIAddress matchedAddress, LispAFIAddress unMatchedAddress, int mask)
844             throws SocketTimeoutException {
845
846         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
847         mapRegister.setWantMapNotify(true);
848         mapRegister.setNonce((long) 8);
849         mapRegister.setWantMapNotify(true);
850         mapRegister.setKeyId((short) 0);
851         mapRegister.setAuthenticationData(new byte[0]);
852         mapRegister.setNonce((long) 8);
853         mapRegister.setProxyMapReply(false);
854         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
855         etlr.setRecordTtl(254);
856         etlr.setAction(Action.NoAction);
857         etlr.setAuthoritative(false);
858         etlr.setMapVersion((short) 0);
859         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(registerEID));
860         etlr.setMaskLength((short) registerdMask);
861         etlr.setRecordTtl(254);
862         LocatorRecordBuilder record = new LocatorRecordBuilder();
863         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
864         record.setLocalLocator(false);
865         record.setRlocProbed(false);
866         record.setRouted(true);
867         record.setMulticastPriority((short) 0);
868         record.setMulticastWeight((short) 0);
869         record.setPriority((short) 0);
870         record.setWeight((short) 0);
871         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
872         etlr.getLocatorRecord().add(record.build());
873         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
874         mapRegister.getEidToLocatorRecord().add(etlr.build());
875         sendMapRegister(mapRegister.build());
876         MapNotify mapNotify = receiveMapNotify();
877         assertEquals(8, mapNotify.getNonce().longValue());
878         MapRequestBuilder mapRequest = new MapRequestBuilder();
879         mapRequest.setNonce((long) 4);
880         mapRequest.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
881         mapRequest.setEidRecord(new ArrayList<EidRecord>());
882         mapRequest.setAuthoritative(false);
883         mapRequest.setMapDataPresent(false);
884         mapRequest.setPitr(false);
885         mapRequest.setProbe(false);
886         mapRequest.setSmr(false);
887         mapRequest.setSmrInvoked(false);
888         mapRequest.getEidRecord().add(
889                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(matchedAddress)).build());
890         mapRequest.setItrRloc(new ArrayList<ItrRloc>());
891         mapRequest.getItrRloc().add(
892                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ourAddress))).build());
893         sendMapRequest(mapRequest.build());
894         MapReply mapReply = receiveMapReply();
895         assertEquals(4, mapReply.getNonce().longValue());
896         assertEquals(record.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
897         mapRequest.setEidRecord(new ArrayList<EidRecord>());
898         mapRequest.getEidRecord().add(
899                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(unMatchedAddress)).build());
900         sendMapRequest(mapRequest.build());
901         mapReply = receiveMapReply();
902         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
903     }
904
905     // This registers an IP with a MapRegister, then adds a password via the
906     // northbound REST API
907     // and checks that the password works
908     public void testPasswordExactMatch() throws Exception {
909         cleanUP();
910         String ipString = "10.0.0.1";
911         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress(ipString);
912         int mask = 32;
913         String pass = "pass";
914
915         URL url = createPutURL("key");
916
917         String jsonAuthData = createAuthKeyJSON(pass, address, mask);
918
919         logger.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
920         logger.trace("Address: " + address);
921
922         byte[] expectedSha = new byte[] { (byte) 146, (byte) 234, (byte) 52, (byte) 247, (byte) 186, (byte) 232, (byte) 31, (byte) 249, (byte) 87,
923                 (byte) 73, (byte) 234, (byte) 54, (byte) 225, (byte) 160, (byte) 129, (byte) 251, (byte) 73, (byte) 53, (byte) 196, (byte) 62 };
924
925         byte[] zeros = new byte[20];
926
927         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
928
929         // build a MapRegister
930         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
931         mapRegister.setWantMapNotify(true);
932         mapRegister.setNonce((long) 8);
933         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
934         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(address));
935         etlr.setMaskLength((short) mask);
936         etlr.setRecordTtl(254);
937         LocatorRecordBuilder record = new LocatorRecordBuilder();
938         record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
939         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
940         etlr.getLocatorRecord().add(record.build());
941         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
942         mapRegister.getEidToLocatorRecord().add(etlr.build());
943
944         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
945         mapRegister.setAuthenticationData(zeros);
946
947         sendMapRegister(mapRegister.build());
948         assertNoPacketReceived(3000);
949
950         mapRegister.setAuthenticationData(expectedSha);
951
952         sendMapRegister(mapRegister.build());
953
954         assertMapNotifyRecieved();
955     }
956
957     public void testPasswordMaskMatch() throws Exception {
958         cleanUP();
959         LispIpv4Address addressInRange = LispAFIConvertor.asIPAfiAddress("10.20.30.40");
960         LispIpv4Address addressOutOfRange = LispAFIConvertor.asIPAfiAddress("20.40.30.40");
961         LispIpv4Address range = LispAFIConvertor.asIPAfiAddress("10.20.30.0");
962
963         int mask = 32;
964         String pass = "pass";
965
966         URL url = createPutURL("key");
967         String jsonAuthData = createAuthKeyJSON(pass, range, 8);
968
969         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
970         // build a MapRegister
971         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
972
973         mapRegister.setWantMapNotify(true);
974         mapRegister.setNonce((long) 8);
975         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
976         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressInRange));
977         etlr.setMaskLength((short) mask);
978         etlr.setRecordTtl(254);
979         LocatorRecordBuilder record = new LocatorRecordBuilder();
980         record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
981         record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
982         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
983         etlr.getLocatorRecord().add(record.build());
984         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
985         mapRegister.getEidToLocatorRecord().add(etlr.build());
986
987         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
988         mapRegister
989                 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
990
991         sendMapRegister(mapRegister.build());
992
993         assertMapNotifyRecieved();
994
995         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
996         mapRegister
997                 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
998
999         sendMapRegister(mapRegister.build());
1000         assertNoPacketReceived(3000);
1001     }
1002
1003     private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
1004         return registerAddressAndQuery(eid, -1);
1005     }
1006
1007     // takes an address, packs it in a MapRegister, sends it, returns the
1008     // MapReply
1009     private MapReply registerAddressAndQuery(LispAFIAddress eid, int maskLength) throws SocketTimeoutException {
1010         MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
1011         mapRegisterBuilder.setWantMapNotify(true);
1012         mapRegisterBuilder.setKeyId((short) 0);
1013         mapRegisterBuilder.setAuthenticationData(new byte[0]);
1014         mapRegisterBuilder.setNonce((long) 8);
1015         mapRegisterBuilder.setProxyMapReply(false);
1016         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1017         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1018         if (maskLength != -1) {
1019             etlrBuilder.setMaskLength((short) maskLength);
1020         } else {
1021             etlrBuilder.setMaskLength((short) 0);
1022         }
1023         etlrBuilder.setRecordTtl(254);
1024         etlrBuilder.setAction(Action.NoAction);
1025         etlrBuilder.setAuthoritative(false);
1026         etlrBuilder.setMapVersion((short) 0);
1027         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1028         recordBuilder.setLocalLocator(false);
1029         recordBuilder.setRlocProbed(false);
1030         recordBuilder.setRouted(true);
1031         recordBuilder.setMulticastPriority((short) 0);
1032         recordBuilder.setMulticastWeight((short) 0);
1033         recordBuilder.setPriority((short) 0);
1034         recordBuilder.setWeight((short) 0);
1035         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
1036         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1037         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1038         mapRegisterBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1039         mapRegisterBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1040         sendMapRegister(mapRegisterBuilder.build());
1041         MapNotify mapNotify = receiveMapNotify();
1042         assertEquals(8, mapNotify.getNonce().longValue());
1043         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1044         mapRequestBuilder.setNonce((long) 4);
1045         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1046         mapRequestBuilder.getEidRecord().add(
1047                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1048         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1049         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress)))
1050                 .build());
1051         mapRequestBuilder.getItrRloc().add(
1052                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1053         mapRequestBuilder.setAuthoritative(false);
1054         mapRequestBuilder.setMapDataPresent(false);
1055         mapRequestBuilder.setPitr(false);
1056         mapRequestBuilder.setProbe(false);
1057         mapRequestBuilder.setSmr(false);
1058         mapRequestBuilder.setSmrInvoked(false);
1059         sendMapRequest(mapRequestBuilder.build());
1060         return receiveMapReply();
1061     }
1062
1063     // ------------------------------- LCAF Tests ---------------------------
1064
1065     public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1066         cleanUP();
1067         String ipString = "10.20.30.200";
1068         String macString = "01:02:03:04:05:06";
1069         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1070         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1071         LcafSourceDestBuilder builder = new LcafSourceDestBuilder();
1072         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1073         builder.setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode());
1074         builder.setSrcMaskLength((short) 0);
1075         builder.setDstMaskLength((short) 0);
1076         builder.setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(addrToSend1).build());
1077         builder.setDstAddress(new DstAddressBuilder().setPrimitiveAddress(addrToSend2).build());
1078
1079         MapReply reply = registerAddressAndQuery(builder.build());
1080
1081         LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1082         assertTrue(fromNetwork.getAddress() instanceof LcafSourceDestAddress);
1083         LcafSourceDestAddress sourceDestFromNetwork = (LcafSourceDestAddress) fromNetwork.getAddress();
1084
1085         LispAFIAddress receivedAddr1 = (LispAFIAddress) sourceDestFromNetwork.getSrcAddress().getPrimitiveAddress();
1086         LispAFIAddress receivedAddr2 = (LispAFIAddress) sourceDestFromNetwork.getDstAddress().getPrimitiveAddress();
1087
1088         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1089         assertTrue(receivedAddr2 instanceof LispMacAddress);
1090
1091         LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1092         LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1093
1094         assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1095         assertEquals(macString, receivedMAC.getMacAddress().getValue());
1096     }
1097
1098     @Test
1099     public void registerAndQuery__KeyValueLCAF() throws SocketTimeoutException {
1100         cleanUP();
1101         String ipString = "10.20.30.200";
1102         String macString = "01:02:03:04:05:06";
1103         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1104         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1105         LcafKeyValueBuilder builder = new LcafKeyValueBuilder();
1106         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1107         builder.setLcafType((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode());
1108         builder.setKey(new KeyBuilder().setPrimitiveAddress(addrToSend1).build());
1109         builder.setValue(new ValueBuilder().setPrimitiveAddress(addrToSend2).build());
1110
1111         MapReply reply = registerAddressAndQuery(builder.build());
1112
1113         LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1114         assertTrue(fromNetwork.getAddress() instanceof LcafKeyValueAddress);
1115         LcafKeyValueAddress keyValueFromNetwork = (LcafKeyValueAddress) fromNetwork.getAddress();
1116
1117         LispAFIAddress receivedAddr1 = (LispAFIAddress) keyValueFromNetwork.getKey().getPrimitiveAddress();
1118         LispAFIAddress receivedAddr2 = (LispAFIAddress) keyValueFromNetwork.getValue().getPrimitiveAddress();
1119
1120         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1121         assertTrue(receivedAddr2 instanceof LispMacAddress);
1122
1123         LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1124         LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1125
1126         assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1127         assertEquals(macString, receivedMAC.getMacAddress().getValue());
1128     }
1129
1130     public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1131         cleanUP();
1132         String macString = "01:02:03:04:05:06";
1133         String ipString = "10.20.255.30";
1134         LcafListBuilder listbuilder = new LcafListBuilder();
1135         listbuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
1136         listbuilder.setAddresses(new ArrayList<Addresses>());
1137         listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1138         listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString))).build());
1139
1140         MapReply reply = registerAddressAndQuery(listbuilder.build());
1141
1142         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1143
1144         assertTrue(receivedAddress instanceof LcafListAddress);
1145
1146         LcafListAddress listAddrFromNetwork = (LcafListAddress) receivedAddress;
1147         LispAFIAddress receivedAddr1 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(0).getPrimitiveAddress();
1148         LispAFIAddress receivedAddr2 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(1).getPrimitiveAddress();
1149
1150         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1151         assertTrue(receivedAddr2 instanceof LispMacAddress);
1152
1153         assertEquals(macString, ((LispMacAddress) receivedAddr2).getMacAddress().getValue());
1154         assertEquals(ipString, ((LispIpv4Address) receivedAddr1).getIpv4Address().getValue());
1155     }
1156
1157     public void registerAndQuery__SegmentLCAF() throws SocketTimeoutException {
1158         cleanUP();
1159         String ipString = "10.20.255.30";
1160         int instanceId = 6;
1161         LcafSegmentBuilder builder = new LcafSegmentBuilder();
1162         builder.setInstanceId((long) instanceId);
1163         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode());
1164         builder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1165
1166         MapReply reply = registerAddressAndQuery(builder.build());
1167
1168         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1169         assertTrue(receivedAddress instanceof LcafSegmentAddress);
1170
1171         LcafSegmentAddress segmentfromNetwork = (LcafSegmentAddress) receivedAddress;
1172         LispAFIAddress addrFromSegment = (LispAFIAddress) segmentfromNetwork.getAddress().getPrimitiveAddress();
1173         assertTrue(addrFromSegment instanceof LispIpv4Address);
1174         assertEquals(ipString, ((LispIpv4Address) addrFromSegment).getIpv4Address().getValue());
1175
1176         assertEquals(instanceId, segmentfromNetwork.getInstanceId().intValue());
1177     }
1178
1179     public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1180         cleanUP();
1181         String macString = "01:02:03:04:05:06";
1182         String ipString = "10.20.255.30";
1183         HopBuilder hopBuilder = new HopBuilder();
1184         hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString)));
1185         Hop hop1 = hopBuilder.build();
1186         hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString)));
1187         Hop hop2 = hopBuilder.build();
1188         HopsBuilder hb = new HopsBuilder();
1189         hb.setHop(hop1);
1190         hb.setLookup(true);
1191         hb.setRLOCProbe(false);
1192         hb.setStrict(true);
1193         HopsBuilder hb2 = new HopsBuilder();
1194         hb2.setHop(hop2);
1195         hb2.setLookup(false);
1196         hb2.setRLOCProbe(true);
1197         hb2.setStrict(false);
1198         Hops hops1 = hb.build();
1199         Hops hops2 = hb2.build();
1200         LcafTrafficEngineeringBuilder trafficBuilder = new LcafTrafficEngineeringBuilder();
1201         trafficBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
1202                 (short) LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode());
1203         trafficBuilder.setHops(new ArrayList<Hops>());
1204         trafficBuilder.getHops().add(hb.build());
1205         trafficBuilder.getHops().add(hb2.build());
1206
1207         MapReply reply = registerAddressAndQuery(trafficBuilder.build());
1208
1209         assertTrue(LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer()) instanceof LcafTrafficEngineeringAddress);
1210
1211         LcafTrafficEngineeringAddress receivedAddress = (LcafTrafficEngineeringAddress) LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0)
1212                 .getLispAddressContainer());
1213
1214         ReencapHop receivedHop1 = receivedAddress.getHops().get(0);
1215         ReencapHop receivedHop2 = receivedAddress.getHops().get(1);
1216
1217         assertEquals(true, hops1.isLookup());
1218         assertEquals(false, hops1.isRLOCProbe());
1219         assertEquals(true, hops1.isStrict());
1220
1221         assertEquals(false, hops2.isLookup());
1222         assertEquals(true, hops2.isRLOCProbe());
1223         assertEquals(false, hops2.isStrict());
1224
1225         assertTrue(receivedHop1.getHop().getPrimitiveAddress() instanceof LispIpv4Address);
1226         assertTrue(receivedHop2.getHop().getPrimitiveAddress() instanceof LispMacAddress);
1227
1228         assertEquals(ipString, ((LispIpv4Address) receivedHop1.getHop().getPrimitiveAddress()).getIpv4Address().getValue());
1229         assertEquals(macString, ((LispMacAddress) receivedHop2.getHop().getPrimitiveAddress()).getMacAddress().getValue());
1230     }
1231
1232     public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1233         cleanUP();
1234         String ipString = "1.2.3.4";
1235         short protocol = 1;
1236         int ipTOs = 2;
1237         int localPort = 3;
1238         int remotePort = 4;
1239
1240         LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
1241         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
1242         builder.setIpTos(ipTOs);
1243         builder.setProtocol(protocol);
1244         builder.setLocalPort(new PortNumber(localPort));
1245         builder.setRemotePort(new PortNumber(remotePort));
1246         builder.setAddress(new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder()
1247                 .setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1248
1249         LcafApplicationDataAddress addressToSend = builder.build();
1250
1251         MapReply reply = registerAddressAndQuery(addressToSend);
1252
1253         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1254
1255         assertTrue(receivedAddress instanceof LcafApplicationDataAddress);
1256
1257         LcafApplicationDataAddress receivedApplicationDataAddress = (LcafApplicationDataAddress) receivedAddress;
1258         assertEquals(protocol, receivedApplicationDataAddress.getProtocol().intValue());
1259         assertEquals(ipTOs, receivedApplicationDataAddress.getIpTos().intValue());
1260         assertEquals(localPort, receivedApplicationDataAddress.getLocalPort().getValue().intValue());
1261         assertEquals(remotePort, receivedApplicationDataAddress.getRemotePort().getValue().intValue());
1262
1263         LispIpv4Address ipAddressReceived = (LispIpv4Address) receivedApplicationDataAddress.getAddress().getPrimitiveAddress();
1264         assertEquals(ipString, ipAddressReceived.getIpv4Address().getValue());
1265     }
1266
1267     // ------------------- TimeOut Tests -----------
1268
1269     public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1270         cleanUP();
1271         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1272         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1273         mapRequestBuilder.setNonce((long) 4);
1274         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1275                 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1276         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1277         mapRequestBuilder.getEidRecord().add(
1278                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1279         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1280         mapRequestBuilder.getItrRloc().add(
1281                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1282         sendMapRequest(mapRequestBuilder.build());
1283         MapReply mapReply = receiveMapReply();
1284         assertEquals(4, mapReply.getNonce().longValue());
1285         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1286         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1287         mapRegisterbuilder.setWantMapNotify(true);
1288         mapRegisterbuilder.setNonce((long) 8);
1289         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1290         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1291         etlrBuilder.setMaskLength((short) 32);
1292         etlrBuilder.setRecordTtl(254);
1293         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1294         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
1295         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1296         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1297         mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1298         mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1299         sendMapRegister(mapRegisterbuilder.build());
1300         MapNotify mapNotify = receiveMapNotify();
1301         assertEquals(8, mapNotify.getNonce().longValue());
1302         sendMapRequest(mapRequestBuilder.build());
1303         mapReply = receiveMapReply();
1304         assertEquals(4, mapReply.getNonce().longValue());
1305         assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1306                 .getLispAddressContainer());
1307         causeEntryToBeCleaned();
1308         sendMapRequest(mapRequestBuilder.build());
1309         mapReply = receiveMapReply();
1310         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1311     }
1312
1313     public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1314         cleanUP();
1315         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1316         MapRequest mapRequest = createMapRequest(eid);
1317
1318         testTTLBeforeRegister(mapRequest);
1319
1320         registerForTTL(eid);
1321
1322         testTTLAfterRegister(mapRequest);
1323
1324         causeEntryToBeCleaned();
1325         testTTLAfterClean(mapRequest);
1326
1327         northboundAddKey();
1328         testTTLAfterAutherize(mapRequest);
1329
1330     }
1331
1332     private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1333         MapReply mapReply;
1334         sendMapRequest(mapRequest);
1335         mapReply = receiveMapReply();
1336         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1337     }
1338
1339     private void causeEntryToBeCleaned() {
1340         clusterService.setTimeUnit(TimeUnit.NANOSECONDS);
1341         clusterService.cleanOld();
1342     }
1343
1344     private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1345         MapReply mapReply;
1346         sendMapRequest(mapRequest);
1347         mapReply = receiveMapReply();
1348         assertEquals(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1349                 .getLispAddressContainer());
1350         assertCorrectMapReplyTTLAndAction(mapReply, 254, Action.NoAction);
1351     }
1352
1353     private void registerForTTL(LispIpv4Address eid) throws SocketTimeoutException {
1354         MapRegister mapRegister = createMapRegister(eid);
1355         sendMapRegister(mapRegister);
1356         assertMapNotifyRecieved();
1357     }
1358
1359     private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1360         MapReply mapReply;
1361         sendMapRequest(mapRequest);
1362         mapReply = receiveMapReply();
1363         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1364     }
1365
1366     private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1367         MapReply mapReply;
1368         sendMapRequest(mapRequest);
1369         mapReply = receiveMapReply();
1370         assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1371     }
1372
1373     private void assertCorrectMapReplyTTLAndAction(MapReply mapReply, int expectedTTL, Action expectedAction) {
1374         assertEquals(expectedTTL, mapReply.getEidToLocatorRecord().get(0).getRecordTtl().intValue());
1375         assertEquals(expectedAction, mapReply.getEidToLocatorRecord().get(0).getAction());
1376     }
1377
1378     private MapRegister createMapRegister(LispAFIAddress eid, LispAFIAddress rloc) {
1379         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1380         mapRegisterbuilder.setWantMapNotify(true);
1381         mapRegisterbuilder.setNonce((long) 8);
1382         mapRegisterbuilder.setKeyId((short) 0);
1383         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1384         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1385         etlrBuilder.setMaskLength((short) 24);
1386         etlrBuilder.setRecordTtl(254);
1387         etlrBuilder.setAuthoritative(false);
1388         etlrBuilder.setAction(Action.NoAction);
1389         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1390         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
1391         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1392         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1393         mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1394         mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1395         MapRegister mapRegister = mapRegisterbuilder.build();
1396         return mapRegister;
1397     }
1398
1399     private MapRegister createMapRegister(LispIpv4Address eid) {
1400         return createMapRegister(eid, asIPAfiAddress("4.3.2.1"));
1401     }
1402
1403     private MapRequest createMapRequest(LispAFIAddress eid) {
1404         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1405         mapRequestBuilder.setNonce((long) 4);
1406         mapRequestBuilder.setPitr(false);
1407         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1408                 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1409         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1410         mapRequestBuilder.getEidRecord().add(
1411                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1412         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1413         mapRequestBuilder.getItrRloc().add(
1414                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1415         MapRequest mr = mapRequestBuilder.build();
1416         return mr;
1417     }
1418
1419     public void testSimpleNonProxy() throws SocketTimeoutException, SocketException {
1420         cleanUP();
1421         String rloc = "127.0.0.3";
1422         int port = LispMessage.PORT_NUM;
1423         Ipv4 ipRloc = LispAFIConvertor.asIPAfiAddress(rloc);
1424         sendProxyMapRequest(rloc, port, ipRloc);
1425
1426     }
1427
1428     public void testNonProxyOtherPort() throws SocketTimeoutException, SocketException {
1429         cleanUP();
1430         String rloc = "127.0.0.3";
1431         int port = 4350;
1432         LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1433                 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1434                 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1435                 .setAddress(
1436                         new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1437                                 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1438         sendProxyMapRequest(rloc, port, adLcaf);
1439
1440     }
1441
1442     public void testRecievingNonProxyOnXtrPort() throws SocketTimeoutException, SocketException, Throwable {
1443         cleanUP();
1444         configLispPlugin.shouldListenOnXtrPort(true);
1445         notificationCalled = false;
1446         final String eid = "10.10.10.10";
1447         String rloc = "127.0.0.3";
1448         int port = LispMessage.XTR_PORT_NUM;
1449         LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1450                 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1451                 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1452                 .setAddress(
1453                         new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1454                                 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1455         final MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1456         ((LispMappingService) lms).registerNotificationListener(XtrRequestMapping.class, new NotificationListener<XtrRequestMapping>() {
1457
1458             @Override
1459             public void onNotification(XtrRequestMapping notification) {
1460                 assertEquals(((LispIpv4Address) mapRequest.getEidRecord().get(0).getLispAddressContainer().getAddress()).getIpv4Address().getValue(),
1461                         eid);
1462                 notificationCalled = true;
1463                 logger.warn("notification arrived");
1464             }
1465         });
1466         sendMapRequest(mapRequest, port);
1467         for (int i = 0; i < MAX_NOTIFICATION_RETRYS; i++) {
1468             if (notificationCalled) {
1469                 return;
1470             } else {
1471                 logger.warn("notification hasn't arrived, sleeping...");
1472                 Thread.sleep(500);
1473             }
1474         }
1475
1476         fail("Notification hasn't arrived");
1477
1478     }
1479
1480     private void sendProxyMapRequest(String rloc, int port, LispAFIAddress adLcaf) throws SocketTimeoutException, SocketException {
1481         String eid = "10.1.0.1";
1482         MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1483         sendMapRequest(mapRequest);
1484         DatagramSocket nonProxySocket = new DatagramSocket(new InetSocketAddress(rloc, port));
1485         MapRequest recievedMapRequest = receiveMapRequest(nonProxySocket);
1486         assertEquals(mapRequest.getNonce(), recievedMapRequest.getNonce());
1487         assertEquals(mapRequest.getSourceEid(), recievedMapRequest.getSourceEid());
1488         assertEquals(mapRequest.getItrRloc(), recievedMapRequest.getItrRloc());
1489         assertEquals(mapRequest.getEidRecord(), recievedMapRequest.getEidRecord());
1490         nonProxySocket.close();
1491     }
1492
1493     private MapRequest createNonProxyMapRequest(String eid, LispAFIAddress adLcaf) throws SocketTimeoutException {
1494         MapRegister mr = createMapRegister(LispAFIConvertor.asIPAfiAddress(eid));
1495         LocatorRecord record = new LocatorRecordBuilder(mr.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)).setLispAddressContainer(
1496                 LispAFIConvertor.toContainer(adLcaf)).build();
1497         mr.getEidToLocatorRecord().get(0).getLocatorRecord().set(0, record);
1498         sendMapRegister(mr);
1499         assertMapNotifyRecieved();
1500         MapRequest mapRequest = createMapRequest(LispAFIConvertor.asIPAfiAddress(eid));
1501         MapRequestBuilder builder = new MapRequestBuilder(mapRequest);
1502         builder.setPitr(true);
1503         mapRequest = builder.build();
1504         return mapRequest;
1505     }
1506
1507     private void assertMapNotifyRecieved() throws SocketTimeoutException {
1508         receiveMapNotify();
1509     }
1510
1511     private MapReply receiveMapReply() throws SocketTimeoutException {
1512         return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1513     }
1514
1515     private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
1516         return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData()));
1517     }
1518
1519     private MapNotify receiveMapNotify() throws SocketTimeoutException {
1520         return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1521     }
1522
1523     private void sendMapRequest(MapRequest mapRequest) {
1524         sendMapRequest(mapRequest, LispMessage.PORT_NUM);
1525     }
1526
1527     private void sendMapRequest(MapRequest mapRequest, int port) {
1528         sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array(), port);
1529     }
1530
1531     private void sendMapRegister(MapRegister mapRegister) {
1532         sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
1533     }
1534
1535     private void sendPacket(byte[] bytesToSend) {
1536         sendPacket(bytesToSend, LispMessage.PORT_NUM);
1537     }
1538
1539     private void sendPacket(byte[] bytesToSend, int port) {
1540         try {
1541             DatagramPacket packet = new DatagramPacket(bytesToSend, bytesToSend.length);
1542             initPacketAddress(packet, port);
1543             logger.trace("Sending packet to LispPlugin on socket, port {}", port);
1544             socket.send(packet);
1545         } catch (Throwable t) {
1546             fail();
1547         }
1548     }
1549
1550     private DatagramPacket receivePacket() throws SocketTimeoutException {
1551         return receivePacket(6000);
1552     }
1553
1554     private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
1555         return receivePacket(socket, timeout);
1556     }
1557
1558     private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException {
1559         try {
1560             byte[] buffer = new byte[4096];
1561             DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
1562             logger.trace("Waiting for packet from socket...");
1563             receivedSocket.setSoTimeout(timeout);
1564             receivedSocket.receive(receivePacket);
1565             logger.trace("Recieved packet from socket!");
1566             return receivePacket;
1567         } catch (SocketTimeoutException ste) {
1568             throw ste;
1569         } catch (Throwable t) {
1570             fail();
1571             return null;
1572         }
1573     }
1574
1575     private void initPacketAddress(DatagramPacket packet, int port) throws UnknownHostException {
1576         packet.setAddress(InetAddress.getByName(lispBindAddress));
1577         packet.setPort(port);
1578     }
1579
1580     private DatagramSocket initSocket(DatagramSocket socket, int port) {
1581         try {
1582             socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
1583         } catch (SocketException e) {
1584             e.printStackTrace();
1585             fail();
1586         }
1587         return socket;
1588     }
1589
1590     private byte[] extractWSUdpByteArray(String wiresharkHex) {
1591         final int HEADER_LEN = 42;
1592         byte[] res = new byte[1000];
1593         String[] split = wiresharkHex.split(" ");
1594         int counter = 0;
1595         for (String cur : split) {
1596             cur = cur.trim();
1597             if (cur.length() == 2) {
1598                 ++counter;
1599                 if (counter > HEADER_LEN) {
1600                     res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
1601                 }
1602
1603             }
1604         }
1605         return Arrays.copyOf(res, counter - HEADER_LEN);
1606     }
1607
1608     private String stateToString(int state) {
1609         switch (state) {
1610         case Bundle.ACTIVE:
1611             return "ACTIVE";
1612         case Bundle.INSTALLED:
1613             return "INSTALLED";
1614         case Bundle.RESOLVED:
1615             return "RESOLVED";
1616         case Bundle.UNINSTALLED:
1617             return "UNINSTALLED";
1618         default:
1619             return "Not CONVERTED";
1620         }
1621     }
1622
1623     @SuppressWarnings({ "rawtypes", "unchecked" })
1624     private void areWeReady() throws InvalidSyntaxException {
1625         assertNotNull(bc);
1626         boolean debugit = false;
1627         Bundle b[] = bc.getBundles();
1628         for (Bundle element : b) {
1629             int state = element.getState();
1630             logger.trace("Bundle[" + element.getBundleId() + "]:" + element.getSymbolicName() + ",v" + element.getVersion() + ", state:"
1631                     + stateToString(state));
1632             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
1633                 logger.trace("Bundle:" + element.getSymbolicName() + " state:" + stateToString(state));
1634
1635                 // try {
1636                 // String host = element.getHeaders().get("FRAGMENT-HOST");
1637                 // if (host != null) {
1638                 // logger.warn("Bundle " + element.getSymbolicName() +
1639                 // " is a fragment which is part of: " + host);
1640                 // logger.warn("Required imports are: " +
1641                 // element.getHeaders().get("IMPORT-PACKAGE"));
1642                 // } else {
1643                 // element.start();
1644                 // }
1645                 // } catch (BundleException e) {
1646                 // logger.error("BundleException:", e);
1647                 // fail();
1648                 // }
1649
1650                 debugit = true;
1651
1652             }
1653         }
1654         if (debugit) {
1655             logger.warn(("Do some debugging because some bundle is unresolved"));
1656         }
1657         // assertNotNull(broker);
1658
1659         int retry = 0;
1660         ServiceReference r = null;
1661         while (this.lms == null && retry < MAX_SERVICE_LOAD_RETRIES) {
1662
1663             r = bc.getServiceReference(IFlowMapping.class.getName());
1664             // r.getPropertyKeys();
1665             if (r != null) {
1666                 this.lms = (IFlowMapping) bc.getService(r);
1667             } else {
1668                 try {
1669                     Thread.sleep(1000);
1670                 } catch (InterruptedException e) {
1671                 }
1672             }
1673             retry += 1;
1674         }
1675
1676         assertNotNull(IFlowMapping.class.getName() + " service wasn't found in bundle context ", this.lms);
1677
1678         r = bc.getServiceReference(ILispDAO.class.getName());
1679         if (r != null) {
1680             this.clusterService = (ClusterDAOService) bc.getService(r);
1681         }
1682
1683         assertNotNull(ILispDAO.class.getName() + " service wasn't found in bundle context ", this.clusterService);
1684         r = bc.getServiceReference(IConfigLispSouthboundPlugin.class.getName());
1685         if (r != null) {
1686             this.configLispPlugin = (IConfigLispSouthboundPlugin) bc.getService(r);
1687         }
1688
1689         assertNotNull(IConfigLispSouthboundPlugin.class.getName() + " service wasn't found in bundle context ", this.configLispPlugin);
1690         configLispPlugin.setLispAddress(lispBindAddress);
1691
1692         // Uncomment this code to Know which services were actually loaded to
1693         // BundleContext
1694         /*
1695          * for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
1696          * logger.trace(sr.getBundle().getSymbolicName());
1697          * logger.trace(sr.toString()); }
1698          */
1699         try {
1700             Thread.sleep(1000);
1701         } catch (InterruptedException e) {
1702         }
1703     }
1704
1705     private Ipv4 asIPAfiAddress(String ip) {
1706         return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1707     }
1708
1709     private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 asPrimitiveIPAfiAddress(String ip) {
1710         return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4Builder()
1711                 .setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1712     }
1713
1714     private Mac asMacAfiAddress(String mac) {
1715         return new MacBuilder().setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1716     }
1717
1718     private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac asPrimitiveMacAfiAddress(String mac) {
1719         return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.MacBuilder()
1720                 .setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1721     }
1722
1723     private void cleanUP() {
1724         after();
1725         lms.clean();
1726         configLispPlugin.shouldListenOnXtrPort(false);
1727         socket = initSocket(socket, LispMessage.PORT_NUM);
1728
1729     }
1730
1731 }