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.serializer;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
19 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
20 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.NoAddressAfi;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6Binary;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRlocBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.MapReplyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
39 public class MapRequestSerializationTest extends BaseTestCase {
42 public void prefix__NoPrefix() throws Exception {
43 MapRequestBuilder mrBuilder = new MapRequestBuilder();
44 mrBuilder.setEidItem(new ArrayList<EidItem>());
45 mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.getNoAddressEid()).setEidItemId("bar")
49 assertEquals(NoAddressAfi.class, mrBuilder.getEidItem().get(0).getEid().getAddressType());
53 public void deserialize__FlagsInFirstByte() throws Exception {
54 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 00 00 01 3d 8d "
55 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
56 + "00 01 01 02 03 04"), null);
57 assertFalse(mr.getAuthoritative());
58 assertTrue(mr.getMapDataPresent());
59 assertTrue(mr.getProbe());
60 assertFalse(mr.getSmr());
62 mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 00 00 01 3d 8d "
63 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
64 + "00 01 01 02 03 04"), null);
65 assertTrue(mr.getAuthoritative());
66 assertFalse(mr.getMapDataPresent());
67 assertFalse(mr.getProbe());
68 assertTrue(mr.getSmr());
70 mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("1C 00 00 01 3d 8d "
71 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
72 + "00 01 01 02 03 04"), null);
73 assertTrue(mr.getAuthoritative());
74 assertTrue(mr.getMapDataPresent());
75 assertFalse(mr.getProbe());
76 assertFalse(mr.getSmr());
80 public void deserialize__LispMobMapRequestWithReply() throws Exception {
81 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("14 00 00 01 3e f7 "
82 + "7f 5b 41 7c 77 3c 00 01 01 01 01 01 00 01 c0 a8 "
83 + "38 66 00 20 00 01 01 02 03 04 00 00 00 0a 01 20 "
84 + "10 00 00 00 00 01 01 01 01 01 01 64 ff 00 00 05 "
85 + "00 01 c0 a8 38 66"), null);
86 assertArrayEquals(new byte[] {1, 1, 1, 1},
87 ((Ipv4Binary) mr.getSourceEid().getEid().getAddress()).getIpv4Binary().getValue());
88 assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
93 public void serialize__EmptyMapRequest() throws Exception {
95 MapRequestBuilder mrBuilder = new MapRequestBuilder();
96 ByteBuffer expected = hexToByteBuffer("10 00 00 00 00 00 "
97 + "00 00 00 00 00 00 00 00");
98 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
102 public void serialize__FlagsInFirstByte() throws Exception {
104 MapRequestBuilder mrBuilder = new MapRequestBuilder();
105 mrBuilder.setAuthoritative(true);
106 mrBuilder.setProbe(true);
107 ByteBuffer expected = hexToByteBuffer("1A 00 00 00 00 00 "
108 + "00 00 00 00 00 00 00 00");
109 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
110 mrBuilder = new MapRequestBuilder();
111 mrBuilder.setSmr(true);
112 mrBuilder.setMapDataPresent(true);
113 expected = hexToByteBuffer("15 00 00 00 00 00 "
114 + "00 00 00 00 00 00 00 00");
115 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
116 mrBuilder.setAuthoritative(true);
117 mrBuilder.setProbe(true);
118 expected = hexToByteBuffer("1F 00 00 00 00 00 "
119 + "00 00 00 00 00 00 00 00");
120 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
124 public void serialize__FlagsInSecondByte() throws Exception {
125 MapRequestBuilder mrBuilder = new MapRequestBuilder();
126 mrBuilder.setPitr(true);
127 mrBuilder.setSmrInvoked(true);
128 ByteBuffer expected = hexToByteBuffer("10 C0 00 00 00 00 "
129 + "00 00 00 00 00 00 00 00");
130 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
131 mrBuilder.setPitr(false);
132 expected = hexToByteBuffer("10 40 00 00 00 00 "
133 + "00 00 00 00 00 00 00 00");
134 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
139 public void deserialize__FlagsInSecondByte() throws Exception {
140 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 01 3d 8d "
141 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
142 + "00 01 01 02 03 04"), null);
143 assertTrue(mr.getPitr());
144 assertFalse(mr.getSmrInvoked());
146 mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 40 00 01 3d 8d "
147 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
148 + "00 01 01 02 03 04"), null);
149 assertFalse(mr.getPitr());
150 assertTrue(mr.getSmrInvoked());
154 public void deserialize__SingleEidItem() throws Exception {
155 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
156 + "01 " // single record
157 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a "
158 + "00 20 00 01 01 02 03 04"), null);
160 assertEquals(1, mr.getEidItem().size());
161 assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
165 public void deserialize__ContainsMapReply() throws Exception {
166 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
167 + "01 " // single record
168 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a "
169 + "00 20 00 01 01 02 03 04 "// end of map request
171 + "00 02 01 20 00 00 00 00 "
172 + "00 01 01 02 03 04 01 02 "
173 + "03 04 00 06 00 01 0a 0a "
178 assertEquals(1, mr.getEidItem().size());
179 assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
180 MappingRecord record = mr.getMapReply().getMappingRecord();
181 assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), record.getEid());
182 assertEquals(false, record.getAuthoritative());
183 assertEquals(Action.NoAction, record.getAction());
184 assertEquals(0, record.getMapVersion().shortValue());
185 assertEquals(32, MaskUtil.getMaskForAddress(record.getEid().getAddress()));
186 assertEquals(2, record.getRecordTtl().byteValue());
187 assertArrayEquals(new byte[] {10, 10, 10, 10},
188 ((Ipv4Binary) record.getLocatorRecord().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
189 assertEquals(1, record.getLocatorRecord().get(0).getPriority().byteValue());
190 assertEquals(2, record.getLocatorRecord().get(0).getWeight().byteValue());
191 assertEquals(3, record.getLocatorRecord().get(0).getMulticastPriority().byteValue());
192 assertEquals(4, record.getLocatorRecord().get(0).getMulticastWeight().byteValue());
193 assertEquals(true, record.getLocatorRecord().get(0).getLocalLocator());
194 assertEquals(true, record.getLocatorRecord().get(0).getRlocProbed());
195 assertEquals(false, record.getLocatorRecord().get(0).getRouted());
199 public void serialize__SingleEidItem() throws Exception {
200 MapRequestBuilder mrBuilder = new MapRequestBuilder();
201 mrBuilder.setEidItem(new ArrayList<EidItem>());
202 mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
203 .setEidItemId("bar").build());
204 ByteBuffer expected = hexToByteBuffer("10 00 00 01 00 00 " //
205 + "00 00 00 00 00 00 00 00 00 20 00 01 01 02 03 04");
206 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
210 public void deserialize__MultipleEidItem() throws Exception {
211 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
214 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a " //
215 + "00 20 00 01 01 02 03 04 " //
216 + "00 80 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05"), null);
218 assertEquals(2, mr.getEidItem().size());
219 assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
220 assertEquals(LispAddressUtil.asIpv6PrefixBinaryEid("0:0:0:0:0:0:0:5/128"), mr.getEidItem().get(1).getEid());
224 public void serialize__MultipleEidItem() throws Exception {
225 MapRequestBuilder mrBuilder = new MapRequestBuilder();
226 mrBuilder.setEidItem(new ArrayList<EidItem>());
227 mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
228 .setEidItemId("bar").build());
229 mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("4.3.2.1/0"))
230 .setEidItemId("bar").build());
231 ByteBuffer expected = hexToByteBuffer("10 00 00 02 00 00 " //
232 + "00 00 00 00 00 00 00 00 00 20 00 01 01 02 03 04 00 00 00 01 04 03 02 01");
233 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
237 public void deserialize__SingleItrRloc() throws Exception {
238 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 "
240 + "00 " // This means 1 ITR-RLOC
241 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
242 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 1)
243 + "00 20 00 01 01 02 03 04"), null);
245 assertEquals(1, mr.getItrRloc().size());
246 assertArrayEquals(new byte[] {(byte) 192, (byte) 168, (byte) 136, (byte) 10},
247 ((Ipv4Binary) mr.getItrRloc().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
251 public void serialize__SingleItrRloc() throws Exception {
252 MapRequestBuilder mrBuilder = new MapRequestBuilder();
253 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
254 mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
255 .setItrRlocId("foo").build());
256 ByteBuffer expected = hexToByteBuffer("10 00 00 00 00 00 " //
257 + "00 00 00 00 00 00 00 00 00 01 01 02 03 04");
258 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
262 public void serialize__MultipleItrRloc() throws Exception {
263 MapRequestBuilder mrBuilder = new MapRequestBuilder();
264 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
265 mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
266 .setItrRlocId("foo").build());
267 mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1"))
268 .setItrRlocId("foo").build());
269 ByteBuffer expected = hexToByteBuffer("10 00 01 00 00 00 " //
270 + "00 00 00 00 00 00 00 00 00 01 01 02 03 04 00 01 04 03 02 01");
271 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
275 public void deserialize__MultipleItrRlocs() throws Exception {
276 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 "
277 + "02 " // This means 3 ITR - RLOCs
278 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 "
279 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 3)
280 // IPv6 (ITR-RLOC #2 of 3)
281 + "00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 " //
282 + "00 01 11 22 34 56 " // IPv4 (ITR-RLOC #3 of 3)
283 + "00 20 00 01 01 02 03 04"), null);
285 assertEquals(3, mr.getItrRloc().size());
286 assertArrayEquals(new byte[] {(byte) 192, (byte) 168, (byte) 136, (byte) 10},
287 ((Ipv4Binary) mr.getItrRloc().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
288 assertArrayEquals(new byte[] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
289 ((Ipv6Binary) mr.getItrRloc().get(1).getRloc().getAddress()).getIpv6Binary().getValue());
290 assertArrayEquals(new byte[] {17, 34, 52, 86},
291 ((Ipv4Binary) mr.getItrRloc().get(2).getRloc().getAddress()).getIpv4Binary().getValue());
295 public void serialize__All() throws Exception {
296 MapRequestBuilder mrBuilder = new MapRequestBuilder();
297 mrBuilder.setProbe(true);
298 mrBuilder.setPitr(true);
299 mrBuilder.setNonce((long) 13);
300 mrBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid("10.0.0.1")).build());
301 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
302 mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
303 .setItrRlocId("foo").build());
304 mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv6Rloc("1:2:3:4:5:6:7:8"))
305 .setItrRlocId("foo").build());
306 mrBuilder.setEidItem(new ArrayList<EidItem>());
307 mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
308 .setEidItemId("bar").build());
309 ByteBuffer expected = hexToByteBuffer("12 80 01 01 00 00 " //
310 + "00 00 00 00 00 0D 00 01 0a 00 00 01 00 01 01 02 03 04 00 02 00 01 00 02 00 03 00 04 00 05 00 06 "
311 + "00 07 00 08 00 20 00 01 01 02 03 04");
312 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
316 public void serialize__AllWithMapReply() throws Exception {
317 MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
318 mapRequestBuilder.setProbe(true);
319 mapRequestBuilder.setMapDataPresent(true);
320 mapRequestBuilder.setPitr(true);
321 mapRequestBuilder.setNonce((long) 13);
322 mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid("10.0.0.1"))
324 mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
325 mapRequestBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
326 .setItrRlocId("foo").build());
327 mapRequestBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv6Rloc("1:2:3:4:5:6:7:8"))
328 .setItrRlocId("foo").build());
329 mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
330 mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
331 .setEidItemId("bar").build());
333 MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
334 recordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/0"));
335 recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
337 LocatorRecordBuilder locatorBuilder = new LocatorRecordBuilder().setLocatorId("baz");
338 locatorBuilder.setRloc(LispAddressUtil.asIpv4Rloc("0.0.0.2"));
339 recordBuilder.getLocatorRecord().add(locatorBuilder.build());
341 MapReplyBuilder mapreplyBuilder = new MapReplyBuilder();
342 mapreplyBuilder.setMappingRecord(recordBuilder.build());
343 mapRequestBuilder.setMapReply(mapreplyBuilder.build());
345 ByteBuffer expected = hexToByteBuffer("16 80 01 01 00 00 "
346 + "00 00 00 00 00 0D 00 01 0a 00 00 01 00 01 01 02 03 04 00 02 00 01 00 02 00 03 00 04 00 05 00 06 "
347 + "00 07 00 08 00 20 00 01 01 02 03 04 "// map
348 + "00 00 00 00 01 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 02");
349 assertArrayEquals(expected.array(),
350 MapRequestSerializer.getInstance().serialize(mapRequestBuilder.build()).array());