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