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