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