2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.implementation;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.Lists;
15 import java.lang.reflect.Field;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.Arrays;
19 import java.util.Date;
20 import java.util.EnumMap;
21 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.lispflowmapping.config.ConfigIni;
32 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
33 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
34 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
35 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
36 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
37 import org.opendaylight.lispflowmapping.interfaces.mapcache.IAuthKeyDb;
38 import org.opendaylight.lispflowmapping.interfaces.mapcache.ILispMapCache;
39 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
40 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
41 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
42 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
43 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
44 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
45 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
58 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;
59 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;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.container.MappingRecord;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.container.MappingRecordBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyKey;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingKey;
80 import org.opendaylight.yangtools.yang.common.Uint16;
81 import org.opendaylight.yangtools.yang.common.Uint32;
82 import org.opendaylight.yangtools.yang.common.Uint8;
83 import org.powermock.api.mockito.PowerMockito;
84 import org.powermock.core.classloader.annotations.PrepareForTest;
85 import org.powermock.modules.junit4.PowerMockRunner;
86 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
88 @RunWith(PowerMockRunner.class)
89 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
90 @PrepareForTest(MappingMergeUtil.class)
91 public class MappingSystemTest {
93 private static ILispDAO dao = new HashMapDb();
94 @Mock private static ILispMapCache smcMock;
95 @Mock private static IMapCache pmcMock;
96 @Mock private static IAuthKeyDb akdbMock;
97 @Mock private static DataStoreBackEnd dsbeMock;
98 @Mock private static NotificationPublishService npsMock;
99 @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
100 @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(dao, false, npsMock, true);
102 private static final String IPV4_SRC = "127.0.0.1";
103 private static final String IPV4_DST = "192.168.0.1";
104 private static final String IPV4_STRING_1 = "1.2.3.0";
105 private static final String IPV4_STRING_2 = "1.2.4.0";
106 private static final String IPV4_STRING_3 = "1.2.5.0";
107 private static final String MASK = "/20";
109 private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
110 private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
111 private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
112 private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
113 private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
114 // with index out of bounds
115 private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
116 .setAddress(getDefaultServicePath((short) 200)).build();
118 public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
119 public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
120 public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
122 private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
123 private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
124 private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
126 private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
127 .setKeyType(Uint16.ONE).setKeyString("pass-1");
128 private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
129 private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
130 private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
131 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
132 private static final Object DUMMY_OBJECT = "dummy-object";
133 private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
134 private static final XtrId XTR_ID = new XtrId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7,
135 8, 9, 10, 11, 12, 13, 14, 15});
138 public void init() throws Exception {
139 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
140 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
143 setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
147 * This method injects mocks which can not be injected the usual way using Mockito.
149 private static void injectMocks() throws NoSuchFieldException, IllegalAccessException {
150 final Field smcField = MappingSystem.class.getDeclaredField("smc");
151 smcField.setAccessible(true);
152 smcField.set(mappingSystem, smcMock);
154 final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
155 pmcField.setAccessible(true);
156 pmcField.set(mappingSystem, pmcMock);
158 final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
159 tableMapField.setAccessible(true);
160 tableMapField.set(mappingSystem, tableMapMock);
164 * This method changes the lookup policy.
166 private static void setLookupPolicy(final IMappingService.LookupPolicy policy) throws NoSuchFieldException,
167 IllegalAccessException {
168 final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
169 lookupPolicy.setAccessible(true);
170 lookupPolicy.set(CONFIG_INI, policy);
174 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
177 public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
178 final MappingData mappingData = getDefaultMappingData();
179 mappingData.setTimestamp(EXPIRED_DATE);
180 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
181 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData, null);
183 final Mapping mapping = new MappingBuilder()
184 .setEidUri(new EidUri("ipv4:" + IPV4_DST))
185 .setOrigin(MappingOrigin.Southbound)
186 .setSiteId(Set.of(SITE_ID))
187 .setMappingRecord(mappingData.getRecord()).build();
189 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
190 Mockito.verify(dsbeMock).removeMapping(mapping);
194 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
195 * Returns the original mapping.
198 public void getMappingTest_NbFirst_withNullNbMapping() {
199 final MappingData mappingData = getDefaultMappingData();
200 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
201 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
203 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
207 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
208 * Returns the original mapping.
211 public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
212 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
213 .setLocatorRecord(Lists.newArrayList(
215 getDefaultLocatorRecordBuilder().build(),
216 getDefaultLocatorRecordBuilder().build())).build();
217 final MappingData mappingData = getDefaultMappingData(mappingRecord);
218 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
220 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
224 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
225 * record. Returns the original mapping.
228 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
229 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
230 .setLocatorRecord(Lists.newArrayList(
232 getDefaultLocatorRecordBuilder().build())).build();
233 final MappingData mappingData = getDefaultMappingData(mappingRecord);
234 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
236 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
240 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
241 * type locator record. Service index out of bounds. Returns the original mapping.
244 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
245 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
246 .setLocatorRecord(Lists.newArrayList(
247 getDefaultLocatorRecordBuilder()
248 .setRloc(getELPTypeRloc()).build())).build();
249 final MappingData mappingData = getDefaultMappingData(mappingRecord);
250 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingData);
252 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
256 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
257 * type locator record.
260 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
261 throws UnknownHostException {
262 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
263 .setLocatorRecord(Lists.newArrayList(
264 getDefaultLocatorRecordBuilder()
265 .setRloc(getELPTypeRloc()).build())).build();
266 final MappingData mappingData = getDefaultMappingData(mappingRecord);
267 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
269 final MappingData result = mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
270 final Ipv4Binary ipv4Result = (Ipv4Binary) result.getRecord().getLocatorRecord().get(0).getRloc().getAddress();
272 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
276 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
277 * record. Returns the original mapping.
280 public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
281 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
282 .setLocatorRecord(Lists.newArrayList(
283 getDefaultLocatorRecordBuilder()
284 .setRloc(getIpPrefixTypeRloc()).build())).build();
285 final MappingData mappingData = getDefaultMappingData(mappingRecord);
286 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
288 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
292 * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
295 public void getMappingTest_NbFirst() {
296 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
297 .setLocatorRecord(Lists.newArrayList(
298 getDefaultLocatorRecordBuilder().build())).build();
299 final MappingData mappingData = getDefaultMappingData(mappingRecord);
300 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingData);
302 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
306 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
309 public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
310 IllegalAccessException {
311 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
312 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
314 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
318 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
319 * address and single Ipv4 type locator record. Returns the original mapping.
322 public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
323 IllegalAccessException {
324 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
325 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
326 .setLocatorRecord(Lists.newArrayList(
328 getDefaultLocatorRecordBuilder().build())).build();
329 final MappingData mappingData = getDefaultMappingData(mappingRecord);
330 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
332 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
336 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
337 * locator, southbound null. Returns the original mapping.
340 public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
341 IllegalAccessException {
342 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
344 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
345 .setLocatorRecord(Lists.newArrayList(
347 getDefaultLocatorRecordBuilder().build())).build();
348 MappingData nbMappingData = getDefaultMappingData(mappingRecord);
349 MappingData sbMappingData = getDefaultMappingData(mappingRecord);
350 sbMappingData.setTimestamp(EXPIRED_DATE);
352 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
353 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData, null);
355 final Mapping mapping = new MappingBuilder()
356 .setEidUri(new EidUri("ipv4:" + IPV4_DST))
357 .setOrigin(MappingOrigin.Southbound)
358 .setSiteId(Set.of(SITE_ID))
359 .setMappingRecord(mappingRecord).build();
361 assertEquals(nbMappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
362 Mockito.verify(dsbeMock).removeMapping(mapping);
366 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
367 * locator, southbound non-null. Returns the merged mapping.
370 public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
371 IllegalAccessException {
372 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
374 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
375 .setLocatorRecord(Lists.newArrayList(
376 getDefaultLocatorRecordBuilder().build())).build();
377 final MappingData nbMappingData = getDefaultMappingData(mappingRecord);
378 final MappingData sbMappingData = getDefaultMappingData(mappingRecord);
380 // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
381 final MappingData resultMock = Mockito.mock(MappingData.class);
383 PowerMockito.mockStatic(MappingMergeUtil.class);
384 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
385 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData);
386 PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData))
387 .thenReturn(resultMock);
389 assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
393 * Tests {@link MappingSystem#getMapping(Eid dst)} method.
396 public void getMappingTest_withSrcNull() {
397 final MappingData mappingData = getDefaultMappingData();
398 Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
399 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
401 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_DST));
405 * Tests {@link MappingSystem#getMapping(Eid src, Eid dst, XtrId xtrId)} method.
408 public void getMappingTest_withXtrId() {
409 final MappingData mappingData = getDefaultMappingData();
410 mappingData.setXtrId(XTR_ID);
412 Mockito.when(smcMock.getMapping(EID_IPV4_DST, XTR_ID)).thenReturn(mappingData);
414 assertEquals(mappingData, mappingSystem.getMapping(null, EID_IPV4_DST, XTR_ID));
418 * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
421 public void getMappingTest_withMappingOrigin() {
422 final MappingData mappingData = getDefaultMappingData();
423 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, (XtrId) null)).thenReturn(mappingData);
425 mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
426 Mockito.verify(smcMock).getMapping(EID_IPV4_SRC, (XtrId) null);
428 mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
429 Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
433 * Tests {@link MappingSystem#removeMapping} method.
436 public void removeMappingTest_nb() {
437 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
439 mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
440 Mockito.verify(pmcMock).removeMapping(EID_IPV4_1);
444 * Tests {@link MappingSystem#removeMapping} method.
447 public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
448 mappingSystem = new MappingSystem(dao, false, npsMock, false);
450 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
452 mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
453 Mockito.verify(smcMock).removeMapping(EID_IPV4_1);
457 * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
460 public void restoreDaoFromDatastoreTest() {
461 final Mapping mapping_1 = new MappingBuilder()
462 .withKey(new MappingKey(new EidUri(LispAddressStringifier.getURIString(EID_IPV4_1)),
463 MappingOrigin.Northbound))
464 .setOrigin(MappingOrigin.Northbound)
465 .setMappingRecord(getDefaultMappingRecordBuilder()
466 .setEid(EID_IPV4_1).build()).build();
467 final Mapping mapping_2 = new MappingBuilder()
468 .withKey(new MappingKey(new EidUri(LispAddressStringifier.getURIString(EID_IPV4_2)),
469 MappingOrigin.Northbound))
470 .setOrigin(MappingOrigin.Northbound)
471 .setMappingRecord(getDefaultMappingRecordBuilder()
472 .setEid(EID_IPV4_2).build()).build();
474 final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
475 final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
476 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
477 .setMappingAuthkey(mappingAuthkey_1)
478 .setEid(EID_IPV4_1).build();
480 final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
481 final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
482 .withKey(new AuthenticationKeyKey(new EidUri("uri-2")))
483 .setMappingAuthkey(mappingAuthkey_2)
484 .setEid(EID_IPV4_2).build();
486 final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
487 final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
489 Mockito.when(dsbeMock.getLastUpdateTimestamp()).thenReturn(System.currentTimeMillis());
490 Mockito.when(dsbeMock.getAllMappings(LogicalDatastoreType.CONFIGURATION)).thenReturn(mappings);
491 Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
492 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
494 mappingSystem.initialize();
496 ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
497 Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_1), captor.capture());
498 assertEquals(captor.getValue().getRecord(), mapping_1.getMappingRecord());
499 Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_2), captor.capture());
500 assertEquals(captor.getValue().getRecord(), mapping_2.getMappingRecord());
501 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
502 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
506 * Tests {@link MappingSystem#addAuthenticationKey} method.
509 public void addAuthenticationKeyTest() {
510 mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
511 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
515 * Tests {@link MappingSystem#getAuthenticationKey} method.
518 public void getAuthenticationKeyTest() {
519 Mockito.when(akdbMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
520 assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
524 * Tests {@link MappingSystem#removeAuthenticationKey} method.
527 public void removeAuthenticationKeyTest() {
528 mappingSystem.removeAuthenticationKey(EID_IPV4_1);
529 Mockito.verify(akdbMock).removeAuthenticationKey(EID_IPV4_1);
533 * Tests {@link MappingSystem#addData} method.
536 public void addDataTest() {
537 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
539 mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
540 Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
544 * Tests {@link MappingSystem#getData} method.
547 public void getDataTest() {
548 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
549 Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
551 assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
555 * Tests {@link MappingSystem#removeData} method.
558 public void removeDataTest() {
559 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
561 mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
562 Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
566 * Following test are executed for coverage-increase purpose.
569 public void otherTests() throws NoSuchFieldException, IllegalAccessException {
570 mappingSystem.printMappings();
571 Mockito.verify(pmcMock).printMappings();
572 Mockito.verify(smcMock).printMappings();
574 mappingSystem.destroy();
575 mappingSystem = new MappingSystem(dao, true, npsMock, true);
576 mappingSystem.setDataStoreBackEnd(dsbeMock);
577 mappingSystem.setMappingMerge(false);
578 mappingSystem.setIterateMask(true);
581 private static MappingData getDefaultMappingData() {
582 return getDefaultMappingData(null);
585 private static MappingData getDefaultMappingData(MappingRecord mappingRecord) {
586 if (mappingRecord == null) {
587 mappingRecord = getDefaultMappingRecordBuilder().build();
589 return new MappingData(mappingRecord, System.currentTimeMillis());
592 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
593 return new MappingRecordBuilder()
595 .setEid(EID_IPV4_DST);
598 private static ServicePath getDefaultServicePath(final short index) {
599 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
600 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
601 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
603 return new ServicePathBuilder().setServicePath(servicePathBuilder
604 .setServiceIndex(Uint8.valueOf(index))
605 .setServicePathId(new ServicePathIdType(Uint32.ONE)).build())
609 private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
610 return new LocatorRecordBuilder()
611 .setLocatorId("locator-id")
615 private static Rloc getELPTypeRloc() {
617 final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
618 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
619 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
620 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
622 return new RlocBuilder()
623 .setAddress(explicitLocatorPath)
624 .setAddressType(ExplicitLocatorPathLcaf.VALUE).build();
627 private static Rloc getIpPrefixTypeRloc() {
629 final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
630 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
632 return new RlocBuilder()
634 .setAddressType(Ipv4PrefixAfi.VALUE).build();