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