Fix race condition in IT
[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(mapRegisterPacketWithNotify);
519         receiveMapNotify();
520
521         sendPacket(mapRequestPacket);
522         try {
523             Thread.sleep(1000);
524         } catch (InterruptedException e) {
525         }
526
527         mapRegisterPacketWithoutNotify[mapRegisterPacketWithoutNotify.length - 1] += 1;
528         sendPacket(mapRegisterPacketWithoutNotify);
529
530         ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
531         MapRequest smr = MapRequestSerializer.getInstance().deserialize(readBuf);
532         assertTrue(smr.isSmr());
533         LispAddressContainer smrEid = smr.getEidRecord().get(0).getLispAddressContainer();
534         assertTrue(LispAFIConvertor.toContainer(asIPAfiAddress("153.16.254.1")).equals(smrEid));
535     }
536
537     // --------------------- Northbound Tests ---------------------------
538
539     private void northboundAddKey() throws Exception {
540         cleanUP();
541         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
542         int mask = 32;
543         String pass = "asdf";
544
545         URL url = createPutURL("key");
546         String authKeyJSON = createAuthKeyJSON(pass, address, mask);
547         callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
548
549         String retrievedKey = lms.getAuthenticationKey(LispAFIConvertor.toContainer(address), mask);
550
551         // Check stored password matches the one sent
552         assertEquals(pass, retrievedKey);
553
554     }
555
556     private void northboundRetrieveSourceDestKey() throws Exception {
557         cleanUP();
558         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
559                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
560         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
561                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
562         int mask1 = 32;
563         int mask2 = 32;
564         LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
565                 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
566                 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
567                 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
568         String pass = "asdf";
569
570         lms.addAuthenticationKey(LispAFIConvertor.toContainer(sourceDestAddress), mask1, pass);
571
572         // URL url = createGetKeyIPv4URL(address1, mask1);
573         URL url = createGetKeySourceDestURL(address1.getAfi(), ((LispIpv4Address) sourceDestAddress.getSrcAddress().getPrimitiveAddress())
574                 .getIpv4Address().getValue(), sourceDestAddress.getSrcMaskLength(), ((LispIpv4Address) sourceDestAddress.getDstAddress()
575                 .getPrimitiveAddress()).getIpv4Address().getValue(), sourceDestAddress.getDstMaskLength());
576         String reply = callURL("GET", null, "application/json", null, url);
577         JSONTokener jt = new JSONTokener(reply);
578         JSONObject json = new JSONObject(jt);
579
580         // test that the password matches what was we expected.
581         assertEquals(pass, json.get("key"));
582
583     }
584
585     private void northboundRetrieveKey() throws Exception {
586         cleanUP();
587         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
588         int mask = 32;
589         String pass = "asdf";
590
591         lms.addAuthenticationKey(LispAFIConvertor.toContainer(address), mask, pass);
592
593         URL url = createGetKeyIPv4URL(address, mask);
594         String reply = callURL("GET", null, "application/json", null, url);
595         JSONTokener jt = new JSONTokener(reply);
596         JSONObject json = new JSONObject(jt);
597
598         // test that the password matches what was we expected.
599         assertEquals(pass, json.get("key"));
600
601     }
602
603     private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
604         return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
605                 + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
606     }
607
608     private void northboundAddMapping() throws Exception {
609         cleanUP();
610         String pass = "asdf";
611         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
612         int mask = 32;
613         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
614
615         // NB add mapping always checks the key
616         lms.addAuthenticationKey(LispAFIConvertor.toContainer(eid), mask, pass);
617
618         URL url = createPutURL("mapping");
619         String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
620         callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
621
622         // Retrieve the RLOC from the database
623         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
624         mapRequestBuilder.setPitr(false);
625         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
626         mapRequestBuilder.getEidRecord().add(
627                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
628         MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
629
630         LispIpv4Address retrievedRloc = (LispIpv4Address) mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer()
631                 .getAddress();
632
633         assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
634
635     }
636
637     private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
638         String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ " + "\"proxyMapReply\" : false, "
639                 + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : " + "{ " + "\"ipAddress\" : \""
640                 + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "}," + "\"mapVersion\" : 0,"
641                 + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ " + "\"multicastPriority\" : 1,"
642                 + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\"," + "\"afi\" : "
643                 + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50," + "\"rlocProbed\" : false, "
644                 + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], " + "\"recordTtl\" : 100" + "} " + "], "
645                 + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
646
647         return jsonString;
648     }
649
650     private void northboundRetrieveMapping() throws Exception {
651         cleanUP();
652         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("10.0.0.1");
653         int mask = 32;
654         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
655         // Insert mapping in the database
656         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
657         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
658         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
659         etlr.setMaskLength((short) mask);
660         etlr.setRecordTtl(254);
661         etlr.setAuthoritative(false);
662         etlr.setAction(Action.NoAction);
663         LocatorRecordBuilder record = new LocatorRecordBuilder();
664         record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
665         record.setRouted(true);
666         record.setRlocProbed(false);
667         record.setLocalLocator(false);
668         record.setPriority((short) 1);
669         record.setWeight((short) 50);
670         record.setMulticastPriority((short) 1);
671         record.setMulticastWeight((short) 1);
672         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
673         etlr.getLocatorRecord().add(record.build());
674         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
675         mapRegister.getEidToLocatorRecord().add(etlr.build());
676         lms.handleMapRegister(mapRegister.build(), false);
677
678         // Get mapping using NB interface. No IID used
679         URL url = createGetMappingIPv4URL(0, eid, mask);
680         String reply = callURL("GET", null, "application/json", null, url);
681         JSONTokener jt = new JSONTokener(reply);
682         JSONObject json = new JSONObject(jt);
683
684         // With just one locator, locators is not a JSONArray
685         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
686
687         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
688
689     }
690
691     private void northboundRetrieveSourceDestMapping() throws Exception {
692         cleanUP();
693         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address1 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
694                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.1"));
695         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 address2 = (org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4) LispAFIConvertor
696                 .toPrimitive(LispAFIConvertor.asIPAfiAddress("10.0.0.2"));
697         int mask1 = 32;
698         int mask2 = 32;
699         LcafSourceDest sourceDestAddress = new LcafSourceDestBuilder().setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
700                 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
701                 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build()).setSrcMaskLength((short) mask1)
702                 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build()).setDstMaskLength((short) mask2).build();
703         LispIpv4Address rloc = LispAFIConvertor.asIPAfiAddress("20.0.0.2");
704
705         // Insert mapping in the database
706         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
707         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
708         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(sourceDestAddress));
709         etlr.setMaskLength((short) mask1);
710         etlr.setRecordTtl(254);
711         etlr.setAuthoritative(false);
712         etlr.setAction(Action.NoAction);
713         LocatorRecordBuilder record = new LocatorRecordBuilder();
714         record.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
715         record.setRouted(true);
716         record.setRlocProbed(false);
717         record.setLocalLocator(false);
718         record.setPriority((short) 1);
719         record.setWeight((short) 50);
720         record.setMulticastPriority((short) 1);
721         record.setMulticastWeight((short) 1);
722         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
723         etlr.getLocatorRecord().add(record.build());
724         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
725         mapRegister.getEidToLocatorRecord().add(etlr.build());
726         lms.handleMapRegister(mapRegister.build(), false);
727
728         // Get mapping using NB interface. No IID used
729         URL url = createGetMappingSourceDestURL(address1.getAfi(), address1.getIpv4Address().getValue(), mask1, address2.getIpv4Address().getValue(),
730                 mask2);
731         String reply = callURL("GET", null, "application/json", null, url);
732         JSONTokener jt = new JSONTokener(reply);
733         JSONObject json = new JSONObject(jt);
734
735         // With just one locator, locators is not a JSONArray
736         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric").getString("ipAddress");
737
738         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
739
740     }
741
742     private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
743         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key", address.getAfi().shortValue(),
744                 address.getIpv4Address().getValue(), mask);
745         URL url = new URL(restUrl);
746         return url;
747     }
748
749     private URL createGetKeySourceDestURL(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", "key", afi, srcAddress, srcMask,
751                 dstAddress, dstMask);
752         URL url = new URL(restUrl);
753         return url;
754     }
755
756     private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask) throws MalformedURLException {
757         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d", "mapping", afi, srcAddress,
758                 srcMask, dstAddress, dstMask);
759         URL url = new URL(restUrl);
760         return url;
761     }
762
763     private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
764         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping", iid, address.getAfi()
765                 .shortValue(), address.getIpv4Address().getValue(), mask);
766         URL url = new URL(restUrl);
767         return url;
768     }
769
770     private URL createPutURL(String resource) throws MalformedURLException {
771
772         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
773
774         URL url = new URL(restUrl);
775         return url;
776     }
777
778     private String createAuthenticationString() {
779         String authString = "admin:admin";
780         byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
781         String authStringEnc = new String(authEncBytes);
782         return authStringEnc;
783     }
784
785     private String callURL(String method, String content, String accept, String body, URL url) throws IOException, JSONException {
786         String authStringEnc = createAuthenticationString();
787         connection = (HttpURLConnection) url.openConnection();
788         connection.setRequestMethod(method);
789         connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
790         if (content != null) {
791             connection.setRequestProperty("Content-Type", content);
792         }
793         if (accept != null) {
794             connection.setRequestProperty("Accept", accept);
795         }
796         if (body != null) {
797             // now add the request body
798             connection.setDoOutput(true);
799             OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
800             wr.write(body);
801             wr.flush();
802         }
803         connection.connect();
804
805         // getting the result, first check response code
806         Integer httpResponseCode = connection.getResponseCode();
807
808         if (httpResponseCode > 299) {
809             logger.trace("HTTP Address: " + url);
810             logger.trace("HTTP Response Code: " + httpResponseCode);
811             fail();
812         }
813
814         InputStream is = connection.getInputStream();
815         BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
816         StringBuilder sb = new StringBuilder();
817         int cp;
818         while ((cp = rd.read()) != -1) {
819             sb.append((char) cp);
820         }
821         is.close();
822         connection.disconnect();
823         return (sb.toString());
824     }
825
826     // timePeriod - in ms
827     public void assertNoPacketReceived(int timePeriod) {
828         try {
829             receivePacket(timePeriod);
830             // If didn't timeout then fail:
831             fail();
832         } catch (SocketTimeoutException ste) {
833         }
834     }
835
836     // ------------------------------- Mask Tests ---------------------------
837
838     public void eidPrefixLookupIPv4() throws SocketTimeoutException {
839         cleanUP();
840         runPrefixTest(LispAFIConvertor.asIPAfiAddress("1.2.3.4"), 16, LispAFIConvertor.asIPAfiAddress("1.2.3.2"),
841                 LispAFIConvertor.asIPAfiAddress("1.1.1.1"), (byte) 32);
842     }
843
844     public void eidPrefixLookupIPv6() throws SocketTimeoutException {
845         cleanUP();
846         runPrefixTest(LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:6:7:8"), 64, LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:1:2:3"),
847                 LispAFIConvertor.asIPv6AfiAddress("1:2:3:1:2:3:1:2"), 128);
848     }
849
850     private void runPrefixTest(LispAFIAddress registerEID, int registerdMask, LispAFIAddress matchedAddress, LispAFIAddress unMatchedAddress, int mask)
851             throws SocketTimeoutException {
852
853         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
854         mapRegister.setWantMapNotify(true);
855         mapRegister.setNonce((long) 8);
856         mapRegister.setWantMapNotify(true);
857         mapRegister.setKeyId((short) 0);
858         mapRegister.setAuthenticationData(new byte[0]);
859         mapRegister.setNonce((long) 8);
860         mapRegister.setProxyMapReply(false);
861         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
862         etlr.setRecordTtl(254);
863         etlr.setAction(Action.NoAction);
864         etlr.setAuthoritative(false);
865         etlr.setMapVersion((short) 0);
866         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(registerEID));
867         etlr.setMaskLength((short) registerdMask);
868         etlr.setRecordTtl(254);
869         LocatorRecordBuilder record = new LocatorRecordBuilder();
870         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
871         record.setLocalLocator(false);
872         record.setRlocProbed(false);
873         record.setRouted(true);
874         record.setMulticastPriority((short) 0);
875         record.setMulticastWeight((short) 0);
876         record.setPriority((short) 0);
877         record.setWeight((short) 0);
878         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
879         etlr.getLocatorRecord().add(record.build());
880         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
881         mapRegister.getEidToLocatorRecord().add(etlr.build());
882         sendMapRegister(mapRegister.build());
883         MapNotify mapNotify = receiveMapNotify();
884         assertEquals(8, mapNotify.getNonce().longValue());
885         MapRequestBuilder mapRequest = new MapRequestBuilder();
886         mapRequest.setNonce((long) 4);
887         mapRequest.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
888         mapRequest.setEidRecord(new ArrayList<EidRecord>());
889         mapRequest.setAuthoritative(false);
890         mapRequest.setMapDataPresent(false);
891         mapRequest.setPitr(false);
892         mapRequest.setProbe(false);
893         mapRequest.setSmr(false);
894         mapRequest.setSmrInvoked(false);
895         mapRequest.getEidRecord().add(
896                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(matchedAddress)).build());
897         mapRequest.setItrRloc(new ArrayList<ItrRloc>());
898         mapRequest.getItrRloc().add(
899                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress(ourAddress))).build());
900         sendMapRequest(mapRequest.build());
901         MapReply mapReply = receiveMapReply();
902         assertEquals(4, mapReply.getNonce().longValue());
903         assertEquals(record.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0).getLispAddressContainer());
904         mapRequest.setEidRecord(new ArrayList<EidRecord>());
905         mapRequest.getEidRecord().add(
906                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(LispAFIConvertor.toContainer(unMatchedAddress)).build());
907         sendMapRequest(mapRequest.build());
908         mapReply = receiveMapReply();
909         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
910     }
911
912     // This registers an IP with a MapRegister, then adds a password via the
913     // northbound REST API
914     // and checks that the password works
915     public void testPasswordExactMatch() throws Exception {
916         cleanUP();
917         String ipString = "10.0.0.1";
918         LispIpv4Address address = LispAFIConvertor.asIPAfiAddress(ipString);
919         int mask = 32;
920         String pass = "pass";
921
922         URL url = createPutURL("key");
923
924         String jsonAuthData = createAuthKeyJSON(pass, address, mask);
925
926         logger.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
927         logger.trace("Address: " + address);
928
929         byte[] expectedSha = new byte[] { (byte) 146, (byte) 234, (byte) 52, (byte) 247, (byte) 186, (byte) 232, (byte) 31, (byte) 249, (byte) 87,
930                 (byte) 73, (byte) 234, (byte) 54, (byte) 225, (byte) 160, (byte) 129, (byte) 251, (byte) 73, (byte) 53, (byte) 196, (byte) 62 };
931
932         byte[] zeros = new byte[20];
933
934         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
935
936         // build a MapRegister
937         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
938         mapRegister.setWantMapNotify(true);
939         mapRegister.setNonce((long) 8);
940         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
941         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(address));
942         etlr.setMaskLength((short) mask);
943         etlr.setRecordTtl(254);
944         LocatorRecordBuilder record = new LocatorRecordBuilder();
945         record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
946         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
947         etlr.getLocatorRecord().add(record.build());
948         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
949         mapRegister.getEidToLocatorRecord().add(etlr.build());
950
951         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
952         mapRegister.setAuthenticationData(zeros);
953
954         sendMapRegister(mapRegister.build());
955         assertNoPacketReceived(3000);
956
957         mapRegister.setAuthenticationData(expectedSha);
958
959         sendMapRegister(mapRegister.build());
960
961         assertMapNotifyRecieved();
962     }
963
964     public void testPasswordMaskMatch() throws Exception {
965         cleanUP();
966         LispIpv4Address addressInRange = LispAFIConvertor.asIPAfiAddress("10.20.30.40");
967         LispIpv4Address addressOutOfRange = LispAFIConvertor.asIPAfiAddress("20.40.30.40");
968         LispIpv4Address range = LispAFIConvertor.asIPAfiAddress("10.20.30.0");
969
970         int mask = 32;
971         String pass = "pass";
972
973         URL url = createPutURL("key");
974         String jsonAuthData = createAuthKeyJSON(pass, range, 8);
975
976         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
977         // build a MapRegister
978         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
979
980         mapRegister.setWantMapNotify(true);
981         mapRegister.setNonce((long) 8);
982         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
983         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressInRange));
984         etlr.setMaskLength((short) mask);
985         etlr.setRecordTtl(254);
986         LocatorRecordBuilder record = new LocatorRecordBuilder();
987         record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
988         record.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
989         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
990         etlr.getLocatorRecord().add(record.build());
991         mapRegister.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
992         mapRegister.getEidToLocatorRecord().add(etlr.build());
993
994         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
995         mapRegister
996                 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4, -67, -113, 104, -110, -71 });
997
998         sendMapRegister(mapRegister.build());
999
1000         assertMapNotifyRecieved();
1001
1002         etlr.setLispAddressContainer(LispAFIConvertor.toContainer(addressOutOfRange));
1003         mapRegister
1004                 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123, -71, -14, -99, 67, -23, -73 });
1005
1006         sendMapRegister(mapRegister.build());
1007         assertNoPacketReceived(3000);
1008     }
1009
1010     private MapReply registerAddressAndQuery(LispAFIAddress eid) throws SocketTimeoutException {
1011         return registerAddressAndQuery(eid, -1);
1012     }
1013
1014     // takes an address, packs it in a MapRegister, sends it, returns the
1015     // MapReply
1016     private MapReply registerAddressAndQuery(LispAFIAddress eid, int maskLength) throws SocketTimeoutException {
1017         MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
1018         mapRegisterBuilder.setWantMapNotify(true);
1019         mapRegisterBuilder.setKeyId((short) 0);
1020         mapRegisterBuilder.setAuthenticationData(new byte[0]);
1021         mapRegisterBuilder.setNonce((long) 8);
1022         mapRegisterBuilder.setProxyMapReply(false);
1023         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1024         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1025         if (maskLength != -1) {
1026             etlrBuilder.setMaskLength((short) maskLength);
1027         } else {
1028             etlrBuilder.setMaskLength((short) 0);
1029         }
1030         etlrBuilder.setRecordTtl(254);
1031         etlrBuilder.setAction(Action.NoAction);
1032         etlrBuilder.setAuthoritative(false);
1033         etlrBuilder.setMapVersion((short) 0);
1034         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1035         recordBuilder.setLocalLocator(false);
1036         recordBuilder.setRlocProbed(false);
1037         recordBuilder.setRouted(true);
1038         recordBuilder.setMulticastPriority((short) 0);
1039         recordBuilder.setMulticastWeight((short) 0);
1040         recordBuilder.setPriority((short) 0);
1041         recordBuilder.setWeight((short) 0);
1042         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(locatorEid));
1043         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1044         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1045         mapRegisterBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1046         mapRegisterBuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1047         sendMapRegister(mapRegisterBuilder.build());
1048         MapNotify mapNotify = receiveMapNotify();
1049         assertEquals(8, mapNotify.getNonce().longValue());
1050         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1051         mapRequestBuilder.setNonce((long) 4);
1052         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1053         mapRequestBuilder.getEidRecord().add(
1054                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1055         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1056         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress)))
1057                 .build());
1058         mapRequestBuilder.getItrRloc().add(
1059                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1060         mapRequestBuilder.setAuthoritative(false);
1061         mapRequestBuilder.setMapDataPresent(false);
1062         mapRequestBuilder.setPitr(false);
1063         mapRequestBuilder.setProbe(false);
1064         mapRequestBuilder.setSmr(false);
1065         mapRequestBuilder.setSmrInvoked(false);
1066         sendMapRequest(mapRequestBuilder.build());
1067         return receiveMapReply();
1068     }
1069
1070     // ------------------------------- LCAF Tests ---------------------------
1071
1072     public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1073         cleanUP();
1074         String ipString = "10.20.30.200";
1075         String macString = "01:02:03:04:05:06";
1076         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1077         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1078         LcafSourceDestBuilder builder = new LcafSourceDestBuilder();
1079         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1080         builder.setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode());
1081         builder.setSrcMaskLength((short) 0);
1082         builder.setDstMaskLength((short) 0);
1083         builder.setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(addrToSend1).build());
1084         builder.setDstAddress(new DstAddressBuilder().setPrimitiveAddress(addrToSend2).build());
1085
1086         MapReply reply = registerAddressAndQuery(builder.build());
1087
1088         LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1089         assertTrue(fromNetwork.getAddress() instanceof LcafSourceDestAddress);
1090         LcafSourceDestAddress sourceDestFromNetwork = (LcafSourceDestAddress) fromNetwork.getAddress();
1091
1092         LispAFIAddress receivedAddr1 = (LispAFIAddress) sourceDestFromNetwork.getSrcAddress().getPrimitiveAddress();
1093         LispAFIAddress receivedAddr2 = (LispAFIAddress) sourceDestFromNetwork.getDstAddress().getPrimitiveAddress();
1094
1095         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1096         assertTrue(receivedAddr2 instanceof LispMacAddress);
1097
1098         LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1099         LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1100
1101         assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1102         assertEquals(macString, receivedMAC.getMacAddress().getValue());
1103     }
1104
1105     @Test
1106     public void registerAndQuery__KeyValueLCAF() throws SocketTimeoutException {
1107         cleanUP();
1108         String ipString = "10.20.30.200";
1109         String macString = "01:02:03:04:05:06";
1110         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 addrToSend1 = asPrimitiveIPAfiAddress(ipString);
1111         org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac addrToSend2 = asPrimitiveMacAfiAddress(macString);
1112         LcafKeyValueBuilder builder = new LcafKeyValueBuilder();
1113         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode());
1114         builder.setLcafType((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode());
1115         builder.setKey(new KeyBuilder().setPrimitiveAddress(addrToSend1).build());
1116         builder.setValue(new ValueBuilder().setPrimitiveAddress(addrToSend2).build());
1117
1118         MapReply reply = registerAddressAndQuery(builder.build());
1119
1120         LispAddressContainer fromNetwork = reply.getEidToLocatorRecord().get(0).getLispAddressContainer();
1121         assertTrue(fromNetwork.getAddress() instanceof LcafKeyValueAddress);
1122         LcafKeyValueAddress keyValueFromNetwork = (LcafKeyValueAddress) fromNetwork.getAddress();
1123
1124         LispAFIAddress receivedAddr1 = (LispAFIAddress) keyValueFromNetwork.getKey().getPrimitiveAddress();
1125         LispAFIAddress receivedAddr2 = (LispAFIAddress) keyValueFromNetwork.getValue().getPrimitiveAddress();
1126
1127         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1128         assertTrue(receivedAddr2 instanceof LispMacAddress);
1129
1130         LispIpv4Address receivedIP = (LispIpv4Address) receivedAddr1;
1131         LispMacAddress receivedMAC = (LispMacAddress) receivedAddr2;
1132
1133         assertEquals(ipString, receivedIP.getIpv4Address().getValue());
1134         assertEquals(macString, receivedMAC.getMacAddress().getValue());
1135     }
1136
1137     public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1138         cleanUP();
1139         String macString = "01:02:03:04:05:06";
1140         String ipString = "10.20.255.30";
1141         LcafListBuilder listbuilder = new LcafListBuilder();
1142         listbuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.LIST.getLispCode());
1143         listbuilder.setAddresses(new ArrayList<Addresses>());
1144         listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1145         listbuilder.getAddresses().add(new AddressesBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString))).build());
1146
1147         MapReply reply = registerAddressAndQuery(listbuilder.build());
1148
1149         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1150
1151         assertTrue(receivedAddress instanceof LcafListAddress);
1152
1153         LcafListAddress listAddrFromNetwork = (LcafListAddress) receivedAddress;
1154         LispAFIAddress receivedAddr1 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(0).getPrimitiveAddress();
1155         LispAFIAddress receivedAddr2 = (LispAFIAddress) listAddrFromNetwork.getAddresses().get(1).getPrimitiveAddress();
1156
1157         assertTrue(receivedAddr1 instanceof LispIpv4Address);
1158         assertTrue(receivedAddr2 instanceof LispMacAddress);
1159
1160         assertEquals(macString, ((LispMacAddress) receivedAddr2).getMacAddress().getValue());
1161         assertEquals(ipString, ((LispIpv4Address) receivedAddr1).getIpv4Address().getValue());
1162     }
1163
1164     public void registerAndQuery__SegmentLCAF() throws SocketTimeoutException {
1165         cleanUP();
1166         String ipString = "10.20.255.30";
1167         int instanceId = 6;
1168         LcafSegmentBuilder builder = new LcafSegmentBuilder();
1169         builder.setInstanceId((long) instanceId);
1170         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode());
1171         builder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1172
1173         MapReply reply = registerAddressAndQuery(builder.build());
1174
1175         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1176         assertTrue(receivedAddress instanceof LcafSegmentAddress);
1177
1178         LcafSegmentAddress segmentfromNetwork = (LcafSegmentAddress) receivedAddress;
1179         LispAFIAddress addrFromSegment = (LispAFIAddress) segmentfromNetwork.getAddress().getPrimitiveAddress();
1180         assertTrue(addrFromSegment instanceof LispIpv4Address);
1181         assertEquals(ipString, ((LispIpv4Address) addrFromSegment).getIpv4Address().getValue());
1182
1183         assertEquals(instanceId, segmentfromNetwork.getInstanceId().intValue());
1184     }
1185
1186     public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1187         cleanUP();
1188         String macString = "01:02:03:04:05:06";
1189         String ipString = "10.20.255.30";
1190         HopBuilder hopBuilder = new HopBuilder();
1191         hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString)));
1192         Hop hop1 = hopBuilder.build();
1193         hopBuilder.setPrimitiveAddress(LispAFIConvertor.toPrimitive(asMacAfiAddress(macString)));
1194         Hop hop2 = hopBuilder.build();
1195         HopsBuilder hb = new HopsBuilder();
1196         hb.setHop(hop1);
1197         hb.setLookup(true);
1198         hb.setRLOCProbe(false);
1199         hb.setStrict(true);
1200         HopsBuilder hb2 = new HopsBuilder();
1201         hb2.setHop(hop2);
1202         hb2.setLookup(false);
1203         hb2.setRLOCProbe(true);
1204         hb2.setStrict(false);
1205         Hops hops1 = hb.build();
1206         Hops hops2 = hb2.build();
1207         LcafTrafficEngineeringBuilder trafficBuilder = new LcafTrafficEngineeringBuilder();
1208         trafficBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
1209                 (short) LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode());
1210         trafficBuilder.setHops(new ArrayList<Hops>());
1211         trafficBuilder.getHops().add(hb.build());
1212         trafficBuilder.getHops().add(hb2.build());
1213
1214         MapReply reply = registerAddressAndQuery(trafficBuilder.build());
1215
1216         assertTrue(LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer()) instanceof LcafTrafficEngineeringAddress);
1217
1218         LcafTrafficEngineeringAddress receivedAddress = (LcafTrafficEngineeringAddress) LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0)
1219                 .getLispAddressContainer());
1220
1221         ReencapHop receivedHop1 = receivedAddress.getHops().get(0);
1222         ReencapHop receivedHop2 = receivedAddress.getHops().get(1);
1223
1224         assertEquals(true, hops1.isLookup());
1225         assertEquals(false, hops1.isRLOCProbe());
1226         assertEquals(true, hops1.isStrict());
1227
1228         assertEquals(false, hops2.isLookup());
1229         assertEquals(true, hops2.isRLOCProbe());
1230         assertEquals(false, hops2.isStrict());
1231
1232         assertTrue(receivedHop1.getHop().getPrimitiveAddress() instanceof LispIpv4Address);
1233         assertTrue(receivedHop2.getHop().getPrimitiveAddress() instanceof LispMacAddress);
1234
1235         assertEquals(ipString, ((LispIpv4Address) receivedHop1.getHop().getPrimitiveAddress()).getIpv4Address().getValue());
1236         assertEquals(macString, ((LispMacAddress) receivedHop2.getHop().getPrimitiveAddress()).getMacAddress().getValue());
1237     }
1238
1239     public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1240         cleanUP();
1241         String ipString = "1.2.3.4";
1242         short protocol = 1;
1243         int ipTOs = 2;
1244         int localPort = 3;
1245         int remotePort = 4;
1246
1247         LcafApplicationDataBuilder builder = new LcafApplicationDataBuilder();
1248         builder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
1249         builder.setIpTos(ipTOs);
1250         builder.setProtocol(protocol);
1251         builder.setLocalPort(new PortNumber(localPort));
1252         builder.setRemotePort(new PortNumber(remotePort));
1253         builder.setAddress(new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder()
1254                 .setPrimitiveAddress(LispAFIConvertor.toPrimitive(asIPAfiAddress(ipString))).build());
1255
1256         LcafApplicationDataAddress addressToSend = builder.build();
1257
1258         MapReply reply = registerAddressAndQuery(addressToSend);
1259
1260         LispAFIAddress receivedAddress = LispAFIConvertor.toAFI(reply.getEidToLocatorRecord().get(0).getLispAddressContainer());
1261
1262         assertTrue(receivedAddress instanceof LcafApplicationDataAddress);
1263
1264         LcafApplicationDataAddress receivedApplicationDataAddress = (LcafApplicationDataAddress) receivedAddress;
1265         assertEquals(protocol, receivedApplicationDataAddress.getProtocol().intValue());
1266         assertEquals(ipTOs, receivedApplicationDataAddress.getIpTos().intValue());
1267         assertEquals(localPort, receivedApplicationDataAddress.getLocalPort().getValue().intValue());
1268         assertEquals(remotePort, receivedApplicationDataAddress.getRemotePort().getValue().intValue());
1269
1270         LispIpv4Address ipAddressReceived = (LispIpv4Address) receivedApplicationDataAddress.getAddress().getPrimitiveAddress();
1271         assertEquals(ipString, ipAddressReceived.getIpv4Address().getValue());
1272     }
1273
1274     // ------------------- TimeOut Tests -----------
1275
1276     public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1277         cleanUP();
1278         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1279         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1280         mapRequestBuilder.setNonce((long) 4);
1281         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1282                 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1283         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1284         mapRequestBuilder.getEidRecord().add(
1285                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1286         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1287         mapRequestBuilder.getItrRloc().add(
1288                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1289         sendMapRequest(mapRequestBuilder.build());
1290         MapReply mapReply = receiveMapReply();
1291         assertEquals(4, mapReply.getNonce().longValue());
1292         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1293         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1294         mapRegisterbuilder.setWantMapNotify(true);
1295         mapRegisterbuilder.setNonce((long) 8);
1296         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1297         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1298         etlrBuilder.setMaskLength((short) 32);
1299         etlrBuilder.setRecordTtl(254);
1300         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1301         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")));
1302         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1303         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1304         mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1305         mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1306         sendMapRegister(mapRegisterbuilder.build());
1307         MapNotify mapNotify = receiveMapNotify();
1308         assertEquals(8, mapNotify.getNonce().longValue());
1309         sendMapRequest(mapRequestBuilder.build());
1310         mapReply = receiveMapReply();
1311         assertEquals(4, mapReply.getNonce().longValue());
1312         assertEquals(recordBuilder.getLispAddressContainer(), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1313                 .getLispAddressContainer());
1314         causeEntryToBeCleaned();
1315         sendMapRequest(mapRequestBuilder.build());
1316         mapReply = receiveMapReply();
1317         assertEquals(0, mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().size());
1318     }
1319
1320     public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1321         cleanUP();
1322         LispIpv4Address eid = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
1323         MapRequest mapRequest = createMapRequest(eid);
1324
1325         testTTLBeforeRegister(mapRequest);
1326
1327         registerForTTL(eid);
1328
1329         testTTLAfterRegister(mapRequest);
1330
1331         causeEntryToBeCleaned();
1332         testTTLAfterClean(mapRequest);
1333
1334         northboundAddKey();
1335         testTTLAfterAutherize(mapRequest);
1336
1337     }
1338
1339     private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1340         MapReply mapReply;
1341         sendMapRequest(mapRequest);
1342         mapReply = receiveMapReply();
1343         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1344     }
1345
1346     private void causeEntryToBeCleaned() {
1347         clusterService.setTimeUnit(TimeUnit.NANOSECONDS);
1348         clusterService.cleanOld();
1349     }
1350
1351     private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1352         MapReply mapReply;
1353         sendMapRequest(mapRequest);
1354         mapReply = receiveMapReply();
1355         assertEquals(LispAFIConvertor.toContainer(asIPAfiAddress("4.3.2.1")), mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)
1356                 .getLispAddressContainer());
1357         assertCorrectMapReplyTTLAndAction(mapReply, 254, Action.NoAction);
1358     }
1359
1360     private void registerForTTL(LispIpv4Address eid) throws SocketTimeoutException {
1361         MapRegister mapRegister = createMapRegister(eid);
1362         sendMapRegister(mapRegister);
1363         assertMapNotifyRecieved();
1364     }
1365
1366     private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1367         MapReply mapReply;
1368         sendMapRequest(mapRequest);
1369         mapReply = receiveMapReply();
1370         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1371     }
1372
1373     private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1374         MapReply mapReply;
1375         sendMapRequest(mapRequest);
1376         mapReply = receiveMapReply();
1377         assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1378     }
1379
1380     private void assertCorrectMapReplyTTLAndAction(MapReply mapReply, int expectedTTL, Action expectedAction) {
1381         assertEquals(expectedTTL, mapReply.getEidToLocatorRecord().get(0).getRecordTtl().intValue());
1382         assertEquals(expectedAction, mapReply.getEidToLocatorRecord().get(0).getAction());
1383     }
1384
1385     private MapRegister createMapRegister(LispAFIAddress eid, LispAFIAddress rloc) {
1386         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1387         mapRegisterbuilder.setWantMapNotify(true);
1388         mapRegisterbuilder.setNonce((long) 8);
1389         mapRegisterbuilder.setKeyId((short) 0);
1390         EidToLocatorRecordBuilder etlrBuilder = new EidToLocatorRecordBuilder();
1391         etlrBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(eid));
1392         etlrBuilder.setMaskLength((short) 24);
1393         etlrBuilder.setRecordTtl(254);
1394         etlrBuilder.setAuthoritative(false);
1395         etlrBuilder.setAction(Action.NoAction);
1396         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1397         recordBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(rloc));
1398         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1399         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1400         mapRegisterbuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
1401         mapRegisterbuilder.getEidToLocatorRecord().add(etlrBuilder.build());
1402         MapRegister mapRegister = mapRegisterbuilder.build();
1403         return mapRegister;
1404     }
1405
1406     private MapRegister createMapRegister(LispIpv4Address eid) {
1407         return createMapRegister(eid, asIPAfiAddress("4.3.2.1"));
1408     }
1409
1410     private MapRequest createMapRequest(LispAFIAddress eid) {
1411         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1412         mapRequestBuilder.setNonce((long) 4);
1413         mapRequestBuilder.setPitr(false);
1414         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
1415                 LispAFIConvertor.toContainer(new NoBuilder().setAfi((short) 0).build())).build());
1416         mapRequestBuilder.setEidRecord(new ArrayList<EidRecord>());
1417         mapRequestBuilder.getEidRecord().add(
1418                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(eid)).build());
1419         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1420         mapRequestBuilder.getItrRloc().add(
1421                 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(asIPAfiAddress(ourAddress))).build());
1422         MapRequest mr = mapRequestBuilder.build();
1423         return mr;
1424     }
1425
1426     public void testSimpleNonProxy() throws SocketTimeoutException, SocketException {
1427         cleanUP();
1428         String rloc = "127.0.0.3";
1429         int port = LispMessage.PORT_NUM;
1430         Ipv4 ipRloc = LispAFIConvertor.asIPAfiAddress(rloc);
1431         sendProxyMapRequest(rloc, port, ipRloc);
1432
1433     }
1434
1435     public void testNonProxyOtherPort() throws SocketTimeoutException, SocketException {
1436         cleanUP();
1437         String rloc = "127.0.0.3";
1438         int port = 4350;
1439         LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1440                 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1441                 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1442                 .setAddress(
1443                         new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1444                                 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1445         sendProxyMapRequest(rloc, port, adLcaf);
1446
1447     }
1448
1449     public void testRecievingNonProxyOnXtrPort() throws SocketTimeoutException, SocketException, Throwable {
1450         cleanUP();
1451         configLispPlugin.shouldListenOnXtrPort(true);
1452         notificationCalled = false;
1453         final String eid = "10.10.10.10";
1454         String rloc = "127.0.0.3";
1455         int port = LispMessage.XTR_PORT_NUM;
1456         LcafApplicationData adLcaf = new LcafApplicationDataBuilder()
1457                 .setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode())
1458                 .setLcafType((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode())
1459                 .setAddress(
1460                         new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder().setPrimitiveAddress(
1461                                 LispAFIConvertor.asPrimitiveIPAfiAddress(rloc)).build()).setLocalPort(new PortNumber(port)).build();
1462         final MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1463         ((LispMappingService) lms).registerNotificationListener(XtrRequestMapping.class, new NotificationListener<XtrRequestMapping>() {
1464
1465             @Override
1466             public void onNotification(XtrRequestMapping notification) {
1467                 assertEquals(((LispIpv4Address) mapRequest.getEidRecord().get(0).getLispAddressContainer().getAddress()).getIpv4Address().getValue(),
1468                         eid);
1469                 notificationCalled = true;
1470                 logger.warn("notification arrived");
1471             }
1472         });
1473         sendMapRequest(mapRequest, port);
1474         for (int i = 0; i < MAX_NOTIFICATION_RETRYS; i++) {
1475             if (notificationCalled) {
1476                 return;
1477             } else {
1478                 logger.warn("notification hasn't arrived, sleeping...");
1479                 Thread.sleep(500);
1480             }
1481         }
1482
1483         fail("Notification hasn't arrived");
1484
1485     }
1486
1487     private void sendProxyMapRequest(String rloc, int port, LispAFIAddress adLcaf) throws SocketTimeoutException, SocketException {
1488         String eid = "10.1.0.1";
1489         MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
1490         sendMapRequest(mapRequest);
1491         DatagramSocket nonProxySocket = new DatagramSocket(new InetSocketAddress(rloc, port));
1492         MapRequest recievedMapRequest = receiveMapRequest(nonProxySocket);
1493         assertEquals(mapRequest.getNonce(), recievedMapRequest.getNonce());
1494         assertEquals(mapRequest.getSourceEid(), recievedMapRequest.getSourceEid());
1495         assertEquals(mapRequest.getItrRloc(), recievedMapRequest.getItrRloc());
1496         assertEquals(mapRequest.getEidRecord(), recievedMapRequest.getEidRecord());
1497         nonProxySocket.close();
1498     }
1499
1500     private MapRequest createNonProxyMapRequest(String eid, LispAFIAddress adLcaf) throws SocketTimeoutException {
1501         MapRegister mr = createMapRegister(LispAFIConvertor.asIPAfiAddress(eid));
1502         LocatorRecord record = new LocatorRecordBuilder(mr.getEidToLocatorRecord().get(0).getLocatorRecord().get(0)).setLispAddressContainer(
1503                 LispAFIConvertor.toContainer(adLcaf)).build();
1504         mr.getEidToLocatorRecord().get(0).getLocatorRecord().set(0, record);
1505         sendMapRegister(mr);
1506         assertMapNotifyRecieved();
1507         MapRequest mapRequest = createMapRequest(LispAFIConvertor.asIPAfiAddress(eid));
1508         MapRequestBuilder builder = new MapRequestBuilder(mapRequest);
1509         builder.setPitr(true);
1510         mapRequest = builder.build();
1511         return mapRequest;
1512     }
1513
1514     private void assertMapNotifyRecieved() throws SocketTimeoutException {
1515         receiveMapNotify();
1516     }
1517
1518     private MapReply receiveMapReply() throws SocketTimeoutException {
1519         return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1520     }
1521
1522     private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
1523         return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData()));
1524     }
1525
1526     private MapNotify receiveMapNotify() throws SocketTimeoutException {
1527         return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
1528     }
1529
1530     private void sendMapRequest(MapRequest mapRequest) {
1531         sendMapRequest(mapRequest, LispMessage.PORT_NUM);
1532     }
1533
1534     private void sendMapRequest(MapRequest mapRequest, int port) {
1535         sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array(), port);
1536     }
1537
1538     private void sendMapRegister(MapRegister mapRegister) {
1539         sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
1540     }
1541
1542     private void sendPacket(byte[] bytesToSend) {
1543         sendPacket(bytesToSend, LispMessage.PORT_NUM);
1544     }
1545
1546     private void sendPacket(byte[] bytesToSend, int port) {
1547         try {
1548             DatagramPacket packet = new DatagramPacket(bytesToSend, bytesToSend.length);
1549             initPacketAddress(packet, port);
1550             logger.trace("Sending packet to LispPlugin on socket, port {}", port);
1551             socket.send(packet);
1552         } catch (Throwable t) {
1553             fail();
1554         }
1555     }
1556
1557     private DatagramPacket receivePacket() throws SocketTimeoutException {
1558         return receivePacket(6000);
1559     }
1560
1561     private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
1562         return receivePacket(socket, timeout);
1563     }
1564
1565     private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException {
1566         try {
1567             byte[] buffer = new byte[4096];
1568             DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
1569             logger.trace("Waiting for packet from socket...");
1570             receivedSocket.setSoTimeout(timeout);
1571             receivedSocket.receive(receivePacket);
1572             logger.trace("Recieved packet from socket!");
1573             return receivePacket;
1574         } catch (SocketTimeoutException ste) {
1575             throw ste;
1576         } catch (Throwable t) {
1577             fail();
1578             return null;
1579         }
1580     }
1581
1582     private void initPacketAddress(DatagramPacket packet, int port) throws UnknownHostException {
1583         packet.setAddress(InetAddress.getByName(lispBindAddress));
1584         packet.setPort(port);
1585     }
1586
1587     private DatagramSocket initSocket(DatagramSocket socket, int port) {
1588         try {
1589             socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
1590         } catch (SocketException e) {
1591             e.printStackTrace();
1592             fail();
1593         }
1594         return socket;
1595     }
1596
1597     private byte[] extractWSUdpByteArray(String wiresharkHex) {
1598         final int HEADER_LEN = 42;
1599         byte[] res = new byte[1000];
1600         String[] split = wiresharkHex.split(" ");
1601         int counter = 0;
1602         for (String cur : split) {
1603             cur = cur.trim();
1604             if (cur.length() == 2) {
1605                 ++counter;
1606                 if (counter > HEADER_LEN) {
1607                     res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
1608                 }
1609
1610             }
1611         }
1612         return Arrays.copyOf(res, counter - HEADER_LEN);
1613     }
1614
1615     private String stateToString(int state) {
1616         switch (state) {
1617         case Bundle.ACTIVE:
1618             return "ACTIVE";
1619         case Bundle.INSTALLED:
1620             return "INSTALLED";
1621         case Bundle.RESOLVED:
1622             return "RESOLVED";
1623         case Bundle.UNINSTALLED:
1624             return "UNINSTALLED";
1625         default:
1626             return "Not CONVERTED";
1627         }
1628     }
1629
1630     @SuppressWarnings({ "rawtypes", "unchecked" })
1631     private void areWeReady() throws InvalidSyntaxException {
1632         assertNotNull(bc);
1633         boolean debugit = false;
1634         Bundle b[] = bc.getBundles();
1635         for (Bundle element : b) {
1636             int state = element.getState();
1637             logger.trace("Bundle[" + element.getBundleId() + "]:" + element.getSymbolicName() + ",v" + element.getVersion() + ", state:"
1638                     + stateToString(state));
1639             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
1640                 logger.trace("Bundle:" + element.getSymbolicName() + " state:" + stateToString(state));
1641
1642                 // try {
1643                 // String host = element.getHeaders().get("FRAGMENT-HOST");
1644                 // if (host != null) {
1645                 // logger.warn("Bundle " + element.getSymbolicName() +
1646                 // " is a fragment which is part of: " + host);
1647                 // logger.warn("Required imports are: " +
1648                 // element.getHeaders().get("IMPORT-PACKAGE"));
1649                 // } else {
1650                 // element.start();
1651                 // }
1652                 // } catch (BundleException e) {
1653                 // logger.error("BundleException:", e);
1654                 // fail();
1655                 // }
1656
1657                 debugit = true;
1658
1659             }
1660         }
1661         if (debugit) {
1662             logger.warn(("Do some debugging because some bundle is unresolved"));
1663         }
1664         // assertNotNull(broker);
1665
1666         int retry = 0;
1667         ServiceReference r = null;
1668         while (this.lms == null && retry < MAX_SERVICE_LOAD_RETRIES) {
1669
1670             r = bc.getServiceReference(IFlowMapping.class.getName());
1671             // r.getPropertyKeys();
1672             if (r != null) {
1673                 this.lms = (IFlowMapping) bc.getService(r);
1674             } else {
1675                 try {
1676                     Thread.sleep(1000);
1677                 } catch (InterruptedException e) {
1678                 }
1679             }
1680             retry += 1;
1681         }
1682
1683         assertNotNull(IFlowMapping.class.getName() + " service wasn't found in bundle context ", this.lms);
1684
1685         r = bc.getServiceReference(ILispDAO.class.getName());
1686         if (r != null) {
1687             this.clusterService = (ClusterDAOService) bc.getService(r);
1688         }
1689
1690         assertNotNull(ILispDAO.class.getName() + " service wasn't found in bundle context ", this.clusterService);
1691         r = bc.getServiceReference(IConfigLispSouthboundPlugin.class.getName());
1692         if (r != null) {
1693             this.configLispPlugin = (IConfigLispSouthboundPlugin) bc.getService(r);
1694         }
1695
1696         assertNotNull(IConfigLispSouthboundPlugin.class.getName() + " service wasn't found in bundle context ", this.configLispPlugin);
1697         configLispPlugin.setLispAddress(lispBindAddress);
1698
1699         // Uncomment this code to Know which services were actually loaded to
1700         // BundleContext
1701         /*
1702          * for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
1703          * logger.trace(sr.getBundle().getSymbolicName());
1704          * logger.trace(sr.toString()); }
1705          */
1706         try {
1707             Thread.sleep(1000);
1708         } catch (InterruptedException e) {
1709         }
1710     }
1711
1712     private Ipv4 asIPAfiAddress(String ip) {
1713         return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1714     }
1715
1716     private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4 asPrimitiveIPAfiAddress(String ip) {
1717         return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Ipv4Builder()
1718                 .setIpv4Address(new Ipv4Address(ip)).setAfi((short) AddressFamilyNumberEnum.IP.getIanaCode()).build();
1719     }
1720
1721     private Mac asMacAfiAddress(String mac) {
1722         return new MacBuilder().setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1723     }
1724
1725     private org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.Mac asPrimitiveMacAfiAddress(String mac) {
1726         return new org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.MacBuilder()
1727                 .setMacAddress(new MacAddress(mac)).setAfi((short) AddressFamilyNumberEnum.MAC.getIanaCode()).build();
1728     }
1729
1730     private void cleanUP() {
1731         after();
1732         lms.clean();
1733         configLispPlugin.shouldListenOnXtrPort(false);
1734         socket = initSocket(socket, LispMessage.PORT_NUM);
1735
1736     }
1737
1738 }