fce1ed52b60048c1882ee75fab9bfb3d2ee447e3
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / MapRequestSerializationTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.lispflowmapping.serializer;
9
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;
14
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;
38
39 public class MapRequestSerializationTest extends BaseTestCase {
40
41     @Test
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")
46                 .build());
47
48         assertEquals(NoAddressAfi.VALUE, mrBuilder.getEidItem().get(0).getEid().getAddressType());
49     }
50
51     @Test
52     public void deserialize__FlagsInFirstByte() throws Exception {
53         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 00 00 01 3d 8d "
54                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
55                 + "00 01 01 02 03 04"), null);
56         assertFalse(mr.getAuthoritative());
57         assertTrue(mr.getMapDataPresent());
58         assertTrue(mr.getProbe());
59         assertFalse(mr.getSmr());
60
61         mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 00 00 01 3d 8d "
62                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
63                 + "00 01 01 02 03 04"), null);
64         assertTrue(mr.getAuthoritative());
65         assertFalse(mr.getMapDataPresent());
66         assertFalse(mr.getProbe());
67         assertTrue(mr.getSmr());
68
69         mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("1C 00 00 01 3d 8d "
70                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
71                 + "00 01 01 02 03 04"), null);
72         assertTrue(mr.getAuthoritative());
73         assertTrue(mr.getMapDataPresent());
74         assertFalse(mr.getProbe());
75         assertFalse(mr.getSmr());
76     }
77
78     @Test
79     public void deserialize__LispMobMapRequestWithReply() throws Exception {
80         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("14 00 00 01 3e f7 "
81                 + "7f 5b 41 7c 77 3c 00 01 01 01 01 01 00 01 c0 a8 "
82                 + "38 66 00 20 00 01 01 02 03 04 00 00 00 0a 01 20 "
83                 + "10 00 00 00 00 01 01 01 01 01 01 64 ff 00 00 05 "
84                 + "00 01 c0 a8 38 66"), null);
85         assertArrayEquals(new byte[] {1, 1, 1, 1},
86                 ((Ipv4Binary) mr.getSourceEid().getEid().getAddress()).getIpv4Binary().getValue());
87         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
88
89     }
90
91     @Test
92     public void serialize__EmptyMapRequest() throws Exception {
93
94         MapRequestBuilder mrBuilder = new MapRequestBuilder();
95         ByteBuffer expected = hexToByteBuffer("10 00 00 00 00 00 "
96                 + "00 00 00 00 00 00 00 00");
97         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
98     }
99
100     @Test
101     public void serialize__FlagsInFirstByte() throws Exception {
102
103         MapRequestBuilder mrBuilder = new MapRequestBuilder();
104         mrBuilder.setAuthoritative(true);
105         mrBuilder.setProbe(true);
106         ByteBuffer expected = hexToByteBuffer("1A 00 00 00 00 00 "
107                 + "00 00 00 00 00 00 00 00");
108         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
109         mrBuilder = new MapRequestBuilder();
110         mrBuilder.setSmr(true);
111         mrBuilder.setMapDataPresent(true);
112         expected = hexToByteBuffer("15 00 00 00 00 00 "
113                 + "00 00 00 00 00 00 00 00");
114         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
115         mrBuilder.setAuthoritative(true);
116         mrBuilder.setProbe(true);
117         expected = hexToByteBuffer("1F 00 00 00 00 00 "
118                 + "00 00 00 00 00 00 00 00");
119         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
120     }
121
122     @Test
123     public void serialize__FlagsInSecondByte() throws Exception {
124         MapRequestBuilder mrBuilder = new MapRequestBuilder();
125         mrBuilder.setPitr(true);
126         mrBuilder.setSmrInvoked(true);
127         ByteBuffer expected = hexToByteBuffer("10 C0 00 00 00 00 "
128                 + "00 00 00 00 00 00 00 00");
129         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
130         mrBuilder.setPitr(false);
131         expected = hexToByteBuffer("10 40 00 00 00 00 "
132                 + "00 00 00 00 00 00 00 00");
133         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
134
135     }
136
137     @Test
138     public void deserialize__FlagsInSecondByte() throws Exception {
139         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 01 3d 8d "
140                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
141                 + "00 01 01 02 03 04"), null);
142         assertTrue(mr.getPitr());
143         assertFalse(mr.getSmrInvoked());
144
145         mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 40 00 01 3d 8d "
146                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 "
147                 + "00 01 01 02 03 04"), null);
148         assertFalse(mr.getPitr());
149         assertTrue(mr.getSmrInvoked());
150     }
151
152     @Test
153     public void deserialize__SingleEidItem() throws Exception {
154         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
155                 + "01 " // single record
156                 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a "
157                 + "00 20 00 01 01 02 03 04"), null);
158
159         assertEquals(1, mr.getEidItem().size());
160         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
161     }
162
163     @Test
164     public void deserialize__ContainsMapReply() throws Exception {
165         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
166                 + "01 " // single record
167                 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a "
168                 + "00 20 00 01 01 02 03 04 "// end of map request
169                 + "00 00 "
170                 + "00 02 01 20 00 00 00 00 "
171                 + "00 01 01 02 03 04 01 02 "
172                 + "03 04 00 06 00 01 0a 0a "
173                 + "0a 0a"
174
175         ), null);
176
177         assertEquals(1, mr.getEidItem().size());
178         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
179         MappingRecord record = mr.getMapReply().getMappingRecord();
180         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), record.getEid());
181         assertEquals(false, record.getAuthoritative());
182         assertEquals(Action.NoAction, record.getAction());
183         assertEquals(0, record.getMapVersion().shortValue());
184         assertEquals(32, MaskUtil.getMaskForAddress(record.getEid().getAddress()));
185         assertEquals(2, record.getRecordTtl().byteValue());
186         assertArrayEquals(new byte[] {10, 10, 10, 10},
187                 ((Ipv4Binary) record.getLocatorRecord().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
188         assertEquals(1, record.getLocatorRecord().get(0).getPriority().byteValue());
189         assertEquals(2, record.getLocatorRecord().get(0).getWeight().byteValue());
190         assertEquals(3, record.getLocatorRecord().get(0).getMulticastPriority().byteValue());
191         assertEquals(4, record.getLocatorRecord().get(0).getMulticastWeight().byteValue());
192         assertEquals(true, record.getLocatorRecord().get(0).getLocalLocator());
193         assertEquals(true, record.getLocatorRecord().get(0).getRlocProbed());
194         assertEquals(false, record.getLocatorRecord().get(0).getRouted());
195     }
196
197     @Test
198     public void serialize__SingleEidItem() throws Exception {
199         MapRequestBuilder mrBuilder = new MapRequestBuilder();
200         mrBuilder.setEidItem(new ArrayList<EidItem>());
201         mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
202                 .setEidItemId("bar").build());
203         ByteBuffer expected = hexToByteBuffer("10 00 00 01 00 00 " //
204                 + "00 00 00 00 00 00 00 00 00 20 00 01 01 02 03 04");
205         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
206     }
207
208     @Test
209     public void deserialize__MultipleEidItem() throws Exception {
210         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 "
211         //
212                 + "02 " // 2 records
213                 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a " //
214                 + "00 20 00 01 01 02 03 04 " //
215                 + "00 80 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05"), null);
216
217         assertEquals(2, mr.getEidItem().size());
218         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), mr.getEidItem().get(0).getEid());
219         assertEquals(LispAddressUtil.asIpv6PrefixBinaryEid("0:0:0:0:0:0:0:5/128"), mr.getEidItem().get(1).getEid());
220     }
221
222     @Test
223     public void serialize__MultipleEidItem() throws Exception {
224         MapRequestBuilder mrBuilder = new MapRequestBuilder();
225         mrBuilder.setEidItem(new ArrayList<EidItem>());
226         mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
227                 .setEidItemId("bar").build());
228         mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("4.3.2.1/0"))
229                 .setEidItemId("bar").build());
230         ByteBuffer expected = hexToByteBuffer("10 00 00 02 00 00 " //
231                 + "00 00 00 00 00 00 00 00 00 20 00 01 01 02 03 04 00 00 00 01 04 03 02 01");
232         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
233     }
234
235     @Test
236     public void deserialize__SingleItrRloc() throws Exception {
237         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 "
238         //
239                 + "00 " // This means 1 ITR-RLOC
240                 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
241                 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 1)
242                 + "00 20 00 01 01 02 03 04"), null);
243
244         assertEquals(1, mr.getItrRloc().size());
245         assertArrayEquals(new byte[] {(byte) 192, (byte) 168, (byte) 136, (byte) 10},
246                 ((Ipv4Binary) mr.getItrRloc().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
247     }
248
249     @Test
250     public void serialize__SingleItrRloc() throws Exception {
251         MapRequestBuilder mrBuilder = new MapRequestBuilder();
252         mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
253         mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
254                 .setItrRlocId("foo").build());
255         ByteBuffer expected = hexToByteBuffer("10 00 00 00 00 00 " //
256                 + "00 00 00 00 00 00 00 00 00 01 01 02 03 04");
257         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
258     }
259
260     @Test
261     public void serialize__MultipleItrRloc() throws Exception {
262         MapRequestBuilder mrBuilder = new MapRequestBuilder();
263         mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
264         mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
265                 .setItrRlocId("foo").build());
266         mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1"))
267                 .setItrRlocId("foo").build());
268         ByteBuffer expected = hexToByteBuffer("10 00 01 00 00 00 " //
269                 + "00 00 00 00 00 00 00 00 00 01 01 02 03 04 00 01 04 03 02 01");
270         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
271     }
272
273     @Test
274     public void deserialize__MultipleItrRlocs() throws Exception {
275         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 "
276                 + "02 " // This means 3 ITR - RLOCs
277                 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 "
278                 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 3)
279                 // IPv6 (ITR-RLOC #2 of 3)
280                 + "00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 " //
281                 + "00 01 11 22 34 56 " // IPv4 (ITR-RLOC #3 of 3)
282                 + "00 20 00 01 01 02 03 04"), null);
283
284         assertEquals(3, mr.getItrRloc().size());
285         assertArrayEquals(new byte[] {(byte) 192, (byte) 168, (byte) 136, (byte) 10},
286                 ((Ipv4Binary) mr.getItrRloc().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
287         assertArrayEquals(new byte[] {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
288                 ((Ipv6Binary) mr.getItrRloc().get(1).getRloc().getAddress()).getIpv6Binary().getValue());
289         assertArrayEquals(new byte[] {17, 34, 52, 86},
290                 ((Ipv4Binary) mr.getItrRloc().get(2).getRloc().getAddress()).getIpv4Binary().getValue());
291     }
292
293     @Test
294     public void serialize__All() throws Exception {
295         MapRequestBuilder mrBuilder = new MapRequestBuilder();
296         mrBuilder.setProbe(true);
297         mrBuilder.setPitr(true);
298         mrBuilder.setNonce((long) 13);
299         mrBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid("10.0.0.1")).build());
300         mrBuilder.setItrRloc(new ArrayList<ItrRloc>());
301         mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
302             .setItrRlocId("foo").build());
303         mrBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv6Rloc("1:2:3:4:5:6:7:8"))
304             .setItrRlocId("foo").build());
305         mrBuilder.setEidItem(new ArrayList<EidItem>());
306         mrBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
307                 .setEidItemId("bar").build());
308         ByteBuffer expected = hexToByteBuffer("12 80 01 01 00 00 " //
309                 + "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 "
310                 + "00 07 00 08 00 20 00 01 01 02 03 04");
311         assertArrayEquals(expected.array(), MapRequestSerializer.getInstance().serialize(mrBuilder.build()).array());
312     }
313
314     @Test
315     public void serialize__AllWithMapReply() throws Exception {
316         MapRequestBuilder mapRequestBuilder = new MapRequestBuilder();
317         mapRequestBuilder.setProbe(true);
318         mapRequestBuilder.setMapDataPresent(true);
319         mapRequestBuilder.setPitr(true);
320         mapRequestBuilder.setNonce((long) 13);
321         mapRequestBuilder.setSourceEid(new SourceEidBuilder().setEid(LispAddressUtil.asIpv4Eid("10.0.0.1"))
322                 .build());
323         mapRequestBuilder.setItrRloc(new ArrayList<ItrRloc>());
324         mapRequestBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv4Rloc("1.2.3.4"))
325                 .setItrRlocId("foo").build());
326         mapRequestBuilder.getItrRloc().add(new ItrRlocBuilder().setRloc(LispAddressUtil.asIpv6Rloc("1:2:3:4:5:6:7:8"))
327                 .setItrRlocId("foo").build());
328         mapRequestBuilder.setEidItem(new ArrayList<EidItem>());
329         mapRequestBuilder.getEidItem().add(new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"))
330                 .setEidItemId("bar").build());
331
332         MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
333         recordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/0"));
334         recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
335
336         LocatorRecordBuilder locatorBuilder = new LocatorRecordBuilder().setLocatorId("baz");
337         locatorBuilder.setRloc(LispAddressUtil.asIpv4Rloc("0.0.0.2"));
338         recordBuilder.getLocatorRecord().add(locatorBuilder.build());
339
340         MapReplyBuilder mapreplyBuilder = new MapReplyBuilder();
341         mapreplyBuilder.setMappingRecord(recordBuilder.build());
342         mapRequestBuilder.setMapReply(mapreplyBuilder.build());
343
344         ByteBuffer expected = hexToByteBuffer("16 80 01 01 00 00 "
345                 + "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 "
346                 + "00 07 00 08 00 20 00 01 01 02 03 04 "// map
347                 + "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");
348         assertArrayEquals(expected.array(),
349                 MapRequestSerializer.getInstance().serialize(mapRequestBuilder.build()).array());
350     }
351 }