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