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.junit.MockitoJUnitRunner;
30 import org.opendaylight.lispflowmapping.config.ConfigIni;
31 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
32 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
33 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
34 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
35 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
36 import org.opendaylight.lispflowmapping.interfaces.mapcache.IAuthKeyDb;
37 import org.opendaylight.lispflowmapping.interfaces.mapcache.ILispMapCache;
38 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
39 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
40 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
41 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
42 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
43 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
44 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
57 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;
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.HopBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
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.mapping.authkey.container.MappingAuthkey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingKey;
79 import org.opendaylight.yangtools.yang.common.Uint16;
80 import org.opendaylight.yangtools.yang.common.Uint32;
81 import org.opendaylight.yangtools.yang.common.Uint8;
82 import org.powermock.api.mockito.PowerMockito;
83 import org.powermock.core.classloader.annotations.PrepareForTest;
84 import org.powermock.modules.junit4.PowerMockRunner;
85 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
87 @RunWith(PowerMockRunner.class)
88 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
89 @PrepareForTest(MappingMergeUtil.class)
90 public class MappingSystemTest {
92 private static ILispDAO dao = new HashMapDb();
93 @Mock private static ILispMapCache smcMock;
94 @Mock private static IMapCache pmcMock;
95 @Mock private static IAuthKeyDb akdbMock;
96 @Mock private static DataStoreBackEnd dsbeMock;
97 @Mock private static NotificationPublishService npsMock;
98 @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
99 @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(dao, false, npsMock, true);
101 private static final String IPV4_SRC = "127.0.0.1";
102 private static final String IPV4_DST = "192.168.0.1";
103 private static final String IPV4_STRING_1 = "1.2.3.0";
104 private static final String IPV4_STRING_2 = "1.2.4.0";
105 private static final String IPV4_STRING_3 = "1.2.5.0";
106 private static final String MASK = "/20";
108 private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
109 private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
110 private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
111 private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
112 private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
113 // with index out of bounds
114 private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
115 .setAddress(getDefaultServicePath((short) 200)).build();
117 public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
118 public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
119 public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
121 private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
122 private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
123 private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
125 private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
126 .setKeyType(Uint16.ONE).setKeyString("pass-1");
127 private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
128 private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
129 private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
130 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
131 private static final Object DUMMY_OBJECT = "dummy-object";
132 private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
133 private static final XtrId XTR_ID = new XtrId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7,
134 8, 9, 10, 11, 12, 13, 14, 15});
137 public void init() throws Exception {
138 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
139 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
142 setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
146 * This method injects mocks which can not be injected the usual way using Mockito.
148 private static void injectMocks() throws NoSuchFieldException, IllegalAccessException {
149 final Field smcField = MappingSystem.class.getDeclaredField("smc");
150 smcField.setAccessible(true);
151 smcField.set(mappingSystem, smcMock);
153 final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
154 pmcField.setAccessible(true);
155 pmcField.set(mappingSystem, pmcMock);
157 final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
158 tableMapField.setAccessible(true);
159 tableMapField.set(mappingSystem, tableMapMock);
163 * This method changes the lookup policy.
165 private static void setLookupPolicy(IMappingService.LookupPolicy policy) throws NoSuchFieldException,
166 IllegalAccessException {
167 final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
168 lookupPolicy.setAccessible(true);
169 lookupPolicy.set(CONFIG_INI, policy);
173 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
176 public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
177 final MappingData mappingData = getDefaultMappingData();
178 mappingData.setTimestamp(EXPIRED_DATE);
179 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
180 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData, null);
182 final Mapping mapping = new MappingBuilder()
183 .setEidUri(new EidUri("ipv4:" + IPV4_DST))
184 .setOrigin(MappingOrigin.Southbound)
185 .setSiteId(Lists.newArrayList(SITE_ID))
186 .setMappingRecord(mappingData.getRecord()).build();
188 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
189 Mockito.verify(dsbeMock).removeMapping(mapping);
193 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
194 * Returns the original mapping.
197 public void getMappingTest_NbFirst_withNullNbMapping() {
198 final MappingData mappingData = getDefaultMappingData();
199 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
200 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
202 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
206 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
207 * Returns the original mapping.
210 public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
211 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
212 .setLocatorRecord(Lists.newArrayList(
214 getDefaultLocatorRecordBuilder().build(),
215 getDefaultLocatorRecordBuilder().build())).build();
216 final MappingData mappingData = getDefaultMappingData(mappingRecord);
217 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
219 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
223 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
224 * record. Returns the original mapping.
227 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
228 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
229 .setLocatorRecord(Lists.newArrayList(
231 getDefaultLocatorRecordBuilder().build())).build();
232 final MappingData mappingData = getDefaultMappingData(mappingRecord);
233 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
235 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
239 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
240 * type locator record. Service index out of bounds. Returns the original mapping.
243 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
244 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
245 .setLocatorRecord(Lists.newArrayList(
246 getDefaultLocatorRecordBuilder()
247 .setRloc(getELPTypeRloc()).build())).build();
248 final MappingData mappingData = getDefaultMappingData(mappingRecord);
249 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingData);
251 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
255 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
256 * type locator record.
259 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
260 throws UnknownHostException {
261 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
262 .setLocatorRecord(Lists.newArrayList(
263 getDefaultLocatorRecordBuilder()
264 .setRloc(getELPTypeRloc()).build())).build();
265 final MappingData mappingData = getDefaultMappingData(mappingRecord);
266 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
268 final MappingData result = mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
269 final Ipv4Binary ipv4Result = (Ipv4Binary) result.getRecord().getLocatorRecord().get(0).getRloc().getAddress();
271 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
275 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
276 * record. Returns the original mapping.
279 public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
280 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
281 .setLocatorRecord(Lists.newArrayList(
282 getDefaultLocatorRecordBuilder()
283 .setRloc(getIpPrefixTypeRloc()).build())).build();
284 final MappingData mappingData = getDefaultMappingData(mappingRecord);
285 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
287 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
291 * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
294 public void getMappingTest_NbFirst() {
295 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
296 .setLocatorRecord(Lists.newArrayList(
297 getDefaultLocatorRecordBuilder().build())).build();
298 final MappingData mappingData = getDefaultMappingData(mappingRecord);
299 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingData);
301 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
305 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
308 public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
309 IllegalAccessException {
310 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
311 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
313 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
317 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
318 * address and single Ipv4 type locator record. Returns the original mapping.
321 public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
322 IllegalAccessException {
323 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
324 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
325 .setLocatorRecord(Lists.newArrayList(
327 getDefaultLocatorRecordBuilder().build())).build();
328 final MappingData mappingData = getDefaultMappingData(mappingRecord);
329 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
331 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
335 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
336 * locator, southbound null. Returns the original mapping.
339 public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
340 IllegalAccessException {
341 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
343 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
344 .setLocatorRecord(Lists.newArrayList(
346 getDefaultLocatorRecordBuilder().build())).build();
347 MappingData nbMappingData = getDefaultMappingData(mappingRecord);
348 MappingData sbMappingData = getDefaultMappingData(mappingRecord);
349 sbMappingData.setTimestamp(EXPIRED_DATE);
351 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
352 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData, null);
354 final Mapping mapping = new MappingBuilder()
355 .setEidUri(new EidUri("ipv4:" + IPV4_DST))
356 .setOrigin(MappingOrigin.Southbound)
357 .setSiteId(Lists.newArrayList(SITE_ID))
358 .setMappingRecord(mappingRecord).build();
360 assertEquals(nbMappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
361 Mockito.verify(dsbeMock).removeMapping(mapping);
365 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
366 * locator, southbound non-null. Returns the merged mapping.
369 public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
370 IllegalAccessException {
371 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
373 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
374 .setLocatorRecord(Lists.newArrayList(
375 getDefaultLocatorRecordBuilder().build())).build();
376 final MappingData nbMappingData = getDefaultMappingData(mappingRecord);
377 final MappingData sbMappingData = getDefaultMappingData(mappingRecord);
379 // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
380 final MappingData resultMock = Mockito.mock(MappingData.class);
382 PowerMockito.mockStatic(MappingMergeUtil.class);
383 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
384 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData);
385 PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData))
386 .thenReturn(resultMock);
388 assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
392 * Tests {@link MappingSystem#getMapping(Eid dst)} method.
395 public void getMappingTest_withSrcNull() {
396 final MappingData mappingData = getDefaultMappingData();
397 Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
398 Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
400 assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_DST));
404 * Tests {@link MappingSystem#getMapping(Eid src, Eid dst, XtrId xtrId)} method.
407 public void getMappingTest_withXtrId() {
408 final MappingData mappingData = getDefaultMappingData();
409 mappingData.setXtrId(XTR_ID);
411 Mockito.when(smcMock.getMapping(EID_IPV4_DST, XTR_ID)).thenReturn(mappingData);
413 assertEquals(mappingData, mappingSystem.getMapping(null, EID_IPV4_DST, XTR_ID));
417 * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
420 public void getMappingTest_withMappingOrigin() {
421 final MappingData mappingData = getDefaultMappingData();
422 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, (XtrId) null)).thenReturn(mappingData);
424 mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
425 Mockito.verify(smcMock).getMapping(EID_IPV4_SRC, (XtrId) null);
427 mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
428 Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
432 * Tests {@link MappingSystem#removeMapping} method.
435 public void removeMappingTest_nb() {
436 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
438 mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
439 Mockito.verify(pmcMock).removeMapping(EID_IPV4_1);
443 * Tests {@link MappingSystem#removeMapping} method.
446 public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
447 mappingSystem = new MappingSystem(dao, false, npsMock, false);
449 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
451 mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
452 Mockito.verify(smcMock).removeMapping(EID_IPV4_1);
456 * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
459 public void restoreDaoFromDatastoreTest() {
460 final Mapping mapping_1 = new MappingBuilder()
461 .withKey(new MappingKey(new EidUri(LispAddressStringifier.getURIString(EID_IPV4_1)),
462 MappingOrigin.Northbound))
463 .setOrigin(MappingOrigin.Northbound)
464 .setMappingRecord(getDefaultMappingRecordBuilder()
465 .setEid(EID_IPV4_1).build()).build();
466 final Mapping mapping_2 = new MappingBuilder()
467 .withKey(new MappingKey(new EidUri(LispAddressStringifier.getURIString(EID_IPV4_2)),
468 MappingOrigin.Northbound))
469 .setOrigin(MappingOrigin.Northbound)
470 .setMappingRecord(getDefaultMappingRecordBuilder()
471 .setEid(EID_IPV4_2).build()).build();
473 final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
474 final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
475 .withKey(new AuthenticationKeyKey(new EidUri("uri-1")))
476 .setMappingAuthkey(mappingAuthkey_1)
477 .setEid(EID_IPV4_1).build();
479 final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
480 final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
481 .withKey(new AuthenticationKeyKey(new EidUri("uri-2")))
482 .setMappingAuthkey(mappingAuthkey_2)
483 .setEid(EID_IPV4_2).build();
485 final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
486 final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
488 Mockito.when(dsbeMock.getLastUpdateTimestamp()).thenReturn(System.currentTimeMillis());
489 Mockito.when(dsbeMock.getAllMappings(LogicalDatastoreType.CONFIGURATION)).thenReturn(mappings);
490 Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
491 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
493 mappingSystem.initialize();
495 ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
496 Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_1), captor.capture());
497 assertEquals(captor.getValue().getRecord(), mapping_1.getMappingRecord());
498 Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_2), captor.capture());
499 assertEquals(captor.getValue().getRecord(), mapping_2.getMappingRecord());
500 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
501 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
505 * Tests {@link MappingSystem#addAuthenticationKey} method.
508 public void addAuthenticationKeyTest() {
509 mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
510 Mockito.verify(akdbMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
514 * Tests {@link MappingSystem#getAuthenticationKey} method.
517 public void getAuthenticationKeyTest() {
518 Mockito.when(akdbMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
519 assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
523 * Tests {@link MappingSystem#removeAuthenticationKey} method.
526 public void removeAuthenticationKeyTest() {
527 mappingSystem.removeAuthenticationKey(EID_IPV4_1);
528 Mockito.verify(akdbMock).removeAuthenticationKey(EID_IPV4_1);
532 * Tests {@link MappingSystem#addData} method.
535 public void addDataTest() {
536 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
538 mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
539 Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
543 * Tests {@link MappingSystem#getData} method.
546 public void getDataTest() {
547 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
548 Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
550 assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
554 * Tests {@link MappingSystem#removeData} method.
557 public void removeDataTest() {
558 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
560 mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
561 Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
565 * Following test are executed for coverage-increase purpose.
568 public void otherTests() throws NoSuchFieldException, IllegalAccessException {
569 mappingSystem.printMappings();
570 Mockito.verify(pmcMock).printMappings();
571 Mockito.verify(smcMock).printMappings();
573 mappingSystem.destroy();
574 mappingSystem = new MappingSystem(dao, true, npsMock, true);
575 mappingSystem.setDataStoreBackEnd(dsbeMock);
576 mappingSystem.setMappingMerge(false);
577 mappingSystem.setIterateMask(true);
580 private static MappingData getDefaultMappingData() {
581 return getDefaultMappingData(null);
584 private static MappingData getDefaultMappingData(MappingRecord mappingRecord) {
585 if (mappingRecord == null) {
586 mappingRecord = getDefaultMappingRecordBuilder().build();
588 return new MappingData(mappingRecord, System.currentTimeMillis());
591 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
592 return new MappingRecordBuilder()
594 .setEid(EID_IPV4_DST);
597 private static ServicePath getDefaultServicePath(short index) {
598 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
599 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
600 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
602 return new ServicePathBuilder().setServicePath(servicePathBuilder
603 .setServiceIndex(Uint8.valueOf(index))
604 .setServicePathId(new ServicePathIdType(Uint32.ONE)).build())
608 private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
609 return new LocatorRecordBuilder()
610 .setLocatorId("locator-id")
614 private static Rloc getELPTypeRloc() {
616 final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
617 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
618 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
619 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
621 return new RlocBuilder()
622 .setAddress(explicitLocatorPath)
623 .setAddressType(ExplicitLocatorPathLcaf.class).build();
626 private static Rloc getIpPrefixTypeRloc() {
628 final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
629 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
631 return new RlocBuilder()
633 .setAddressType(Ipv4PrefixAfi.class).build();