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