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