2 * Copyright (c) 2014 Contextream, 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.implementation.lisp;
10 import static org.junit.Assert.assertEquals;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
17 import junit.framework.Assert;
19 import org.jmock.api.Invocation;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.lispflowmapping.implementation.LispMappingService;
23 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
24 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
25 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
26 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
27 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
28 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
32 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
33 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
34 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
35 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord.Action;
36 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
37 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
38 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
39 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
40 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
43 public class MapResolverTest extends BaseTestCase {
45 // private MapResolver testedMapResolver;
46 private LispMappingService testedMapResolver;
48 private ILispDAO lispDAO;
49 private MapRequestBuilder mapRequest;
50 private LispAFIAddress v4Address;
51 private LispAFIAddress v6Address;
53 private HashMap<IMappingServiceKey, Map<String, MappingServiceRLOCGroup>> daoResults;
57 public void before() throws Exception {
59 lispDAO = context.mock(ILispDAO.class);
60 testedMapResolver = new LispMappingService();
61 testedMapResolver.basicInit(lispDAO);
63 mapRequest = new MapRequestBuilder();
64 v4Address = LispAFIConvertor.asIPAfiAddress("1.2.3.4");
65 v6Address = LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1");
66 daoResults = new HashMap<IMappingServiceKey, Map<String, MappingServiceRLOCGroup>>();
70 public void handleMapRequest__ReplyWithSingleLocator() throws Exception {
71 mapRequest = getDefaultMapRequestBuilder();
72 mapRequest.getEidRecord().add(
73 new EidRecordBuilder().setMask((short) 32)
74 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
76 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
77 record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
79 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
80 locator.setLocalLocator(false);
81 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
82 locator.setRouted(true);
83 locator.setMulticastPriority((short) 5);
84 locator.setWeight((short) 17);
85 locator.setPriority((short) 16);
86 record.getLocatorRecord().add(locator.build());
87 prepareMapping(record.build());
89 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
91 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
92 assertEquals(1, eidToLocators.getLocatorRecord().size());
93 LocatorRecord resultLocator = mapReply.getEidToLocatorRecord().get(0).getLocatorRecord().get(0);
94 assertEquals(locator.isLocalLocator(), resultLocator.isLocalLocator());
95 assertEquals(locator.isRouted(), resultLocator.isRouted());
96 assertEquals(locator.getMulticastPriority(), resultLocator.getMulticastPriority());
97 assertEquals(locator.getMulticastWeight(), resultLocator.getMulticastWeight());
98 assertEquals(locator.getPriority(), resultLocator.getPriority());
99 assertEquals(locator.getWeight(), resultLocator.getWeight());
101 assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators.getLocatorRecord().get(0));
105 public void handleMapRequest__VerifyBasicFields() throws Exception {
106 mapRequest = getDefaultMapRequestBuilder();
107 mapRequest.getEidRecord().add(
108 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
110 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
111 record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
112 record.setRecordTtl(100);
114 record.setAuthoritative(true);
115 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
116 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
117 record.getLocatorRecord().add(locator.build());
118 prepareMapping(record.build());
120 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
122 assertEquals(mapRequest.getNonce(), mapReply.getNonce());
123 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
124 assertEquals((byte) 32, eidToLocators.getMaskLength().byteValue());
125 assertEquals(v4Address, LispAFIConvertor.toAFI(eidToLocators.getLispAddressContainer()));
126 assertEquals(record.isAuthoritative(), eidToLocators.isAuthoritative());
127 assertEquals(record.getAction(), eidToLocators.getAction());
128 assertEquals(record.getRecordTtl(), eidToLocators.getRecordTtl());
132 public void handleMapRequest__VerifyMask() throws Exception {
133 mapRequest = getDefaultMapRequestBuilder();
134 mapRequest.getEidRecord().add(
135 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
137 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
138 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.0")));
139 record.setMaskLength((short) 24);
141 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
142 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
143 record.getLocatorRecord().add(locator.build());
144 prepareMapping(record.build());
146 MapRequest mr = mapRequest.build();
148 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
150 assertEquals(mr.getNonce(), mapReply.getNonce());
151 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
152 assertEquals((byte) 32, eidToLocators.getMaskLength().byteValue());
153 assertEquals(v4Address, LispAFIConvertor.toAFI(eidToLocators.getLispAddressContainer()));
157 public void handleMapRequest__VerifyMaskIPv6() throws Exception {
158 mapRequest = getDefaultMapRequestBuilder();
159 mapRequest.getEidRecord().add(
160 new EidRecordBuilder().setMask((short) 128).setLispAddressContainer(LispAFIConvertor.toContainer(v6Address)).build());
162 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
163 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:0")));
164 record.setMaskLength((short) 128);
166 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
167 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
168 record.getLocatorRecord().add(locator.build());
169 prepareMapping(record.build());
171 MapRequest mr = mapRequest.build();
173 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
175 assertEquals(mr.getNonce(), mapReply.getNonce());
176 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
177 assertEquals((byte) 128, eidToLocators.getMaskLength().byteValue());
178 assertEquals(v6Address, LispAFIConvertor.toAFI(eidToLocators.getLispAddressContainer()));
182 public void handleMapRequest__VerifyMaskIPv6NoMatch() throws Exception {
183 mapRequest = getDefaultMapRequestBuilder();
184 mapRequest.getEidRecord().add(
185 new EidRecordBuilder().setMask((short) 128).setLispAddressContainer(LispAFIConvertor.toContainer(v6Address)).build());
187 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
188 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:1:0:0:0:0:0:1")));
189 record.setMaskLength((short) 112);
191 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
192 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
193 record.getLocatorRecord().add(locator.build());
194 prepareMapping(record.build());
196 MapRequest mr = mapRequest.build();
198 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
199 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
200 assertEquals(0, eidToLocators.getLocatorRecord().size());
204 public void handleMapRequest_VerifyNativelyForwardAutherized() {
205 MapRequest mr = getDefaultMapRequest();
207 Map<String, List<MappingServiceRLOCGroup>> result = new HashMap<String, List<MappingServiceRLOCGroup>>();
208 result.put(AbstractLispComponent.ADDRESS_SUBKEY, new ArrayList<MappingServiceRLOCGroup>());
210 MapReply mapReply = getNativelyForwardMapReply(mr, result);
212 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
213 assertEquals(1, eidToLocators.getRecordTtl().intValue());
214 assertEquals(Action.NativelyForward, eidToLocators.getAction());
217 private MapReply getNativelyForwardMapReply(MapRequest mr, Map<String, List<MappingServiceRLOCGroup>> result) {
218 allowing(lispDAO).get(wany(IMappingServiceKey.class));
220 allowing(lispDAO).getSpecific(wany(IMappingServiceKey.class), with(AbstractLispComponent.PASSWORD_SUBKEY));
222 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
226 private MapRequest getDefaultMapRequest() {
227 mapRequest = getDefaultMapRequestBuilder();
228 mapRequest.getEidRecord().add(
229 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
230 MapRequest mr = mapRequest.build();
235 public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
237 mapRequest = getDefaultMapRequestBuilder();
238 mapRequest.getEidRecord().add(
239 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
241 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
242 record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.4.0")));
243 record.setMaskLength((short) 24);
245 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
246 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
247 record.getLocatorRecord().add(locator.build());
248 prepareMapping(record.build());
250 MapRequest mr = mapRequest.build();
252 MapReply mapReply = testedMapResolver.handleMapRequest(mr);
254 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
255 assertEquals(0, eidToLocators.getLocatorRecord().size());
260 public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
262 mapRequest = getDefaultMapRequestBuilder();
263 mapRequest.getEidRecord().add(
264 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
266 EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
267 record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
269 LocatorRecordBuilder locator = getDefaultLocatorBuilder();
270 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
271 record.getLocatorRecord().add(locator.build());
272 locator = getDefaultLocatorBuilder();
273 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")));
274 record.getLocatorRecord().add(locator.build());
275 locator = getDefaultLocatorBuilder();
276 locator.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.8.2.7")));
277 record.getLocatorRecord().add(locator.build());
279 prepareMapping(record.build());
281 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
283 EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
284 assertEquals(3, eidToLocators.getLocatorRecord().size());
286 assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators.getLocatorRecord().get(0));
287 assertLocator(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"), eidToLocators.getLocatorRecord().get(1));
288 assertLocator(LispAFIConvertor.asIPAfiAddress("1.8.2.7"), eidToLocators.getLocatorRecord().get(2));
292 public void handleMapRequest__MultipleEIDs() throws Exception {
294 mapRequest = getDefaultMapRequestBuilder();
295 mapRequest.getEidRecord().add(
296 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
297 mapRequest.getEidRecord().add(
298 new EidRecordBuilder().setMask((short) 128).setLispAddressContainer(LispAFIConvertor.toContainer(v6Address)).build());
300 EidToLocatorRecordBuilder record1 = getDefaultEidToLocatorBuilder();
301 record1.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
302 record1.setRecordTtl(100);
304 LocatorRecordBuilder locator1 = getDefaultLocatorBuilder();
305 locator1.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
306 record1.getLocatorRecord().add(locator1.build());
308 EidToLocatorRecordBuilder record2 = getDefaultEidToLocatorBuilder();
309 record2.setLispAddressContainer(LispAFIConvertor.toContainer(v6Address));
310 record2.setMaskLength((short) 128);
311 record2.setRecordTtl(100);
313 LocatorRecordBuilder locator2 = getDefaultLocatorBuilder();
314 locator2.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")));
315 record2.getLocatorRecord().add(locator2.build());
317 prepareMapping(record1.build(), record2.build());
319 MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
321 EidToLocatorRecord eidToLocators1 = mapReply.getEidToLocatorRecord().get(0);
322 assertEquals(1, eidToLocators1.getLocatorRecord().size());
323 assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators1.getLocatorRecord().get(0));
325 EidToLocatorRecord eidToLocators2 = mapReply.getEidToLocatorRecord().get(1);
326 assertEquals(1, eidToLocators2.getLocatorRecord().size());
327 assertLocator(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"), eidToLocators2.getLocatorRecord().get(0));
330 private void assertLocator(LispAFIAddress expectedAddress, LocatorRecord locatorRecord) {
331 assertEquals(LispAFIConvertor.toContainer(expectedAddress), locatorRecord.getLispAddressContainer());
332 Assert.assertTrue(locatorRecord.isRouted());
335 private Map<String, MappingServiceRLOCGroup> prepareMapping(EidToLocatorRecord... records) {
336 if (records.length > 0) {
337 for (EidToLocatorRecord eidToLocatorRecord : records) {
338 Map<String, MappingServiceRLOCGroup> result = new HashMap<String, MappingServiceRLOCGroup>();
339 MappingServiceRLOCGroup rlocs = new MappingServiceRLOCGroup(eidToLocatorRecord.getRecordTtl(), eidToLocatorRecord.getAction(),
340 eidToLocatorRecord.isAuthoritative());
341 for (LocatorRecord locator : eidToLocatorRecord.getLocatorRecord()) {
342 rlocs.addRecord(locator);
344 result.put(AbstractLispComponent.ADDRESS_SUBKEY, rlocs);
347 MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getLispAddressContainer(),
348 (short) eidToLocatorRecord.getMaskLength()), result);
352 ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
354 protected boolean validate(IMappingServiceKey value) {
359 public Object invoke(Invocation invocation) throws Throwable {
360 return daoResults.get(lastValue);
364 allowing(lispDAO).get(with(daoGetSaverAction));
365 will(daoGetSaverAction);
366 allowing(lispDAO).getSpecific(wany(IMappingServiceKey.class), with(AbstractLispComponent.PASSWORD_SUBKEY));
368 return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(LispAFIConvertor.toContainer(v4Address)));
371 private MapRequestBuilder getDefaultMapRequestBuilder() {
372 MapRequestBuilder mrBuilder = new MapRequestBuilder();
373 mrBuilder.setAuthoritative(false);
374 mrBuilder.setEidRecord(new ArrayList<EidRecord>());
375 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
376 mrBuilder.setMapDataPresent(true);
377 mrBuilder.setNonce((long) 0);
378 mrBuilder.setPitr(false);
379 mrBuilder.setProbe(false);
380 mrBuilder.setSmr(false);
381 mrBuilder.setSmrInvoked(true);
382 mrBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
383 LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("127.0.0.1"))).build());
387 private EidToLocatorRecordBuilder getDefaultEidToLocatorBuilder() {
388 EidToLocatorRecordBuilder builder = new EidToLocatorRecordBuilder();
389 builder.setAction(Action.NoAction);
390 builder.setAuthoritative(false);
391 builder.setLocatorRecord(new ArrayList<LocatorRecord>());
392 builder.setMapVersion((short) 0);
393 builder.setMaskLength((short) 32);
394 builder.setRecordTtl(60);
398 private LocatorRecordBuilder getDefaultLocatorBuilder() {
399 LocatorRecordBuilder builder = new LocatorRecordBuilder();
400 builder.setLocalLocator(false);
401 builder.setMulticastPriority((short) 0);
402 builder.setMulticastWeight((short) 0);
403 builder.setPriority((short) 0);
404 builder.setRlocProbed(false);
405 builder.setRouted(true);
406 builder.setWeight((short) 0);