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.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKeyUtil;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.lispflowmapping.type.lisp.EidRecord;
23 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
24 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
25 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
26 import org.opendaylight.lispflowmapping.type.lisp.MapReplyAction;
27 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
28 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
33 * Copyright (c) 2013 Contextream, Inc. and others. All rights reserved.
35 * This program and the accompanying materials are made available under the
36 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
37 * and is available at http://www.eclipse.org/legal/epl-v10.html
40 public class MapResolverTest extends BaseTestCase {
42 private MapResolver testedMapResolver;
44 private ILispDAO lispDAO;
45 private MapRequest mapRequest;
46 private LispIpv4Address v4Address;
47 private LispIpv6Address v6Address;
49 private HashMap<IMappingServiceKey, Map<String, MappingServiceValue>> daoResults;
53 public void before() throws Exception {
55 lispDAO = context.mock(ILispDAO.class);
56 testedMapResolver = new MapResolver(lispDAO);
58 mapRequest = new MapRequest();
59 v4Address = new LispIpv4Address("1.2.3.4");
60 v6Address = new LispIpv6Address("0:0:0:0:0:0:0:1");
61 daoResults = new HashMap<IMappingServiceKey, Map<String, MappingServiceValue>>();
66 public void handleMapRequest__NumRLOCsMismatch() throws Exception {
67 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
68 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
69 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
70 // Map<String, Object> rlocs = prepareMapping(record);
71 // rlocs.put("NumRLOCs", 5);
73 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
75 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
76 assertEquals(1, eidToLocators.getLocators().size());
77 assertLocator(new LispIpv4Address(0x44332211), eidToLocators.getLocators().get(0));
82 public void handleMapRequest__BadRLOCType() throws Exception {
83 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
84 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
85 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x71717171)));
86 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
87 // Map<String, Object> rlocs = prepareMapping(record);
88 // rlocs.put("RLOC0", "Ooga booga");
90 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
92 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
93 assertEquals(1, eidToLocators.getLocators().size());
94 assertLocator(new LispIpv4Address(0x44332211), eidToLocators.getLocators().get(0));
99 public void handleMapRequest__NoNumRLOCs() throws Exception {
100 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
101 allowing(lispDAO).get(wany(IMappingServiceKey.class));
102 ret(new HashMap<String, Object>());
104 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
106 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
107 assertEquals(0, eidToLocators.getLocators().size());
112 public void handleMapRequest__IllegalNumRLOCs() throws Exception {
113 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
114 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
115 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
116 // Map<String, Object> rlocs = prepareMapping(record);
117 // rlocs.put("NumRLOCs", "Bla");
119 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
121 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
122 assertEquals(0, eidToLocators.getLocators().size());
126 public void handleMapRequest__ReplyWithSingleLocator() throws Exception {
127 mapRequest.addEidRecord(new EidRecord((byte) 0, new LispIpv4Address("1.2.3.4")));
129 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
130 LocatorRecord locator = new LocatorRecord().setLocator(new LispIpv4Address(0x04030201));
131 locator.setLocalLocator(false);
132 locator.setRouted(true);
133 locator.setMulticastPriority((byte) 5);
134 locator.setMulticastWeight((byte) 17);
135 locator.setPriority((byte) 16);
136 record.addLocator(locator);
137 prepareMapping(record);
139 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
141 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
142 assertEquals(1, eidToLocators.getLocators().size());
143 LocatorRecord resultLocator = mapReply.getEidToLocatorRecords().get(0).getLocators().get(0);
144 assertEquals(locator.isLocalLocator(), resultLocator.isLocalLocator());
145 assertEquals(locator.isRouted(), resultLocator.isRouted());
146 assertEquals(locator.getMulticastPriority(), resultLocator.getMulticastPriority());
147 assertEquals(locator.getMulticastWeight(), resultLocator.getMulticastWeight());
148 assertEquals(locator.getPriority(), resultLocator.getPriority());
150 assertLocator(new LispIpv4Address(0x04030201), eidToLocators.getLocators().get(0));
154 public void handleMapRequest__VerifyBasicFields() throws Exception {
155 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
157 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
158 record.setAction(MapReplyAction.NativelyForward);
159 record.setAuthoritative(true);
160 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
161 prepareMapping(record);
163 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
165 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
166 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
167 assertEquals((byte) 0, eidToLocators.getMaskLength());
168 assertEquals(v4Address, eidToLocators.getPrefix());
169 assertEquals(record.isAuthoritative(), eidToLocators.isAuthoritative());
170 assertEquals(record.getAction(), eidToLocators.getAction());
171 assertEquals(record.getRecordTtl(), eidToLocators.getRecordTtl());
175 public void handleMapRequest__VerifyMask() throws Exception {
176 EidRecord mapRequestRecord = new EidRecord((byte) 0, v4Address);
177 mapRequestRecord.setMaskLength(32);
178 mapRequest.addEidRecord(mapRequestRecord);
180 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv4Address("1.2.3.0")).setMaskLength(24);
181 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
182 prepareMapping(record);
184 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
186 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
187 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
188 assertEquals((byte) 32, eidToLocators.getMaskLength());
189 assertEquals(v4Address, eidToLocators.getPrefix());
193 public void handleMapRequest__VerifyMaskIPv6() throws Exception {
194 EidRecord mapRequestRecord = new EidRecord((byte) 0, v6Address);
195 mapRequestRecord.setMaskLength(128);
196 mapRequest.addEidRecord(mapRequestRecord);
198 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv6Address("0:0:0:0:0:0:0:0")).setMaskLength(128);
199 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
200 prepareMapping(record);
202 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
204 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
205 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
206 assertEquals((byte) 128, eidToLocators.getMaskLength());
207 assertEquals(v6Address, eidToLocators.getPrefix());
211 public void handleMapRequest__VerifyMaskIPv6NoMatch() throws Exception {
212 EidRecord mapRequestRecord = new EidRecord((byte) 128, v6Address);
213 mapRequest.addEidRecord(mapRequestRecord);
215 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv6Address("0:0:0:0:0:0:0:1")).setMaskLength(112);
216 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
217 prepareMapping(record);
219 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
221 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
222 assertEquals(0, eidToLocators.getLocators().size());
226 public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
227 EidRecord mapRequestRecord = new EidRecord((byte) 0, v4Address);
228 mapRequestRecord.setMaskLength(32);
229 mapRequest.addEidRecord(mapRequestRecord);
231 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv4Address("1.2.4.0")).setMaskLength(24);
232 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
233 prepareMapping(record);
235 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
237 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
238 assertEquals(0, eidToLocators.getLocators().size());
243 public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
244 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
246 EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
247 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
248 record.addLocator(new LocatorRecord().setLocator(new LispIpv6Address("::1")));
249 record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address("1.8.2.7")));
250 prepareMapping(record);
252 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
254 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
255 assertEquals(3, eidToLocators.getLocators().size());
257 assertLocator(new LispIpv4Address(0x04030201), eidToLocators.getLocators().get(0));
258 assertLocator(new LispIpv6Address("::1"), eidToLocators.getLocators().get(1));
259 assertLocator(new LispIpv4Address("1.8.2.7"), eidToLocators.getLocators().get(2));
262 private void assertLocator(LispAddress expectedAddress, LocatorRecord locatorRecord) {
263 assertEquals(expectedAddress, locatorRecord.getLocator());
264 Assert.assertTrue(locatorRecord.isRouted());
268 public void handleMapRequest__MultipleEIDs() throws Exception {
269 mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
270 mapRequest.addEidRecord(new EidRecord((byte) 0, v6Address));
272 EidToLocatorRecord record1 = new EidToLocatorRecord().setPrefix(v4Address);
273 record1.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
275 EidToLocatorRecord record2 = new EidToLocatorRecord().setPrefix(v6Address);
276 record2.addLocator(new LocatorRecord().setLocator(new LispIpv6Address("0:0:0:0:0:0:0:1")));
277 // prepareMapping(record2);
278 prepareMapping(record1, record2);
280 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
282 EidToLocatorRecord eidToLocators1 = mapReply.getEidToLocatorRecords().get(0);
283 assertEquals(1, eidToLocators1.getLocators().size());
284 assertLocator(new LispIpv4Address(0x04030201), eidToLocators1.getLocators().get(0));
286 EidToLocatorRecord eidToLocators2 = mapReply.getEidToLocatorRecords().get(1);
287 assertEquals(1, eidToLocators2.getLocators().size());
288 assertLocator(new LispIpv6Address("0:0:0:0:0:0:0:1"), eidToLocators2.getLocators().get(0));
291 private Map<String, MappingServiceValue> prepareMapping(EidToLocatorRecord... records) {
292 if (records.length > 0) {
293 for (EidToLocatorRecord eidToLocatorRecord : records) {
294 MappingServiceValue value = new MappingServiceValue();
295 Map<String, MappingServiceValue> result = new HashMap<String, MappingServiceValue>();
296 result.put("value", value);
297 List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
298 for (LocatorRecord locator : eidToLocatorRecord.getLocators()) {
299 rlocs.add(new MappingServiceRLOC(locator, eidToLocatorRecord.getRecordTtl(), eidToLocatorRecord.getAction(), eidToLocatorRecord
300 .isAuthoritative()));
302 value.setRlocs(rlocs);
305 MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getPrefix(), (byte) eidToLocatorRecord.getMaskLength()),
310 ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
312 protected boolean validate(IMappingServiceKey value) {
317 public Object invoke(Invocation invocation) throws Throwable {
318 return daoResults.get(lastValue);
322 allowing(lispDAO).get(with(daoGetSaverAction));
323 will(daoGetSaverAction);
325 return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(v4Address));