1 package org.opendaylight.lispflowmapping.implementation.lisp;
3 import static org.junit.Assert.assertEquals;
5 import java.util.ArrayList;
6 import java.util.HashMap;
10 import junit.framework.Assert;
12 import org.jmock.api.Invocation;
13 import org.junit.Before;
14 import org.junit.Ignore;
15 import org.junit.Test;
16 import org.opendaylight.lispflowmapping.implementation.LispMappingService;
17 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
18 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKeyUtil;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
22 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
23 import org.opendaylight.lispflowmapping.type.lisp.EidRecord;
24 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
25 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
26 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
27 import org.opendaylight.lispflowmapping.type.lisp.MapReplyAction;
28 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
31 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
34 * Copyright (c) 2013 Contextream, Inc. and others. All rights reserved.
36 * This program and the accompanying materials are made available under the
37 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
38 * and is available at http://www.eclipse.org/legal/epl-v10.html
41 public class MapResolverTest extends BaseTestCase {
43 // private MapResolver testedMapResolver;
44 private LispMappingService testedMapResolver;
46 private ILispDAO lispDAO;
47 private MapRequest mapRequest;
48 private LispIpv4Address v4Address;
49 private LispIpv6Address v6Address;
51 private HashMap<IMappingServiceKey, Map<String, MappingServiceValue>> daoResults;
55 public void before() throws Exception {
57 lispDAO = context.mock(ILispDAO.class);
58 testedMapResolver = new LispMappingService();
59 testedMapResolver.basicInit(lispDAO);
61 mapRequest = new MapRequest();
62 v4Address = new LispIpv4Address("1.2.3.4");
63 v6Address = new LispIpv6Address("0:0:0:0:0:0:0:1");
64 daoResults = new HashMap<IMappingServiceKey, Map<String, MappingServiceValue>>();
69 public void handleMapRequest__NumRLOCsMismatch() throws Exception {
70 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
71 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
72 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
73 // Map<String, Object> rlocs = prepareMapping(record);
74 // rlocs.put("NumRLOCs", 5);
76 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
78 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
79 assertEquals(1, eidToLocators.getLocators().size());
80 assertLocator(new LispIpv4Address(0x44332211), eidToLocators.getLocators().get(0));
85 public void handleMapRequest__BadRLOCType() throws Exception {
86 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
87 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
88 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x71717171)));
89 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
90 // Map<String, Object> rlocs = prepareMapping(record);
91 // rlocs.put("RLOC0", "Ooga booga");
93 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
95 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
96 assertEquals(1, eidToLocators.getLocators().size());
97 assertLocator(new LispIpv4Address(0x44332211), eidToLocators.getLocators().get(0));
102 public void handleMapRequest__NoNumRLOCs() throws Exception {
103 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
104 allowing(lispDAO).get(wany(IMappingServiceKey.class));
105 ret(new HashMap<String, Object>());
107 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
109 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
110 assertEquals(0, eidToLocators.getLocators().size());
115 public void handleMapRequest__IllegalNumRLOCs() throws Exception {
116 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
117 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
118 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
119 // Map<String, Object> rlocs = prepareMapping(record);
120 // rlocs.put("NumRLOCs", "Bla");
122 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
124 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
125 assertEquals(0, eidToLocators.getLocators().size());
129 public void handleMapRequest__ReplyWithSingleLocator() throws Exception {
130 mapRequest.addEidRecord(new EidRecord((byte) 0, new LispIpv4Address("1.2.3.4")));
132 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
133 record.setRecordTtl(100);
134 LocatorRecord locator = new LocatorRecord().setLocator(new LispIpv4Address(0x04030201));
135 locator.setLocalLocator(false);
136 locator.setRouted(true);
137 locator.setMulticastPriority((byte) 5);
138 locator.setMulticastWeight((byte) 17);
139 locator.setPriority((byte) 16);
140 record.addLocator(locator);
141 prepareMapping(record);
143 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
145 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
146 assertEquals(1, eidToLocators.getLocators().size());
147 LocatorRecord resultLocator = mapReply.getEidToLocatorRecords().get(0).getLocators().get(0);
148 assertEquals(locator.isLocalLocator(), resultLocator.isLocalLocator());
149 assertEquals(locator.isRouted(), resultLocator.isRouted());
150 assertEquals(locator.getMulticastPriority(), resultLocator.getMulticastPriority());
151 assertEquals(locator.getMulticastWeight(), resultLocator.getMulticastWeight());
152 assertEquals(locator.getPriority(), resultLocator.getPriority());
154 assertLocator(new LispIpv4Address(0x04030201), eidToLocators.getLocators().get(0));
158 public void handleMapRequest__VerifyBasicFields() throws Exception {
159 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
161 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
162 record.setRecordTtl(100);
163 record.setAction(MapReplyAction.NativelyForward);
164 record.setAuthoritative(true);
165 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
166 prepareMapping(record);
168 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
170 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
171 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
172 assertEquals((byte) 0, eidToLocators.getMaskLength());
173 assertEquals(v4Address, eidToLocators.getPrefix());
174 assertEquals(record.isAuthoritative(), eidToLocators.isAuthoritative());
175 assertEquals(record.getAction(), eidToLocators.getAction());
176 assertEquals(record.getRecordTtl(), eidToLocators.getRecordTtl());
180 public void handleMapRequest__VerifyMask() throws Exception {
181 EidRecord mapRequestRecord = new EidRecord((byte) 0, v4Address);
182 mapRequestRecord.setMaskLength(32);
183 mapRequest.addEidRecord(mapRequestRecord);
185 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv4Address("1.2.3.0")).setMaskLength(24);
186 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
187 prepareMapping(record);
189 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
191 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
192 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
193 assertEquals((byte) 32, eidToLocators.getMaskLength());
194 assertEquals(v4Address, eidToLocators.getPrefix());
198 public void handleMapRequest__VerifyMaskIPv6() throws Exception {
199 EidRecord mapRequestRecord = new EidRecord((byte) 0, v6Address);
200 mapRequestRecord.setMaskLength(128);
201 mapRequest.addEidRecord(mapRequestRecord);
203 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv6Address("0:0:0:0:0:0:0:0")).setMaskLength(128);
204 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
205 prepareMapping(record);
207 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
209 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
210 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
211 assertEquals((byte) 128, eidToLocators.getMaskLength());
212 assertEquals(v6Address, eidToLocators.getPrefix());
216 public void handleMapRequest__VerifyMaskIPv6NoMatch() throws Exception {
217 EidRecord mapRequestRecord = new EidRecord((byte) 128, v6Address);
218 mapRequest.addEidRecord(mapRequestRecord);
220 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv6Address("0:1:0:0:0:0:0:1")).setMaskLength(112);
221 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
222 prepareMapping(record);
224 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
226 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
227 assertEquals(0, eidToLocators.getLocators().size());
231 public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
232 EidRecord mapRequestRecord = new EidRecord((byte) 0, v4Address);
233 mapRequestRecord.setMaskLength(32);
234 mapRequest.addEidRecord(mapRequestRecord);
236 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv4Address("1.2.4.0")).setMaskLength(24);
237 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
238 prepareMapping(record);
240 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
242 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
243 assertEquals(0, eidToLocators.getLocators().size());
248 public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
249 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
251 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
252 record.setRecordTtl(100);
253 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
254 record.addLocator(new LocatorRecord().setLocator(new LispIpv6Address("::1")));
255 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address("1.8.2.7")));
256 prepareMapping(record);
258 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
260 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
261 assertEquals(3, eidToLocators.getLocators().size());
263 assertLocator(new LispIpv4Address(0x04030201), eidToLocators.getLocators().get(0));
264 assertLocator(new LispIpv6Address("::1"), eidToLocators.getLocators().get(1));
265 assertLocator(new LispIpv4Address("1.8.2.7"), eidToLocators.getLocators().get(2));
268 private void assertLocator(LispAddress expectedAddress, LocatorRecord locatorRecord) {
269 assertEquals(expectedAddress, locatorRecord.getLocator());
270 Assert.assertTrue(locatorRecord.isRouted());
274 public void handleMapRequest__MultipleEIDs() throws Exception {
275 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
276 mapRequest.addEidRecord(new EidRecord((byte) 0, v6Address));
278 EidToLocatorRecord record1 = new EidToLocatorRecord().setPrefix(v4Address);
279 record1.setRecordTtl(100);
280 record1.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
282 EidToLocatorRecord record2 = new EidToLocatorRecord().setPrefix(v6Address);
283 record2.addLocator(new LocatorRecord().setLocator(new LispIpv6Address("0:0:0:0:0:0:0:1")));
284 record2.setRecordTtl(100);
285 prepareMapping(record1, record2);
287 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
289 EidToLocatorRecord eidToLocators1 = mapReply.getEidToLocatorRecords().get(0);
290 assertEquals(1, eidToLocators1.getLocators().size());
291 assertLocator(new LispIpv4Address(0x04030201), eidToLocators1.getLocators().get(0));
293 EidToLocatorRecord eidToLocators2 = mapReply.getEidToLocatorRecords().get(1);
294 assertEquals(1, eidToLocators2.getLocators().size());
295 assertLocator(new LispIpv6Address("0:0:0:0:0:0:0:1"), eidToLocators2.getLocators().get(0));
298 private Map<String, MappingServiceValue> prepareMapping(EidToLocatorRecord... records) {
299 if (records.length > 0) {
300 for (EidToLocatorRecord eidToLocatorRecord : records) {
301 MappingServiceValue value = new MappingServiceValue();
302 Map<String, MappingServiceValue> result = new HashMap<String, MappingServiceValue>();
303 result.put("value", value);
304 List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
305 for (LocatorRecord locator : eidToLocatorRecord.getLocators()) {
306 rlocs.add(new MappingServiceRLOC(locator, eidToLocatorRecord.getRecordTtl(), eidToLocatorRecord.getAction(), eidToLocatorRecord
307 .isAuthoritative()));
309 value.setRlocs(rlocs);
312 MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getPrefix(), (byte) eidToLocatorRecord.getMaskLength()),
317 ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
319 protected boolean validate(IMappingServiceKey value) {
324 public Object invoke(Invocation invocation) throws Throwable {
325 return daoResults.get(lastValue);
329 allowing(lispDAO).get(with(daoGetSaverAction));
330 will(daoGetSaverAction);
332 return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(v4Address));