Remove maskLength from MappingRecord
[lispflowmapping.git] / 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.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_A;
15 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_A_SB;
16 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_B;
17 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_B_SB;
18 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_C;
19 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_C_RLOC_10;
20 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_C_SB;
21 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_C_WP_100_1_SB;
22 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_C_WP_50_2_SB;
23 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_D4;
24 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_D5;
25 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_D_DELETE_SB;
26 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_D_WP_100_1_SB;
27 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_D_WP_50_2_SB;
28 import static org.opendaylight.lispflowmapping.integrationtest.MultiSiteScenarioUtil.SITE_E_SB;
29 import static org.ops4j.pax.exam.CoreOptions.composite;
30 import static org.ops4j.pax.exam.CoreOptions.maven;
31 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
32
33 import java.io.IOException;
34 import java.net.DatagramPacket;
35 import java.net.DatagramSocket;
36 import java.net.InetAddress;
37 import java.net.InetSocketAddress;
38 import java.net.SocketException;
39 import java.net.SocketTimeoutException;
40 import java.net.UnknownHostException;
41 import java.nio.ByteBuffer;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
45
46 import javax.inject.Inject;
47
48 //import org.codehaus.jettison.json.JSONException;
49 //import org.codehaus.jettison.json.JSONObject;
50 //import org.codehaus.jettison.json.JSONTokener;
51 import org.junit.After;
52 import org.junit.Assert;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
57 import org.opendaylight.lispflowmapping.implementation.LispMappingService;
58 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
59 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
60 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
61 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
62 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
63 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
64 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
65 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
66 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
67 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregisternotification.MapRegisterBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder;
98 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
99 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
100 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
101 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
102 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
103 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ApplicationDataLcaf;
104 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
105 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
106 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
107 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.KeyValueAddressLcaf;
108 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.MacAfi;
109 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
110 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
111 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.AfiList;
112 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ApplicationData;
113 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
114 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Prefix;
115 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
116 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
117 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Mac;
118 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
119 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.afi.list.AfiListBuilder;
120 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationDataBuilder;
121 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.ExplicitLocatorPathBuilder;
122 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
123 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop.LrsBits;
124 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
125 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
126 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
127 import org.ops4j.pax.exam.Option;
128 import org.ops4j.pax.exam.junit.PaxExam;
129 import org.ops4j.pax.exam.util.Filter;
130 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
131 import org.ops4j.pax.exam.options.MavenUrlReference;
132 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
133 import org.ops4j.pax.exam.spi.reactors.PerClass;
134 import org.osgi.framework.Bundle;
135 import org.osgi.framework.BundleContext;
136 import org.osgi.framework.InvalidSyntaxException;
137 import org.slf4j.Logger;
138 import org.slf4j.LoggerFactory;
139
140 @RunWith(PaxExam.class)
141 @ExamReactorStrategy(PerClass.class)
142 public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
143     private static final Logger LOG = LoggerFactory.getLogger(MappingServiceIntegrationTest.class);
144
145     private byte[] mapRequestPacket;
146     private byte[] mapRegisterPacketWithNotify;
147     private byte[] mapRegisterPacketWithoutNotify;
148     String lispBindAddress = "127.0.0.1";
149     static final String ourAddress = "127.0.0.2";
150     private Rloc locatorEid;
151     private DatagramSocket socket;
152     private byte[] mapRegisterPacketWithAuthenticationAndMapNotify;
153
154     public static final String ODL = "org.opendaylight.controller";
155     public static final String YANG = "org.opendaylight.yangtools";
156     public static final String JERSEY = "com.sun.jersey";
157     private static final int MAX_NOTIFICATION_RETRYS = 20;
158     private static final MappingAuthkey NULL_AUTH_KEY = new MappingAuthkeyBuilder().setKeyType(0).build();
159     private static final MappingAuthkey AUTH_KEY =
160             new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build();
161
162     @Override
163     public String getModuleName() {
164         return "mappingservice-impl";
165     }
166
167     @Override
168     public String getInstanceName() {
169         return "mappingservice-default";
170     }
171
172     // This is temporary, since the properties in the pom file are not picked up
173     @Override
174     public String getKarafDistro() {
175         return maven()
176                 .groupId("org.opendaylight.lispflowmapping")
177                 .artifactId("distribution-karaf")
178                 .versionAsInProject()
179                 .type("zip")
180                 .getURL();
181     }
182
183     @Override
184     public MavenUrlReference getFeatureRepo() {
185         return maven()
186                 .groupId("org.opendaylight.lispflowmapping")
187                 .artifactId("features-lispflowmapping")
188                 .classifier("features")
189                 .type("xml")
190                 .versionAsInProject();
191     }
192
193     @Override
194     public String getFeatureName() {
195         return "odl-lispflowmapping-msmr";
196     }
197
198     @Override
199     public Option getLoggingOption() {
200         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
201                 "log4j.logger.org.opendaylight.lispflowmapping",
202                 LogLevel.DEBUG.name());
203         option = composite(option, super.getLoggingOption());
204         return option;
205     }
206
207     @Test
208     public void testLispFlowMappingFeatureLoad() {
209         Assert.assertTrue(true);
210     }
211
212     @After
213     public void after() {
214         if (socket != null) {
215             socket.close();
216         }
217 //        if (connection != null) {
218 //            connection.disconnect();
219 //        }
220     }
221
222     @Before
223     public void before() throws Exception {
224         areWeReady();
225         mapService.setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
226         mapService.setMappingOverwrite(true);
227
228         locatorEid = LispAddressUtil.asIpv4Rloc("4.3.2.1");
229         socket = initSocket(socket, LispMessage.PORT_NUM);
230
231         // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
232         // LISP(Type = 8 - Encapsulated)
233         // IP: 192.168.136.10 -> 153.16.254.1
234         // UDP: 56756
235         // LISP(Type = 1 Map-Request
236         // Record Count: 1
237         // ITR-RLOC count: 0
238         // Source EID AFI: 1
239         // Source EID 1.2.3.4
240         // Nonce: 0x3d8d2acd39c8d608
241         // ITR-RLOC AFI=1 Address=192.168.136.10
242         // Record 1: 153.16.254.1/32
243         mapRequestPacket = extractWSUdpByteArray(new String("0000   00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
244                 + "0010   00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
245                 + "0020   00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
246                 + "0030   00 3c d4 31 00 00 ff 11 56 f3 7f 00 00 02 99 10 "
247                 + "0040   fe 01 dd b4 10 f6 00 28 ef 3a 10 00 00 01 3d 8d "
248                 + "0050   2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 7f 00 00 02 00 20 " //
249                 + "0060   00 01 99 10 fe 01"));
250
251         // IP: 192.168.136.10 -> 128.223.156.35
252         // UDP: 49289 -> 4342
253         // LISP(Type = 3 Map-Register, P=1, M=1
254         // Record Counter: 1
255         // Nonce: 0
256         // Key ID: 0x0001
257         // AuthDataLength: 20 Data:
258         // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
259         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
260         // No-Action
261         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
262         // Priority/Weight: 1/100, Multicast Priority/Weight:
263         // 255/0
264         //
265
266         mapRegisterPacketWithAuthenticationAndMapNotify = extractWSUdpByteArray(new String(
267                   "0000   00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
268                 + "0010   00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
269                 + "0020   9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
270                 + "0030   00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
271                 + "0040   71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
272                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
273                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
274
275         // IP: 192.168.136.10 -> 128.223.156.35
276         // UDP: 49289 -> 4342
277         // LISP(Type = 3 Map-Register, P=1, M=1
278         // Record Counter: 1
279         // Nonce: 7
280         // Key ID: 0x0000 NO AUTHENTICATION!!
281         // AuthDataLength: 00 Data:
282         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
283         // No-Action
284         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
285         // Priority/Weight: 1/100, Multicast Priority/Weight:
286         // 255/0
287         //
288
289         mapRegisterPacketWithNotify = extractWSUdpByteArray(new String(
290                   "0000   00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
291                 + "0010   00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
292                 + "0020   9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
293                 + "0030   00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
294                 + "0040   71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
295                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
296                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
297
298         // IP: 192.168.136.10 -> 128.223.156.35
299         // UDP: 49289 -> 4342
300         // LISP(Type = 3 Map-Register, P=1, M=1
301         // Record Counter: 1
302         // Nonce: 7
303         // Key ID: 0x0000 NO AUTHENTICATION!!
304         // AuthDataLength: 00 Data:
305         // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
306         // No-Action
307         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
308         // Priority/Weight: 1/100, Multicast Priority/Weight:
309         // 255/0
310         //
311
312         mapRegisterPacketWithoutNotify = extractWSUdpByteArray(new String(
313                   "0000   00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
314                 + "0010   00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
315                 + "0020   9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
316                 + "0030   00 00 00 00 00 07 00 00 00 14 0e a4 c6 d8 a4 06 "
317                 + "0040   71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
318                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
319                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
320     }
321
322     @Inject
323     private BundleContext bc;
324     //private HttpURLConnection connection;
325     protected static boolean notificationCalled;
326
327     @Inject @Filter(timeout=60000)
328     private IFlowMapping lms;
329
330     @Inject @Filter(timeout=60000)
331     private IMappingService mapService;
332
333     @Inject @Filter(timeout=10000)
334     private IConfigLispSouthboundPlugin configLispPlugin;
335
336     @Test
337     public void testSimpleUsage() throws Exception {
338         mapRequestSimple();
339         mapRegisterWithMapNotify();
340         mapRegisterWithMapNotifyAndMapRequest();
341         registerAndQuery__MAC();
342         mapRequestMapRegisterAndMapRequest();
343         mapRegisterWithAuthenticationWithoutConfiguringAKey();
344         mapRegisterWithoutMapNotify();
345     }
346
347     @Test
348     public void testLCAFs() throws Exception {
349         //registerAndQuery__SrcDestLCAF();
350         //registerAndQuery__SrcDestLCAFOverlap();
351         //registerAndQuery__KeyValueLCAF();
352         //registerAndQuery__ListLCAF();
353         //registerAndQuery__ApplicationData();
354         //registerAndQuery__TrafficEngineering();
355         //registerAndQuery__SegmentLCAF();
356     }
357
358     @Test
359     public void testMask() throws Exception {
360         //testPasswordExactMatch();                     TODO commented because it needs NB
361         //testPasswordMaskMatch();                      TODO commented because it needs NB
362         eidPrefixLookupIPv4();
363         eidPrefixLookupIPv6();
364     }
365 /*
366     @Test
367     public void testNorthbound() throws Exception {
368         northboundAddKey();
369         northboundAddMapping();
370         northboundDeleteMapping();
371         northboundRetrieveKey();
372         northboundRetrieveMapping();
373         northboundRetrieveSourceDestKey();
374         northboundRetrieveSourceDestMapping();
375     }
376 */
377     @Test
378     public void testOverWriting() throws Exception {
379         //testMapRegisterDosntOverwritesOtherSubKeys(); TODO weird failure, needs debug
380
381         // TODO: remove, we don't support overwrite flag any longer and RLOCs are not saved as independent RLOC groups
382         // testMapRegisterOverwritesSameSubkey();
383         // testMapRegisterOverwritesNoSubkey();
384         // testMapRegisterDoesntOverwritesNoSubkey();
385     }
386
387     @Test
388     public void testTimeOuts() throws Exception {
389         mapRequestMapRegisterAndMapRequestTestTimeout();
390         //mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse();   TODO commented because it needs NB
391     }
392
393 //    @Test
394 //    public void testNonProxy() throws Throwable {
395 //        testSimpleNonProxy();
396 //        testNonProxyOtherPort();
397 //        testRecievingNonProxyOnXtrPort();
398 //    }
399
400     @Test
401     public void testSmr() throws Exception {
402         registerQueryRegisterWithSmr();
403     }
404
405
406     /**
407      * TEST SCENARIO A
408      */
409     @Test
410     public void testMultiSiteScenarioA() throws IOException {
411         cleanUP();
412
413         final MultiSiteScenario multiSiteScenario = new MultiSiteScenario(mapService, lms);
414         multiSiteScenario.setCommonAuthentication();
415
416         restartSocket();
417         final SocketReader socketReader = SocketReader.startReadingInStandaloneThread(socket);
418
419         //TEST CASE 1
420         multiSiteScenario.storeSouthboundMappings(SITE_A, SITE_B, SITE_C, SITE_D4, SITE_D5);
421         multiSiteScenario.storeNorthMappingSrcDst(SITE_B, SITE_C);
422         multiSiteScenario.storeNorthMappingNegative(SITE_C, Action.Drop);
423         sleepForSeconds(2);
424         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
425         multiSiteScenario.assertPingWorks(SITE_B, 5, SITE_C, 4);
426         multiSiteScenario.assertPingFails(SITE_A, 1, SITE_C, 4);
427
428         //TEST CASE 2
429         //following action should trigger generatting of SMR messages:
430         // 1) 192.0.2.5/32
431         // 2) 192.0.1.1/32
432         multiSiteScenario.storeNorthMappingSrcDst(SITE_A, SITE_C);
433         sleepForSeconds(2);
434         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B.getHost(5), SITE_A.getHost(1));
435         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_C, 4);
436         multiSiteScenario.assertPingWorks(SITE_B, 5, SITE_C, 4);
437         multiSiteScenario.assertPingFails(SITE_D4, 5, SITE_C, 4);
438
439         //TEST CASE 3
440         // following action should trigger generatting of SMR messages:
441         // 1) 192.0.2.5/32
442         // 2) 192.0.1.1/32
443         // 3) 192.0.1.5/32
444         // 4) 192.0.4.5/32
445         multiSiteScenario.deleteNorthMappingNegative(SITE_C);
446         sleepForSeconds(2);
447         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B.getHost(5), SITE_A.getHost(1), SITE_A
448                         .getHost(5),
449                 SITE_D4.getHost(5));
450         multiSiteScenario.assertPingWorks(SITE_D4, 5, SITE_C, 4);
451
452         //TEST CASE 4
453         // following action should trigger generatting of SMR messages:
454         // 1) 192.0.4.5/32
455         multiSiteScenario.storeNorthMappingSrcDst(SITE_B, SITE_C_RLOC_10);
456         sleepForSeconds(2);
457         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D4.getHost(5));
458         //way of testing ping - get RLOC for mapping src-dst and compare it with awaited value doesn't test
459         //that ping won't be successfull
460         multiSiteScenario.assertPingFails(SITE_B, 5, SITE_C, 4);
461
462         //TEST CASE 5
463         // following action should trigger generatting of SMR messages:
464         // 1) 192.0.4.5/32
465         // 2) 192.0.2.5/32
466         multiSiteScenario.storeNorthMappingNegative(SITE_C, Action.Drop);
467         sleepForSeconds(2);
468         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D4.getHost(5), SITE_B.getHost(5));
469         multiSiteScenario.assertPingFails(SITE_D4, 5, SITE_C, 4);
470
471         //TEST CASE 6
472         multiSiteScenario.assertPingFails(SITE_D5, 5, SITE_C, 3);
473
474         //TEST CASE 7
475         multiSiteScenario.deleteNorthMapingSrcDst(SITE_A, SITE_C);
476         sleepForSeconds(2);
477         // following action should trigger generatting of SMR messages:
478         // 1) 192.0.4.5/32
479         // 2) 192.0.2.5/32
480         // 3) 192.0.5.5/32
481         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
482                 SITE_B.getHost(5));
483
484         // following action should trigger generatting of SMR messages:
485         // 1) 192.0.4.5/32
486         // 2) 192.0.2.5/32
487         // 3) 192.0.5.5/32
488         multiSiteScenario.storeNorthMappingSrcDst(SITE_B, SITE_C);
489         sleepForSeconds(2);
490         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
491                 SITE_B.getHost(5));
492
493         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
494         multiSiteScenario.assertPingWorks(SITE_B, 5, SITE_C, 4);
495         multiSiteScenario.assertPingFails(SITE_A, 1, SITE_C, 4);
496
497         //TEST CASE 8
498         // following action should trigger generatting of SMR messages:
499         // 1) 192.0.4.5/32
500         // 2) 192.0.2.5/32
501         // 3) 192.0.5.5/32
502         // 4) 192.0.1.1/32
503         multiSiteScenario.deleteNorthMapingSrcDst(SITE_B, SITE_C);
504         sleepForSeconds(2);
505         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
506                 SITE_B.getHost(5),
507                 SITE_A.getHost(1));
508         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
509         multiSiteScenario.assertPingFails(SITE_B, 5, SITE_C, 4);
510         multiSiteScenario.assertPingFails(SITE_A, 1, SITE_C, 4);
511
512         //TEST CASE 9
513         // following action should trigger generatting of SMR messages:
514         // 1) 192.0.4.5/32
515         // 2) 192.0.2.5/32
516         // 3) 192.0.5.5/32
517         // 4) 192.0.1.1/32
518         multiSiteScenario.deleteNorthMappingNegative(SITE_C);
519         sleepForSeconds(2);
520         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
521                 SITE_B.getHost(5),
522                 SITE_A.getHost(1));
523         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
524         multiSiteScenario.assertPingWorks(SITE_B, 5, SITE_C, 4);
525         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_C, 4);
526
527         socketReader.stopReading();
528
529     }
530
531     /**
532      * TEST SCENARIO B
533      */
534     @Test
535     public void testMultiSiteScenarioB() throws IOException {
536         cleanUP();
537
538         final MultiSiteScenario multiSiteScenario = new MultiSiteScenario(mapService, lms);
539         multiSiteScenario.setCommonAuthentication();
540
541         restartSocket();
542         final SocketReader socketReader = SocketReader.startReadingInStandaloneThread(socket);
543
544         mapService.setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
545         mapService.setMappingOverwrite(false);
546
547         //TEST CASE 1
548         multiSiteScenario.storeSouthboundMappings(SITE_A_SB, SITE_B_SB, SITE_C_WP_100_1_SB, SITE_D_WP_100_1_SB,
549                 SITE_E_SB);
550         multiSiteScenario.storeNorthMappingIpPrefix(SITE_A_SB);
551         multiSiteScenario.storeNorthMappingIpPrefix(SITE_B_SB);
552         multiSiteScenario.storeNorthMappingIpPrefix(SITE_C_WP_50_2_SB, SITE_D_WP_50_2_SB);
553         sleepForSeconds(2);
554         multiSiteScenario.assertPingWorks(SITE_A_SB, 5, SITE_C_WP_50_2_SB, 4, SITE_D_WP_50_2_SB);
555         multiSiteScenario.assertPingWorks(SITE_B_SB, 5, SITE_C_WP_50_2_SB, 4, SITE_D_WP_50_2_SB);
556
557         //TEST CASE 2
558         // following action should trigger generatting of SMR messages:
559         // 1) 192.0.2.5/32
560         // 2) 192.0.1.5/32
561         multiSiteScenario.storeNorthMappingSrcDst(SITE_A_SB, SITE_C_WP_50_2_SB, SITE_D_WP_50_2_SB);
562         sleepForSeconds(2);
563         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
564
565         // following action should trigger generatting of SMR messages:
566         // 1) 192.0.2.5/32
567         // 2) 192.0.1.5/32
568         multiSiteScenario.storeNorthMappingNegative(SITE_C_SB, Action.Drop);
569         sleepForSeconds(2);
570         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
571
572         multiSiteScenario.assertPingWorks(SITE_A_SB, 5, SITE_C_WP_50_2_SB, 4, SITE_D_WP_50_2_SB);
573         multiSiteScenario.assertPingFails(SITE_B_SB, 5, SITE_C_SB, 4);
574
575
576         //TEST CASE 3
577         // following action should trigger generatting of SMR messages:
578         // 1) 192.0.2.5/32
579         // 2) 192.0.1.5/32
580         multiSiteScenario.storeNorthMappingSrcDst(SITE_A_SB, SITE_C_WP_50_2_SB);
581         sleepForSeconds(2);
582         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
583         multiSiteScenario.assertPingWorks(SITE_A_SB, 5, SITE_C_WP_50_2_SB, 4);
584
585         //TEST CASE 4
586         multiSiteScenario.storeNorthMappingSrcDst(SITE_B_SB, SITE_C_WP_50_2_SB, SITE_D_WP_50_2_SB);
587         sleepForSeconds(2);
588         // following action should trigger generatting of SMR messages:
589         // 1) 192.0.2.5/32
590         // 2) 192.0.1.5/32
591         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
592         multiSiteScenario.assertPingWorks(SITE_B_SB, 5, SITE_C_WP_50_2_SB, 4, SITE_D_WP_50_2_SB);
593
594         //TEST CASE 5
595         multiSiteScenario.deleteSouthboundMappings(SITE_D_DELETE_SB);
596         sleepForSeconds(2);
597         // following action should trigger generatting of SMR messages:
598         // 1) 192.0.2.5/32
599         // 2) 192.0.1.5/32
600         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
601
602         multiSiteScenario.assertPingWorks(SITE_B_SB, 5, SITE_C_WP_50_2_SB, 4);
603
604         //TEST CASE 6
605         multiSiteScenario.deleteNorthMapingSrcDst(SITE_A_SB, SITE_C_WP_50_2_SB);
606         sleepForSeconds(2);
607         // following action should trigger generatting of SMR messages:
608         // 1) 192.0.2.5/32
609         // 2) 192.0.1.5/32
610         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
611
612         multiSiteScenario.deleteNorthMapingSrcDst(SITE_B_SB, SITE_C_WP_50_2_SB);
613         sleepForSeconds(2);
614         multiSiteScenario.assertPingFails(SITE_B_SB, 5, SITE_C_WP_50_2_SB, 4);
615
616         socketReader.stopReading();
617
618     }
619
620     // ------------------------------- Simple Tests ---------------------------
621
622     public void mapRequestSimple() throws SocketTimeoutException {
623         cleanUP();
624
625         // This Map-Request is sent from a source port different from 4342
626         // We close and bind the socket on the correct port
627         if (socket != null) {
628             socket.close();
629         }
630         socket = initSocket(socket, 56756);
631
632         sendPacket(mapRequestPacket);
633         ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
634         MapReply reply = MapReplySerializer.getInstance().deserialize(readBuf);
635         assertEquals(4435248268955932168L, reply.getNonce().longValue());
636
637     }
638
639     public void mapRegisterWithMapNotify() throws SocketTimeoutException {
640         cleanUP();
641         mapService.addAuthenticationKey(LispAddressUtil.asIpv4PrefixEid("153.16.254.1/32"), NULL_AUTH_KEY);
642         sleepForSeconds(1);
643         sendPacket(mapRegisterPacketWithNotify);
644         MapNotify reply = receiveMapNotify();
645         assertEquals(7, reply.getNonce().longValue());
646     }
647
648     public void mapRegisterWithMapNotifyAndMapRequest() throws SocketTimeoutException {
649         cleanUP();
650         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
651
652         MapReply mapReply = registerAddressAndQuery(eid);
653
654         assertEquals(4, mapReply.getNonce().longValue());
655         assertEquals(locatorEid, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
656                 .getRloc());
657
658     }
659
660     public void registerAndQuery__MAC() throws SocketTimeoutException {
661         cleanUP();
662         String macAddress = "01:02:03:04:05:06";
663
664         MapReply reply = registerAddressAndQuery(LispAddressUtil.asMacEid(macAddress));
665
666         assertTrue(true);
667         Eid addressFromNetwork = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
668         assertEquals(MacAfi.class, addressFromNetwork.getAddressType());
669         String macAddressFromReply = ((Mac) addressFromNetwork.getAddress()).getMac().getValue();
670
671         assertEquals(macAddress, macAddressFromReply);
672     }
673
674     public void mapRequestMapRegisterAndMapRequest() throws SocketTimeoutException {
675         cleanUP();
676         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
677         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
678         sleepForSeconds(1);
679
680         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
681         mapRequestBuilder.setNonce((long) 4);
682         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.getNoAddressEid()).build());
683         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
684         mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(eid).build());
685         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
686         mapRequestBuilder.getItrRloc().add(
687                 new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc(ourAddress)).build());
688         sendMapRequest(mapRequestBuilder.build());
689         MapReply mapReply = receiveMapReply();
690         assertEquals(4, mapReply.getNonce().longValue());
691         assertEquals(0, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
692         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
693         mapRegisterbuilder.setWantMapNotify(true);
694         mapRegisterbuilder.setNonce((long) 8);
695         MappingRecordBuilder etlrBuilder = new MappingRecordBuilder();
696         etlrBuilder.setEid(eid);
697         etlrBuilder.setRecordTtl(254);
698         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
699         recordBuilder.setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1"));
700         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
701         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
702         mapRegisterbuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
703         mapRegisterbuilder.getMappingRecordItem().add(
704                 new MappingRecordItemBuilder().setMappingRecord(etlrBuilder.build()).build());
705         sendMapRegister(mapRegisterbuilder.build());
706         MapNotify mapNotify = receiveMapNotify();
707         assertEquals(8, mapNotify.getNonce().longValue());
708         sleepForSeconds(1);
709         sendMapRequest(mapRequestBuilder.build());
710         mapReply = receiveMapReply();
711         assertEquals(4, mapReply.getNonce().longValue());
712         assertEquals(recordBuilder.getRloc(), mapReply.getMappingRecordItem().get(0).getMappingRecord()
713                 .getLocatorRecord().get(0).getRloc());
714
715     }
716
717     public void testMapRegisterDosntOverwritesOtherSubKeys() throws SocketTimeoutException {
718         cleanUP();
719         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
720         SimpleAddress rloc1Value = new SimpleAddress(new IpAddress(new Ipv4Address("4.3.2.1")));
721         Rloc rloc1 = LispAddressUtil.asKeyValueAddress("subkey1", rloc1Value);
722         SimpleAddress rloc2Value = new SimpleAddress(new IpAddress(new Ipv4Address("4.3.2.2")));
723         Rloc rloc2 = LispAddressUtil.asKeyValueAddress("subkey2", rloc2Value);
724         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
725         assertEquals(2, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
726         assertEquals(rloc2, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
727                 .getRloc());
728         assertEquals(rloc1, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
729                 .getRloc());
730     }
731
732     public void testMapRegisterOverwritesSameSubkey() throws SocketTimeoutException {
733         cleanUP();
734         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
735         SimpleAddress rloc1Value = new SimpleAddress(new IpAddress(new Ipv4Address("4.3.2.1")));
736         Rloc rloc1 = LispAddressUtil.asKeyValueAddress("subkey1", rloc1Value);
737         SimpleAddress rloc2Value = new SimpleAddress(new IpAddress(new Ipv4Address("4.3.2.2")));
738         Rloc rloc2 = LispAddressUtil.asKeyValueAddress("subkey2", rloc2Value);
739         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1, rloc2);
740         assertEquals(1, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
741         assertEquals(rloc2, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
742                 .getRloc());
743     }
744
745     public void testMapRegisterOverwritesNoSubkey() throws SocketTimeoutException {
746         cleanUP();
747         mapService.setMappingOverwrite(true);
748         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
749         Rloc rloc1Value = LispAddressUtil.asIpv4Rloc("4.3.2.1");
750         Rloc rloc2Value = LispAddressUtil.asIpv4Rloc("4.3.2.2");
751         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
752         assertEquals(1, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
753         assertEquals(rloc2Value, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
754                 .getRloc());
755     }
756
757     public void testMapRegisterDoesntOverwritesNoSubkey() throws SocketTimeoutException {
758         cleanUP();
759         mapService.setMappingOverwrite(false);
760         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
761         Rloc rloc1Value = LispAddressUtil.asIpv4Rloc("4.3.2.1");
762         Rloc rloc2Value = LispAddressUtil.asIpv4Rloc("4.3.2.2");
763         MapReply mapReply = sendMapRegisterTwiceWithDiffrentValues(eid, rloc1Value, rloc2Value);
764         assertEquals(1, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
765         Rloc rloc1ReturnValueContainer = mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord()
766                 .get(0).getRloc();
767         Rloc rloc2ReturnValueContainer = mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord()
768                 .get(1).getRloc();
769         assertTrue((rloc1Value.equals(rloc1ReturnValueContainer) && rloc2Value.equals(rloc2ReturnValueContainer))
770                 || (rloc1Value.equals(rloc2ReturnValueContainer) && rloc2Value.equals(rloc1ReturnValueContainer)));
771     }
772
773     private MapReply sendMapRegisterTwiceWithDiffrentValues(Eid eid, Rloc rloc1, Rloc rloc2)
774             throws SocketTimeoutException {
775         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
776         sleepForSeconds(1);
777         MapRegister mb = createMapRegister(eid, rloc1);
778         MapNotify mapNotify = lms.handleMapRegister(mb).getLeft();
779         MapRequest mr = createMapRequest(eid);
780         MapReply mapReply = lms.handleMapRequest(mr);
781         assertEquals(mb.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0).getRloc(),
782                 mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0).getRloc());
783         mb = createMapRegister(eid, rloc2);
784         mapNotify = lms.handleMapRegister(mb).getLeft();
785         assertEquals(8, mapNotify.getNonce().longValue());
786         mr = createMapRequest(eid);
787         sendMapRequest(mr);
788         mapReply = lms.handleMapRequest(mr);
789         return mapReply;
790     }
791
792     public void mapRegisterWithAuthenticationWithoutConfiguringAKey() throws SocketTimeoutException {
793         cleanUP();
794         sendPacket(mapRegisterPacketWithAuthenticationAndMapNotify);
795         try {
796             receivePacket(3000);
797             // If didn't timeout then fail:
798             fail();
799         } catch (SocketTimeoutException ste) {
800         }
801     }
802
803     public void mapRegisterWithoutMapNotify() {
804         cleanUP();
805         sendPacket(mapRegisterPacketWithoutNotify);
806         try {
807             receivePacket(3000);
808             // If didn't timeout then fail:
809             fail();
810         } catch (SocketTimeoutException ste) {
811         }
812     }
813
814     public void registerQueryRegisterWithSmr() throws SocketTimeoutException {
815         cleanUP();
816         lms.setShouldUseSmr(true);
817         mapService.addAuthenticationKey(LispAddressUtil.asIpv4PrefixEid("153.16.254.1/32"), NULL_AUTH_KEY);
818         sleepForSeconds(1);
819
820         sendPacket(mapRegisterPacketWithNotify);
821         receiveMapNotify();
822
823         sleepForSeconds(1);
824         sendPacket(mapRequestPacket);
825         sleepForSeconds(1);
826
827         mapRegisterPacketWithoutNotify[mapRegisterPacketWithoutNotify.length - 1] += 1;
828         sendPacket(mapRegisterPacketWithoutNotify);
829
830         ByteBuffer readBuf = ByteBuffer.wrap(receivePacket().getData());
831         MapRequest smr = MapRequestSerializer.getInstance().deserialize(readBuf);
832         assertTrue(smr.isSmr());
833         Eid sourceEid = smr.getSourceEid().getEid();
834         assertTrue(LispAddressUtil.asIpv4Eid("153.16.254.1").equals(sourceEid));
835         Eid smrEid = smr.getEidItem().get(0).getEid();
836         assertTrue(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32").equals(smrEid));
837     }
838
839     // --------------------- Northbound Tests ---------------------------
840 /*
841     private void northboundAddKey() throws Exception {
842         cleanUP();
843         LispIpv4Address address = LispAddressUtil.asIPAfiAddress("1.2.3.4");
844         int mask = 32;
845         String pass = "asdf";
846
847         URL url = createPutURL("key");
848         String authKeyJSON = createAuthKeyJSON(pass, address, mask);
849         callURL("PUT", "application/json", "text/plain", authKeyJSON, url);
850
851         String retrievedKey = lms.getAuthenticationKey(LispAddressUtil.toContainer(address), mask);
852
853         // Check stored password matches the one sent
854         assertEquals(pass, retrievedKey);
855
856     }
857
858     private void northboundRetrieveSourceDestKey() throws Exception {
859         cleanUP();
860         org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispsimpleaddress.primitiveaddress.Ipv4
861                 address1 = (org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispsimpleaddress
862                 .primitiveaddress.Ipv4) LispAddressUtil
863                 .toPrimitive(LispAddressUtil.asIPAfiAddress("10.0.0.1"));
864         org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispsimpleaddress.primitiveaddress.Ipv4
865                 address2 = (org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispsimpleaddress
866                 .primitiveaddress.Ipv4) LispAddressUtil
867                 .toPrimitive(LispAddressUtil.asIPAfiAddress("10.0.0.2"));
868         int mask1 = 32;
869         int mask2 = 32;
870         LcafSourceDestAddr sourceDestAddress = new LcafSourceDestAddrBuilder().setAfi(
871                 AddressFamilyNumberEnum.LCAF.getIanaCode())
872                 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
873                 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(address1).build())
874                 .setSrcMaskLength((short) mask1)
875                 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(address2).build())
876                 .setDstMaskLength((short) mask2).build();
877         String pass = "asdf";
878
879         lms.addAuthenticationKey(LispAddressUtil.toContainer(sourceDestAddress), mask1, pass);
880
881         // URL url = createGetKeyIPv4URL(address1, mask1);
882         URL url = createGetKeySourceDestURL(address1.getIpv4Address().getAfi(),
883                 ((LispIpv4Address) LispAddressUtil.toAFIfromPrimitive(sourceDestAddress.getSrcAddress()
884                 .getPrimitiveAddress())).getIpv4Address().getValue(), sourceDestAddress.getSrcMaskLength(),
885                 ((LispIpv4Address) LispAddressUtil.toAFIfromPrimitive(sourceDestAddress.getDstAddress()
886                 .getPrimitiveAddress())).getIpv4Address().getValue(), sourceDestAddress.getDstMaskLength());
887         String reply = callURL("GET", null, "application/json", null, url);
888         JSONTokener jt = new JSONTokener(reply);
889         JSONObject json = new JSONObject(jt);
890
891         // test that the password matches what was we expected.
892         assertEquals(pass, json.get("key"));
893
894     }
895
896     private void northboundRetrieveKey() throws Exception {
897         cleanUP();
898         LispIpv4Address address = LispAddressUtil.asIPAfiAddress("10.0.0.1");
899         int mask = 32;
900         String pass = "asdf";
901
902         lms.addAuthenticationKey(LispAddressUtil.toContainer(address), mask, pass);
903
904         URL url = createGetKeyIPv4URL(address, mask);
905         String reply = callURL("GET", null, "application/json", null, url);
906         JSONTokener jt = new JSONTokener(reply);
907         JSONObject json = new JSONObject(jt);
908
909         // test that the password matches what was we expected.
910         assertEquals(pass, json.get("key"));
911
912     }
913
914     private String createAuthKeyJSON(String key, LispIpv4Address address, int mask) {
915         return "{\"key\" : \"" + key + "\",\"maskLength\" : " + mask + ",\"address\" : " + "{\"ipAddress\" : \""
916                 + address.getIpv4Address().getValue() + "\",\"afi\" : " + address.getAfi().shortValue() + "}}";
917     }
918
919     private void northboundAddMapping() throws Exception {
920         cleanUP();
921         String pass = "asdf";
922         LispIpv4Address eid = LispAddressUtil.asIPAfiAddress("10.0.0.1");
923         int mask = 32;
924         LispIpv4Address rloc = LispAddressUtil.asIPAfiAddress("20.0.0.2");
925
926         // NB add mapping always checks the key
927         lms.addAuthenticationKey(LispAddressUtil.toContainer(eid), mask, pass);
928
929         URL url = createPutURL("mapping");
930         String mapRegisterJSON = createMapRegisterJSON(pass, eid, mask, rloc);
931         callURL("PUT", "application/json", "text/plain", mapRegisterJSON, url);
932
933         // Retrieve the RLOC from the database
934         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
935         mapRequestBuilder.setPitr(false);
936         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
937         mapRequestBuilder.getEidItem().add(
938                 new EidRecordBuilder().setMask((short) mask).setLispAddressContainer(
939                 LispAddressUtil.toContainer(eid)).build());
940         MapReply mapReply = lms.handleMapRequest(mapRequestBuilder.build());
941
942         LispIpv4Address retrievedRloc = (LispIpv4Address) LispAddressUtil.toAFI(
943                 mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
944                 .getLispAddressContainer());
945
946         assertEquals(rloc.getIpv4Address().getValue(), retrievedRloc.getIpv4Address().getValue());
947
948     }
949
950     private String createMapRegisterJSON(String key, LispIpv4Address eid, int mask, LispIpv4Address rloc) {
951         String jsonString = "{ " + "\"key\" : \"" + key + "\"," + "\"mapregister\" : " + "{ "
952                 + "\"proxyMapReply\" : false, "
953                 + "\"eidToLocatorRecords\" : " + "[ " + "{ " + "\"authoritative\" : true," + "\"prefixGeneric\" : "
954                 + "{ " + "\"ipAddress\" : \""
955                 + eid.getIpv4Address().getValue() + "\"," + "\"afi\" : " + eid.getAfi().shortValue() + "},"
956                 + "\"mapVersion\" : 0,"
957                 + "\"maskLength\" : " + mask + ", " + "\"action\" : \"NoAction\"," + "\"locators\" : " + "[ " + "{ "
958                 + "\"multicastPriority\" : 1,"
959                 + "\"locatorGeneric\" : " + "{ " + "\"ipAddress\" : \"" + rloc.getIpv4Address().getValue() + "\","
960                 + "\"afi\" : "
961                 + rloc.getAfi().shortValue() + "}, " + "\"routed\" : true," + "\"multicastWeight\" : 50,"
962                 + "\"rlocProbed\" : false, "
963                 + "\"localLocator\" : false, " + "\"priority\" : 1, " + "\"weight\" : 50 " + "} " + "], "
964                 + "\"recordTtl\" : 100" + "} " + "], "
965                 + "\"nonce\" : 3," + "\"keyId\" : 0 " + "} " + "}";
966
967         return jsonString;
968     }
969
970     private void northboundRetrieveMapping() throws Exception {
971         cleanUP();
972         LispIpv4Address eid = LispAddressUtil.asIPAfiAddress("10.0.0.1");
973         int mask = 32;
974         LispIpv4Address rloc = LispAddressUtil.asIPAfiAddress("20.0.0.2");
975         // Insert mapping in the database
976         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
977         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
978         etlr.setLispAddressContainer(LispAddressUtil.toContainer(eid));
979         etlr.setMaskLength((short) mask);
980         etlr.setRecordTtl(254);
981         etlr.setAuthoritative(false);
982         etlr.setAction(Action.NoAction);
983         LocatorRecordBuilder record = new LocatorRecordBuilder();
984         record.setLispAddressContainer(LispAddressUtil.toContainer(rloc));
985         record.setRouted(true);
986         record.setRlocProbed(false);
987         record.setLocalLocator(false);
988         record.setPriority((short) 1);
989         record.setWeight((short) 50);
990         record.setMulticastPriority((short) 1);
991         record.setMulticastWeight((short) 1);
992         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
993         etlr.getLocatorRecord().add(record.build());
994         mapRegister.setMappingRecordItem(new ArrayList<MappingRecordItem>());
995         mapRegister.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(etlr.build()).build());
996         lms.handleMapRegister(mapRegister.build());
997
998         // Get mapping using NB interface. No IID used
999         URL url = createGetMappingIPv4URL(0, eid, mask);
1000         String reply = callURL("GET", null, "application/json", null, url);
1001         JSONTokener jt = new JSONTokener(reply);
1002         JSONObject json = new JSONObject(jt);
1003
1004         // With just one locator, locators is not a JSONArray
1005         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric")
1006                 .getString("ipAddress");
1007
1008         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
1009
1010     }
1011
1012     private void northboundDeleteMapping() throws Exception {
1013         cleanUP();
1014         LispIpv4Address eid = LispAddressUtil.asIPAfiAddress("10.0.0.1");
1015         int mask = 32;
1016         LispIpv4Address rloc = LispAddressUtil.asIPAfiAddress("20.0.0.2");
1017         // Insert mapping in the database
1018         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
1019         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
1020         etlr.setLispAddressContainer(LispAddressUtil.toContainer(eid));
1021         etlr.setMaskLength((short) mask);
1022         etlr.setRecordTtl(254);
1023         etlr.setAuthoritative(false);
1024         etlr.setAction(Action.NoAction);
1025         LocatorRecordBuilder record = new LocatorRecordBuilder();
1026         record.setLispAddressContainer(LispAddressUtil.toContainer(rloc));
1027         record.setRouted(true);
1028         record.setRlocProbed(false);
1029         record.setLocalLocator(false);
1030         record.setPriority((short) 1);
1031         record.setWeight((short) 50);
1032         record.setMulticastPriority((short) 1);
1033         record.setMulticastWeight((short) 1);
1034         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
1035         etlr.getLocatorRecord().add(record.build());
1036         mapRegister.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1037         mapRegister.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(etlr.build()).build());
1038         lms.handleMapRegister(mapRegister.build());
1039
1040         // Delete mapping using NB interface. No IID used
1041         URL url = createDeleteMappingIPv4URL(0, eid, mask);
1042         String reply = callURL("DELETE", null, "application/json", null, url);
1043
1044         // Get mapping using NB interface. No IID used
1045         url = createGetMappingIPv4URL(0, eid, mask);
1046         reply = callURL("GET", null, "application/json", null, url);
1047         JSONTokener jt = new JSONTokener(reply);
1048         JSONObject json = new JSONObject(jt);
1049
1050         // With just one locator, locators is not a JSONArray
1051         assertEquals(json.getJSONArray("locators").length(), 0);
1052     }
1053
1054     private void northboundRetrieveSourceDestMapping() throws Exception {
1055         cleanUP();
1056         org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispsimpleaddress.primitiveaddress.Ipv4
1057                 address1 = (org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispsimpleaddress
1058                 .primitiveaddress.Ipv4) LispAddressUtil
1059                 .toPrimitive(LispAddressUtil.asIPAfiAddress("10.0.0.1"));
1060         org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispsimpleaddress.primitiveaddress.Ipv4
1061                 address2 = (org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispsimpleaddress
1062                 .primitiveaddress.Ipv4) LispAddressUtil
1063                 .toPrimitive(LispAddressUtil.asIPAfiAddress("10.0.0.2"));
1064         int mask1 = 32;
1065         int mask2 = 32;
1066         LcafSourceDestAddr sourceDestAddress = new LcafSourceDestAddrBuilder().setAfi(
1067                 AddressFamilyNumberEnum.LCAF.getIanaCode())
1068                 .setLcafType((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode())
1069                 .setSrcAddress(new SrcAddressBuilder().setPrimitiveAddress(
1070                         address1).build()).setSrcMaskLength((short) mask1)
1071                 .setDstAddress(new DstAddressBuilder().setPrimitiveAddress(
1072                         address2).build()).setDstMaskLength((short) mask2).build();
1073         LispIpv4Address rloc = LispAddressUtil.asIPAfiAddress("20.0.0.2");
1074
1075         // Insert mapping in the database
1076         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
1077         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
1078         etlr.setLispAddressContainer(LispAddressUtil.toContainer(sourceDestAddress));
1079         etlr.setMaskLength((short) mask1);
1080         etlr.setRecordTtl(254);
1081         etlr.setAuthoritative(false);
1082         etlr.setAction(Action.NoAction);
1083         LocatorRecordBuilder record = new LocatorRecordBuilder();
1084         record.setLispAddressContainer(LispAddressUtil.toContainer(rloc));
1085         record.setRouted(true);
1086         record.setRlocProbed(false);
1087         record.setLocalLocator(false);
1088         record.setPriority((short) 1);
1089         record.setWeight((short) 50);
1090         record.setMulticastPriority((short) 1);
1091         record.setMulticastWeight((short) 1);
1092         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
1093         etlr.getLocatorRecord().add(record.build());
1094         mapRegister.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1095         mapRegister.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(etlr.build()).build());
1096         lms.handleMapRegister(mapRegister.build());
1097
1098         // Get mapping using NB interface. No IID used
1099         URL url = createGetMappingSourceDestURL(address1.getIpv4Address().getAfi(),
1100                 address1.getIpv4Address().getIpv4Address().getValue(),
1101                 mask1,
1102                 address2.getIpv4Address().getIpv4Address().getValue(),
1103                 mask2);
1104         String reply = callURL("GET", null, "application/json", null, url);
1105         JSONTokener jt = new JSONTokener(reply);
1106         JSONObject json = new JSONObject(jt);
1107
1108         // With just one locator, locators is not a JSONArray
1109         String rlocRetrieved = json.getJSONArray("locators").getJSONObject(0).getJSONObject("locatorGeneric")
1110         .getString("ipAddress");
1111
1112         assertEquals(rloc.getIpv4Address().getValue(), rlocRetrieved);
1113
1114     }
1115
1116     private URL createGetKeyIPv4URL(LispIpv4Address address, int mask) throws MalformedURLException {
1117         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d", "key",
1118                 address.getAfi().shortValue(),
1119                 address.getIpv4Address().getValue(), mask);
1120         URL url = new URL(restUrl);
1121         return url;
1122     }
1123
1124     private URL createGetKeySourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask)
1125             throws MalformedURLException {
1126         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d",
1127                 "key", afi, srcAddress, srcMask,
1128                 dstAddress, dstMask);
1129         URL url = new URL(restUrl);
1130         return url;
1131     }
1132
1133     private URL createGetMappingSourceDestURL(int afi, String srcAddress, int srcMask, String dstAddress, int dstMask)
1134             throws MalformedURLException {
1135         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/0/%d/%s/%d/%s/%d",
1136                 "mapping", afi, srcAddress,
1137                 srcMask, dstAddress, dstMask);
1138         URL url = new URL(restUrl);
1139         return url;
1140     }
1141
1142     private URL createGetMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
1143         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping",
1144                 iid, address.getAfi()
1145                 .shortValue(), address.getIpv4Address().getValue(), mask);
1146         URL url = new URL(restUrl);
1147         return url;
1148     }
1149
1150     private URL createDeleteMappingIPv4URL(int iid, LispIpv4Address address, int mask) throws MalformedURLException {
1151         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s/%d/%d/%s/%d", "mapping",
1152                 iid, address.getAfi()
1153                 .shortValue(), address.getIpv4Address().getValue(), mask);
1154         URL url = new URL(restUrl);
1155         return url;
1156     }
1157
1158     private URL createPutURL(String resource) throws MalformedURLException {
1159
1160         String restUrl = String.format("http://localhost:8080/lispflowmapping/nb/v2/default/%s", resource);
1161
1162         URL url = new URL(restUrl);
1163         return url;
1164     }
1165
1166     private String createAuthenticationString() {
1167         String authString = "admin:admin";
1168         byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
1169         String authStringEnc = new String(authEncBytes);
1170         return authStringEnc;
1171     }
1172
1173     private String callURL(String method, String content, String accept, String body, URL url) throws IOException,
1174             JSONException {
1175         String authStringEnc = createAuthenticationString();
1176         connection = (HttpURLConnection) url.openConnection();
1177         connection.setRequestMethod(method);
1178         connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
1179         if (content != null) {
1180             connection.setRequestProperty("Content-Type", content);
1181         }
1182         if (accept != null) {
1183             connection.setRequestProperty("Accept", accept);
1184         }
1185         if (body != null) {
1186             // now add the request body
1187             connection.setDoOutput(true);
1188             OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
1189             wr.write(body);
1190             wr.flush();
1191         }
1192         connection.connect();
1193
1194         // getting the result, first check response code
1195         Integer httpResponseCode = connection.getResponseCode();
1196
1197         if (httpResponseCode > 299) {
1198             LOG.trace("HTTP Address: " + url);
1199             LOG.trace("HTTP Response Code: " + httpResponseCode);
1200             fail();
1201         }
1202
1203         InputStream is = connection.getInputStream();
1204         BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
1205         StringBuilder sb = new StringBuilder();
1206         int cp;
1207         while ((cp = rd.read()) != -1) {
1208             sb.append((char) cp);
1209         }
1210         is.close();
1211         connection.disconnect();
1212         return (sb.toString());
1213     }
1214
1215     // timePeriod - in ms
1216     public void assertNoPacketReceived(int timePeriod) {
1217         try {
1218             receivePacket(timePeriod);
1219             // If didn't timeout then fail:
1220             fail();
1221         } catch (SocketTimeoutException ste) {
1222         }
1223     }
1224 */
1225     // ------------------------------- Mask Tests ---------------------------
1226
1227     public void eidPrefixLookupIPv4() throws SocketTimeoutException {
1228         cleanUP();
1229         runPrefixTest(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/16"), LispAddressUtil.asIpv4PrefixEid("1.2.3.2/32"),
1230                 LispAddressUtil.asIpv4PrefixEid("1.1.1.1/32"));
1231     }
1232
1233     public void eidPrefixLookupIPv6() throws SocketTimeoutException {
1234         cleanUP();
1235         runPrefixTest(LispAddressUtil.asIpv6PrefixEid("1:2:3:4:5:6:7:8/64"),
1236                 LispAddressUtil.asIpv6PrefixEid("1:2:3:4:5:1:2:3/128"),
1237                 LispAddressUtil.asIpv6PrefixEid("1:2:3:1:2:3:1:2/128"));
1238     }
1239
1240     private void runPrefixTest(Eid registerEID, Eid matchedAddress, Eid unMatchedAddress)
1241             throws SocketTimeoutException {
1242         mapService.addAuthenticationKey(registerEID, NULL_AUTH_KEY);
1243         sleepForSeconds(1);
1244
1245         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
1246         mapRegister.setWantMapNotify(true);
1247         mapRegister.setNonce((long) 8);
1248         mapRegister.setWantMapNotify(true);
1249         mapRegister.setKeyId((short) 0);
1250         mapRegister.setAuthenticationData(new byte[0]);
1251         mapRegister.setNonce((long) 8);
1252         mapRegister.setProxyMapReply(false);
1253         MappingRecordBuilder etlr = new MappingRecordBuilder();
1254         etlr.setRecordTtl(254);
1255         etlr.setAction(Action.NoAction);
1256         etlr.setAuthoritative(false);
1257         etlr.setMapVersion((short) 0);
1258         etlr.setEid(registerEID);
1259         etlr.setRecordTtl(254);
1260         LocatorRecordBuilder record = new LocatorRecordBuilder();
1261         record.setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1"));
1262         record.setLocalLocator(false);
1263         record.setRlocProbed(false);
1264         record.setRouted(true);
1265         record.setMulticastPriority((short) 0);
1266         record.setMulticastWeight((short) 0);
1267         record.setPriority((short) 0);
1268         record.setWeight((short) 0);
1269         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
1270         etlr.getLocatorRecord().add(record.build());
1271         mapRegister.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1272         mapRegister.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(etlr.build()).build());
1273         sendMapRegister(mapRegister.build());
1274         MapNotify mapNotify = receiveMapNotify();
1275         assertEquals(8, mapNotify.getNonce().longValue());
1276         sleepForSeconds(1);
1277         MapRequestBuilder mapRequest = new MapRequestBuilder();
1278         mapRequest.setNonce((long) 4);
1279         mapRequest.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(ourAddress)).build());
1280         mapRequest.setEidItem(new ArrayList<EidItem>());
1281         mapRequest.setAuthoritative(false);
1282         mapRequest.setMapDataPresent(false);
1283         mapRequest.setPitr(false);
1284         mapRequest.setProbe(false);
1285         mapRequest.setSmr(false);
1286         mapRequest.setSmrInvoked(false);
1287         mapRequest.getEidItem().add(new EidItemBuilder().setEid(matchedAddress).build());
1288         mapRequest.setItrRloc(new ArrayList<ItrRloc>());
1289         mapRequest.getItrRloc().add(
1290                 new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc(ourAddress)).build());
1291         sendMapRequest(mapRequest.build());
1292         MapReply mapReply = receiveMapReply();
1293         assertEquals(4, mapReply.getNonce().longValue());
1294         assertEquals(record.getRloc(), mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord()
1295                 .get(0).getRloc());
1296         mapRequest.setEidItem(new ArrayList<EidItem>());
1297         mapRequest.getEidItem().add(new EidItemBuilder().setEid(unMatchedAddress).build());
1298         sendMapRequest(mapRequest.build());
1299         mapReply = receiveMapReply();
1300         assertEquals(0, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
1301     }
1302 /*
1303     // This registers an IP with a MapRegister, then adds a password via the
1304     // northbound REST API
1305     // and checks that the password works
1306     public void testPasswordExactMatch() throws Exception {
1307         cleanUP();
1308         String ipString = "10.0.0.1";
1309         LispIpv4Address address = LispAddressUtil.asIPAfiAddress(ipString);
1310         int mask = 32;
1311         String pass = "pass";
1312
1313         URL url = createPutURL("key");
1314
1315         String jsonAuthData = createAuthKeyJSON(pass, address, mask);
1316
1317         LOG.trace("Sending this JSON to LISP server: \n" + jsonAuthData);
1318         LOG.trace("Address: " + address);
1319
1320         byte[] expectedSha = new byte[] { (byte) 146, (byte) 234, (byte) 52, (byte) 247, (byte) 186, (byte) 232,
1321                 (byte) 31, (byte) 249, (byte) 87,
1322                 (byte) 73, (byte) 234, (byte) 54, (byte) 225, (byte) 160, (byte) 129, (byte) 251, (byte) 73, (byte) 53,
1323                 (byte) 196, (byte) 62 };
1324
1325         byte[] zeros = new byte[20];
1326
1327         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
1328
1329         // build a MapRegister
1330         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
1331         mapRegister.setWantMapNotify(true);
1332         mapRegister.setNonce((long) 8);
1333         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
1334         etlr.setLispAddressContainer(LispAddressUtil.toContainer(address));
1335         etlr.setMaskLength((short) mask);
1336         etlr.setRecordTtl(254);
1337         LocatorRecordBuilder record = new LocatorRecordBuilder();
1338         record.setLispAddressContainer(LispAddressUtil.toContainer(locatorEid));
1339         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
1340         etlr.getLocatorRecord().add(record.build());
1341         mapRegister.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1342         mapRegister.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(etlr.build()).build());
1343
1344         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
1345         mapRegister.setAuthenticationData(zeros);
1346
1347         sendMapRegister(mapRegister.build());
1348         assertNoPacketReceived(3000);
1349
1350         mapRegister.setAuthenticationData(expectedSha);
1351
1352         sendMapRegister(mapRegister.build());
1353
1354         assertMapNotifyReceived();
1355     }
1356
1357     public void testPasswordMaskMatch() throws Exception {
1358         cleanUP();
1359         LispIpv4Address addressInRange = LispAddressUtil.asIPAfiAddress("10.20.30.40");
1360         LispIpv4Address addressOutOfRange = LispAddressUtil.asIPAfiAddress("20.40.30.40");
1361         LispIpv4Address range = LispAddressUtil.asIPAfiAddress("10.20.30.0");
1362
1363         int mask = 32;
1364         String pass = "pass";
1365
1366         URL url = createPutURL("key");
1367         String jsonAuthData = createAuthKeyJSON(pass, range, 8);
1368
1369         callURL("PUT", "application/json", "text/plain", jsonAuthData, url);
1370         // build a MapRegister
1371         MapRegisterBuilder mapRegister = new MapRegisterBuilder();
1372
1373         mapRegister.setWantMapNotify(true);
1374         mapRegister.setNonce((long) 8);
1375         EidToLocatorRecordBuilder etlr = new EidToLocatorRecordBuilder();
1376         etlr.setLispAddressContainer(LispAddressUtil.toContainer(addressInRange));
1377         etlr.setMaskLength((short) mask);
1378         etlr.setRecordTtl(254);
1379         LocatorRecordBuilder record = new LocatorRecordBuilder();
1380         record.setLispAddressContainer(LispAddressUtil.toContainer(locatorEid));
1381         record.setLispAddressContainer(LispAddressUtil.toContainer(locatorEid));
1382         etlr.setLocatorRecord(new ArrayList<LocatorRecord>());
1383         etlr.getLocatorRecord().add(record.build());
1384         mapRegister.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1385         mapRegister.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(etlr.build()).build());
1386
1387         mapRegister.setKeyId((short) 1); // LispKeyIDEnum.SHA1.getKeyID()
1388         mapRegister
1389                 .setAuthenticationData(new byte[] { -15, -52, 38, -94, 125, -111, -68, -79, 68, 6, 101, 45, -1, 47, -4,
1390                 -67, -113, 104, -110, -71 });
1391
1392         sendMapRegister(mapRegister.build());
1393
1394         assertMapNotifyReceived();
1395
1396         etlr.setLispAddressContainer(LispAddressUtil.toContainer(addressOutOfRange));
1397         mapRegister
1398                 .setAuthenticationData(new byte[] { -54, 68, -58, -91, -23, 22, -88, -31, 113, 39, 115, 78, -68, -123,
1399                 -71, -14, -99, 67, -23, -73 });
1400
1401         sendMapRegister(mapRegister.build());
1402         assertNoPacketReceived(3000);
1403     }
1404 */
1405     // takes an address, packs it in a MapRegister and sends it
1406     private void registerAddress(Eid eid) throws SocketTimeoutException {
1407         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
1408         sleepForSeconds(1);
1409         MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
1410         mapRegisterBuilder.setWantMapNotify(true);
1411         mapRegisterBuilder.setKeyId((short) 0);
1412         mapRegisterBuilder.setAuthenticationData(new byte[0]);
1413         mapRegisterBuilder.setNonce((long) 8);
1414         mapRegisterBuilder.setProxyMapReply(false);
1415         MappingRecordBuilder etlrBuilder = new MappingRecordBuilder();
1416         etlrBuilder.setEid(eid);
1417         etlrBuilder.setRecordTtl(254);
1418         etlrBuilder.setAction(Action.NoAction);
1419         etlrBuilder.setAuthoritative(false);
1420         etlrBuilder.setMapVersion((short) 0);
1421         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1422         recordBuilder.setLocalLocator(false);
1423         recordBuilder.setRlocProbed(false);
1424         recordBuilder.setRouted(true);
1425         recordBuilder.setMulticastPriority((short) 0);
1426         recordBuilder.setMulticastWeight((short) 0);
1427         recordBuilder.setPriority((short) 0);
1428         recordBuilder.setWeight((short) 0);
1429         recordBuilder.setRloc(locatorEid);
1430         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1431         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1432         mapRegisterBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1433         mapRegisterBuilder.getMappingRecordItem().add(
1434                 new MappingRecordItemBuilder().setMappingRecord(etlrBuilder.build()).build());
1435         sendMapRegister(mapRegisterBuilder.build());
1436         MapNotify mapNotify = receiveMapNotify();
1437         assertEquals(8, mapNotify.getNonce().longValue());
1438     }
1439
1440     private MapReply queryForAddress(Eid eid, String srcEid) throws SocketTimeoutException {
1441         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1442         mapRequestBuilder.setNonce((long) 4);
1443         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
1444         mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(eid).build());
1445         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1446         if (srcEid != null) {
1447             mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(srcEid)).build());
1448         } else {
1449             mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(ourAddress))
1450                     .build());
1451         }
1452         mapRequestBuilder.getItrRloc().add(
1453                 new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc(ourAddress)).build());
1454         mapRequestBuilder.setAuthoritative(false);
1455         mapRequestBuilder.setMapDataPresent(false);
1456         mapRequestBuilder.setPitr(false);
1457         mapRequestBuilder.setProbe(false);
1458         mapRequestBuilder.setSmr(false);
1459         mapRequestBuilder.setSmrInvoked(false);
1460         sendMapRequest(mapRequestBuilder.build());
1461         return receiveMapReply();
1462     }
1463
1464     // takes an address, packs it in a MapRegister, sends it, returns the
1465     // MapReply
1466     private MapReply registerAddressAndQuery(Eid eid) throws SocketTimeoutException {
1467         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
1468         sleepForSeconds(1);
1469         MapRegisterBuilder mapRegisterBuilder = new MapRegisterBuilder();
1470         mapRegisterBuilder.setWantMapNotify(true);
1471         mapRegisterBuilder.setKeyId((short) 0);
1472         mapRegisterBuilder.setAuthenticationData(new byte[0]);
1473         mapRegisterBuilder.setNonce((long) 8);
1474         mapRegisterBuilder.setProxyMapReply(false);
1475         MappingRecordBuilder etlrBuilder = new MappingRecordBuilder();
1476         etlrBuilder.setEid(eid);
1477         etlrBuilder.setRecordTtl(254);
1478         etlrBuilder.setAction(Action.NoAction);
1479         etlrBuilder.setAuthoritative(false);
1480         etlrBuilder.setMapVersion((short) 0);
1481         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1482         recordBuilder.setLocalLocator(false);
1483         recordBuilder.setRlocProbed(false);
1484         recordBuilder.setRouted(true);
1485         recordBuilder.setMulticastPriority((short) 0);
1486         recordBuilder.setMulticastWeight((short) 0);
1487         recordBuilder.setPriority((short) 0);
1488         recordBuilder.setWeight((short) 0);
1489         recordBuilder.setRloc(locatorEid);
1490         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1491         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1492         mapRegisterBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1493         mapRegisterBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(
1494                 etlrBuilder.build()).build());
1495         sendMapRegister(mapRegisterBuilder.build());
1496         MapNotify mapNotify = receiveMapNotify();
1497         assertEquals(8, mapNotify.getNonce().longValue());
1498         // wait for the notifications to propagate
1499         sleepForSeconds(1);
1500         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1501         mapRequestBuilder.setNonce((long) 4);
1502         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
1503         mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(eid).build());
1504         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1505         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid(ourAddress)).build());
1506         mapRequestBuilder.getItrRloc().add(
1507                 new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc(ourAddress)).build());
1508         mapRequestBuilder.setAuthoritative(false);
1509         mapRequestBuilder.setMapDataPresent(false);
1510         mapRequestBuilder.setPitr(false);
1511         mapRequestBuilder.setProbe(false);
1512         mapRequestBuilder.setSmr(false);
1513         mapRequestBuilder.setSmrInvoked(false);
1514         sendMapRequest(mapRequestBuilder.build());
1515         return receiveMapReply();
1516     }
1517
1518     // ------------------------------- LCAF Tests ---------------------------
1519
1520     @Test
1521     public void registerAndQuery__SrcDestLCAF() throws SocketTimeoutException {
1522         cleanUP();
1523         String ipPrefix = "10.20.30.200/32";
1524         String macString = "01:02:03:04:05:06";
1525
1526         SourceDestKeyBuilder builder = new SourceDestKeyBuilder();
1527         builder.setSource(new SimpleAddress(new IpPrefix(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1528                 .ietf.inet.types.rev100924.Ipv4Prefix(ipPrefix))));
1529         builder.setDest(new SimpleAddress(new MacAddress(macString)));
1530
1531         EidBuilder eb = new EidBuilder();
1532         eb.setAddressType(SourceDestKeyLcaf.class);
1533         eb.setVirtualNetworkId(null);
1534         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
1535                 .lisp.address.address.SourceDestKeyBuilder().setSourceDestKey(builder.build()).build());
1536
1537         MapReply reply = registerAddressAndQuery(eb.build());
1538
1539         Eid fromNetwork = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1540         assertEquals(SourceDestKeyLcaf.class, fromNetwork.getAddressType());
1541         SourceDestKey sourceDestFromNetwork = (SourceDestKey) fromNetwork.getAddress();
1542
1543         SimpleAddress receivedAddr1 = sourceDestFromNetwork.getSourceDestKey().getSource();
1544         SimpleAddress receivedAddr2 = sourceDestFromNetwork.getSourceDestKey().getDest();
1545
1546         assertNotNull(receivedAddr1.getIpPrefix().getIpv4Prefix());
1547         assertNotNull(receivedAddr2.getMacAddress());
1548
1549         IpPrefix receivedIP = receivedAddr1.getIpPrefix();
1550         MacAddress receivedMAC = receivedAddr2.getMacAddress();
1551
1552         assertEquals(ipPrefix, receivedIP.getIpv4Prefix().getValue());
1553         assertEquals(macString, receivedMAC.getValue());
1554     }
1555
1556     @Test
1557     public void registerAndQuery__SrcDestLCAFOverlap() throws SocketTimeoutException {
1558         cleanUP();
1559         String ipString1 = "10.10.10.0";
1560         String ipString2 = "20.20.20.0";
1561         String ipPrefix1 = ipString1 + "/24";
1562         String ipPrefix2 = ipString2 + "/24";
1563
1564         Eid srcDst = LispAddressUtil.asSrcDstEid(ipString1, ipString2, 24, 24, 0);
1565         registerAddress(LispAddressUtil.asIpv4PrefixEid(ipPrefix2));
1566         registerAddress(srcDst);
1567
1568         // exact match
1569         MapReply reply = queryForAddress(srcDst, null);
1570
1571         Eid fromNetwork = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1572         assertEquals(SourceDestKeyLcaf.class, fromNetwork.getAddressType());
1573         SourceDestKey sourceDestFromNetwork = (SourceDestKey) fromNetwork.getAddress();
1574
1575         IpPrefix receivedAddr1 = sourceDestFromNetwork.getSourceDestKey().getSource().getIpPrefix();
1576         IpPrefix receivedAddr2 = sourceDestFromNetwork.getSourceDestKey().getDest().getIpPrefix();
1577
1578         assertNotNull(receivedAddr1.getIpv4Prefix());
1579         assertNotNull(receivedAddr2.getIpv4Prefix());
1580
1581         assertEquals(ipPrefix1, receivedAddr1.getIpv4Prefix().getValue());
1582         assertEquals(ipPrefix2, receivedAddr2.getIpv4Prefix().getValue());
1583
1584         // srcEid/dstEid match
1585         reply = queryForAddress(LispAddressUtil.asIpv4PrefixEid("20.20.20.1/32"), "10.10.10.1");
1586         fromNetwork = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1587         assertEquals(Ipv4PrefixAfi.class, fromNetwork.getAddressType());
1588
1589         Ipv4Prefix ipAddr2 = (Ipv4Prefix) fromNetwork.getAddress();
1590         assertEquals(ipPrefix2, ipAddr2.getIpv4Prefix().getValue());
1591
1592         // dstEid match only
1593         reply = queryForAddress(LispAddressUtil.asIpv4PrefixEid("20.20.20.1/32"), "1.2.3.4");
1594         fromNetwork = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1595         assertEquals(Ipv4PrefixAfi.class, fromNetwork.getAddressType());
1596
1597         ipAddr2 = (Ipv4Prefix) fromNetwork.getAddress();
1598         assertEquals(ipPrefix2, ipAddr2.getIpv4Prefix().getValue());
1599     }
1600
1601     @Test
1602     public void registerAndQuery__KeyValueLCAF() throws SocketTimeoutException {
1603         cleanUP();
1604         String ipString = "10.20.30.200";
1605         String macString = "01:02:03:04:05:06";
1606         SimpleAddress addrToSend1 = new SimpleAddress(new IpAddress(new Ipv4Address(ipString)));
1607         SimpleAddress addrToSend2 = new SimpleAddress(new MacAddress(macString));
1608         Eid kv = LispAddressUtil.asKeyValueAddressEid(addrToSend1, addrToSend2);
1609
1610         MapReply reply = registerAddressAndQuery(kv);
1611
1612         Eid fromNetwork = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1613         assertEquals(KeyValueAddressLcaf.class, fromNetwork.getAddressType());
1614         KeyValueAddress keyValueFromNetwork = (KeyValueAddress) fromNetwork.getAddress();
1615
1616         SimpleAddress receivedAddr1 = keyValueFromNetwork.getKeyValueAddress().getKey();
1617         SimpleAddress receivedAddr2 = keyValueFromNetwork.getKeyValueAddress().getValue();
1618
1619         assertNotNull(receivedAddr1.getIpAddress().getIpv4Address());
1620         assertNotNull(receivedAddr2.getMacAddress());
1621
1622         Ipv4Address receivedIP = receivedAddr1.getIpAddress().getIpv4Address();
1623         MacAddress receivedMAC = receivedAddr2.getMacAddress();
1624
1625         assertEquals(ipString, receivedIP.getValue());
1626         assertEquals(macString, receivedMAC.getValue());
1627     }
1628
1629     public void registerAndQuery__ListLCAF() throws SocketTimeoutException {
1630         cleanUP();
1631         String macString = "01:02:03:04:05:06";
1632         String ipString = "10.20.255.30";
1633         List<SimpleAddress> addresses = new ArrayList<SimpleAddress>();
1634         addresses.add(new SimpleAddress(new IpAddress(new Ipv4Address(ipString))));
1635         addresses.add(new SimpleAddress(new MacAddress(macString)));
1636         AfiListBuilder listbuilder = new AfiListBuilder();
1637         listbuilder.setAddressList(addresses);
1638
1639         EidBuilder eb = new EidBuilder();
1640         eb.setAddressType(AfiListLcaf.class);
1641         eb.setVirtualNetworkId(null);
1642         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
1643                 .lisp.address.address.AfiListBuilder().setAfiList(listbuilder.build()).build());
1644
1645         MapReply reply = registerAddressAndQuery(eb.build());
1646
1647         Eid receivedAddress = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1648
1649         assertEquals(AfiListLcaf.class, receivedAddress.getAddressType());
1650
1651         AfiList listAddrFromNetwork = (AfiList) receivedAddress.getAddress();
1652         SimpleAddress receivedAddr1 = (SimpleAddress) listAddrFromNetwork.getAfiList().getAddressList().get(0);
1653         SimpleAddress receivedAddr2 = (SimpleAddress) listAddrFromNetwork.getAfiList().getAddressList().get(1);
1654
1655         assertNotNull(receivedAddr1.getIpAddress().getIpv4Address());
1656         assertNotNull(receivedAddr2.getMacAddress());
1657
1658         assertEquals(macString, receivedAddr2.getMacAddress().getValue());
1659         assertEquals(ipString, receivedAddr1.getIpAddress().getIpv4Address().getValue());
1660     }
1661
1662     public void registerAndQuery__SegmentLCAF() throws SocketTimeoutException {
1663         cleanUP();
1664         String ipString = "10.20.255.30";
1665         int instanceId = 6;
1666
1667         EidBuilder eb = new EidBuilder();
1668         eb.setAddressType(Ipv4PrefixAfi.class);
1669         eb.setVirtualNetworkId(new InstanceIdType((long) instanceId));
1670         eb.setAddress(new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns
1671                 .yang.ietf.inet.types.rev100924.Ipv4Prefix(ipString)).build());
1672
1673         MapReply reply = registerAddressAndQuery(eb.build());
1674
1675         Eid receivedAddress = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1676         assertEquals(Ipv4PrefixAfi.class, receivedAddress.getAddressType());
1677
1678         assertEquals(ipString, ((Ipv4Prefix) receivedAddress.getAddress()).getIpv4Prefix().getValue());
1679
1680         assertEquals(instanceId, receivedAddress.getVirtualNetworkId().getValue().intValue());
1681     }
1682
1683     public void registerAndQuery__TrafficEngineering() throws SocketTimeoutException {
1684         cleanUP();
1685         String macString = "01:02:03:04:05:06";
1686         String ipString = "10.20.255.30";
1687         HopBuilder hopBuilder = new HopBuilder();
1688         hopBuilder.setAddress(new SimpleAddress(new IpAddress(new Ipv4Address(ipString))));
1689         hopBuilder.setLrsBits(new LrsBits(true, false, true));
1690         Hop hop1 = hopBuilder.build();
1691         hopBuilder.setAddress(new SimpleAddress(new MacAddress(macString)));
1692         hopBuilder.setLrsBits(new LrsBits(false, true, false));
1693         Hop hop2 = hopBuilder.build();
1694         ExplicitLocatorPathBuilder elpBuilder = new ExplicitLocatorPathBuilder();
1695         elpBuilder.setHop(new ArrayList<Hop>());
1696         elpBuilder.getHop().add(hop1);
1697         elpBuilder.getHop().add(hop2);
1698
1699         EidBuilder eb = new EidBuilder();
1700         eb.setAddressType(ExplicitLocatorPathLcaf.class);
1701         eb.setVirtualNetworkId(null);
1702         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
1703                 .lisp.address.address.ExplicitLocatorPathBuilder().setExplicitLocatorPath(elpBuilder.build()).build());
1704
1705         MapReply reply = registerAddressAndQuery(eb.build());
1706
1707         assertEquals(ExplicitLocatorPathLcaf.class, reply.getMappingRecordItem().get(0).getMappingRecord().getEid()
1708                 .getAddressType());
1709
1710         ExplicitLocatorPath receivedAddress = (ExplicitLocatorPath) reply.getMappingRecordItem().get(0)
1711                 .getMappingRecord().getEid().getAddress();
1712
1713         Hop receivedHop1 = (Hop) receivedAddress.getExplicitLocatorPath().getHop().get(0);
1714         Hop receivedHop2 = (Hop) receivedAddress.getExplicitLocatorPath().getHop().get(1);
1715
1716         assertEquals(true, receivedHop1.getLrsBits().isLookup());
1717         assertEquals(false, receivedHop1.getLrsBits().isRlocProbe());
1718         assertEquals(true, receivedHop1.getLrsBits().isStrict());
1719
1720         assertEquals(false, receivedHop2.getLrsBits().isLookup());
1721         assertEquals(true, receivedHop2.getLrsBits().isRlocProbe());
1722         assertEquals(false, receivedHop2.getLrsBits().isStrict());
1723
1724         assertNotNull(receivedHop1.getAddress().getIpAddress().getIpv4Address());
1725         assertNotNull(receivedHop2.getAddress().getMacAddress());
1726
1727         assertEquals(ipString, receivedHop1.getAddress().getIpAddress().getIpv4Address().getValue());
1728         assertEquals(macString, receivedHop2.getAddress().getMacAddress().getValue());
1729     }
1730
1731     public void registerAndQuery__ApplicationData() throws SocketTimeoutException {
1732         cleanUP();
1733         String ipString = "1.2.3.4";
1734         short protocol = 1;
1735         int ipTOs = 2;
1736         int localPortLow = 3;
1737         int localPortHigh = 4;
1738         int remotePortLow = 4;
1739         int remotePortHigh = 5;
1740
1741         ApplicationDataBuilder builder = new ApplicationDataBuilder();
1742         builder.setIpTos(ipTOs);
1743         builder.setProtocol(protocol);
1744         builder.setLocalPortLow(new PortNumber(localPortLow));
1745         builder.setLocalPortHigh(new PortNumber(localPortHigh));
1746         builder.setRemotePortLow(new PortNumber(remotePortLow));
1747         builder.setRemotePortHigh(new PortNumber(remotePortHigh));
1748         builder.setAddress(new SimpleAddress(new IpAddress(new Ipv4Address(ipString))));
1749
1750         EidBuilder eb = new EidBuilder();
1751         eb.setAddressType(ApplicationDataLcaf.class);
1752         eb.setVirtualNetworkId(null);
1753         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
1754                 .lisp.address.address.ApplicationDataBuilder().setApplicationData(builder.build()).build());
1755         Eid addressToSend = eb.build();
1756
1757         MapReply reply = registerAddressAndQuery(addressToSend);
1758
1759         Eid receivedAddress = reply.getMappingRecordItem().get(0).getMappingRecord().getEid();
1760
1761         assertEquals(ApplicationDataLcaf.class, receivedAddress.getAddressType());
1762
1763         ApplicationData receivedApplicationDataAddress = (ApplicationData) receivedAddress.getAddress();
1764         assertEquals(protocol, receivedApplicationDataAddress.getApplicationData().getProtocol().intValue());
1765         assertEquals(ipTOs, receivedApplicationDataAddress.getApplicationData().getIpTos().intValue());
1766         assertEquals(localPortLow, receivedApplicationDataAddress.getApplicationData().getLocalPortLow().getValue()
1767                 .intValue());
1768         assertEquals(localPortHigh, receivedApplicationDataAddress.getApplicationData().getLocalPortHigh().getValue()
1769                 .intValue());
1770         assertEquals(remotePortLow, receivedApplicationDataAddress.getApplicationData().getRemotePortLow().getValue()
1771                 .intValue());
1772         assertEquals(remotePortHigh, receivedApplicationDataAddress.getApplicationData().getRemotePortHigh().getValue()
1773                 .intValue());
1774
1775         SimpleAddress ipAddressReceived = receivedApplicationDataAddress.getApplicationData().getAddress();
1776         assertEquals(ipString, ipAddressReceived.getIpAddress().getIpv4Address().getValue());
1777     }
1778
1779     // ------------------- TimeOut Tests -----------
1780
1781     public void mapRequestMapRegisterAndMapRequestTestTimeout() throws SocketTimeoutException {
1782         cleanUP();
1783         Eid eid = LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32");
1784         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
1785         sleepForSeconds(1);
1786         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1787         mapRequestBuilder.setNonce((long) 4);
1788         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.getNoAddressEid()).build());
1789         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
1790         mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(eid).build());
1791         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1792         mapRequestBuilder.getItrRloc().add(
1793                 new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc(ourAddress)).build());
1794
1795         sendMapRequest(mapRequestBuilder.build());
1796         MapReply mapReply = receiveMapReply();
1797         assertEquals(4, mapReply.getNonce().longValue());
1798         assertEquals(0, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
1799
1800         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1801         mapRegisterbuilder.setWantMapNotify(true);
1802         mapRegisterbuilder.setNonce((long) 8);
1803
1804         MappingRecordBuilder etlrBuilder = new MappingRecordBuilder();
1805         etlrBuilder.setEid(eid);
1806         etlrBuilder.setRecordTtl(254);
1807
1808         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1809         recordBuilder.setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1"));
1810         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1811         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1812         mapRegisterbuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1813         mapRegisterbuilder.getMappingRecordItem().add(
1814                 new MappingRecordItemBuilder().setMappingRecord(etlrBuilder.build()).build());
1815
1816         sendMapRegister(mapRegisterbuilder.build());
1817         MapNotify mapNotify = receiveMapNotify();
1818         assertEquals(8, mapNotify.getNonce().longValue());
1819         sleepForSeconds(1);
1820
1821         sendMapRequest(mapRequestBuilder.build());
1822         mapReply = receiveMapReply();
1823         assertEquals(4, mapReply.getNonce().longValue());
1824         assertEquals(recordBuilder.getRloc(), mapReply.getMappingRecordItem().get(0).getMappingRecord()
1825                 .getLocatorRecord().get(0).getRloc());
1826
1827         causeEntryToBeCleaned();
1828         sendMapRequest(mapRequestBuilder.build());
1829         mapReply = receiveMapReply();
1830         assertEquals(0, mapReply.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().size());
1831     }
1832
1833     public void mapRequestMapRegisterAndMapRequestTestNativelyForwardTimeoutResponse() throws Exception {
1834         cleanUP();
1835         Eid eid = LispAddressUtil.asIpv6PrefixEid("1.2.3.4/32");
1836         MapRequest mapRequest = createMapRequest(eid);
1837
1838         testTTLBeforeRegister(mapRequest);
1839
1840         registerForTTL(eid);
1841
1842         testTTLAfterRegister(mapRequest);
1843
1844         causeEntryToBeCleaned();
1845         testTTLAfterClean(mapRequest);
1846
1847         //northboundAddKey();
1848         //testTTLAfterAutherize(mapRequest);
1849
1850     }
1851
1852     private void testTTLAfterClean(MapRequest mapRequest) throws SocketTimeoutException {
1853         MapReply mapReply;
1854         sendMapRequest(mapRequest);
1855         mapReply = receiveMapReply();
1856         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1857     }
1858
1859     private void causeEntryToBeCleaned() {
1860         // TODO XXX for the time being, to keep master and stable/lithium in sync, we need to remove the forceful
1861         // expiration of DAO entries. Once we're past this, we'll have to expose methods to setTimeUnit(TimeUnit)
1862         // and cleanOld() (expired) entries in IFlowMapping (and perhaps ILispDAO) and use them here.
1863         mapService.cleanCachedMappings();
1864     }
1865
1866     private void testTTLAfterRegister(MapRequest mapRequest) throws SocketTimeoutException {
1867         MapReply mapReply;
1868         sendMapRequest(mapRequest);
1869         mapReply = receiveMapReply();
1870         assertEquals(LispAddressUtil.asIpv4Rloc("4.3.2.1"), mapReply.getMappingRecordItem().get(0).getMappingRecord()
1871                 .getLocatorRecord().get(0).getRloc());
1872         assertCorrectMapReplyTTLAndAction(mapReply, 254, Action.NoAction);
1873     }
1874
1875     private void registerForTTL(Eid eid) throws SocketTimeoutException {
1876         MapRegister mapRegister = createMapRegister(eid);
1877         sendMapRegister(mapRegister);
1878         assertMapNotifyReceived();
1879     }
1880
1881     private void testTTLBeforeRegister(MapRequest mapRequest) throws SocketTimeoutException {
1882         MapReply mapReply;
1883         sendMapRequest(mapRequest);
1884         mapReply = receiveMapReply();
1885         assertCorrectMapReplyTTLAndAction(mapReply, 15, Action.NativelyForward);
1886     }
1887 /*
1888     private void testTTLAfterAutherize(MapRequest mapRequest) throws SocketTimeoutException {
1889         MapReply mapReply;
1890         sendMapRequest(mapRequest);
1891         mapReply = receiveMapReply();
1892         assertCorrectMapReplyTTLAndAction(mapReply, 1, Action.NativelyForward);
1893     }
1894 */
1895     private void assertCorrectMapReplyTTLAndAction(MapReply mapReply, int expectedTTL, Action expectedAction) {
1896         assertEquals(expectedTTL, mapReply.getMappingRecordItem().get(0).getMappingRecord().getRecordTtl().intValue());
1897         assertEquals(expectedAction, mapReply.getMappingRecordItem().get(0).getMappingRecord().getAction());
1898     }
1899
1900     private MapRegister createMapRegister(Eid eid, Rloc rloc) {
1901         MapRegisterBuilder mapRegisterbuilder = new MapRegisterBuilder();
1902         mapRegisterbuilder.setWantMapNotify(true);
1903         mapRegisterbuilder.setNonce((long) 8);
1904         mapRegisterbuilder.setKeyId((short) 0);
1905         MappingRecordBuilder etlrBuilder = new MappingRecordBuilder();
1906         etlrBuilder.setEid(eid);
1907         etlrBuilder.setRecordTtl(254);
1908         etlrBuilder.setAuthoritative(false);
1909         etlrBuilder.setAction(Action.NoAction);
1910         LocatorRecordBuilder recordBuilder = new LocatorRecordBuilder();
1911         recordBuilder.setRloc(rloc);
1912         etlrBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
1913         etlrBuilder.getLocatorRecord().add(recordBuilder.build());
1914         mapRegisterbuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
1915         mapRegisterbuilder.getMappingRecordItem().add(
1916                 new MappingRecordItemBuilder().setMappingRecord(etlrBuilder.build()).build());
1917         MapRegister mapRegister = mapRegisterbuilder.build();
1918         return mapRegister;
1919     }
1920
1921     private MapRegister createMapRegister(Eid eid) {
1922         return createMapRegister(eid, LispAddressUtil.asIpv4Rloc("4.3.2.1"));
1923     }
1924
1925     private MapRequest createMapRequest(Eid eid) {
1926         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
1927         mapRequestBuilder.setNonce((long) 4);
1928         mapRequestBuilder.setPitr(false);
1929         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.getNoAddressEid()).build());
1930         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
1931         mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(eid).build());
1932         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
1933         mapRequestBuilder.getItrRloc().add(
1934                 new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc(ourAddress)).build());
1935         MapRequest mr = mapRequestBuilder.build();
1936         return mr;
1937     }
1938
1939     public void testSimpleNonProxy() throws SocketTimeoutException, SocketException {
1940         cleanUP();
1941         String rloc = "127.0.0.3";
1942         int port = LispMessage.PORT_NUM;
1943         Rloc ipRloc = LispAddressUtil.asIpv4Rloc(rloc);
1944         sendProxyMapRequest(rloc, port, ipRloc);
1945
1946     }
1947
1948     public void testNonProxyOtherPort() throws SocketTimeoutException, SocketException {
1949         cleanUP();
1950         String rloc = "127.0.0.3";
1951         int port = 4350;
1952
1953         RlocBuilder rb = new RlocBuilder();
1954         rb.setAddressType(ApplicationDataLcaf.class);
1955         rb.setVirtualNetworkId(null);
1956         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
1957                 .lisp.address.address.ApplicationDataBuilder()
1958                 .setApplicationData(new ApplicationDataBuilder().setAddress(new SimpleAddress(new IpAddress(
1959                 new Ipv4Address(rloc)))).setLocalPortLow(new PortNumber(port)).build()).build());
1960         Rloc adLcaf = rb.build();
1961
1962         LOG.info("testNonProxyOtherPort:" + LispAddressStringifier.getString(adLcaf));
1963         sendProxyMapRequest(rloc, port, adLcaf);
1964
1965     }
1966
1967     private class XtrRequestMappingListener implements OdlLispProtoListener {
1968
1969         @Override
1970         public void onGotMapReply(GotMapReply notification) {
1971         }
1972
1973         @Override
1974         public void onAddMapping(AddMapping notification) {
1975         }
1976
1977         @Override
1978         public void onXtrReplyMapping(XtrReplyMapping notification) {
1979         }
1980
1981         @Override
1982         public void onRequestMapping(RequestMapping notification) {
1983         }
1984
1985         @Override
1986         public void onGotMapNotify(GotMapNotify notification) {
1987         }
1988
1989         @Override
1990         public void onXtrRequestMapping(XtrRequestMapping notification) {
1991         }
1992
1993     }
1994
1995     public void testRecievingNonProxyOnXtrPort() throws SocketTimeoutException, SocketException, Throwable {
1996         cleanUP();
1997         configLispPlugin.shouldListenOnXtrPort(true);
1998         notificationCalled = false;
1999         final String eid = "10.10.10.10/32";
2000         String rloc = "127.0.0.3";
2001         int port = LispMessage.XTR_PORT_NUM;
2002
2003         RlocBuilder rb = new RlocBuilder();
2004         rb.setAddressType(ApplicationDataLcaf.class);
2005         rb.setVirtualNetworkId(null);
2006         rb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
2007                 .lisp.address.address.ApplicationDataBuilder()
2008                 .setApplicationData(new ApplicationDataBuilder().setAddress(new SimpleAddress(new IpAddress(
2009                 new Ipv4Address(rloc)))).setLocalPortLow(new PortNumber(port)).build()).build());
2010         Rloc adLcaf = rb.build();
2011
2012         final MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
2013         ((LispMappingService) lms).getNotificationService().registerNotificationListener(
2014                 new XtrRequestMappingListener() {
2015
2016             @Override
2017             public void onXtrRequestMapping(XtrRequestMapping notification) {
2018                 assertEquals(((Ipv4Prefix) mapRequest.getEidItem().get(0).getEid().getAddress())
2019                         .getIpv4Prefix().getValue(), eid);
2020                 notificationCalled = true;
2021                 LOG.warn("notification arrived");
2022             }
2023         });
2024         sendMapRequest(mapRequest, port);
2025         for (int i = 0; i < MAX_NOTIFICATION_RETRYS; i++) {
2026             if (notificationCalled) {
2027                 return;
2028             } else {
2029                 LOG.warn("notification hasn't arrived, sleeping...");
2030                 Thread.sleep(500);
2031             }
2032         }
2033
2034         fail("Notification hasn't arrived");
2035
2036     }
2037
2038     private void sendProxyMapRequest(String rloc, int port, Rloc adLcaf) throws SocketTimeoutException,
2039             SocketException {
2040         String eid = "10.1.0.1/32";
2041         MapRequest mapRequest = createNonProxyMapRequest(eid, adLcaf);
2042         sendMapRequest(mapRequest);
2043         DatagramSocket nonProxySocket = new DatagramSocket(new InetSocketAddress(rloc, port));
2044         MapRequest receivedMapRequest = receiveMapRequest(nonProxySocket);
2045         assertEquals(mapRequest.getNonce(), receivedMapRequest.getNonce());
2046         assertEquals(mapRequest.getSourceEid(), receivedMapRequest.getSourceEid());
2047         assertEquals(mapRequest.getItrRloc(), receivedMapRequest.getItrRloc());
2048         assertEquals(mapRequest.getEidItem(), receivedMapRequest.getEidItem());
2049         nonProxySocket.close();
2050     }
2051
2052     private MapRequest createNonProxyMapRequest(String eid, Rloc adLcaf) throws SocketTimeoutException {
2053         MapRegister mr = createMapRegister(LispAddressUtil.asIpv4PrefixEid(eid));
2054         LocatorRecord record = new LocatorRecordBuilder(mr.getMappingRecordItem().get(0).getMappingRecord()
2055                 .getLocatorRecord().get(0)).setRloc(adLcaf).build();
2056         mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().set(0, record);
2057         sendMapRegister(mr);
2058         assertMapNotifyReceived();
2059         MapRequest mapRequest = createMapRequest(LispAddressUtil.asIpv4PrefixEid(eid));
2060         MapRequestBuilder builder = new MapRequestBuilder(mapRequest);
2061         builder.setPitr(true);
2062         mapRequest = builder.build();
2063         return mapRequest;
2064     }
2065
2066     private void assertMapNotifyReceived() throws SocketTimeoutException {
2067         receiveMapNotify();
2068     }
2069
2070     private MapReply receiveMapReply() throws SocketTimeoutException {
2071         return MapReplySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
2072     }
2073
2074     private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
2075         return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(
2076                 datagramSocket, 30000).getData()));
2077     }
2078
2079     private MapNotify receiveMapNotify() throws SocketTimeoutException {
2080         return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
2081     }
2082
2083     private void sendMapRequest(MapRequest mapRequest) {
2084         sendMapRequest(mapRequest, LispMessage.PORT_NUM);
2085     }
2086
2087     private void sendMapRequest(MapRequest mapRequest, int port) {
2088         sendPacket(MapRequestSerializer.getInstance().serialize(mapRequest).array(), port);
2089     }
2090
2091     private void sendMapRegister(MapRegister mapRegister) {
2092         sendPacket(MapRegisterSerializer.getInstance().serialize(mapRegister).array());
2093     }
2094
2095     private void sendPacket(byte[] bytesToSend) {
2096         sendPacket(bytesToSend, LispMessage.PORT_NUM);
2097     }
2098
2099     private void sendPacket(byte[] bytesToSend, int port) {
2100         try {
2101             DatagramPacket packet = new DatagramPacket(bytesToSend, bytesToSend.length);
2102             initPacketAddress(packet, port);
2103             LOG.trace("Sending packet to LispPlugin on socket, port {}", port);
2104             socket.send(packet);
2105         } catch (Throwable t) {
2106             fail();
2107         }
2108     }
2109
2110     private DatagramPacket receivePacket() throws SocketTimeoutException {
2111         return receivePacket(6000);
2112     }
2113
2114     private DatagramPacket receivePacket(int timeout) throws SocketTimeoutException {
2115         return receivePacket(socket, timeout);
2116     }
2117
2118     private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException {
2119         try {
2120             byte[] buffer = new byte[4096];
2121             DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
2122             LOG.trace("Waiting for packet from socket...");
2123             receivedSocket.setSoTimeout(timeout);
2124             receivedSocket.receive(receivePacket);
2125             LOG.trace("Received packet from socket!");
2126             return receivePacket;
2127         } catch (SocketTimeoutException ste) {
2128             throw ste;
2129         } catch (Throwable t) {
2130             fail();
2131             return null;
2132         }
2133     }
2134
2135     private void sleepForSeconds(int seconds) {
2136         try {
2137             Thread.sleep(seconds*1000);
2138         } catch (InterruptedException e) {
2139             LOG.warn("Interrupted while sleeping", e);
2140         }
2141     }
2142
2143     private void initPacketAddress(DatagramPacket packet, int port) throws UnknownHostException {
2144         packet.setAddress(InetAddress.getByName(lispBindAddress));
2145         packet.setPort(port);
2146     }
2147
2148     private DatagramSocket initSocket(DatagramSocket socket, int port) {
2149         try {
2150             socket = new DatagramSocket(new InetSocketAddress(ourAddress, port));
2151         } catch (SocketException e) {
2152             e.printStackTrace();
2153             fail();
2154         }
2155         return socket;
2156     }
2157
2158     private byte[] extractWSUdpByteArray(String wiresharkHex) {
2159         final int HEADER_LEN = 42;
2160         byte[] res = new byte[1000];
2161         String[] split = wiresharkHex.split(" ");
2162         int counter = 0;
2163         for (String cur : split) {
2164             cur = cur.trim();
2165             if (cur.length() == 2) {
2166                 ++counter;
2167                 if (counter > HEADER_LEN) {
2168                     res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
2169                 }
2170
2171             }
2172         }
2173         return Arrays.copyOf(res, counter - HEADER_LEN);
2174     }
2175
2176     private String stateToString(int state) {
2177         switch (state) {
2178         case Bundle.ACTIVE:
2179             return "ACTIVE";
2180         case Bundle.INSTALLED:
2181             return "INSTALLED";
2182         case Bundle.RESOLVED:
2183             return "RESOLVED";
2184         case Bundle.UNINSTALLED:
2185             return "UNINSTALLED";
2186         default:
2187             return "Not CONVERTED";
2188         }
2189     }
2190
2191     private void areWeReady() throws InvalidSyntaxException {
2192         sleepForSeconds(5);
2193
2194         assertNotNull(bc);
2195         boolean debugit = false;
2196         Bundle b[] = bc.getBundles();
2197         for (Bundle element : b) {
2198             int state = element.getState();
2199             LOG.trace("Bundle[" + element.getBundleId() + "]:" + element.getSymbolicName() + ",v"
2200                     + element.getVersion() + ", state:" + stateToString(state));
2201             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
2202                 LOG.debug("Bundle:" + element.getSymbolicName() + " state:" + stateToString(state));
2203
2204                 // try {
2205                 // String host = element.getHeaders().get("FRAGMENT-HOST");
2206                 // if (host != null) {
2207                 // LOG.warn("Bundle " + element.getSymbolicName() +
2208                 // " is a fragment which is part of: " + host);
2209                 // LOG.warn("Required imports are: " +
2210                 // element.getHeaders().get("IMPORT-PACKAGE"));
2211                 // } else {
2212                 // element.start();
2213                 // }
2214                 // } catch (BundleException e) {
2215                 // LOG.error("BundleException:", e);
2216                 // fail();
2217                 // }
2218
2219                 debugit = true;
2220
2221             }
2222         }
2223         if (debugit) {
2224             LOG.warn(("Do some debugging because some bundle is unresolved"));
2225         }
2226         // assertNotNull(broker);
2227
2228         configLispPlugin.setLispAddress(lispBindAddress);
2229
2230         // Uncomment this code to Know which services were actually loaded to
2231         // BundleContext
2232
2233         /*
2234         for (ServiceReference sr : bc.getAllServiceReferences(null, null)) {
2235             LOG.info(sr.getBundle().getSymbolicName());
2236             LOG.info(sr.toString());
2237         }
2238         */
2239
2240         sleepForSeconds(1);
2241     }
2242
2243     private void cleanUP() {
2244         after();
2245         mapService.cleanCachedMappings();
2246         configLispPlugin.shouldListenOnXtrPort(false);
2247         socket = initSocket(socket, LispMessage.PORT_NUM);
2248
2249     }
2250
2251     private void restartSocket() {
2252         after();
2253         socket = initSocket(socket, LispMessage.PORT_NUM);
2254     }
2255
2256 }