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.dsbackend.DataStoreBackEnd;
30 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
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.ExplicitLocatorPathLcaf;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
50 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;
51 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;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
69 import org.powermock.api.mockito.PowerMockito;
70 import org.powermock.core.classloader.annotations.PrepareForTest;
71 import org.powermock.modules.junit4.PowerMockRunner;
72 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
74 @RunWith(PowerMockRunner.class)
75 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
76 @PrepareForTest(MappingMergeUtil.class)
77 public class MappingSystemTest {
79 private static ILispDAO daoMock = Mockito.mock(ILispDAO.class);
80 @Mock private static IMapCache smcMock;
81 @Mock private static IMapCache pmcMock;
82 @Mock private static DataStoreBackEnd dsbeMock;
83 @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
84 @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(daoMock, false, true, true);
86 private static final String IPV4_SRC = "127.0.0.1";
87 private static final String IPV4_DST = "192.168.0.1";
88 private static final String IPV4_STRING_1 = "1.2.3.0";
89 private static final String IPV4_STRING_2 = "1.2.4.0";
90 private static final String IPV4_STRING_3 = "1.2.5.0";
91 private static final String MASK = "/20";
93 private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
94 private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
95 private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
96 private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
97 private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
98 private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
99 .setAddress(getDefaultServicePath((short) 200)).build(); // with index out of bounds
101 public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
102 public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
103 public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
105 private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
106 private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
107 private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
109 private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
110 .setKeyType(1).setKeyString("pass-1");
111 private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
112 private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
113 private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
114 private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
115 private static final Object DUMMY_OBJECT = "dummy-object";
116 private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
119 public void init() throws Exception {
120 Mockito.when(daoMock.putTable(MappingOrigin.Southbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
121 Mockito.when(daoMock.putTable(MappingOrigin.Northbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
123 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
124 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
127 setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
131 * This method injects mocks which can not be injected the usual way using Mockito.
133 private static void injectMocks() throws NoSuchFieldException, IllegalAccessException {
134 final Field smcField = MappingSystem.class.getDeclaredField("smc");
135 smcField.setAccessible(true);
136 smcField.set(mappingSystem, smcMock);
138 final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
139 pmcField.setAccessible(true);
140 pmcField.set(mappingSystem, pmcMock);
142 final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
143 tableMapField.setAccessible(true);
144 tableMapField.set(mappingSystem, tableMapMock);
148 * This method changes the lookup policy.
150 private static void setLookupPolicy(IMappingService.LookupPolicy policy) throws NoSuchFieldException,
151 IllegalAccessException {
152 final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
153 lookupPolicy.setAccessible(true);
154 lookupPolicy.set(CONFIG_INI, policy);
158 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
161 public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
162 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
163 .setTimestamp(EXPIRED_DATE.getTime()).build();
164 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
165 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
167 final Mapping mapping = new MappingBuilder()
168 .setEidUri(new EidUri("ipv4:1.2.3.0"))
169 .setOrigin(MappingOrigin.Southbound)
170 .setSiteId(Lists.newArrayList(SITE_ID))
171 .setMappingRecord(mappingRecord).build();
173 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
174 Mockito.verify(dsbeMock).removeMapping(mapping);
178 * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
179 * Returns the original mapping.
182 public void getMappingTest_NbFirst_withNullNbMapping() {
183 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
184 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
185 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
187 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
191 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
192 * Returns the original mapping.
195 public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
196 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
197 .setLocatorRecord(Lists.newArrayList(
198 getDefaultLocatorRecordBuilder().build(), // set two locators
199 getDefaultLocatorRecordBuilder().build())).build();
200 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
202 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
206 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
207 * record. Returns the original mapping.
210 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
211 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
212 .setLocatorRecord(Lists.newArrayList(
213 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
214 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
216 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
220 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
221 * type locator record. Service index out of bounds. Returns the original mapping.
224 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
225 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
226 .setLocatorRecord(Lists.newArrayList(
227 getDefaultLocatorRecordBuilder()
228 .setRloc(getELPTypeRloc()).build())).build();
229 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingRecord);
231 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
235 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
236 * type locator record.
239 public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
240 throws UnknownHostException {
241 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
242 .setLocatorRecord(Lists.newArrayList(
243 getDefaultLocatorRecordBuilder()
244 .setRloc(getELPTypeRloc()).build())).build();
245 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
247 final MappingRecord result = (MappingRecord) mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
248 final Ipv4Binary ipv4Result = (Ipv4Binary) result.getLocatorRecord().get(0).getRloc().getAddress();
250 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
254 * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
255 * record. Returns the original mapping.
258 public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
259 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
260 .setLocatorRecord(Lists.newArrayList(
261 getDefaultLocatorRecordBuilder()
262 .setRloc(getIpPrefixTypeRloc()).build())).build();
263 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
265 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
269 * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
272 public void getMappingTest_NbFirst() {
273 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
274 .setLocatorRecord(Lists.newArrayList(
275 getDefaultLocatorRecordBuilder().build())).build();
276 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingRecord);
278 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
282 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
285 public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
286 IllegalAccessException {
287 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
288 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
290 assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
294 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
295 * address and single Ipv4 type locator record. Returns the original mapping.
298 public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
299 IllegalAccessException {
300 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
301 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
302 .setLocatorRecord(Lists.newArrayList(
303 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
304 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingRecord);
306 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
310 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
311 * locator, southbound null. Returns the original mapping.
314 public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
315 IllegalAccessException {
316 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
318 final MappingRecord nbMappingRecord = getDefaultMappingRecordBuilder()
319 .setLocatorRecord(Lists.newArrayList(
320 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
322 final MappingRecord sbMappingRecord = getDefaultMappingRecordBuilder()
323 .setTimestamp(EXPIRED_DATE.getTime())
324 .setLocatorRecord(Lists.newArrayList(
325 getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
327 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingRecord);
328 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(sbMappingRecord);
330 final Mapping mapping = new MappingBuilder()
331 .setEidUri(new EidUri("ipv4:1.2.3.0"))
332 .setOrigin(MappingOrigin.Southbound)
333 .setSiteId(Lists.newArrayList(SITE_ID))
334 .setMappingRecord(sbMappingRecord).build();
336 assertEquals(nbMappingRecord, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
337 Mockito.verify(dsbeMock).removeMapping(mapping);
341 * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
342 * locator, southbound non-null. Returns the merged mapping.
345 public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
346 IllegalAccessException {
347 setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
349 final MappingRecord nbMappingRecord = getDefaultMappingRecordBuilder()
350 .setLocatorRecord(Lists.newArrayList(
351 getDefaultLocatorRecordBuilder().build())).build();
353 final MappingRecord sbMappingRecord = getDefaultMappingRecordBuilder()
354 .setLocatorRecord(Lists.newArrayList(
355 getDefaultLocatorRecordBuilder().build())).build();
357 // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
358 final MappingRecord resultMock = Mockito.mock(MappingRecord.class);
360 PowerMockito.mockStatic(MappingMergeUtil.class);
361 Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingRecord);
362 Mockito.when(smcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(sbMappingRecord);
363 PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingRecord, sbMappingRecord))
364 .thenReturn(resultMock);
366 assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
370 * Tests {@link MappingSystem#getMapping(Eid dst)} method.
373 public void getMappingTest_withSrcNull() {
374 final MappingRecord mappingRecord = getDefaultMappingRecordBuilder().build();
375 Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
376 Mockito.when(smcMock.getMapping(null, EID_IPV4_DST)).thenReturn(mappingRecord);
378 assertEquals(mappingRecord, mappingSystem.getMapping(EID_IPV4_DST));
382 * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
385 public void getMappingTest_withMappingOrigin() {
386 mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
387 Mockito.verify(smcMock).getMapping(null, EID_IPV4_SRC);
389 mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
390 Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
394 * Tests {@link MappingSystem#removeMapping} method.
397 public void removeMappingTest_nb() {
398 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
400 mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
401 Mockito.verify(pmcMock).removeMapping(EID_IPV4_1, true);
405 * Tests {@link MappingSystem#removeMapping} method.
408 public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
409 mappingSystem = new MappingSystem(daoMock, false, true, false);
411 Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
413 mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
414 Mockito.verify(smcMock).removeMapping(EID_IPV4_1, false);
418 * Tests {@link MappingSystem#updateMappingRegistration} method.
421 public void updateMappingRegistrationTest() {
422 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
424 mappingSystem.updateMappingRegistration(MappingOrigin.Northbound, EID_IPV4_1, 1L);
425 Mockito.verify(pmcMock).updateMappingRegistration(EID_IPV4_1, 1L);
429 * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
432 public void restoreDaoFromDatastoreTest() {
433 final Mapping mapping_1 = new MappingBuilder()
434 .setOrigin(MappingOrigin.Northbound)
435 .setMappingRecord(getDefaultMappingRecordBuilder()
437 .setTimestamp(EXPIRED_DATE.getTime()).build()).build();
438 final Mapping mapping_2 = new MappingBuilder()
439 .setOrigin(MappingOrigin.Northbound)
440 .setMappingRecord(getDefaultMappingRecordBuilder()
441 .setEid(EID_IPV4_2).build()).build();
443 final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
444 final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
445 .setMappingAuthkey(mappingAuthkey_1)
446 .setEid(EID_IPV4_1).build();
448 final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
449 final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
450 .setMappingAuthkey(mappingAuthkey_2)
451 .setEid(EID_IPV4_2).build();
453 final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
454 final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
456 Mockito.when(dsbeMock.getAllMappings()).thenReturn(mappings);
457 Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
458 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
460 mappingSystem.initialize();
461 Mockito.verify(dsbeMock, Mockito.times(1)).removeMapping(mapping_1);
462 Mockito.verify(pmcMock).addMapping(EID_IPV4_2, mapping_2.getMappingRecord(), true, false);
463 Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
464 Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
468 * Tests {@link MappingSystem#addAuthenticationKey} method.
471 public void addAuthenticationKeyTest() {
472 mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
473 Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
477 * Tests {@link MappingSystem#getAuthenticationKey} method.
480 public void getAuthenticationKeyTest() {
481 Mockito.when(smcMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
482 assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
486 * Tests {@link MappingSystem#removeAuthenticationKey} method.
489 public void removeAuthenticationKeyTest() {
490 mappingSystem.removeAuthenticationKey(EID_IPV4_1);
491 Mockito.verify(smcMock).removeAuthenticationKey(EID_IPV4_1);
495 * Tests {@link MappingSystem#addData} method.
498 public void addDataTest() {
499 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
501 mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
502 Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
506 * Tests {@link MappingSystem#getData} method.
509 public void getDataTest() {
510 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
511 Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
513 assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
517 * Tests {@link MappingSystem#removeData} method.
520 public void removeDataTest() {
521 Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
523 mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
524 Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
528 * Tests {@link MappingSystem#cleanCaches} method.
531 public void cleanCachesTest() {
532 mappingSystem.cleanCaches();
533 Mockito.verify(daoMock).removeAll();
537 * Following test are executed for coverage-increase purpose.
540 public void otherTests() throws NoSuchFieldException, IllegalAccessException {
541 mappingSystem.printMappings();
542 Mockito.verify(pmcMock).printMappings();
543 Mockito.verify(smcMock).printMappings();
545 mappingSystem.destroy();
546 mappingSystem = new MappingSystem(daoMock, true, true, true);
547 mappingSystem.setDataStoreBackEnd(dsbeMock);
548 mappingSystem.setOverwritePolicy(true);
549 mappingSystem.setIterateMask(true);
552 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
553 return new MappingRecordBuilder()
554 .setTimestamp(System.currentTimeMillis())
559 private static ServicePath getDefaultServicePath(short index) {
560 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
561 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
562 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
564 return new ServicePathBuilder().setServicePath(servicePathBuilder
565 .setServiceIndex(index)
566 .setServicePathId(new ServicePathIdType(1L)).build())
570 private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
571 return new LocatorRecordBuilder()
572 .setLocatorId("locator-id")
576 private static Rloc getELPTypeRloc() {
578 final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
579 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
580 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
581 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
583 return new RlocBuilder()
584 .setAddress(explicitLocatorPath)
585 .setAddressType(ExplicitLocatorPathLcaf.class).build();
588 private static Rloc getIpPrefixTypeRloc() {
590 final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
591 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
593 return new RlocBuilder()
595 .setAddressType(Ipv4PrefixAfi.class).build();