added key value to yang and changed map server so that it overides old rlocs CXTMAP-102
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MapResolverTest.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.lispflowmapping.implementation.lisp;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import junit.framework.Assert;
18
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;
42
43 public class MapResolverTest extends BaseTestCase {
44
45     // private MapResolver testedMapResolver;
46     private LispMappingService testedMapResolver;
47
48     private ILispDAO lispDAO;
49     private MapRequestBuilder mapRequest;
50     private LispAFIAddress v4Address;
51     private LispAFIAddress v6Address;
52
53     private HashMap<IMappingServiceKey, Map<String, MappingServiceRLOCGroup>> daoResults;
54
55     @Override
56     @Before
57     public void before() throws Exception {
58         super.before();
59         lispDAO = context.mock(ILispDAO.class);
60         testedMapResolver = new LispMappingService();
61         testedMapResolver.basicInit(lispDAO);
62
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>>();
67     }
68
69     @Test
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());
75
76         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
77         record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
78
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());
88
89         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
90
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());
100
101         assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators.getLocatorRecord().get(0));
102     }
103
104     @Test
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());
109
110         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
111         record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
112         record.setRecordTtl(100);
113
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());
119
120         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
121
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());
129     }
130
131     @Test
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());
136
137         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
138         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.0")));
139         record.setMaskLength((short) 24);
140
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());
145
146         MapRequest mr = mapRequest.build();
147
148         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
149
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()));
154     }
155
156     @Test
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());
161
162         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
163         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:0")));
164         record.setMaskLength((short) 128);
165
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());
170
171         MapRequest mr = mapRequest.build();
172
173         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
174
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()));
179     }
180
181     @Test
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());
186
187         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
188         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:1:0:0:0:0:0:1")));
189         record.setMaskLength((short) 112);
190
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());
195
196         MapRequest mr = mapRequest.build();
197
198         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
199         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
200         assertEquals(0, eidToLocators.getLocatorRecord().size());
201     }
202
203     @Test
204     public void handleMapRequest_VerifyNativelyForwardAutherized() {
205         MapRequest mr = getDefaultMapRequest();
206
207         Map<String, List<MappingServiceRLOCGroup>> result = new HashMap<String, List<MappingServiceRLOCGroup>>();
208         result.put(AbstractLispComponent.ADDRESS_SUBKEY, new ArrayList<MappingServiceRLOCGroup>());
209
210         MapReply mapReply = getNativelyForwardMapReply(mr, result);
211
212         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
213         assertEquals(1, eidToLocators.getRecordTtl().intValue());
214         assertEquals(Action.NativelyForward, eidToLocators.getAction());
215     }
216
217     private MapReply getNativelyForwardMapReply(MapRequest mr, Map<String, List<MappingServiceRLOCGroup>> result) {
218         allowing(lispDAO).get(wany(IMappingServiceKey.class));
219         ret(result);
220         allowing(lispDAO).getSpecific(wany(IMappingServiceKey.class), with(AbstractLispComponent.PASSWORD_SUBKEY));
221         ret("pass");
222         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
223         return mapReply;
224     }
225
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();
231         return mr;
232     }
233
234     @Test
235     public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
236
237         mapRequest = getDefaultMapRequestBuilder();
238         mapRequest.getEidRecord().add(
239                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
240
241         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
242         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.4.0")));
243         record.setMaskLength((short) 24);
244
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());
249
250         MapRequest mr = mapRequest.build();
251
252         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
253
254         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
255         assertEquals(0, eidToLocators.getLocatorRecord().size());
256
257     }
258
259     @Test
260     public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
261
262         mapRequest = getDefaultMapRequestBuilder();
263         mapRequest.getEidRecord().add(
264                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
265
266         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
267         record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
268
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());
278
279         prepareMapping(record.build());
280
281         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
282
283         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
284         assertEquals(3, eidToLocators.getLocatorRecord().size());
285
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));
289     }
290
291     @Test
292     public void handleMapRequest__MultipleEIDs() throws Exception {
293
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());
299
300         EidToLocatorRecordBuilder record1 = getDefaultEidToLocatorBuilder();
301         record1.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
302         record1.setRecordTtl(100);
303
304         LocatorRecordBuilder locator1 = getDefaultLocatorBuilder();
305         locator1.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
306         record1.getLocatorRecord().add(locator1.build());
307
308         EidToLocatorRecordBuilder record2 = getDefaultEidToLocatorBuilder();
309         record2.setLispAddressContainer(LispAFIConvertor.toContainer(v6Address));
310         record2.setMaskLength((short) 128);
311         record2.setRecordTtl(100);
312
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());
316
317         prepareMapping(record1.build(), record2.build());
318
319         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
320
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));
324
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));
328     }
329
330     private void assertLocator(LispAFIAddress expectedAddress, LocatorRecord locatorRecord) {
331         assertEquals(LispAFIConvertor.toContainer(expectedAddress), locatorRecord.getLispAddressContainer());
332         Assert.assertTrue(locatorRecord.isRouted());
333     }
334
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);
343                 }
344                 result.put(AbstractLispComponent.ADDRESS_SUBKEY, rlocs);
345
346                 daoResults.put(
347                         MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getLispAddressContainer(),
348                                 (short) eidToLocatorRecord.getMaskLength()), result);
349             }
350         }
351
352         ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
353             @Override
354             protected boolean validate(IMappingServiceKey value) {
355                 return true;
356             }
357
358             @Override
359             public Object invoke(Invocation invocation) throws Throwable {
360                 return daoResults.get(lastValue);
361             }
362         };
363
364         allowing(lispDAO).get(with(daoGetSaverAction));
365         will(daoGetSaverAction);
366         allowing(lispDAO).getSpecific(wany(IMappingServiceKey.class), with(AbstractLispComponent.PASSWORD_SUBKEY));
367
368         return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(LispAFIConvertor.toContainer(v4Address)));
369     }
370
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());
384         return mrBuilder;
385     }
386
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);
395         return builder;
396     }
397
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);
407         return builder;
408     }
409
410 }