2 * Copyright (c) 2013 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
9 package org.opendaylight.lispflowmapping.implementation.serializer;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
16 import java.nio.ByteBuffer;
17 import java.util.ArrayList;
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
23 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
25 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.NoBuilder;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRlocBuilder;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
33 public class MapRequestSerializationTest extends BaseTestCase {
36 public void prefix__NoPrefix() throws Exception {
37 MapRequestBuilder mrBuilder = new MapRequestBuilder();
38 mrBuilder.setEidRecord(new ArrayList<EidRecord>());
39 mrBuilder.getEidRecord().add(
40 new EidRecordBuilder().setLispAddressContainer(
41 new LispAddressContainerBuilder().setAddress(new NoBuilder().setAfi((short) 0).build()).build()).build());
43 assertEquals(AddressFamilyNumberEnum.NO_ADDRESS.getIanaCode(),
44 LispAFIConvertor.toAFI(mrBuilder.getEidRecord().get(0).getLispAddressContainer()).getAfi().shortValue());
48 public void deserialize__FlagsInFirstByte() throws Exception {
49 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 00 00 01 3d 8d "
51 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
52 + "00 01 01 02 03 04"));
53 assertFalse(mr.isAuthoritative());
54 assertTrue(mr.isMapDataPresent());
55 assertTrue(mr.isProbe());
56 assertFalse(mr.isSmr());
58 mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 00 00 01 3d 8d "
60 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
61 + "00 01 01 02 03 04"));
62 assertTrue(mr.isAuthoritative());
63 assertFalse(mr.isMapDataPresent());
64 assertFalse(mr.isProbe());
65 assertTrue(mr.isSmr());
67 mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("1C 00 00 01 3d 8d "
69 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
70 + "00 01 01 02 03 04"));
71 assertTrue(mr.isAuthoritative());
72 assertTrue(mr.isMapDataPresent());
73 assertFalse(mr.isProbe());
74 assertFalse(mr.isSmr());
78 public void serialize__EmptyMapRequest() throws Exception {
80 MapRequestBuilder mrBuilder = new MapRequestBuilder();
81 ByteBuffer expected = hexToByteBuffer("10 00 00 00 00 00 " //
82 + "00 00 00 00 00 00 00 00");
83 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
87 public void serialize__FlagsInFirstByte() throws Exception {
89 MapRequestBuilder mrBuilder = new MapRequestBuilder();
90 mrBuilder.setAuthoritative(true);
91 mrBuilder.setProbe(true);
92 ByteBuffer expected = hexToByteBuffer("1A 00 00 00 00 00 " //
93 + "00 00 00 00 00 00 00 00");
94 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
95 mrBuilder = new MapRequestBuilder();
96 mrBuilder.setSmr(true);
97 mrBuilder.setMapDataPresent(true);
98 expected = hexToByteBuffer("15 00 00 00 00 00 " //
99 + "00 00 00 00 00 00 00 00");
100 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
101 mrBuilder.setAuthoritative(true);
102 mrBuilder.setProbe(true);
103 expected = hexToByteBuffer("1F 00 00 00 00 00 " //
104 + "00 00 00 00 00 00 00 00");
105 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
109 public void serialize__FlagsInSecondByte() throws Exception {
110 MapRequestBuilder mrBuilder = new MapRequestBuilder();
111 mrBuilder.setPitr(true);
112 mrBuilder.setSmrInvoked(true);
113 ByteBuffer expected = hexToByteBuffer("10 C0 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.setPitr(false);
117 expected = hexToByteBuffer("10 40 00 00 00 00 " //
118 + "00 00 00 00 00 00 00 00");
119 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
124 public void deserialize__FlagsInSecondByte() throws Exception {
125 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 01 3d 8d "
127 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
128 + "00 01 01 02 03 04"));
129 assertTrue(mr.isPitr());
130 assertFalse(mr.isSmrInvoked());
132 mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 40 00 01 3d 8d "
134 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
135 + "00 01 01 02 03 04"));
136 assertFalse(mr.isPitr());
137 assertTrue(mr.isSmrInvoked());
141 public void deserialize__SingleEidRecord() throws Exception {
142 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
144 + "01 " // single record
145 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a " //
146 + "00 20 00 01 01 02 03 04"));
148 assertEquals(1, mr.getEidRecord().size());
149 EidRecord eid = mr.getEidRecord().get(0);
150 assertEquals(0x20, eid.getMask().byteValue());
151 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4")), eid.getLispAddressContainer());
155 public void serialize__SingleEidRecord() throws Exception {
156 MapRequestBuilder mrBuilder = new MapRequestBuilder();
157 mrBuilder.setEidRecord(new ArrayList<EidRecord>());
158 mrBuilder.getEidRecord().add(
159 new EidRecordBuilder().setMask((short) 32)
160 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
161 ByteBuffer expected = hexToByteBuffer("10 00 00 01 00 00 " //
162 + "00 00 00 00 00 00 00 00 00 20 00 01 01 02 03 04");
163 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
167 public void deserialize__MultipleEidRecord() throws Exception {
168 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
171 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a " //
172 + "00 20 00 01 01 02 03 04 " //
173 + "00 80 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05"));
175 assertEquals(2, mr.getEidRecord().size());
177 EidRecord eid = mr.getEidRecord().get(0);
178 assertEquals(0x0020, eid.getMask().shortValue());
179 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4")), eid.getLispAddressContainer());
181 eid = mr.getEidRecord().get(1);
182 assertEquals(0x0080, eid.getMask().shortValue());
183 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:5")), eid.getLispAddressContainer());
187 public void serialize__MultipleEidRecord() throws Exception {
188 MapRequestBuilder mrBuilder = new MapRequestBuilder();
189 mrBuilder.setEidRecord(new ArrayList<EidRecord>());
190 mrBuilder.getEidRecord().add(
191 new EidRecordBuilder().setMask((short) 32)
192 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
193 mrBuilder.getEidRecord().add(
194 new EidRecordBuilder().setMask((short) 0)
195 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1"))).build());
196 ByteBuffer expected = hexToByteBuffer("10 00 00 02 00 00 " //
197 + "00 00 00 00 00 00 00 00 00 20 00 01 01 02 03 04 00 00 00 01 04 03 02 01");
198 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
202 public void deserialize__SingleItrRloc() throws Exception {
203 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 "
205 + "00 " // This means 1 ITR-RLOC
206 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
207 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 1)
208 + "00 20 00 01 01 02 03 04"));
210 assertEquals(1, mr.getItrRloc().size());
211 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("192.168.136.10")), mr.getItrRloc().get(0)
212 .getLispAddressContainer());
216 public void serialize__SingleItrRloc() throws Exception {
217 MapRequestBuilder mrBuilder = new MapRequestBuilder();
218 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
219 mrBuilder.getItrRloc().add(
220 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
221 ByteBuffer expected = hexToByteBuffer("10 00 00 00 00 00 " //
222 + "00 00 00 00 00 00 00 00 00 01 01 02 03 04");
223 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
227 public void serialize__MultipleItrRloc() throws Exception {
228 MapRequestBuilder mrBuilder = new MapRequestBuilder();
229 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
230 mrBuilder.getItrRloc().add(
231 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
232 mrBuilder.getItrRloc().add(
233 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1"))).build());
234 ByteBuffer expected = hexToByteBuffer("10 00 01 00 00 00 " //
235 + "00 00 00 00 00 00 00 00 00 01 01 02 03 04 00 01 04 03 02 01");
236 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
240 public void deserialize__MultipleItrRlocs() throws Exception {
241 MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 "
243 + "02 " // This means 3 ITR - RLOCs
244 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
245 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 3)
246 // IPv6 (ITR-RLOC #2 of 3)
247 + "00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 " //
248 + "00 01 11 22 34 56 " // IPv4 (ITR-RLOC #3 of 3)
249 + "00 20 00 01 01 02 03 04"));
251 assertEquals(3, mr.getItrRloc().size());
252 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("192.168.136.10")), mr.getItrRloc().get(0)
253 .getLispAddressContainer());
254 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")), mr.getItrRloc().get(1)
255 .getLispAddressContainer());
256 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("17.34.52.86")), mr.getItrRloc().get(2).getLispAddressContainer());
260 public void serialize__All() throws Exception {
261 MapRequestBuilder mrBuilder = new MapRequestBuilder();
262 mrBuilder.setProbe(true);
263 mrBuilder.setPitr(true);
264 mrBuilder.setNonce((long) 13);
265 mrBuilder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(
266 LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("10.0.0.1"))).build());
267 mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
268 mrBuilder.getItrRloc().add(
269 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
270 mrBuilder.getItrRloc().add(
271 new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("1:2:3:4:5:6:7:8")))
273 mrBuilder.setEidRecord(new ArrayList<EidRecord>());
274 mrBuilder.getEidRecord().add(
275 new EidRecordBuilder().setMask((short) 32)
276 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("1.2.3.4"))).build());
277 ByteBuffer expected = hexToByteBuffer("12 80 01 01 00 00 " //
278 + "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 00 07 00 08 00 20 00 01 01 02 03 04");
279 assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());