87200e6608dad0694d73db7335559aaa76eb3ada
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServerTest.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.lispflowmapping.implementation.lisp;
10
11 import static org.junit.Assert.assertEquals;
12 import junitx.framework.ArrayAssert;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO.MappingEntry;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKey;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceNoMaskKey;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
23 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
24 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
25 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
26 import org.opendaylight.lispflowmapping.type.lisp.address.IMaskable;
27 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
28 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispNoAddress;
31
32 public class MapServerTest extends BaseTestCase {
33
34     private MapServer testedMapServer;
35     private ILispDAO lispDAO;
36     private MapRegister mapRegister;
37     private LispIpv4Address eid;
38     private LispIpv4Address rloc;
39     private ValueSaverAction<MappingEntry<?>[]> mappingEntriesSaver;
40
41     @Override
42     @Before
43     public void before() throws Exception {
44         super.before();
45         lispDAO = context.mock(ILispDAO.class);
46         testedMapServer = new MapServer(lispDAO);
47         mapRegister = new MapRegister();
48         eid = new LispIpv4Address("10.31.0.5");
49         rloc = new LispIpv4Address(0xC0A8880A);
50         EidToLocatorRecord record = new EidToLocatorRecord();
51         record.setPrefix(eid);
52         record.addLocator(new LocatorRecord().setLocator(rloc));
53         mapRegister.addEidToLocator(record);
54
55         mappingEntriesSaver = new ValueSaverAction<MappingEntry<?>[]>();
56     }
57
58     @Test
59     public void handleMapRegister__NonSetMBit() throws Exception {
60         mapRegister.setWantMapNotify(false);
61
62         expectPut(eid, 0);
63         assertNull(testedMapServer.handleMapRegister(mapRegister));
64
65         MappingEntry<?>[] entries = mappingEntriesSaver.lastValue;
66         assertEquals(2, entries.length);
67
68         assertEquals("NumRLOCs", entries[0].getKey());
69         assertEquals(1, entries[0].getValue());
70         assertEquals("RLOC0", entries[1].getKey());
71         assertEquals(rloc, ((MappingServiceValue) entries[1].getValue()).getRecord().getLocator());
72     }
73
74     @Test
75     public void handleMapRegisterIpv4__ValidNotifyEchoesRegister() throws Exception {
76         mapRegister.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispNoAddress()));
77         mapRegister.setWantMapNotify(true);
78
79         expectPut(eid, 0);
80         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
81         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
82         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
83         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
84         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
85     }
86
87     @Test
88     public void handleMapRegisterIpv4__CloneNotOwnYouClown() throws Exception {
89         mapRegister = new MapRegister();
90         mapRegister.setKeyId((byte) 0);
91         mapRegister.setWantMapNotify(true);
92         EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
93         eid = new LispIpv4Address(1);
94         eidToLocator.setPrefix(eid);
95
96         LocatorRecord locator = new LocatorRecord();
97         locator.setLocator(new LispIpv4Address(2));
98         locator.setPriority((byte) 55);
99         eidToLocator.addLocator(locator);
100
101         mapRegister.addEidToLocator(eidToLocator);
102
103         expectPut(eid, 0);
104
105         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
106
107         mapRegister.getEidToLocatorRecords().get(0).setPrefix(new LispIpv4Address(55));
108         mapRegister.getEidToLocatorRecords().get(0).getLocators().get(0).setPriority((byte) 1);
109
110         EidToLocatorRecord actualEidToLocator = mapNotify.getEidToLocatorRecords().get(0);
111         assertEquals(new LispIpv4Address(1), actualEidToLocator.getPrefix());
112         assertEquals((byte) 55, actualEidToLocator.getLocators().get(0).getPriority());
113
114     }
115
116     @Test
117     public void handleMapRegisterIpv4__ValidMask() throws Exception {
118         int mask = 16;
119         mapRegister = new MapRegister();
120         EidToLocatorRecord record = new EidToLocatorRecord();
121         record.setPrefix(new LispIpv4Address("10.31.0.5")).setMaskLength(mask);
122         record.addLocator(new LocatorRecord().setLocator(rloc));
123         mapRegister.addEidToLocator(record);
124         mapRegister.setWantMapNotify(true);
125
126         expectPut(eid, mask);
127
128         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
129         assertEquals(new LispIpv4Address("10.31.0.0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
130         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
131         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
132         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
133         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
134     }
135     
136     @Test
137     public void handleMapRegisterIpv4__ValidMask32() throws Exception {
138         int mask = 32;
139         mapRegister = new MapRegister();
140         EidToLocatorRecord record = new EidToLocatorRecord();
141         record.setPrefix(new LispIpv4Address("10.31.0.5")).setMaskLength(mask);
142         record.addLocator(new LocatorRecord().setLocator(rloc));
143         mapRegister.addEidToLocator(record);
144         mapRegister.setWantMapNotify(true);
145         
146         expectPut(eid, mask);
147         
148         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
149         assertEquals(new LispIpv4Address("10.31.0.5"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
150         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
151         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
152         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
153         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
154     }
155     
156     @Test
157     public void handleMapRegisterIpv6__ValidMask96() throws Exception {
158         int mask = 96;
159         mapRegister = new MapRegister();
160         EidToLocatorRecord record = new EidToLocatorRecord();
161         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:0");
162         record.setPrefix(addr).setMaskLength(mask);
163         record.addLocator(new LocatorRecord().setLocator(rloc));
164         mapRegister.addEidToLocator(record);
165         mapRegister.setWantMapNotify(true);
166
167         expectPut(addr, mask);
168
169         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
170         assertEquals(new LispIpv6Address("1:1:1:1:1:1:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
171         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
172         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
173         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
174         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
175     }
176     
177     @Test
178     public void handleMapRegisterIpv6__ValidMask32() throws Exception {
179         int mask = 32;
180         mapRegister = new MapRegister();
181         EidToLocatorRecord record = new EidToLocatorRecord();
182         LispIpv6Address addr = new LispIpv6Address("1:1:0:0:0:0:0:0");
183         record.setPrefix(addr).setMaskLength(mask);
184         record.addLocator(new LocatorRecord().setLocator(rloc));
185         mapRegister.addEidToLocator(record);
186         mapRegister.setWantMapNotify(true);
187         
188         expectPut(addr, mask);
189         
190         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
191         assertEquals(new LispIpv6Address("1:1:0:0:0:0:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
192         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
193         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
194         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
195         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
196     }
197     
198     @Test
199     public void handleMapRegisterIpv6__ValidMask128() throws Exception {
200         int mask = 128;
201         mapRegister = new MapRegister();
202         EidToLocatorRecord record = new EidToLocatorRecord();
203         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:2");
204         record.setPrefix(addr).setMaskLength(mask);
205         record.addLocator(new LocatorRecord().setLocator(rloc));
206         mapRegister.addEidToLocator(record);
207         mapRegister.setWantMapNotify(true);
208         
209         expectPut(addr, mask);
210         
211         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
212         assertEquals(new LispIpv6Address("1:1:1:1:1:1:1:2"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
213         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
214         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
215         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
216         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
217     }
218
219
220     @Test
221     public void handleMapRegister__MultipleRLOCs() throws Exception {
222         expectPut(eid, 0);
223
224         LispIpv4Address rloc0 = rloc;
225         LispIpv6Address rloc1 = new LispIpv6Address("::7");
226         mapRegister.getEidToLocatorRecords().get(0).addLocator(new LocatorRecord().setLocator(rloc1));
227
228         testedMapServer.handleMapRegister(mapRegister);
229
230         MappingEntry<?>[] entries = mappingEntriesSaver.lastValue;
231         assertEquals(3, entries.length);
232
233         assertEquals("NumRLOCs", entries[0].getKey());
234         assertEquals(2, entries[0].getValue());
235         assertEquals("RLOC0", entries[1].getKey());
236         assertEquals(rloc0, ((MappingServiceValue) entries[1].getValue()).getRecord().getLocator());
237         assertEquals("RLOC1", entries[2].getKey());
238         assertEquals(rloc1, ((MappingServiceValue) entries[2].getValue()).getRecord().getLocator());
239
240         // When refactoring: reconsider arrays/count
241     }
242
243     private void expectPut(LispAddress address, int mask) {
244         if (address instanceof IMaskable && mask > 0 && mask < ((IMaskable)address).getMaxMask()) {
245             ((IMaskable)address).normalize(mask);
246             allowing(lispDAO).put(weq(new MappingServiceKey(address, (byte) mask)), with(mappingEntriesSaver));
247         } else {
248             allowing(lispDAO).put(weq(new MappingServiceNoMaskKey(address)), with(mappingEntriesSaver));
249         }
250     }
251 }