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 com.google.common.collect.Lists;
14 import java.util.AbstractMap.SimpleImmutableEntry;
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;
34 public class SimpleMapCacheTest {
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;
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});
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);
63 private static final IpAddressBinary IP_ADDRESS = new IpAddressBinary(new Ipv4AddressBinary(IPV4_RLOC_BINARY));
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);
75 * Tests {@link SimpleMapCache#getMapping} method with dstEid == null.
78 public void getMappingTest_withNullDstEid() {
79 assertNull(simpleMapCache.getMapping(null, XTR_ID));
83 * Tests {@link SimpleMapCache#getMapping} method with VNI_100 table == null.
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));
92 * Tests {@link SimpleMapCache#removeMapping} method with xTR-ID.
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);
99 simpleMapCache.removeMapping(EID_IPV4, XTR_ID);
100 Mockito.verify(xtrIdDaoMock).removeSpecific(XTR_ID, SubKeys.RECORD);
104 * Tests {@link SimpleMapCache#removeMapping} method.
107 public void removeMappingTest() {
108 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
110 simpleMapCache.removeMapping(EID_IPV4);
111 Mockito.verify(tableMock).remove(MaskUtil.normalize(EID_IPV4));
112 Mockito.verify(tableMock).isEmpty();
113 Mockito.verifyNoMoreInteractions(tableMock);
117 * Tests {@link SimpleMapCache#removeMapping} method with null VNI_100 table.
120 public void removeMappingTest_withNullVniTable() {
121 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
123 simpleMapCache.removeMapping(EID_IPV4);
124 Mockito.verifyNoMoreInteractions(tableMock);
128 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with maskable address.
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);
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);
144 Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
148 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with non maskable address.
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);
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);
163 Mockito.verify(tableMock).getBest(Mockito.any(Eid.class));
167 * Tests {@link SimpleMapCache#getAllXtrIdMappings} method with null daoEntry.
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);
174 assertNull(simpleMapCache.getAllXtrIdMappings(EID_IPV4_PREFIX_1_VNI));
175 Mockito.verify(tableMock, Mockito.times(1)).getBest(Mockito.any(Eid.class));
179 * Tests {@link SimpleMapCache#getMappingLpmEid} method.
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());
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
195 // with non-expired mapping record
196 assertEquals(mappingData, simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
200 * Tests {@link SimpleMapCache#getMappingLpmEid} method with null XtrId.
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);
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);
217 * Tests {@link SimpleMapCache#getMapping} method with maskable eid.
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);
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);
232 simpleMapCache.getMapping(ipv4PrefixEid, XTR_ID);
233 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
237 * Tests {@link SimpleMapCache#getMapping} method with maskable eid and entry not found.
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);
244 assertNull(simpleMapCache.getMapping(EID_IPV4_PREFIX_DST, XTR_ID));
248 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid.
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);
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);
261 simpleMapCache.getMapping(EID_IPV4, XTR_ID);
262 Mockito.verify(entryMock).get(SubKeys.XTRID_RECORDS);
266 * Tests {@link SimpleMapCache#getMapping} method with non-maskable eid and entry not found.
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);
273 assertNull(simpleMapCache.getMapping(EID_IPV4, XTR_ID));
277 * Tests {@link SimpleMapCache#getMapping} method with Eid VNI_100 == null.
280 public void getVniTableTest_withVniNull() {
281 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
283 simpleMapCache.getMapping(EID_IPV4_PREFIX_2, XTR_ID);
284 Mockito.verify(daoMock).getSpecific(VNI_0, SubKeys.VNI);
288 * Tests {@link SimpleMapCache#getVniTable} method with Eid VNI_100 == 100L.
291 public void getVniTableTest_withVniNotNull() {
292 Mockito.when(daoMock.getSpecific(VNI_100, SubKeys.VNI)).thenReturn(null);
294 simpleMapCache.getMapping(EID_IPV4_PREFIX_1_VNI, XTR_ID);
295 Mockito.verify(daoMock).getSpecific(VNI_100, SubKeys.VNI);
299 * Tests {@link SimpleMapCache#addData} method.
302 public void addDataTest() {
303 final Object dummyData = "dummy-data";
304 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
306 simpleMapCache.addData(EID_IPV4, SubKeys.RECORD, dummyData);
307 Mockito.verify(tableMock).put(NORMALIZED_EID_IPV4, new MappingEntry<>(SubKeys.RECORD, dummyData));
311 * Tests {@link SimpleMapCache#getData} method.
314 public void getDataTest() {
315 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
317 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
318 Mockito.verify(tableMock).getSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
322 * Tests {@link SimpleMapCache#getData} method with no VNI_100 table.
325 public void getDataTest_withNullVniTable() {
326 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
328 simpleMapCache.getData(EID_IPV4, SubKeys.RECORD);
329 Mockito.verifyNoMoreInteractions(tableMock);
333 * Tests {@link SimpleMapCache#removeData} method.
336 public void removeDataTest() {
337 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(tableMock);
339 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
340 Mockito.verify(tableMock).removeSpecific(NORMALIZED_EID_IPV4, SubKeys.RECORD);
344 * Tests {@link SimpleMapCache#removeData} method with no VNI_100 table.
347 public void removeDataTest_withNullTable() {
348 Mockito.when(daoMock.getSpecific(VNI_0, SubKeys.VNI)).thenReturn(null);
350 simpleMapCache.removeData(EID_IPV4, SubKeys.RECORD);
351 Mockito.verifyNoMoreInteractions(tableMock);
355 * Tests {@link SimpleMapCache#addMapping} method.
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);
365 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock);
366 Mockito.verify(tableMock)
367 .put(NORMALIZED_EID_1, new MappingEntry<>(SubKeys.RECORD, mappingDataMock));
371 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null.
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);
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);
384 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == null, table == null.
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);
392 simpleMapCache.addMapping(EID_IPV4_PREFIX_2, mappingDataMock); // Eid VNI_100 == null
393 Mockito.verify(daoMock).putNestedTable(VNI_0, SubKeys.VNI);
397 * Tests {@link SimpleMapCache#getOrInstantiateVniTable} method with vni == 100L, table == null.
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);
405 simpleMapCache.addMapping(EID_IPV4_PREFIX_1_VNI, mappingDataMock); // Eid VNI_100 == null
406 Mockito.verify(daoMock).putNestedTable(VNI_100, SubKeys.VNI);
409 private static MappingRecordBuilder getDefaultMappingRecordBuilder() {
410 return new MappingRecordBuilder()
412 .setLocatorRecord(Lists.newArrayList())
413 .setTimestamp(Long.MAX_VALUE)
415 .setAction(MappingRecord.Action.NativelyForward)
416 .setSourceRloc(IP_ADDRESS);