cebcb93d5718386bf21d68f867cc21ad23afe2d1
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / MappingSystemTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13
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;
86
87 @RunWith(PowerMockRunner.class)
88 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
89 @PrepareForTest(MappingMergeUtil.class)
90 public class MappingSystemTest {
91
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);
100
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";
107
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();
116
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)));
120
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();
124
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});
135
136     @Before
137     public void init() throws Exception {
138         Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
139         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
140
141         injectMocks();
142         setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
143     }
144
145     /**
146      * This method injects mocks which can not be injected the usual way using Mockito.
147      */
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);
152
153         final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
154         pmcField.setAccessible(true);
155         pmcField.set(mappingSystem, pmcMock);
156
157         final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
158         tableMapField.setAccessible(true);
159         tableMapField.set(mappingSystem, tableMapMock);
160     }
161
162     /**
163      * This method changes the lookup policy.
164      */
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);
170     }
171
172     /**
173      * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
174      */
175     @Test
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);
181
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();
187
188         assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
189         Mockito.verify(dsbeMock).removeMapping(mapping);
190     }
191
192     /**
193      * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
194      * Returns the original mapping.
195      */
196     @Test
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);
201
202         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
203     }
204
205     /**
206      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
207      * Returns the original mapping.
208      */
209     @Test
210     public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
211         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
212                 .setLocatorRecord(Lists.newArrayList(
213                         // set two locators
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);
218
219         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
220     }
221
222     /**
223      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
224      * record. Returns the original mapping.
225      */
226     @Test
227     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
228         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
229                 .setLocatorRecord(Lists.newArrayList(
230                         // Ipv4 type Rloc
231                         getDefaultLocatorRecordBuilder().build())).build();
232         final MappingData mappingData = getDefaultMappingData(mappingRecord);
233         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
234
235         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
236     }
237
238     /**
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.
241      */
242     @Test
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);
250
251         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
252     }
253
254     /**
255      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
256      * type locator record.
257      */
258     @Test
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);
267
268         final MappingData result = mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
269         final Ipv4Binary ipv4Result = (Ipv4Binary) result.getRecord().getLocatorRecord().get(0).getRloc().getAddress();
270         assertTrue(Arrays
271                 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
272     }
273
274     /**
275      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
276      * record. Returns the original mapping.
277      */
278     @Test
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);
286
287         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
288     }
289
290     /**
291      * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
292      */
293     @Test
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);
300
301         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
302     }
303
304     /**
305      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
306      */
307     @Test
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);
312
313         assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
314     }
315
316     /**
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.
319      */
320     @Test
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(
326                         // Ipv4 type Rloc
327                         getDefaultLocatorRecordBuilder().build())).build();
328         final MappingData mappingData = getDefaultMappingData(mappingRecord);
329         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
330
331         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
332     }
333
334     /**
335      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
336      * locator, southbound null. Returns the original mapping.
337      */
338     @Test
339     public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
340             IllegalAccessException {
341         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
342
343         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
344                 .setLocatorRecord(Lists.newArrayList(
345                         // Ipv4 type Rloc
346                         getDefaultLocatorRecordBuilder().build())).build();
347         MappingData nbMappingData = getDefaultMappingData(mappingRecord);
348         MappingData sbMappingData = getDefaultMappingData(mappingRecord);
349         sbMappingData.setTimestamp(EXPIRED_DATE);
350
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);
353
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();
359
360         assertEquals(nbMappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
361         Mockito.verify(dsbeMock).removeMapping(mapping);
362     }
363
364     /**
365      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
366      * locator, southbound non-null. Returns the merged mapping.
367      */
368     @Test
369     public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
370             IllegalAccessException {
371         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
372
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);
378
379         // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
380         final MappingData resultMock = Mockito.mock(MappingData.class);
381
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);
387
388         assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
389     }
390
391     /**
392      * Tests {@link MappingSystem#getMapping(Eid dst)} method.
393      */
394     @Test
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);
399
400         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_DST));
401     }
402
403     /**
404      * Tests {@link MappingSystem#getMapping(Eid src, Eid dst, XtrId xtrId)} method.
405      */
406     @Test
407     public void getMappingTest_withXtrId() {
408         final MappingData mappingData = getDefaultMappingData();
409         mappingData.setXtrId(XTR_ID);
410
411         Mockito.when(smcMock.getMapping(EID_IPV4_DST, XTR_ID)).thenReturn(mappingData);
412
413         assertEquals(mappingData, mappingSystem.getMapping(null, EID_IPV4_DST, XTR_ID));
414     }
415
416     /**
417      * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
418      */
419     @Test
420     public void getMappingTest_withMappingOrigin() {
421         final MappingData mappingData = getDefaultMappingData();
422         Mockito.when(smcMock.getMapping(EID_IPV4_SRC, (XtrId) null)).thenReturn(mappingData);
423
424         mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
425         Mockito.verify(smcMock).getMapping(EID_IPV4_SRC, (XtrId) null);
426
427         mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
428         Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
429     }
430
431     /**
432      * Tests {@link MappingSystem#removeMapping} method.
433      */
434     @Test
435     public void removeMappingTest_nb() {
436         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
437
438         mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
439         Mockito.verify(pmcMock).removeMapping(EID_IPV4_1);
440     }
441
442     /**
443      * Tests {@link MappingSystem#removeMapping} method.
444      */
445     @Test
446     public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
447         mappingSystem = new MappingSystem(dao, false, npsMock, false);
448         injectMocks();
449         Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
450
451         mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
452         Mockito.verify(smcMock).removeMapping(EID_IPV4_1);
453     }
454
455     /**
456      * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
457      */
458     @Test
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();
472
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();
478
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();
484
485         final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
486         final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
487
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);
492
493         mappingSystem.initialize();
494
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);
502     }
503
504     /**
505      * Tests {@link MappingSystem#addAuthenticationKey} method.
506      */
507     @Test
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());
511     }
512
513     /**
514      * Tests {@link MappingSystem#getAuthenticationKey} method.
515      */
516     @Test
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));
520     }
521
522     /**
523      * Tests {@link MappingSystem#removeAuthenticationKey} method.
524      */
525     @Test
526     public void removeAuthenticationKeyTest() {
527         mappingSystem.removeAuthenticationKey(EID_IPV4_1);
528         Mockito.verify(akdbMock).removeAuthenticationKey(EID_IPV4_1);
529     }
530
531     /**
532      * Tests {@link MappingSystem#addData} method.
533      */
534     @Test
535     public void addDataTest() {
536         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
537
538         mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
539         Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
540     }
541
542     /**
543      * Tests {@link MappingSystem#getData} method.
544      */
545     @Test
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);
549
550         assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
551     }
552
553     /**
554      * Tests {@link MappingSystem#removeData} method.
555      */
556     @Test
557     public void removeDataTest() {
558         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
559
560         mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
561         Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
562     }
563
564     /**
565      * Following test are executed for coverage-increase purpose.
566      */
567     @Test
568     public void otherTests() throws NoSuchFieldException, IllegalAccessException {
569         mappingSystem.printMappings();
570         Mockito.verify(pmcMock).printMappings();
571         Mockito.verify(smcMock).printMappings();
572
573         mappingSystem.destroy();
574         mappingSystem = new MappingSystem(dao, true, npsMock, true);
575         mappingSystem.setDataStoreBackEnd(dsbeMock);
576         mappingSystem.setMappingMerge(false);
577         mappingSystem.setIterateMask(true);
578     }
579
580     private static MappingData getDefaultMappingData() {
581         return getDefaultMappingData(null);
582     }
583
584     private static MappingData getDefaultMappingData(MappingRecord mappingRecord) {
585         if (mappingRecord == null) {
586             mappingRecord = getDefaultMappingRecordBuilder().build();
587         }
588         return new MappingData(mappingRecord, System.currentTimeMillis());
589     }
590
591     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
592         return new MappingRecordBuilder()
593                 .setSiteId(SITE_ID)
594                 .setEid(EID_IPV4_DST);
595     }
596
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();
601
602         return new ServicePathBuilder().setServicePath(servicePathBuilder
603                 .setServiceIndex(Uint8.valueOf(index))
604                 .setServicePathId(new ServicePathIdType(Uint32.ONE)).build())
605                 .build();
606     }
607
608     private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
609         return new LocatorRecordBuilder()
610                 .setLocatorId("locator-id")
611                 .setRloc(RLOC);
612     }
613
614     private static Rloc getELPTypeRloc() {
615
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();
620
621         return new RlocBuilder()
622                 .setAddress(explicitLocatorPath)
623                 .setAddressType(ExplicitLocatorPathLcaf.class).build();
624     }
625
626     private static Rloc getIpPrefixTypeRloc() {
627
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();
630
631         return new RlocBuilder()
632                 .setAddress(address)
633                 .setAddressType(Ipv4PrefixAfi.class).build();
634     }
635 }