Make SimpleMapCache simple again
[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.runners.MockitoJUnitRunner;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.lispflowmapping.config.ConfigIni;
32 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
33 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
34 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
35 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
36 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
37 import org.opendaylight.lispflowmapping.interfaces.mapcache.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.LispAddressUtil;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ServicePathIdType;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPathBuilder;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4PrefixBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePathBuilder;
54 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;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkeyBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKeyBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
74 import org.powermock.api.mockito.PowerMockito;
75 import org.powermock.core.classloader.annotations.PrepareForTest;
76 import org.powermock.modules.junit4.PowerMockRunner;
77 import org.powermock.modules.junit4.PowerMockRunnerDelegate;
78
79 @RunWith(PowerMockRunner.class)
80 @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
81 @PrepareForTest(MappingMergeUtil.class)
82 public class MappingSystemTest {
83
84     private static ILispDAO daoMock = Mockito.mock(ILispDAO.class);
85     @Mock private static ILispMapCache smcMock;
86     @Mock private static IMapCache pmcMock;
87     @Mock private static DataStoreBackEnd dsbeMock;
88     @Mock private static EnumMap<MappingOrigin, IMapCache> tableMapMock;
89     @InjectMocks private static MappingSystem mappingSystem = new MappingSystem(daoMock, false, true, true);
90
91     private static final String IPV4_SRC =      "127.0.0.1";
92     private static final String IPV4_DST =      "192.168.0.1";
93     private static final String IPV4_STRING_1 = "1.2.3.0";
94     private static final String IPV4_STRING_2 = "1.2.4.0";
95     private static final String IPV4_STRING_3 = "1.2.5.0";
96     private static final String MASK =          "/20";
97
98     private static final Eid EID_IPV4_SRC = LispAddressUtil.asIpv4Eid(IPV4_SRC);
99     private static final Eid EID_IPV4_DST = LispAddressUtil.asIpv4Eid(IPV4_DST);
100     private static final Eid EID_IPV4_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
101     private static final Eid EID_IPV4_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
102     private static final Eid EID_SERVICE_PATH = new EidBuilder().setAddress(getDefaultServicePath((short) 253)).build();
103     private static final Eid EID_SERVICE_PATH_INDEX_OOB = new EidBuilder()
104             .setAddress(getDefaultServicePath((short) 200)).build(); // with index out of bounds
105
106     public static final SimpleAddress SIMPLE_ADDR_1 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_1)));
107     public static final SimpleAddress SIMPLE_ADDR_2 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_2)));
108     public static final SimpleAddress SIMPLE_ADDR_3 = new SimpleAddress(new IpAddress(new Ipv4Address(IPV4_STRING_3)));
109
110     private static final Hop HOP_1 = new HopBuilder().setHopId("hop-id-1").setAddress(SIMPLE_ADDR_1).build();
111     private static final Hop HOP_2 = new HopBuilder().setHopId("hop-id-2").setAddress(SIMPLE_ADDR_2).build();
112     private static final Hop HOP_3 = new HopBuilder().setHopId("hop-id-3").setAddress(SIMPLE_ADDR_3).build();
113
114     private static final MappingAuthkeyBuilder MAPPING_AUTHKEY_BUILDER = new MappingAuthkeyBuilder()
115             .setKeyType(1).setKeyString("pass-1");
116     private static final Rloc RLOC = LispAddressUtil.toRloc(new Ipv4Address(IPV4_STRING_2));
117     private static final ConfigIni CONFIG_INI = ConfigIni.getInstance();
118     private static final long REGISTRATION_VALIDITY = CONFIG_INI.getRegistrationValiditySb();
119     private static final Date EXPIRED_DATE = new Date(System.currentTimeMillis() - (REGISTRATION_VALIDITY + 1L));
120     private static final Object DUMMY_OBJECT = "dummy-object";
121     private static final SiteId SITE_ID = new SiteId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7});
122     private static final XtrId XTR_ID = new XtrId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7,
123                                                                 8, 9, 10, 11, 12, 13, 14, 15});
124
125     @Before
126     public void init() throws Exception {
127         Mockito.when(daoMock.putTable(MappingOrigin.Southbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
128         Mockito.when(daoMock.putTable(MappingOrigin.Northbound.toString())).thenReturn(Mockito.mock(HashMapDb.class));
129
130         Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
131         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
132
133         injectMocks();
134         setLookupPolicy(IMappingService.LookupPolicy.NB_FIRST);
135     }
136
137     /**
138      * This method injects mocks which can not be injected the usual way using Mockito.
139      */
140     private static void injectMocks() throws NoSuchFieldException, IllegalAccessException {
141         final Field smcField = MappingSystem.class.getDeclaredField("smc");
142         smcField.setAccessible(true);
143         smcField.set(mappingSystem, smcMock);
144
145         final Field pmcField = MappingSystem.class.getDeclaredField("pmc");
146         pmcField.setAccessible(true);
147         pmcField.set(mappingSystem, pmcMock);
148
149         final Field tableMapField = MappingSystem.class.getDeclaredField("tableMap");
150         tableMapField.setAccessible(true);
151         tableMapField.set(mappingSystem, tableMapMock);
152     }
153
154     /**
155      * This method changes the lookup policy.
156      */
157     private static void setLookupPolicy(IMappingService.LookupPolicy policy) throws NoSuchFieldException,
158             IllegalAccessException {
159         final Field lookupPolicy = ConfigIni.class.getDeclaredField("lookupPolicy");
160         lookupPolicy.setAccessible(true);
161         lookupPolicy.set(CONFIG_INI, policy);
162     }
163
164     /**
165      * Tests {@link MappingSystem#getMapping} method with NB mapping == null, expired mapping.
166      */
167     @Test
168     public void getMappingTest_NbFirst_withNullExpiredNbMapping() {
169         final MappingData mappingData = getDefaultMappingData();
170         mappingData.setTimestamp(EXPIRED_DATE);
171         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
172         Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
173
174         final Mapping mapping = new MappingBuilder()
175                 .setEidUri(new EidUri("ipv4:1.2.3.0"))
176                 .setOrigin(MappingOrigin.Southbound)
177                 .setSiteId(Lists.newArrayList(SITE_ID))
178                 .setMappingRecord(mappingData.getRecord()).build();
179
180         assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
181         Mockito.verify(dsbeMock).removeMapping(mapping);
182     }
183
184     /**
185      * Tests {@link MappingSystem#getMapping} method with NB mapping == null, not expired mapping.
186      * Returns the original mapping.
187      */
188     @Test
189     public void getMappingTest_NbFirst_withNullNbMapping() {
190         final MappingData mappingData = getDefaultMappingData();
191         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
192         Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
193
194         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
195     }
196
197     /**
198      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and multiple locator record.
199      * Returns the original mapping.
200      */
201     @Test
202     public void getMappingTest_NbFirst_withServicePathDestinationAddress_multipleLocatorRecords() {
203         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
204                 .setLocatorRecord(Lists.newArrayList(
205                         getDefaultLocatorRecordBuilder().build(), // set two locators
206                         getDefaultLocatorRecordBuilder().build())).build();
207         final MappingData mappingData = getDefaultMappingData(mappingRecord);
208         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
209
210         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
211     }
212
213     /**
214      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single Ipv4 type locator
215      * record. Returns the original mapping.
216      */
217     @Test
218     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleIpv4LocatorRecord() {
219         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
220                 .setLocatorRecord(Lists.newArrayList(
221                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
222         final MappingData mappingData = getDefaultMappingData(mappingRecord);
223         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
224
225         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
226     }
227
228     /**
229      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
230      * type locator record. Service index out of bounds. Returns the original mapping.
231      */
232     @Test
233     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord_IndexOutOfBounds() {
234         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
235                 .setLocatorRecord(Lists.newArrayList(
236                         getDefaultLocatorRecordBuilder()
237                                 .setRloc(getELPTypeRloc()).build())).build();
238         final MappingData mappingData = getDefaultMappingData(mappingRecord);
239         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB)).thenReturn(mappingData);
240
241         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH_INDEX_OOB));
242     }
243
244     /**
245      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single ExplicitLocatorPath
246      * type locator record.
247      */
248     @Test
249     public void getMappingTest_NbFirst_withServicePathDestinationAddress_singleELPLocatorRecord()
250             throws UnknownHostException {
251         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
252                 .setLocatorRecord(Lists.newArrayList(
253                         getDefaultLocatorRecordBuilder()
254                                 .setRloc(getELPTypeRloc()).build())).build();
255         final MappingData mappingData = getDefaultMappingData(mappingRecord);
256         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
257
258         final MappingData result = (MappingData) mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH);
259         final Ipv4Binary ipv4Result = (Ipv4Binary) result.getRecord().getLocatorRecord().get(0).getRloc().getAddress();
260         assertTrue(Arrays
261                 .equals(InetAddress.getByName(IPV4_STRING_3).getAddress(), ipv4Result.getIpv4Binary().getValue()));
262     }
263
264     /**
265      * Tests {@link MappingSystem#getMapping} method with ServicePath type dst address and single IpPrefix type locator
266      * record. Returns the original mapping.
267      */
268     @Test
269     public void getMappingTest_NbFirst_withServicePathDestinationAddress_IpPrefixLocatorRecord() {
270         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
271                 .setLocatorRecord(Lists.newArrayList(
272                         getDefaultLocatorRecordBuilder()
273                                 .setRloc(getIpPrefixTypeRloc()).build())).build();
274         final MappingData mappingData = getDefaultMappingData(mappingRecord);
275         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
276
277         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
278     }
279
280     /**
281      * Tests {@link MappingSystem#getMapping} method with Ipv4 type dst address.
282      */
283     @Test
284     public void getMappingTest_NbFirst() {
285         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
286                 .setLocatorRecord(Lists.newArrayList(
287                         getDefaultLocatorRecordBuilder().build())).build();
288         final MappingData mappingData = getDefaultMappingData(mappingRecord);
289         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(mappingData);
290
291         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
292     }
293
294     /**
295      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection.
296      */
297     @Test
298     public void getMappingTest_NbSbIntersection_withNullNbMapping() throws NoSuchFieldException,
299             IllegalAccessException {
300         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
301         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(null);
302
303         assertNull(mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
304     }
305
306     /**
307      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with ServicePath type dst
308      * address and single Ipv4 type locator record. Returns the original mapping.
309      */
310     @Test
311     public void getMappingTest_NbSbIntersection_withServicePathDestinationAddress() throws NoSuchFieldException,
312             IllegalAccessException {
313         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
314         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
315                 .setLocatorRecord(Lists.newArrayList(
316                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
317         final MappingData mappingData = getDefaultMappingData(mappingRecord);
318         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH)).thenReturn(mappingData);
319
320         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_SERVICE_PATH));
321     }
322
323     /**
324      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
325      * locator, southbound null. Returns the original mapping.
326      */
327     @Test
328     public void getMappingTest_NbSbIntersection_withSbNull() throws NoSuchFieldException,
329             IllegalAccessException {
330         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
331
332         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
333                 .setLocatorRecord(Lists.newArrayList(
334                         getDefaultLocatorRecordBuilder().build())).build(); // Ipv4 type Rloc
335         MappingData nbMappingData = getDefaultMappingData(mappingRecord);
336         MappingData sbMappingData = getDefaultMappingData(mappingRecord);
337         sbMappingData.setTimestamp(EXPIRED_DATE);
338
339         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
340         Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData);
341
342         final Mapping mapping = new MappingBuilder()
343                 .setEidUri(new EidUri("ipv4:1.2.3.0"))
344                 .setOrigin(MappingOrigin.Southbound)
345                 .setSiteId(Lists.newArrayList(SITE_ID))
346                 .setMappingRecord(mappingRecord).build();
347
348         assertEquals(nbMappingData, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
349         Mockito.verify(dsbeMock).removeMapping(mapping);
350     }
351
352     /**
353      * Tests {@link MappingSystem#getMapping} method, northbound and southbound intersection with single Ipv4 type
354      * locator, southbound non-null. Returns the merged mapping.
355      */
356     @Test
357     public void getMappingTest_NbSbIntersection_mergeMappings() throws NoSuchFieldException,
358             IllegalAccessException {
359         setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
360
361         final MappingRecord mappingRecord = getDefaultMappingRecordBuilder()
362                 .setLocatorRecord(Lists.newArrayList(
363                         getDefaultLocatorRecordBuilder().build())).build();
364         final MappingData nbMappingData = getDefaultMappingData(mappingRecord);
365         final MappingData sbMappingData = getDefaultMappingData(mappingRecord);
366
367         // this mock will be ultimately returned when MappingMergeUtil.computeNbSbIntersection is called
368         final MappingData resultMock = Mockito.mock(MappingData.class);
369
370         PowerMockito.mockStatic(MappingMergeUtil.class);
371         Mockito.when(pmcMock.getMapping(EID_IPV4_SRC, EID_IPV4_DST)).thenReturn(nbMappingData);
372         Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(sbMappingData);
373         PowerMockito.when(MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData))
374                 .thenReturn(resultMock);
375
376         assertEquals(resultMock, mappingSystem.getMapping(EID_IPV4_SRC, EID_IPV4_DST));
377     }
378
379     /**
380      * Tests {@link MappingSystem#getMapping(Eid dst)} method.
381      */
382     @Test
383     public void getMappingTest_withSrcNull() {
384         final MappingData mappingData = getDefaultMappingData();
385         Mockito.when(pmcMock.getMapping(null, EID_IPV4_DST)).thenReturn(null);
386         Mockito.when(smcMock.getMapping(EID_IPV4_DST, (XtrId) null)).thenReturn(mappingData);
387
388         assertEquals(mappingData, mappingSystem.getMapping(EID_IPV4_DST));
389     }
390
391     /**
392      * Tests {@link MappingSystem#getMapping(Eid src, Eid dst, XtrId xtrId)} method.
393      */
394     @Test
395     public void getMappingTest_withXtrId() {
396         final MappingData mappingData = getDefaultMappingData();
397         mappingData.setXtrId(XTR_ID);
398
399         Mockito.when(smcMock.getMapping(EID_IPV4_DST, XTR_ID)).thenReturn(mappingData);
400
401         assertEquals(mappingData, mappingSystem.getMapping(null, EID_IPV4_DST, XTR_ID));
402     }
403
404     /**
405      * Tests {@link MappingSystem#getMapping(MappingOrigin, Eid)} method.
406      */
407     @Test
408     public void getMappingTest_withMappingOrigin() {
409         final MappingData mappingData = getDefaultMappingData();
410         Mockito.when(smcMock.getMapping(EID_IPV4_SRC, (XtrId) null)).thenReturn(mappingData);
411
412         mappingSystem.getMapping(MappingOrigin.Southbound, EID_IPV4_SRC);
413         Mockito.verify(smcMock).getMapping(EID_IPV4_SRC, (XtrId) null);
414
415         mappingSystem.getMapping(MappingOrigin.Northbound, EID_IPV4_SRC);
416         Mockito.verify(pmcMock).getMapping(null, EID_IPV4_SRC);
417     }
418
419     /**
420      * Tests {@link MappingSystem#removeMapping} method.
421      */
422     @Test
423     public void removeMappingTest_nb() {
424         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
425
426         mappingSystem.removeMapping(MappingOrigin.Northbound, EID_IPV4_1);
427         Mockito.verify(pmcMock).removeMapping(EID_IPV4_1);
428     }
429
430     /**
431      * Tests {@link MappingSystem#removeMapping} method.
432      */
433     @Test
434     public void removeMappingTest_sb() throws NoSuchFieldException, IllegalAccessException {
435         mappingSystem = new MappingSystem(daoMock, false, true, false);
436         injectMocks();
437         Mockito.when(tableMapMock.get(MappingOrigin.Southbound)).thenReturn(smcMock);
438
439         mappingSystem.removeMapping(MappingOrigin.Southbound, EID_IPV4_1);
440         Mockito.verify(smcMock).removeMapping(EID_IPV4_1);
441     }
442
443     /**
444      * Tests {@link MappingSystem#restoreDaoFromDatastore} method.
445      */
446     @Test
447     public void restoreDaoFromDatastoreTest() {
448         final Mapping mapping_1 = new MappingBuilder()
449                 .setOrigin(MappingOrigin.Northbound)
450                 .setMappingRecord(getDefaultMappingRecordBuilder()
451                         .setEid(EID_IPV4_1).build()).build();
452         final Mapping mapping_2 = new MappingBuilder()
453                 .setOrigin(MappingOrigin.Northbound)
454                 .setMappingRecord(getDefaultMappingRecordBuilder()
455                         .setEid(EID_IPV4_2).build()).build();
456
457         final MappingAuthkey mappingAuthkey_1 = MAPPING_AUTHKEY_BUILDER.build();
458         final AuthenticationKey authenticationKey_1 = new AuthenticationKeyBuilder()
459                 .setMappingAuthkey(mappingAuthkey_1)
460                 .setEid(EID_IPV4_1).build();
461
462         final MappingAuthkey mappingAuthkey_2 = MAPPING_AUTHKEY_BUILDER.setKeyString("pass-2").build();
463         final AuthenticationKey authenticationKey_2 = new AuthenticationKeyBuilder()
464                 .setMappingAuthkey(mappingAuthkey_2)
465                 .setEid(EID_IPV4_2).build();
466
467         final List<Mapping> mappings = Lists.newArrayList(mapping_1, mapping_2);
468         final List<AuthenticationKey> authenticationKeys = Lists.newArrayList(authenticationKey_1, authenticationKey_2);
469
470         Mockito.when(dsbeMock.getLastUpdateTimestamp()).thenReturn(System.currentTimeMillis());
471         Mockito.when(dsbeMock.getAllMappings(LogicalDatastoreType.CONFIGURATION)).thenReturn(mappings);
472         Mockito.when(dsbeMock.getAllAuthenticationKeys()).thenReturn(authenticationKeys);
473         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
474
475         mappingSystem.initialize();
476
477         ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
478         Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_1), captor.capture());
479         assertEquals(captor.getValue().getRecord(), mapping_1.getMappingRecord());
480         Mockito.verify(pmcMock).addMapping(Mockito.eq(EID_IPV4_2), captor.capture());
481         assertEquals(captor.getValue().getRecord(), mapping_2.getMappingRecord());
482         Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, mappingAuthkey_1);
483         Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_2, mappingAuthkey_2);
484     }
485
486     /**
487      * Tests {@link MappingSystem#addAuthenticationKey} method.
488      */
489     @Test
490     public void addAuthenticationKeyTest() {
491         mappingSystem.addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
492         Mockito.verify(smcMock).addAuthenticationKey(EID_IPV4_1, MAPPING_AUTHKEY_BUILDER.build());
493     }
494
495     /**
496      * Tests {@link MappingSystem#getAuthenticationKey} method.
497      */
498     @Test
499     public void getAuthenticationKeyTest() {
500         Mockito.when(smcMock.getAuthenticationKey(EID_IPV4_1)).thenReturn(MAPPING_AUTHKEY_BUILDER.build());
501         assertEquals(MAPPING_AUTHKEY_BUILDER.build(), mappingSystem.getAuthenticationKey(EID_IPV4_1));
502     }
503
504     /**
505      * Tests {@link MappingSystem#removeAuthenticationKey} method.
506      */
507     @Test
508     public void removeAuthenticationKeyTest() {
509         mappingSystem.removeAuthenticationKey(EID_IPV4_1);
510         Mockito.verify(smcMock).removeAuthenticationKey(EID_IPV4_1);
511     }
512
513     /**
514      * Tests {@link MappingSystem#addData} method.
515      */
516     @Test
517     public void addDataTest() {
518         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
519
520         mappingSystem.addData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
521         Mockito.verify(pmcMock).addData(EID_IPV4_1, SubKeys.RECORD, DUMMY_OBJECT);
522     }
523
524     /**
525      * Tests {@link MappingSystem#getData} method.
526      */
527     @Test
528     public void getDataTest() {
529         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
530         Mockito.when(pmcMock.getData(EID_IPV4_1, SubKeys.RECORD)).thenReturn(DUMMY_OBJECT);
531
532         assertEquals(DUMMY_OBJECT, mappingSystem.getData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD));
533     }
534
535     /**
536      * Tests {@link MappingSystem#removeData} method.
537      */
538     @Test
539     public void removeDataTest() {
540         Mockito.when(tableMapMock.get(MappingOrigin.Northbound)).thenReturn(pmcMock);
541
542         mappingSystem.removeData(MappingOrigin.Northbound, EID_IPV4_1, SubKeys.RECORD);
543         Mockito.verify(pmcMock).removeData(EID_IPV4_1, SubKeys.RECORD);
544     }
545
546     /**
547      * Tests {@link MappingSystem#cleanCaches} method.
548      */
549     @Test
550     public void cleanCachesTest() {
551         mappingSystem.cleanCaches();
552         Mockito.verify(daoMock).removeAll();
553     }
554
555     /**
556      * Following test are executed for coverage-increase purpose.
557      */
558     @Test
559     public void otherTests() throws NoSuchFieldException, IllegalAccessException {
560         mappingSystem.printMappings();
561         Mockito.verify(pmcMock).printMappings();
562         Mockito.verify(smcMock).printMappings();
563
564         mappingSystem.destroy();
565         mappingSystem = new MappingSystem(daoMock, true, true, true);
566         mappingSystem.setDataStoreBackEnd(dsbeMock);
567         mappingSystem.setOverwritePolicy(true);
568         mappingSystem.setIterateMask(true);
569     }
570
571     private static MappingData getDefaultMappingData() {
572         return getDefaultMappingData(null);
573     }
574
575     private static MappingData getDefaultMappingData(MappingRecord mappingRecord) {
576         if (mappingRecord == null) {
577             mappingRecord = getDefaultMappingRecordBuilder().build();
578         }
579         return new MappingData(mappingRecord, System.currentTimeMillis());
580     }
581
582     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
583         return new MappingRecordBuilder()
584                 .setSiteId(SITE_ID)
585                 .setEid(EID_IPV4_1);
586     }
587
588     private static ServicePath getDefaultServicePath(short index) {
589         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address
590                 .service.path.ServicePathBuilder servicePathBuilder = new org.opendaylight.yang.gen.v1.urn.ietf.params
591                 .xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.service.path.ServicePathBuilder();
592
593         return new ServicePathBuilder().setServicePath(servicePathBuilder
594                 .setServiceIndex(index)
595                 .setServicePathId(new ServicePathIdType(1L)).build())
596                 .build();
597     }
598
599     private static LocatorRecordBuilder getDefaultLocatorRecordBuilder() {
600         return new LocatorRecordBuilder()
601                 .setLocatorId("locator-id")
602                 .setRloc(RLOC);
603     }
604
605     private static Rloc getELPTypeRloc() {
606
607         final ExplicitLocatorPath explicitLocatorPath = new ExplicitLocatorPathBuilder().setExplicitLocatorPath(new org
608                 .opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address
609                 .address.explicit.locator.path.ExplicitLocatorPathBuilder()
610                 .setHop(Lists.newArrayList(HOP_1, HOP_2, HOP_3)).build()).build();
611
612         return new RlocBuilder()
613                 .setAddress(explicitLocatorPath)
614                 .setAddressType(ExplicitLocatorPathLcaf.class).build();
615     }
616
617     private static Rloc getIpPrefixTypeRloc() {
618
619         final Address address = new Ipv4PrefixBuilder().setIpv4Prefix(new org.opendaylight.yang.gen.v1.urn.ietf.params
620                 .xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix(IPV4_STRING_1 + MASK)).build();
621
622         return new RlocBuilder()
623                 .setAddress(address)
624                 .setAddressType(Ipv4PrefixAfi.class).build();
625     }
626 }