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;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.runners.MockitoJUnitRunner;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
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.LispAddressUtil;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
55 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;
56 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;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
75 import org.powermock.api.mockito.PowerMockito;
76 import org.powermock.core.classloader.annotations.PrepareForTest;
77 import org.powermock.modules.junit4.PowerMockRunner;
78 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
80 @RunWith(PowerMockRunner.class)
81 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
82 @PrepareForTest(MappingMergeUtil.class)
83 public class MappingSystemTest {
85 private static ILispDAO daoMock = Mockito.mock(ILispDAO.class);
86 @Mock private static ILispMapCache smcMock;
87 @Mock private static IMapCache pmcMock;
88 @Mock private static IAuthKeyDb akdbMock;
89 @Mock private static DataStoreBackEnd dsbeMock;
90 @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
91 @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(daoMock, false, true, true);
93 private static final String IPV4_SRC = "127.0.0.1";
94 private static final String IPV4_DST = "192.168.0.1";
95 private static final String IPV4_STRING_1 = "1.2.3.0";
96 private static final String IPV4_STRING_2 = "1.2.4.0";
97 private static final String IPV4_STRING_3 = "1.2.5.0";
98 private static final String MASK = "/20";
100 private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
101 private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
102 private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
103 private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
104 private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
105 private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
106 .setAddress(getDefaultServicePath((short) 200)).build(); // with index out of bounds
108 public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
109 public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
110 public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
112 private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
113 private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
114 private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
116 private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
117 .setKeyType(1).setKeyString("pass-1");
118 private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
119 private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
120 private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
121 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
122 private static final Object DUMMY_OBJECT = "dummy-object";
123 private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
124 private static final XtrId XTR_ID = new XtrId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7,
125 8, 9, 10, 11, 12, 13, 14, 15});
128 public void init() throws Exception {
129 Mockito.when(daoMock.putTable(MappingOrigin.Southbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
130 Mockito.when(daoMock.putTable(MappingOrigin.Northbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
132 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
133 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
136 setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
140 * This method injects mocks which can not be injected the usual way using Mockito.
142 private static void injectMocks() throws NoSuchFieldException, IllegalAccessException {
143 final Field smcField = MappingSystem.class.getDeclaredField("smc");
144 smcField.setAccessible(true);
145 smcField.set(mappingSystem, smcMock);
147 final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
148 pmcField.setAccessible(true);
149 pmcField.set(mappingSystem, pmcMock);
151 final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
152 tableMapField.setAccessible(true);
153 tableMapField.set(mappingSystem, tableMapMock);
157 * This method changes the lookup policy.
159 private static void setLookupPolicy(IMappingService.LookupPolicy policy) throws NoSuchFieldException,
160 IllegalAccessException {
161 final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
162 lookupPolicy.setAccessible(true);
163 lookupPolicy.set(CONFIG_INI, policy);
167 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
170 public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
171 final MappingData mappingData = getDefaultMappingData();
172 mappingData.setTimestamp(EXPIRED_DATE);
173 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
174 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
176 final Mapping mapping = new MappingBuilder()
177 .setEidUri(new EidUri("ipv4:1.2.3.0"))
178 .setOrigin(MappingOrigin.Southbound)
179 .setSiteId(Lists.newArrayList(SITE_ID))
180 .setMappingRecord(mappingData.getRecord()).build();
182 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
183 Mockito.verify(dsbeMock).removeMapping(mapping);
187 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
188 * Returns the original mapping.
191 public void getMappingTest_NbFirst_withNullNbMapping() {
192 final MappingData mappingData = getDefaultMappingData();
193 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
194 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
196 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
200 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
201 * Returns the original mapping.
204 public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
205 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
206 .setLocatorRecord(Lists.newArrayList(
207 getDefaultLocatorRecordBuilder().build(), // set two locators
208 getDefaultLocatorRecordBuilder().build())).build();
209 final MappingData mappingData = getDefaultMappingData(mappingRecord);
210 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
212 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
216 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
217 * record. Returns the original mapping.
220 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
221 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
222 .setLocatorRecord(Lists.newArrayList(
223 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
224 final MappingData mappingData = getDefaultMappingData(mappingRecord);
225 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
227 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
231 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
232 * type locator record. Service index out of bounds. Returns the original mapping.
235 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
236 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
237 .setLocatorRecord(Lists.newArrayList(
238 getDefaultLocatorRecordBuilder()
239 .setRloc(getELPTypeRloc()).build())).build();
240 final MappingData mappingData = getDefaultMappingData(mappingRecord);
241 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingData);
243 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
247 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
248 * type locator record.
251 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
252 throws UnknownHostException {
253 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
254 .setLocatorRecord(Lists.newArrayList(
255 getDefaultLocatorRecordBuilder()
256 .setRloc(getELPTypeRloc()).build())).build();
257 final MappingData mappingData = getDefaultMappingData(mappingRecord);
258 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
260 final MappingData result = (MappingData) mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
261 final Ipv4Binary ipv4Result = (Ipv4Binary) result.getRecord().getLocatorRecord().get(0).getRloc().getAddress();
263 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
267 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
268 * record. Returns the original mapping.
271 public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
272 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
273 .setLocatorRecord(Lists.newArrayList(
274 getDefaultLocatorRecordBuilder()
275 .setRloc(getIpPrefixTypeRloc()).build())).build();
276 final MappingData mappingData = getDefaultMappingData(mappingRecord);
277 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
279 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
283 * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
286 public void getMappingTest_NbFirst() {
287 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
288 .setLocatorRecord(Lists.newArrayList(
289 getDefaultLocatorRecordBuilder().build())).build();
290 final MappingData mappingData = getDefaultMappingData(mappingRecord);
291 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingData);
293 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
297 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
300 public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
301 IllegalAccessException {
302 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
303 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
305 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
309 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
310 * address and single Ipv4 type locator record. Returns the original mapping.
313 public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
314 IllegalAccessException {
315 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
316 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
317 .setLocatorRecord(Lists.newArrayList(
318 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
319 final MappingData mappingData = getDefaultMappingData(mappingRecord);
320 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
322 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
326 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
327 * locator, southbound null. Returns the original mapping.
330 public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
331 IllegalAccessException {
332 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
334 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
335 .setLocatorRecord(Lists.newArrayList(
336 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
337 MappingData nbMappingData = getDefaultMappingData(mappingRecord);
338 MappingData sbMappingData = getDefaultMappingData(mappingRecord);
339 sbMappingData.setTimestamp(EXPIRED_DATE);
341 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
342 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData);
344 final Mapping mapping = new MappingBuilder()
345 .setEidUri(new EidUri("ipv4:1.2.3.0"))
346 .setOrigin(MappingOrigin.Southbound)
347 .setSiteId(Lists.newArrayList(SITE_ID))
348 .setMappingRecord(mappingRecord).build();
350 assertEquals(nbMappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
351 Mockito.verify(dsbeMock).removeMapping(mapping);
355 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
356 * locator, southbound non-null. Returns the merged mapping.
359 public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
360 IllegalAccessException {
361 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
363 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
364 .setLocatorRecord(Lists.newArrayList(
365 getDefaultLocatorRecordBuilder().build())).build();
366 final MappingData nbMappingData = getDefaultMappingData(mappingRecord);
367 final MappingData sbMappingData = getDefaultMappingData(mappingRecord);
369 // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
370 final MappingData resultMock = Mockito.mock(MappingData.class);
372 PowerMockito.mockStatic(MappingMergeUtil.class);
373 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
374 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData);
375 PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData))
376 .thenReturn(resultMock);
378 assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
382 * Tests {@link MappingSystem#getMapping(Eid dst)} method.
385 public void getMappingTest_withSrcNull() {
386 final MappingData mappingData = getDefaultMappingData();
387 Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
388 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
390 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_DST));
394 * Tests {@link MappingSystem#getMapping(Eid src, Eid dst, XtrId xtrId)} method.
397 public void getMappingTest_withXtrId() {
398 final MappingData mappingData = getDefaultMappingData();
399 mappingData.setXtrId(XTR_ID);
401 Mockito.when(smcMock.getMapping(EID_IPV4_DST, XTR_ID)).thenReturn(mappingData);
403 assertEquals(mappingData, mappingSystem.getMapping(null, EID_IPV4_DST, XTR_ID));
407 * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
410 public void getMappingTest_withMappingOrigin() {
411 final MappingData mappingData = getDefaultMappingData();
412 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, (XtrId) null)).thenReturn(mappingData);
414 mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
415 Mockito.verify(smcMock).getMapping(EID_IPV4_SRC, (XtrId) null);
417 mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
418 Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
422 * Tests {@link MappingSystem#removeMapping} method.
425 public void removeMappingTest_nb() {
426 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
428 mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
429 Mockito.verify(pmcMock).removeMapping(EID_IPV4_1);
433 * Tests {@link MappingSystem#removeMapping} method.
436 public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
437 mappingSystem = new MappingSystem(daoMock, false, true, false);
439 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
441 mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
442 Mockito.verify(smcMock).removeMapping(EID_IPV4_1);
446 * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
449 public void restoreDaoFromDatastoreTest() {
450 final Mapping mapping_1 = new MappingBuilder()
451 .setOrigin(MappingOrigin.Northbound)
452 .setMappingRecord(getDefaultMappingRecordBuilder()
453 .setEid(EID_IPV4_1).build()).build();
454 final Mapping mapping_2 = new MappingBuilder()
455 .setOrigin(MappingOrigin.Northbound)
456 .setMappingRecord(getDefaultMappingRecordBuilder()
457 .setEid(EID_IPV4_2).build()).build();
459 final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
460 final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
461 .setMappingAuthkey(mappingAuthkey_1)
462 .setEid(EID_IPV4_1).build();
464 final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
465 final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
466 .setMappingAuthkey(mappingAuthkey_2)
467 .setEid(EID_IPV4_2).build();
469 final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
470 final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
472 Mockito.when(dsbeMock.getLastUpdateTimestamp()).thenReturn(System.currentTimeMillis());
473 Mockito.when(dsbeMock.getAllMappings(LogicalDatastoreType.CONFIGURATION)).thenReturn(mappings);
474 Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
475 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
477 mappingSystem.initialize();
479 ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
480 Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_1), captor.capture());
481 assertEquals(captor.getValue().getRecord(), mapping_1.getMappingRecord());
482 Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_2), captor.capture());
483 assertEquals(captor.getValue().getRecord(), mapping_2.getMappingRecord());
484 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
485 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
489 * Tests {@link MappingSystem#addAuthenticationKey} method.
492 public void addAuthenticationKeyTest() {
493 mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
494 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
498 * Tests {@link MappingSystem#getAuthenticationKey} method.
501 public void getAuthenticationKeyTest() {
502 Mockito.when(akdbMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
503 assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
507 * Tests {@link MappingSystem#removeAuthenticationKey} method.
510 public void removeAuthenticationKeyTest() {
511 mappingSystem.removeAuthenticationKey(EID_IPV4_1);
512 Mockito.verify(akdbMock).removeAuthenticationKey(EID_IPV4_1);
516 * Tests {@link MappingSystem#addData} method.
519 public void addDataTest() {
520 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
522 mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
523 Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
527 * Tests {@link MappingSystem#getData} method.
530 public void getDataTest() {
531 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
532 Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
534 assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
538 * Tests {@link MappingSystem#removeData} method.
541 public void removeDataTest() {
542 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
544 mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
545 Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
549 * Tests {@link MappingSystem#cleanCaches} method.
552 public void cleanCachesTest() {
553 mappingSystem.cleanCaches();
554 Mockito.verify(daoMock).removeAll();
558 * Following test are executed for coverage-increase purpose.
561 public void otherTests() throws NoSuchFieldException, IllegalAccessException {
562 mappingSystem.printMappings();
563 Mockito.verify(pmcMock).printMappings();
564 Mockito.verify(smcMock).printMappings();
566 mappingSystem.destroy();
567 mappingSystem = new MappingSystem(daoMock, true, true, true);
568 mappingSystem.setDataStoreBackEnd(dsbeMock);
569 mappingSystem.setMappingMerge(false);
570 mappingSystem.setIterateMask(true);
573 private static MappingData getDefaultMappingData() {
574 return getDefaultMappingData(null);
577 private static MappingData getDefaultMappingData(MappingRecord mappingRecord) {
578 if (mappingRecord == null) {
579 mappingRecord = getDefaultMappingRecordBuilder().build();
581 return new MappingData(mappingRecord, System.currentTimeMillis());
584 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
585 return new MappingRecordBuilder()
590 private static ServicePath getDefaultServicePath(short index) {
591 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
592 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
593 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
595 return new ServicePathBuilder().setServicePath(servicePathBuilder
596 .setServiceIndex(index)
597 .setServicePathId(new ServicePathIdType(1L)).build())
601 private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
602 return new LocatorRecordBuilder()
603 .setLocatorId("locator-id")
607 private static Rloc getELPTypeRloc() {
609 final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
610 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
611 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
612 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
614 return new RlocBuilder()
615 .setAddress(explicitLocatorPath)
616 .setAddressType(ExplicitLocatorPathLcaf.class).build();
619 private static Rloc getIpPrefixTypeRloc() {
621 final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
622 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
624 return new RlocBuilder()
626 .setAddressType(Ipv4PrefixAfi.class).build();