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