support record TTL TELSDN-196: #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.interfaces.dao.ILispDAO;
18 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKeyUtil;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
22 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
23 import org.opendaylight.lispflowmapping.type.lisp.EidRecord;
24 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
25 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
26 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
27 import org.opendaylight.lispflowmapping.type.lisp.MapReplyAction;
28 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
31 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
32
33 /*
34  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
35  *
36  * This program and the accompanying materials are made available under the
37  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
38  * and is available at http://www.eclipse.org/legal/epl-v10.html
39  */
40
41 public class MapResolverTest extends BaseTestCase {
42
43     // private MapResolver testedMapResolver;
44     private LispMappingService testedMapResolver;
45
46     private ILispDAO lispDAO;
47     private MapRequest mapRequest;
48     private LispIpv4Address v4Address;
49     private LispIpv6Address v6Address;
50
51     private HashMap<IMappingServiceKey, Map<String, MappingServiceValue>> daoResults;
52
53     @Override
54     @Before
55     public void before() throws Exception {
56         super.before();
57         lispDAO = context.mock(ILispDAO.class);
58         testedMapResolver = new LispMappingService();
59         testedMapResolver.basicInit(lispDAO);
60
61         mapRequest = new MapRequest();
62         v4Address = new LispIpv4Address("1.2.3.4");
63         v6Address = new LispIpv6Address("0:0:0:0:0:0:0:1");
64         daoResults = new HashMap<IMappingServiceKey, Map<String, MappingServiceValue>>();
65     }
66
67     @Test
68     @Ignore
69     public void handleMapRequest__NumRLOCsMismatch() throws Exception {
70         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
71         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
72         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
73         // Map<String, Object> rlocs = prepareMapping(record);
74         // rlocs.put("NumRLOCs", 5);
75
76         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
77
78         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
79         assertEquals(1, eidToLocators.getLocators().size());
80         assertLocator(new LispIpv4Address(0x44332211), eidToLocators.getLocators().get(0));
81     }
82
83     @Test
84     @Ignore
85     public void handleMapRequest__BadRLOCType() throws Exception {
86         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
87         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
88         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x71717171)));
89         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
90         // Map<String, Object> rlocs = prepareMapping(record);
91         // rlocs.put("RLOC0", "Ooga booga");
92
93         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
94
95         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
96         assertEquals(1, eidToLocators.getLocators().size());
97         assertLocator(new LispIpv4Address(0x44332211), eidToLocators.getLocators().get(0));
98     }
99
100     @Test
101     @Ignore
102     public void handleMapRequest__NoNumRLOCs() throws Exception {
103         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
104         allowing(lispDAO).get(wany(IMappingServiceKey.class));
105         ret(new HashMap<String, Object>());
106
107         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
108
109         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
110         assertEquals(0, eidToLocators.getLocators().size());
111     }
112
113     @Test
114     @Ignore
115     public void handleMapRequest__IllegalNumRLOCs() throws Exception {
116         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
117         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
118         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x44332211)));
119         // Map<String, Object> rlocs = prepareMapping(record);
120         // rlocs.put("NumRLOCs", "Bla");
121
122         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
123
124         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
125         assertEquals(0, eidToLocators.getLocators().size());
126     }
127
128     @Test
129     public void handleMapRequest__ReplyWithSingleLocator() throws Exception {
130         mapRequest.addEidRecord(new EidRecord((byte) 0, new LispIpv4Address("1.2.3.4")));
131
132         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
133         record.setRecordTtl(100);
134         LocatorRecord locator = new LocatorRecord().setLocator(new LispIpv4Address(0x04030201));
135         locator.setLocalLocator(false);
136         locator.setRouted(true);
137         locator.setMulticastPriority((byte) 5);
138         locator.setMulticastWeight((byte) 17);
139         locator.setPriority((byte) 16);
140         record.addLocator(locator);
141         prepareMapping(record);
142
143         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
144
145         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
146         assertEquals(1, eidToLocators.getLocators().size());
147         LocatorRecord resultLocator = mapReply.getEidToLocatorRecords().get(0).getLocators().get(0);
148         assertEquals(locator.isLocalLocator(), resultLocator.isLocalLocator());
149         assertEquals(locator.isRouted(), resultLocator.isRouted());
150         assertEquals(locator.getMulticastPriority(), resultLocator.getMulticastPriority());
151         assertEquals(locator.getMulticastWeight(), resultLocator.getMulticastWeight());
152         assertEquals(locator.getPriority(), resultLocator.getPriority());
153
154         assertLocator(new LispIpv4Address(0x04030201), eidToLocators.getLocators().get(0));
155     }
156
157     @Test
158     public void handleMapRequest__VerifyBasicFields() throws Exception {
159         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
160
161         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
162         record.setRecordTtl(100);
163         record.setAction(MapReplyAction.NativelyForward);
164         record.setAuthoritative(true);
165         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
166         prepareMapping(record);
167
168         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
169
170         assertEquals(mapRequest.getNonce(), mapReply.getNonce());
171         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
172         assertEquals((byte) 0, eidToLocators.getMaskLength());
173         assertEquals(v4Address, eidToLocators.getPrefix());
174         assertEquals(record.isAuthoritative(), eidToLocators.isAuthoritative());
175         assertEquals(record.getAction(), eidToLocators.getAction());
176         assertEquals(record.getRecordTtl(), eidToLocators.getRecordTtl());
177     }
178
179     @Test
180     public void handleMapRequest__VerifyMask() throws Exception {
181         EidRecord mapRequestRecord = new EidRecord((byte) 0, v4Address);
182         mapRequestRecord.setMaskLength(32);
183         mapRequest.addEidRecord(mapRequestRecord);
184
185         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv4Address("1.2.3.0")).setMaskLength(24);
186         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
187         prepareMapping(record);
188
189         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
190
191         assertEquals(mapRequest.getNonce(), mapReply.getNonce());
192         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
193         assertEquals((byte) 32, eidToLocators.getMaskLength());
194         assertEquals(v4Address, eidToLocators.getPrefix());
195     }
196
197     @Test
198     public void handleMapRequest__VerifyMaskIPv6() throws Exception {
199         EidRecord mapRequestRecord = new EidRecord((byte) 0, v6Address);
200         mapRequestRecord.setMaskLength(128);
201         mapRequest.addEidRecord(mapRequestRecord);
202
203         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv6Address("0:0:0:0:0:0:0:0")).setMaskLength(128);
204         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
205         prepareMapping(record);
206
207         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
208
209         assertEquals(mapRequest.getNonce(), mapReply.getNonce());
210         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
211         assertEquals((byte) 128, eidToLocators.getMaskLength());
212         assertEquals(v6Address, eidToLocators.getPrefix());
213     }
214
215     @Test
216     public void handleMapRequest__VerifyMaskIPv6NoMatch() throws Exception {
217         EidRecord mapRequestRecord = new EidRecord((byte) 128, v6Address);
218         mapRequest.addEidRecord(mapRequestRecord);
219
220         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv6Address("0:1:0:0:0:0:0:1")).setMaskLength(112);
221         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
222         prepareMapping(record);
223
224         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
225
226         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
227         assertEquals(0, eidToLocators.getLocators().size());
228     }
229
230     @Test
231     public void handleMapRequest__VerifyMaskNoMatch() throws Exception {
232         EidRecord mapRequestRecord = new EidRecord((byte) 0, v4Address);
233         mapRequestRecord.setMaskLength(32);
234         mapRequest.addEidRecord(mapRequestRecord);
235
236         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(new LispIpv4Address("1.2.4.0")).setMaskLength(24);
237         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
238         prepareMapping(record);
239
240         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
241
242         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
243         assertEquals(0, eidToLocators.getLocators().size());
244
245     }
246
247     @Test
248     public void handleMapRequest__ReplyWithMultipleLocators() throws Exception {
249         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
250
251         EidToLocatorRecord record = new EidToLocatorRecord().setPrefix(v4Address);
252         record.setRecordTtl(100);
253         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
254         record.addLocator(new LocatorRecord().setLocator(new LispIpv6Address("::1")));
255         record.addLocator(new LocatorRecord().setLocator(new LispIpv4Address("1.8.2.7")));
256         prepareMapping(record);
257
258         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
259
260         EidToLocatorRecord eidToLocators = mapReply.getEidToLocatorRecords().get(0);
261         assertEquals(3, eidToLocators.getLocators().size());
262
263         assertLocator(new LispIpv4Address(0x04030201), eidToLocators.getLocators().get(0));
264         assertLocator(new LispIpv6Address("::1"), eidToLocators.getLocators().get(1));
265         assertLocator(new LispIpv4Address("1.8.2.7"), eidToLocators.getLocators().get(2));
266     }
267
268     private void assertLocator(LispAddress expectedAddress, LocatorRecord locatorRecord) {
269         assertEquals(expectedAddress, locatorRecord.getLocator());
270         Assert.assertTrue(locatorRecord.isRouted());
271     }
272
273     @Test
274     public void handleMapRequest__MultipleEIDs() throws Exception {
275         mapRequest.addEidRecord(new EidRecord((byte) 0, v4Address));
276         mapRequest.addEidRecord(new EidRecord((byte) 0, v6Address));
277
278         EidToLocatorRecord record1 = new EidToLocatorRecord().setPrefix(v4Address);
279         record1.setRecordTtl(100);
280         record1.addLocator(new LocatorRecord().setLocator(new LispIpv4Address(0x04030201)));
281
282         EidToLocatorRecord record2 = new EidToLocatorRecord().setPrefix(v6Address);
283         record2.addLocator(new LocatorRecord().setLocator(new LispIpv6Address("0:0:0:0:0:0:0:1")));
284         record2.setRecordTtl(100);
285         prepareMapping(record1, record2);
286
287         MapReply mapReply = testedMapResolver.handleMapRequest(mapRequest);
288
289         EidToLocatorRecord eidToLocators1 = mapReply.getEidToLocatorRecords().get(0);
290         assertEquals(1, eidToLocators1.getLocators().size());
291         assertLocator(new LispIpv4Address(0x04030201), eidToLocators1.getLocators().get(0));
292
293         EidToLocatorRecord eidToLocators2 = mapReply.getEidToLocatorRecords().get(1);
294         assertEquals(1, eidToLocators2.getLocators().size());
295         assertLocator(new LispIpv6Address("0:0:0:0:0:0:0:1"), eidToLocators2.getLocators().get(0));
296     }
297
298     private Map<String, MappingServiceValue> prepareMapping(EidToLocatorRecord... records) {
299         if (records.length > 0) {
300             for (EidToLocatorRecord eidToLocatorRecord : records) {
301                 MappingServiceValue value = new MappingServiceValue();
302                 Map<String, MappingServiceValue> result = new HashMap<String, MappingServiceValue>();
303                 result.put("value", value);
304                 List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
305                 for (LocatorRecord locator : eidToLocatorRecord.getLocators()) {
306                     rlocs.add(new MappingServiceRLOC(locator, eidToLocatorRecord.getRecordTtl(), eidToLocatorRecord.getAction(), eidToLocatorRecord
307                             .isAuthoritative()));
308                 }
309                 value.setRlocs(rlocs);
310
311                 daoResults.put(
312                         MappingServiceKeyUtil.generateMappingServiceKey(eidToLocatorRecord.getPrefix(), (byte) eidToLocatorRecord.getMaskLength()),
313                         result);
314             }
315         }
316
317         ValueSaverAction<IMappingServiceKey> daoGetSaverAction = new ValueSaverAction<IMappingServiceKey>() {
318             @Override
319             protected boolean validate(IMappingServiceKey value) {
320                 return true;
321             }
322
323             @Override
324             public Object invoke(Invocation invocation) throws Throwable {
325                 return daoResults.get(lastValue);
326             }
327         };
328
329         allowing(lispDAO).get(with(daoGetSaverAction));
330         will(daoGetSaverAction);
331
332         return daoResults.get(MappingServiceKeyUtil.generateMappingServiceKey(v4Address));
333     }
334 }