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