2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.mapcache;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
13 import java.util.AbstractMap.SimpleImmutableEntry;
14 import java.util.ArrayList;
15 import java.util.Date;
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;
35 public class SimpleMapCacheTest {
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;
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});
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);
64 private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
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);
76 * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
79 public void getMappingTest_withNullDstEid() {
80 assertNull(simpleMapCache.getMapping(null, XTR_ID));
84 * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
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));
93 * Tests {@link SimpleMapCache#removeMapping} method with xTR-ID.
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);
100 simpleMapCache.removeMapping(EID_IPV4, XTR_ID);
101 Mockito.verify(xtrIdDaoMock).removeSpecific(XTR_ID, SubKeys.RECORD);
105 * Tests {@link SimpleMapCache#removeMapping} method.
108 public void removeMappingTest() {
109 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
111 simpleMapCache.removeMapping(EID_IPV4);
112 Mockito.verify(tableMock).remove(MaskUtil.normalize(EID_IPV4));
113 Mockito.verify(tableMock).isEmpty();
114 Mockito.verifyNoMoreInteractions(tableMock);
118 * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
121 public void removeMappingTest_withNullVniTable() {
122 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
124 simpleMapCache.removeMapping(EID_IPV4);
125 Mockito.verifyNoMoreInteractions(tableMock);
129 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
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);
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);
145 Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
149 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
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);
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);
164 Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
168 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
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);
175 assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
176 Mockito.verify(tableMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
180 * Tests {@link SimpleMapCache#getMappingLpmEid} method.
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());
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);
194 Mockito.when(xtrIdDaoMock.getSpecific(XTR_ID, SubKeys.RECORD)).thenReturn(mappingData);
196 // with non-expired mapping record
197 assertEquals(mappingData, simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
201 * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
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);
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);
218 * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
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);
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);
233 simpleMapCache.getMapping(ipv4PrefixEid, XTR_ID);
234 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
238 * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
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);
245 assertNull(simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
249 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
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);
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);
262 simpleMapCache.getMapping(EID_IPV4, XTR_ID);
263 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
267 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
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);
274 assertNull(simpleMapCache.getMapping(EID_IPV4, XTR_ID));
278 * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
281 public void getVniTableTest_withVniNull() {
282 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
284 simpleMapCache.getMapping(EID_IPV4_PREFIX_2, XTR_ID);
285 Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
289 * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
292 public void getVniTableTest_withVniNotNull() {
293 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
295 simpleMapCache.getMapping(EID_IPV4_PREFIX_1_VNI, XTR_ID);
296 Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
300 * Tests {@link SimpleMapCache#addData} method.
303 public void addDataTest() {
304 final Object dummyData = "dummy-data";
305 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
307 simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
308 Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
312 * Tests {@link SimpleMapCache#getData} method.
315 public void getDataTest() {
316 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
318 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
319 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
323 * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
326 public void getDataTest_withNullVniTable() {
327 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
329 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
330 Mockito.verifyNoMoreInteractions(tableMock);
334 * Tests {@link SimpleMapCache#removeData} method.
337 public void removeDataTest() {
338 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
340 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
341 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
345 * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
348 public void removeDataTest_withNullTable() {
349 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
351 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
352 Mockito.verifyNoMoreInteractions(tableMock);
356 * Tests {@link SimpleMapCache#addMapping} method.
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);
366 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock);
367 Mockito.verify(tableMock)
368 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingDataMock));
372 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
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);
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);
386 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
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);
394 // Eid VNI_100 == null
395 simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingDataMock);
396 Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
400 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
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);
408 // Eid VNI_100 == null
409 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock);
410 Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
413 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
414 return new MappingRecordBuilder()
416 .setLocatorRecord(new ArrayList<>())
417 .setTimestamp(Long.MAX_VALUE)
419 .setAction(MappingRecord.Action.NativelyForward)
420 .setSourceRloc(IP_ADDRESS);