Revert "Bug 7947: Store MappingOrigin in MappingData"
[lispflowmapping.git] / mappingservice / mapcache / src / test / java / org / opendaylight / lispflowmapping / mapcache / SimpleMapCacheTest.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.mapcache;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12
13 import com.google.common.collect.Lists;
14 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.Date;
16 import java.util.Map;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
22 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
23 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
24 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
25 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
33
34 public class SimpleMapCacheTest {
35
36     private static ILispDAO tableMock;
37     private static ILispDAO xtrIdDaoMock;
38     private static MappingData mappingDataMock;
39     private static ILispDAO daoMock;
40     private static SimpleMapCache simpleMapCache;
41
42     private static final String IPV4_STRING_1 =      "1.2.3.0";
43     private static final String IPV4_STRING_DST =    "192.168.0.1";
44     private static final String IPV4_PREFIX_STRING = "/24";
45     private static final short MASK = 24;
46     private static final long VNI_0 = 0L;
47     private static final long VNI_100 = 100L;
48     private static final byte[] IPV4_RLOC_BINARY = new byte[] {0, 1, 4, 0};
49     private static final XtrId XTR_ID = new XtrId(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
50
51     private static final Eid EID_IPV4_PREFIX_1_VNI = LispAddressUtil
52             .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING, new InstanceIdType(VNI_100));
53     private static final Eid EID_IPV4_PREFIX_2 = LispAddressUtil
54             .asIpv4PrefixEid(IPV4_STRING_1 + IPV4_PREFIX_STRING);
55     private static final Eid EID_IPV4_PREFIX_DST = LispAddressUtil
56             .asIpv4PrefixEid(IPV4_STRING_DST + IPV4_PREFIX_STRING);
57     private static final Eid EID_IPV4 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
58     private static final Eid NORMALIZED_EID_1 = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI);
59     private static final Eid NORMALIZED_EID_2 = MaskUtil.normalize(EID_IPV4_PREFIX_2);
60     private static final Eid NORMALIZED_EID_IPV4 = MaskUtil.normalize(EID_IPV4);
61     private static final Eid NORMALIZED_EID_IPV4_PREFIX_DST = MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24);
62
63     private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
64
65     @Before
66     public void init() {
67         daoMock = Mockito.mock(ILispDAO.class, "dao");
68         tableMock = Mockito.mock(ILispDAO.class);
69         xtrIdDaoMock = Mockito.mock(ILispDAO.class);
70         mappingDataMock = Mockito.mock(MappingData.class);
71         simpleMapCache = new SimpleMapCache(daoMock);
72     }
73
74     /**
75      * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
76      */
77     @Test
78     public void getMappingTest_withNullDstEid() {
79         assertNull(simpleMapCache.getMapping(null, XTR_ID));
80     }
81
82     /**
83      * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
84      */
85     @Test
86     public void getMappingTest_withNullVniTable() {
87         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
88         assertNull(simpleMapCache.getMapping(EID_IPV4_PREFIX_1_VNI, XTR_ID));
89     }
90
91     /**
92      * Tests {@link SimpleMapCache#removeMapping} method with xTR-ID.
93      */
94     @Test
95     public void removeMappingTest_withXtrId() {
96         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
97         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_IPV4, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
98
99         simpleMapCache.removeMapping(EID_IPV4, XTR_ID);
100         Mockito.verify(xtrIdDaoMock).removeSpecific(XTR_ID, SubKeys.RECORD);
101     }
102
103     /**
104      * Tests {@link SimpleMapCache#removeMapping} method.
105      */
106     @Test
107     public void removeMappingTest() {
108         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
109
110         simpleMapCache.removeMapping(EID_IPV4);
111         Mockito.verify(tableMock).remove(MaskUtil.normalize(EID_IPV4));
112         Mockito.verify(tableMock).isEmpty();
113         Mockito.verifyNoMoreInteractions(tableMock);
114     }
115
116     /**
117      * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
118      */
119     @Test
120     public void removeMappingTest_withNullVniTable() {
121         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
122
123         simpleMapCache.removeMapping(EID_IPV4);
124         Mockito.verifyNoMoreInteractions(tableMock);
125     }
126
127     /**
128      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
129      */
130     @Test
131     @SuppressWarnings("unchecked")
132     public void getAllXtrIdMappings_withMaskableAddress() {
133         final Eid normalizedKey = MaskUtil.normalize(EID_IPV4_PREFIX_1_VNI, MASK);
134         final Map<String, Object> entryMock = Mockito.mock(Map.class);
135         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
136
137         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
138         Mockito.when(tableMock.getBest(normalizedKey)).thenReturn(entryMock);
139         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
140         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4_PREFIX_1_VNI, SubKeys.RECORD))
141                 .thenReturn(xtrIdDaoMock);
142         simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI);
143
144         Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
145     }
146
147     /**
148      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
149      */
150     @Test
151     @SuppressWarnings("unchecked")
152     public void getAllXtrIdMappings_withNonMaskableAddress() {
153         final Map<String, Object> entryMock = Mockito.mock(Map.class);
154         final ILispDAO xtrIdRecordsMock = Mockito.mock(ILispDAO.class);
155
156         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
157         Mockito.when(tableMock.getBest(NORMALIZED_EID_IPV4)).thenReturn(entryMock);
158         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdRecordsMock);
159         Mockito.when(xtrIdRecordsMock.getSpecific(EID_IPV4, SubKeys.RECORD))
160                 .thenReturn(xtrIdDaoMock);
161         simpleMapCache.getAllXtrIdMappings(EID_IPV4);
162
163         Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
164     }
165
166     /**
167      * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
168      */
169     @Test
170     public void getAllXtrIdMappings_withNullEntry() {
171         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
172         Mockito.when(tableMock.getBest(Mockito.any(Eid.class))).thenReturn(null);
173
174         assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
175         Mockito.verify(tableMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
176     }
177
178     /**
179      * Tests {@link SimpleMapCache#getMappingLpmEid} method.
180      */
181     @Test
182     @SuppressWarnings("unchecked")
183     public void getMappingLpmEidTest() throws Exception {
184         final Map<String, Object> mapMock = Mockito.mock(Map.class);
185         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
186                 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
187         final MappingData mappingData = new MappingData(getDefaultMappingRecordBuilder().build());
188
189         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
190         Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4_PREFIX_DST)).thenReturn(mapPair);
191         Mockito.when(mapMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
192         Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD))
193                 .thenReturn(mappingData);       // second invocation
194
195         // with non-expired mapping record
196         assertEquals(mappingData, simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
197     }
198
199     /**
200      * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
201      */
202     @Test
203     @SuppressWarnings("unchecked")
204     public void getMappingLpmEidTest_withNullXtrId() throws Exception {
205         final Map<String, Object> mapMock = Mockito.mock(Map.class);
206         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
207                 NORMALIZED_EID_IPV4_PREFIX_DST, mapMock);
208         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
209         Mockito.when(tableMock.getBestPair(MaskUtil.normalize(EID_IPV4_PREFIX_DST, (short) 24))).thenReturn(mapPair);
210
211         simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, (XtrId) null);
212         Mockito.verify(tableMock).getBestPair(NORMALIZED_EID_IPV4_PREFIX_DST);
213         Mockito.verify(mapMock).get(SubKeys.RECORD);
214     }
215
216     /**
217      * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
218      */
219     @Test
220     @SuppressWarnings("unchecked")
221     public void getMappingTest_withMaskableEid() {
222         final Eid ipv4PrefixEid = LispAddressUtil.asIpv4PrefixEid("192.168.0.225" + "/32");
223         final Map<String, Object> entryMock = Mockito.mock(Map.class);
224         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
225                 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
226
227         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
228         Mockito.when(tableMock.getBestPair(ipv4PrefixEid)).thenReturn(mapPair);
229         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
230         Mockito.when(xtrIdDaoMock.getSpecific(NORMALIZED_EID_IPV4_PREFIX_DST, SubKeys.XTRID_RECORDS)).thenReturn(null);
231
232         simpleMapCache.getMapping(ipv4PrefixEid, XTR_ID);
233         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
234     }
235
236     /**
237      * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
238      */
239     @Test
240     public void getMappingTest_withMaskableEid_noEntry() {
241         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
242         Mockito.when(tableMock.get(Mockito.any(Eid.class))).thenReturn(null);
243
244         assertNull(simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
245     }
246
247     /**
248      * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
249      */
250     @Test
251     @SuppressWarnings("unchecked")
252     public void getMappingTest_withNonMaskableEid() {
253         final Map<String, Object> entryMock = Mockito.mock(Map.class);
254         final SimpleImmutableEntry<Eid, Map<String, ?>> mapPair = new SimpleImmutableEntry<>(
255                 NORMALIZED_EID_IPV4_PREFIX_DST, entryMock);
256
257         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
258         Mockito.when(tableMock.getBestPair(NORMALIZED_EID_IPV4)).thenReturn(mapPair);
259         Mockito.when(entryMock.get(SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
260
261         simpleMapCache.getMapping(EID_IPV4, XTR_ID);
262         Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
263     }
264
265     /**
266      * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
267      */
268     @Test
269     public void getMappingTest_withNonMaskableEid_noEntry() {
270         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
271         Mockito.when(tableMock.get(NORMALIZED_EID_IPV4)).thenReturn(null);
272
273         assertNull(simpleMapCache.getMapping(EID_IPV4, XTR_ID));
274     }
275
276     /**
277      * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
278      */
279     @Test
280     public void getVniTableTest_withVniNull() {
281         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
282
283         simpleMapCache.getMapping(EID_IPV4_PREFIX_2, XTR_ID);
284         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
285     }
286
287     /**
288      * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
289      */
290     @Test
291     public void getVniTableTest_withVniNotNull() {
292         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
293
294         simpleMapCache.getMapping(EID_IPV4_PREFIX_1_VNI, XTR_ID);
295         Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
296     }
297
298     /**
299      * Tests {@link SimpleMapCache#addData} method.
300      */
301     @Test
302     public void addDataTest() {
303         final Object dummyData = "dummy-data";
304         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
305
306         simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
307         Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
308     }
309
310     /**
311      * Tests {@link SimpleMapCache#getData} method.
312      */
313     @Test
314     public void getDataTest() {
315         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
316
317         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
318         Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
319     }
320
321     /**
322      * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
323      */
324     @Test
325     public void getDataTest_withNullVniTable() {
326         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
327
328         simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
329         Mockito.verifyNoMoreInteractions(tableMock);
330     }
331
332     /**
333      * Tests {@link SimpleMapCache#removeData} method.
334      */
335     @Test
336     public void removeDataTest() {
337         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
338
339         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
340         Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
341     }
342
343     /**
344      * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
345      */
346     @Test
347     public void removeDataTest_withNullTable() {
348         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
349
350         simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
351         Mockito.verifyNoMoreInteractions(tableMock);
352     }
353
354     /**
355      * Tests {@link SimpleMapCache#addMapping} method.
356      */
357     @Test
358     public void addMappingTest() throws Exception {
359         final Date timestamp = new Date(System.currentTimeMillis());
360         Mockito.when(mappingDataMock.getTimestamp()).thenReturn(timestamp);
361         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
362         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
363         Mockito.when(mappingDataMock.getXtrId()).thenReturn(XTR_ID);
364
365         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock);
366         Mockito.verify(tableMock)
367                 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingDataMock));
368     }
369
370     /**
371      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
372      */
373     @Test
374     public void getOrInstantiateVniTableTest_withNullVni() throws Exception {
375         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
376         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
377
378         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingDataMock); // Eid VNI_100 == null
379         Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
380         Mockito.verify(daoMock, Mockito.never()).putNestedTable(VNI_0, SubKeys.VNI);
381     }
382
383     /**
384      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
385      */
386     @Test
387     public void getOrInstantiateVniTableTest_withNullVniAndTable() throws Exception {
388         Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
389         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_2, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
390         Mockito.when(daoMock.putNestedTable(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
391
392         simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingDataMock); // Eid VNI_100 == null
393         Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
394     }
395
396     /**
397      * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
398      */
399     @Test
400     public void getOrInstantiateVniTableTest_withNullTable() throws Exception {
401         Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
402         Mockito.when(tableMock.getSpecific(NORMALIZED_EID_1, SubKeys.XTRID_RECORDS)).thenReturn(xtrIdDaoMock);
403         Mockito.when(daoMock.putNestedTable(VNI_100, SubKeys.VNI)).thenReturn(tableMock);
404
405         simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock); // Eid VNI_100 == null
406         Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
407     }
408
409     private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
410         return new MappingRecordBuilder()
411                 .setEid(EID_IPV4)
412                 .setLocatorRecord(Lists.newArrayList())
413                 .setTimestamp(Long.MAX_VALUE)
414                 .setRecordTtl(10)
415                 .setAction(MappingRecord.Action.NativelyForward)
416                 .setSourceRloc(IP_ADDRESS);
417     }
418 }