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.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
30 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
31 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
32 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
33 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
34 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
35 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
36 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
37 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
41 .ExplicitLocatorPathLcaf;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
47 .address.ExplicitLocatorPath;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
49 .address.ExplicitLocatorPathBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
51 .address.Ipv4PrefixBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
55 .address.ServicePathBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
57 .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
59 .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
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
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
68 .MappingRecordBuilder;
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.Mapping;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container
80 .MappingAuthkeyBuilder;
81 import org.powermock.api.mockito.PowerMockito;
82 import org.powermock.core.classloader.annotations.PrepareForTest;
83 import org.powermock.modules.junit4.PowerMockRunner;
84 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
86 @RunWith(PowerMockRunner.class)
87 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
88 @PrepareForTest(MappingMergeUtil.class)
89 public class MappingSystemTest {
91 private static ILispDAO daoMock = Mockito.mock(ILispDAO.class);
92 @Mock private static IMapCache smcMock;
93 @Mock private static IMapCache pmcMock;
94 @Mock private static DataStoreBackEnd dsbeMock;
95 @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
96 @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(daoMock, false, true, true);
98 private static final String IPV4_SRC = "127.0.0.1";
99 private static final String IPV4_DST = "192.168.0.1";
100 private static final String IPV4_STRING_1 = "1.2.3.0";
101 private static final String IPV4_STRING_2 = "1.2.4.0";
102 private static final String IPV4_STRING_3 = "1.2.5.0";
103 private static final String MASK = "/20";
105 private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
106 private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
107 private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
108 private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
109 private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
110 private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
111 .setAddress(getDefaultServicePath((short) 200)).build(); // with index out of bounds
113 public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
114 public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
115 public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
117 private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
118 private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
119 private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
121 private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
122 .setKeyType(1).setKeyString("pass-1");
123 private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
124 private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
125 private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
126 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
127 private static final Object DUMMY_OBJECT = "dummy-object";
128 private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
131 public void init() throws Exception {
132 Mockito.when(daoMock.putTable(MappingOrigin.Southbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
133 Mockito.when(daoMock.putTable(MappingOrigin.Northbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
135 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
136 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
139 setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
143 * This method injects mocks which can not be injected the usual way using Mockito.
145 * @throws NoSuchFieldException
146 * @throws IllegalAccessException
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 * @throws NoSuchFieldException
166 * @throws IllegalAccessException
168 private static void setLookupPolicy(IMappingService.LookupPolicy policy) throws NoSuchFieldException,
169 IllegalAccessException {
170 final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
171 lookupPolicy.setAccessible(true);
172 lookupPolicy.set(CONFIG_INI, policy);
176 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
179 public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
180 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
181 .setTimestamp(EXPIRED_DATE.getTime()).build();
182 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
183 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
185 final Mapping mapping = new MappingBuilder()
186 .setEidUri(new EidUri("ipv4:1.2.3.0"))
187 .setOrigin(MappingOrigin.Southbound)
188 .setSiteId(Lists.newArrayList(SITE_ID))
189 .setMappingRecord(mappingRecord).build();
191 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
192 Mockito.verify(dsbeMock).removeMapping(mapping);
196 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
197 * Returns the original mapping.
200 public void getMappingTest_NbFirst_withNullNbMapping() {
201 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
202 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
203 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
205 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
209 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
210 * Returns the original mapping.
213 public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
214 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
215 .setLocatorRecord(Lists.newArrayList(
216 getDefaultLocatorRecordBuilder().build(), // set two locators
217 getDefaultLocatorRecordBuilder().build())).build();
218 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
220 assertEquals(mappingRecord, 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(
231 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
232 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
234 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
238 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
239 * type locator record. Service index out of bounds. Returns the original mapping.
242 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
243 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
244 .setLocatorRecord(Lists.newArrayList(
245 getDefaultLocatorRecordBuilder()
246 .setRloc(getELPTypeRloc()).build())).build();
247 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingRecord);
249 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
253 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
254 * type locator record.
257 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
258 throws UnknownHostException {
259 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
260 .setLocatorRecord(Lists.newArrayList(
261 getDefaultLocatorRecordBuilder()
262 .setRloc(getELPTypeRloc()).build())).build();
263 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
265 final MappingRecord result = (MappingRecord) mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
266 final Ipv4Binary ipv4Result = (Ipv4Binary) result.getLocatorRecord().get(0).getRloc().getAddress();
268 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
272 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
273 * record. Returns the original mapping.
276 public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
277 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
278 .setLocatorRecord(Lists.newArrayList(
279 getDefaultLocatorRecordBuilder()
280 .setRloc(getIpPrefixTypeRloc()).build())).build();
281 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
283 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
287 * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
290 public void getMappingTest_NbFirst() {
291 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
292 .setLocatorRecord(Lists.newArrayList(
293 getDefaultLocatorRecordBuilder().build())).build();
294 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
296 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
300 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
303 public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
304 IllegalAccessException {
305 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
306 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
308 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
312 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
313 * address and single Ipv4 type locator record. Returns the original mapping.
316 public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
317 IllegalAccessException {
318 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
319 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
320 .setLocatorRecord(Lists.newArrayList(
321 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
322 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
324 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
328 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
329 * locator, southbound null. Returns the original mapping.
332 public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
333 IllegalAccessException {
334 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
336 final MappingRecord nbMappingRecord = getDefaultMappingRecordBuilder()
337 .setLocatorRecord(Lists.newArrayList(
338 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
340 final MappingRecord sbMappingRecord = getDefaultMappingRecordBuilder()
341 .setTimestamp(EXPIRED_DATE.getTime())
342 .setLocatorRecord(Lists.newArrayList(
343 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
345 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingRecord);
346 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(sbMappingRecord);
348 final Mapping mapping = new MappingBuilder()
349 .setEidUri(new EidUri("ipv4:1.2.3.0"))
350 .setOrigin(MappingOrigin.Southbound)
351 .setSiteId(Lists.newArrayList(SITE_ID))
352 .setMappingRecord(sbMappingRecord).build();
354 assertEquals(nbMappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
355 Mockito.verify(dsbeMock).removeMapping(mapping);
359 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
360 * locator, southbound non-null. Returns the merged mapping.
363 public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
364 IllegalAccessException {
365 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
367 final MappingRecord nbMappingRecord = getDefaultMappingRecordBuilder()
368 .setLocatorRecord(Lists.newArrayList(
369 getDefaultLocatorRecordBuilder().build())).build();
371 final MappingRecord sbMappingRecord = getDefaultMappingRecordBuilder()
372 .setLocatorRecord(Lists.newArrayList(
373 getDefaultLocatorRecordBuilder().build())).build();
375 // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
376 final MappingRecord resultMock = Mockito.mock(MappingRecord.class);
378 PowerMockito.mockStatic(MappingMergeUtil.class);
379 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingRecord);
380 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(sbMappingRecord);
381 PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingRecord, sbMappingRecord))
382 .thenReturn(resultMock);
384 assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
388 * Tests {@link MappingSystem#getMapping(Eid dst)} method.
391 public void getMappingTest_withSrcNull() {
392 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
393 Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
394 Mockito.when(smcMock.getMapping(null, EID_IPV4_DST)).thenReturn(mappingRecord);
396 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_DST));
400 * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
403 public void getMappingTest_withMappingOrigin() {
404 mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
405 Mockito.verify(smcMock).getMapping(null, EID_IPV4_SRC);
407 mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
408 Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
412 * Tests {@link MappingSystem#removeMapping} method.
415 public void removeMappingTest_nb() {
416 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
418 mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
419 Mockito.verify(pmcMock).removeMapping(EID_IPV4_1, true);
423 * Tests {@link MappingSystem#removeMapping} method.
426 public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
427 mappingSystem = new MappingSystem(daoMock, false, true, false);
429 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
431 mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
432 Mockito.verify(smcMock).removeMapping(EID_IPV4_1, false);
436 * Tests {@link MappingSystem#updateMappingRegistration} method.
439 public void updateMappingRegistrationTest() {
440 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
442 mappingSystem.updateMappingRegistration(MappingOrigin.Northbound, EID_IPV4_1, 1L);
443 Mockito.verify(pmcMock).updateMappingRegistration(EID_IPV4_1, 1L);
447 * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
450 public void restoreDaoFromDatastoreTest() {
451 final Mapping mapping_1 = new MappingBuilder()
452 .setOrigin(MappingOrigin.Northbound)
453 .setMappingRecord(getDefaultMappingRecordBuilder()
455 .setTimestamp(EXPIRED_DATE.getTime()).build()).build();
456 final Mapping mapping_2 = new MappingBuilder()
457 .setOrigin(MappingOrigin.Northbound)
458 .setMappingRecord(getDefaultMappingRecordBuilder()
459 .setEid(EID_IPV4_2).build()).build();
461 final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
462 final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
463 .setMappingAuthkey(mappingAuthkey_1)
464 .setEid(EID_IPV4_1).build();
466 final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
467 final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
468 .setMappingAuthkey(mappingAuthkey_2)
469 .setEid(EID_IPV4_2).build();
471 final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
472 final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
474 Mockito.when(dsbeMock.getAllMappings()).thenReturn(mappings);
475 Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
476 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
478 mappingSystem.initialize();
479 Mockito.verify(dsbeMock, Mockito.times(1)).removeMapping(mapping_1);
480 Mockito.verify(pmcMock).addMapping(EID_IPV4_2, mapping_2.getMappingRecord(), true, false);
481 Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
482 Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
486 * Tests {@link MappingSystem#addAuthenticationKey} method.
489 public void addAuthenticationKeyTest() {
490 mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
491 Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
495 * Tests {@link MappingSystem#getAuthenticationKey} method.
498 public void getAuthenticationKeyTest() {
499 Mockito.when(smcMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
500 assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
504 * Tests {@link MappingSystem#removeAuthenticationKey} method.
507 public void removeAuthenticationKeyTest() {
508 mappingSystem.removeAuthenticationKey(EID_IPV4_1);
509 Mockito.verify(smcMock).removeAuthenticationKey(EID_IPV4_1);
513 * Tests {@link MappingSystem#addData} method.
516 public void addDataTest() {
517 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
519 mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
520 Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
524 * Tests {@link MappingSystem#getData} method.
527 public void getDataTest() {
528 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
529 Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
531 assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
535 * Tests {@link MappingSystem#removeData} method.
538 public void removeDataTest() {
539 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
541 mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
542 Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
546 * Tests {@link MappingSystem#cleanCaches} method.
549 public void cleanCachesTest() {
550 mappingSystem.cleanCaches();
551 Mockito.verify(daoMock).removeAll();
555 * Following test are executed for coverage-increase purpose.
558 public void otherTests() throws NoSuchFieldException, IllegalAccessException {
559 mappingSystem.printMappings();
560 Mockito.verify(pmcMock).printMappings();
561 Mockito.verify(smcMock).printMappings();
563 mappingSystem.destroy();
564 mappingSystem = new MappingSystem(daoMock, true, true, true);
565 mappingSystem.setDataStoreBackEnd(dsbeMock);
566 mappingSystem.setOverwritePolicy(true);
567 mappingSystem.setIterateMask(true);
570 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
571 return new MappingRecordBuilder()
572 .setTimestamp(System.currentTimeMillis())
577 private static ServicePath getDefaultServicePath(short index) {
578 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
579 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
580 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
582 return new ServicePathBuilder().setServicePath(servicePathBuilder
583 .setServiceIndex(index)
584 .setServicePathId(new ServicePathIdType(1L)).build())
588 private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
589 return new LocatorRecordBuilder()
590 .setLocatorId("locator-id")
594 private static Rloc getELPTypeRloc() {
596 final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
597 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
598 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
599 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
601 return new RlocBuilder()
602 .setAddress(explicitLocatorPath)
603 .setAddressType(ExplicitLocatorPathLcaf.class).build();
606 private static Rloc getIpPrefixTypeRloc() {
608 final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
609 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
611 return new RlocBuilder()
613 .setAddressType(Ipv4PrefixAfi.class).build();