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