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