fixed northbound source dest mapping and auth TELSDN-644: #close
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MapResolverTest.java
1 package org.opendaylight.lispflowmapping.implementation.lisp;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import junit.framework.Assert;
11
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;
37
38 /*
39  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
40  *
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
44  */
45
46 public class MapResolverTest extends BaseTestCase {
47
48     // private MapResolver testedMapResolver;
49     private LispMappingService testedMapResolver;
50
51     private ILispDAO lispDAO;
52     private MapRequestBuilder mapRequest;
53     private LispAFIAddress v4Address;
54     private LispAFIAddress v6Address;
55
56     private HashMap<IMappingServiceKey, Map<String, MappingServiceValue>> daoResults;
57
58     @Override
59     @Before
60     public void before() throws Exception {
61         super.before();
62         lispDAO = context.mock(ILispDAO.class);
63         testedMapResolver = new LispMappingService();
64         testedMapResolver.basicInit(lispDAO);
65
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>>();
70     }
71
72     @Test
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());
78
79         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
80         record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
81
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());
91
92         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
93
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());
103
104         assertLocator(LispAFIConvertor.asIPAfiAddress("4.3.2.1"), eidToLocators.getLocatorRecord().get(0));
105     }
106
107     @Test
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());
112
113         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
114         record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
115         record.setRecordTtl(100);
116
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());
122
123         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
124
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());
132     }
133
134     @Test
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());
139
140         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
141         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.0")));
142         record.setMaskLength((short) 24);
143
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());
148
149         MapRequest mr = mapRequest.build();
150
151         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
152
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()));
157     }
158
159     @Test
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());
164
165         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
166         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:0")));
167         record.setMaskLength((short) 128);
168
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());
173
174         MapRequest mr = mapRequest.build();
175
176         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
177
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()));
182     }
183
184     @Test
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());
189
190         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
191         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:1:0:0:0:0:0:1")));
192         record.setMaskLength((short) 112);
193
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());
198
199         MapRequest mr = mapRequest.build();
200
201         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
202         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
203         assertEquals(0, eidToLocators.getLocatorRecord().size());
204     }
205
206     @Test
207     public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
208
209         mapRequest = getDefaultMapRequestBuilder();
210         mapRequest.getEidRecord().add(
211                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
212
213         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
214         record.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.4.0")));
215         record.setMaskLength((short) 24);
216
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());
221
222         MapRequest mr = mapRequest.build();
223
224         MapReply mapReply = testedMapResolver.handleMapRequest(mr);
225
226         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
227         assertEquals(0, eidToLocators.getLocatorRecord().size());
228
229     }
230
231     @Test
232     public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
233
234         mapRequest = getDefaultMapRequestBuilder();
235         mapRequest.getEidRecord().add(
236                 new EidRecordBuilder().setMask((short) 32).setLispAddressContainer(LispAFIConvertor.toContainer(v4Address)).build());
237
238         EidToLocatorRecordBuilder record = getDefaultEidToLocatorBuilder();
239         record.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
240
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());
250
251         prepareMapping(record.build());
252
253         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
254
255         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecord().get(0);
256         assertEquals(3, eidToLocators.getLocatorRecord().size());
257
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));
261     }
262
263     @Test
264     public void handleMapRequest__MultipleEIDs() throws Exception {
265
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());
271
272         EidToLocatorRecordBuilder record1 = getDefaultEidToLocatorBuilder();
273         record1.setLispAddressContainer(LispAFIConvertor.toContainer(v4Address));
274         record1.setRecordTtl(100);
275
276         LocatorRecordBuilder locator1 = getDefaultLocatorBuilder();
277         locator1.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1")));
278         record1.getLocatorRecord().add(locator1.build());
279
280         EidToLocatorRecordBuilder record2 = getDefaultEidToLocatorBuilder();
281         record2.setLispAddressContainer(LispAFIConvertor.toContainer(v6Address));
282         record2.setMaskLength((short) 128);
283         record2.setRecordTtl(100);
284
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());
288
289         prepareMapping(record1.build(), record2.build());
290
291         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest.build());
292
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));
296
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));
300     }
301
302     private void assertLocator(LispAFIAddress expectedAddress, LocatorRecord locatorRecord) {
303         assertEquals(LispAFIConvertor.toContainer(expectedAddress), locatorRecord.getLispAddressContainer());
304         Assert.assertTrue(locatorRecord.isRouted());
305     }
306
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()));
317                 }
318                 value.setRlocs(rlocs);
319
320                 daoResults.put(
321                         MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getLispAddressContainer(),
322                                 (short) eidToLocatorRecord.getMaskLength()), result);
323             }
324         }
325
326         ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
327             @Override
328             protected boolean validate(IMappingServiceKey value) {
329                 return true;
330             }
331
332             @Override
333             public Object invoke(Invocation invocation) throws Throwable {
334                 return daoResults.get(lastValue);
335             }
336         };
337
338         allowing(lispDAO).get(with(daoGetSaverAction));
339         will(daoGetSaverAction);
340
341         return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(LispAFIConvertor.toContainer(v4Address)));
342     }
343
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());
357         return mrBuilder;
358     }
359
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);
368         return builder;
369     }
370
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);
380         return builder;
381     }
382
383 }