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.implementation.dao.MappingServiceKeyUtil;
18 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
19 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
20 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
23 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
25 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord.Action;
32 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
33 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
34 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
35 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
36 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
39 * Copyright (c) 2013 Contextream, Inc. and others. All rights reserved.
41 * This program and the accompanying materials are made available under the
42 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
43 * and is available at http://www.eclipse.org/legal/epl-v10.html
46 public class MapResolverTest extends BaseTestCase {
48 // private MapResolver testedMapResolver;
49 private LispMappingService testedMapResolver;
51 private ILispDAO lispDAO;
52 private MapRequestBuilder mapRequest;
53 private LispAFIAddress v4Address;
54 private LispAFIAddress v6Address;
56 private HashMap<IMappingServiceKey, Map<String, MappingServiceValue>> daoResults;
60 public void before() throws Exception {
62 lispDAO = context.mock(ILispDAO.class);
63 testedMapResolver = new LispMappingService();
64 testedMapResolver.basicInit(lispDAO);
66 mapRequest = new MapRequestBuilder();
67 v4Address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
68 v6Address = LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1");
69 daoResults = new HashMap<IMappingServiceKey, Map<String, MappingServiceValue>>();
73 public void handleMapRequest__ReplyWithSingleLocator() throws Exception {
74 mapRequest = getDefaultMapRequestBuilder();
75 mapRequest.getEidRecord().add(
76 new EidRecordBuilder().setMask((short) 32)
77 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
79 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
80 record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
82 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
83 locator.setLocalLocator(false);
84 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
85 locator.setRouted(true);
86 locator.setMulticastPriority((short) 5);
87 locator.setWeight((short) 17);
88 locator.setPriority((short) 16);
89 record.getLocatorRecord().add(locator.build());
90 prepareMapping(record.build());
92 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
94 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
95 assertEquals(1, eidToLocators.getLocatorRecord().size());
96 LocatorRecord resultLocator = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0);
97 assertEquals(locator.isLocalLocator(), resultLocator.isLocalLocator());
98 assertEquals(locator.isRouted(), resultLocator.isRouted());
99 assertEquals(locator.getMulticastPriority(), resultLocator.getMulticastPriority());
100 assertEquals(locator.getMulticastWeight(), resultLocator.getMulticastWeight());
101 assertEquals(locator.getPriority(), resultLocator.getPriority());
102 assertEquals(locator.getWeight(), resultLocator.getWeight());
104 assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators.getLocatorRecord().get(0));
108 public void handleMapRequest__VerifyBasicFields() throws Exception {
109 mapRequest = getDefaultMapRequestBuilder();
110 mapRequest.getEidRecord().add(
111 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
113 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
114 record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
115 record.setRecordTtl(100);
117 record.setAuthoritative(true);
118 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
119 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
120 record.getLocatorRecord().add(locator.build());
121 prepareMapping(record.build());
123 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
125 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
126 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
127 assertEquals((byte) 32, eidToLocators.getMaskLength().byteValue());
128 assertEquals(v4Address, LispAFIConvertor.toAFI(eidToLocators.getLispAddressContainer()));
129 assertEquals(record.isAuthoritative(), eidToLocators.isAuthoritative());
130 assertEquals(record.getAction(), eidToLocators.getAction());
131 assertEquals(record.getRecordTtl(), eidToLocators.getRecordTtl());
135 public void handleMapRequest__VerifyMask() throws Exception {
136 mapRequest = getDefaultMapRequestBuilder();
137 mapRequest.getEidRecord().add(
138 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
140 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
141 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.0")));
142 record.setMaskLength((short) 24);
144 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
145 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
146 record.getLocatorRecord().add(locator.build());
147 prepareMapping(record.build());
149 MapRequest mr = mapRequest.build();
151 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
153 assertEquals(mr.getNonce(), mapReply.getNonce());
154 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
155 assertEquals((byte) 32, eidToLocators.getMaskLength().byteValue());
156 assertEquals(v4Address, LispAFIConvertor.toAFI(eidToLocators.getLispAddressContainer()));
160 public void handleMapRequest__VerifyMaskIPv6() throws Exception {
161 mapRequest = getDefaultMapRequestBuilder();
162 mapRequest.getEidRecord().add(
163 new EidRecordBuilder().setMask((short) 128).setLispAddressContainer(LispAFIConvertor.toContainer(v6Address)).build());
165 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
166 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:0")));
167 record.setMaskLength((short) 128);
169 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
170 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
171 record.getLocatorRecord().add(locator.build());
172 prepareMapping(record.build());
174 MapRequest mr = mapRequest.build();
176 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
178 assertEquals(mr.getNonce(), mapReply.getNonce());
179 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
180 assertEquals((byte) 128, eidToLocators.getMaskLength().byteValue());
181 assertEquals(v6Address, LispAFIConvertor.toAFI(eidToLocators.getLispAddressContainer()));
185 public void handleMapRequest__VerifyMaskIPv6NoMatch() throws Exception {
186 mapRequest = getDefaultMapRequestBuilder();
187 mapRequest.getEidRecord().add(
188 new EidRecordBuilder().setMask((short) 128).setLispAddressContainer(LispAFIConvertor.toContainer(v6Address)).build());
190 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
191 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:1:0:0:0:0:0:1")));
192 record.setMaskLength((short) 112);
194 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
195 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
196 record.getLocatorRecord().add(locator.build());
197 prepareMapping(record.build());
199 MapRequest mr = mapRequest.build();
201 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
202 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
203 assertEquals(0, eidToLocators.getLocatorRecord().size());
207 public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
209 mapRequest = getDefaultMapRequestBuilder();
210 mapRequest.getEidRecord().add(
211 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
213 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
214 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.4.0")));
215 record.setMaskLength((short) 24);
217 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
218 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
219 record.getLocatorRecord().add(locator.build());
220 prepareMapping(record.build());
222 MapRequest mr = mapRequest.build();
224 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
226 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
227 assertEquals(0, eidToLocators.getLocatorRecord().size());
232 public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
234 mapRequest = getDefaultMapRequestBuilder();
235 mapRequest.getEidRecord().add(
236 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
238 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
239 record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
241 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
242 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
243 record.getLocatorRecord().add(locator.build());
244 locator = getDefaultLocatorBuilder();
245 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")));
246 record.getLocatorRecord().add(locator.build());
247 locator = getDefaultLocatorBuilder();
248 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.8.2.7")));
249 record.getLocatorRecord().add(locator.build());
251 prepareMapping(record.build());
253 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
255 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
256 assertEquals(3, eidToLocators.getLocatorRecord().size());
258 assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators.getLocatorRecord().get(0));
259 assertLocator(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"), eidToLocators.getLocatorRecord().get(1));
260 assertLocator(LispAFIConvertor.asIPAfiAddress("1.8.2.7"), eidToLocators.getLocatorRecord().get(2));
264 public void handleMapRequest__MultipleEIDs() throws Exception {
266 mapRequest = getDefaultMapRequestBuilder();
267 mapRequest.getEidRecord().add(
268 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
269 mapRequest.getEidRecord().add(
270 new EidRecordBuilder().setMask((short) 128).setLispAddressContainer(LispAFIConvertor.toContainer(v6Address)).build());
272 EidToLocatorRecordBuilder record1 = getDefaultEidToLocatorBuilder();
273 record1.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
274 record1.setRecordTtl(100);
276 LocatorRecordBuilder locator1 = getDefaultLocatorBuilder();
277 locator1.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
278 record1.getLocatorRecord().add(locator1.build());
280 EidToLocatorRecordBuilder record2 = getDefaultEidToLocatorBuilder();
281 record2.setLispAddressContainer(LispAFIConvertor.toContainer(v6Address));
282 record2.setMaskLength((short) 128);
283 record2.setRecordTtl(100);
285 LocatorRecordBuilder locator2 = getDefaultLocatorBuilder();
286 locator2.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")));
287 record2.getLocatorRecord().add(locator2.build());
289 prepareMapping(record1.build(), record2.build());
291 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
293 EidToLocatorRecord eidToLocators1 = mapReply.getEidToLocatorRecord().get(0);
294 assertEquals(1, eidToLocators1.getLocatorRecord().size());
295 assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators1.getLocatorRecord().get(0));
297 EidToLocatorRecord eidToLocators2 = mapReply.getEidToLocatorRecord().get(1);
298 assertEquals(1, eidToLocators2.getLocatorRecord().size());
299 assertLocator(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"), eidToLocators2.getLocatorRecord().get(0));
302 private void assertLocator(LispAFIAddress expectedAddress, LocatorRecord locatorRecord) {
303 assertEquals(LispAFIConvertor.toContainer(expectedAddress), locatorRecord.getLispAddressContainer());
304 Assert.assertTrue(locatorRecord.isRouted());
307 private Map<String, MappingServiceValue> prepareMapping(EidToLocatorRecord... records) {
308 if (records.length > 0) {
309 for (EidToLocatorRecord eidToLocatorRecord : records) {
310 MappingServiceValue value = new MappingServiceValue();
311 Map<String, MappingServiceValue> result = new HashMap<String, MappingServiceValue>();
312 result.put("value", value);
313 List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
314 for (LocatorRecord locator : eidToLocatorRecord.getLocatorRecord()) {
315 rlocs.add(new MappingServiceRLOC(locator, eidToLocatorRecord.getRecordTtl(), eidToLocatorRecord.getAction(), eidToLocatorRecord
316 .isAuthoritative()));
318 value.setRlocs(rlocs);
321 MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getLispAddressContainer(),
322 (short) eidToLocatorRecord.getMaskLength()), result);
326 ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
328 protected boolean validate(IMappingServiceKey value) {
333 public Object invoke(Invocation invocation) throws Throwable {
334 return daoResults.get(lastValue);
338 allowing(lispDAO).get(with(daoGetSaverAction));
339 will(daoGetSaverAction);
341 return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(LispAFIConvertor.toContainer(v4Address)));
344 private MapRequestBuilder getDefaultMapRequestBuilder() {
345 MapRequestBuilder mrBuilder = new MapRequestBuilder();
346 mrBuilder.setAuthoritative(false);
347 mrBuilder.setEidRecord(new ArrayList<EidRecord>());
348 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
349 mrBuilder.setMapDataPresent(true);
350 mrBuilder.setNonce((long) 0);
351 mrBuilder.setPitr(false);
352 mrBuilder.setProbe(false);
353 mrBuilder.setSmr(false);
354 mrBuilder.setSmrInvoked(true);
355 mrBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
356 LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("127.0.0.1"))).build());
360 private EidToLocatorRecordBuilder getDefaultEidToLocatorBuilder() {
361 EidToLocatorRecordBuilder builder = new EidToLocatorRecordBuilder();
362 builder.setAction(Action.NoAction);
363 builder.setAuthoritative(false);
364 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
365 builder.setMapVersion((short) 0);
366 builder.setMaskLength((short) 32);
367 builder.setRecordTtl(60);
371 private LocatorRecordBuilder getDefaultLocatorBuilder() {
372 LocatorRecordBuilder builder = new LocatorRecordBuilder();
373 builder.setLocalLocator(false);
374 builder.setMulticastPriority((short) 0);
375 builder.setMulticastWeight((short) 0);
376 builder.setPriority((short) 0);
377 builder.setRlocProbed(false);
378 builder.setRouted(true);
379 builder.setWeight((short) 0);