Bump versions by x.(y+1).z
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / MapReplySerializationTest.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.fail;
13 import static org.opendaylight.lispflowmapping.TestUtils.assertHexEquals;
14 import static org.opendaylight.lispflowmapping.TestUtils.hexToByteBuffer;
15
16 import java.nio.ByteBuffer;
17 import java.util.ArrayList;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
21 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
22 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
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.Ipv4PrefixBinary;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6Binary;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.container.MappingRecord.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.container.MappingRecordBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.list.MappingRecordItem;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping._record.list.MappingRecordItemBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint8;
35
36 public class MapReplySerializationTest {
37
38     @Test
39     @Ignore
40     public void todo() throws Exception {
41         fail("support no eid to locator???");
42         fail("Validation of values in setters (map-version)");
43         fail("Non NPE for no locator in LocatorRecord.serialize");
44     }
45
46     @Test
47     public void serialize__SomeFlags() throws Exception {
48         MapReplyBuilder mr = new MapReplyBuilder();
49         mr.setMappingRecordItem(new ArrayList<MappingRecordItem>());
50         MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
51         recordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/32"));
52         mr.getMappingRecordItem().add(
53                 new MappingRecordItemBuilder().setMappingRecord(recordBuilder.build()).setMappingRecordItemId("xyzzy")
54                     .build());
55         mr.setProbe(true);
56         mr.setEchoNonceEnabled(false);
57
58         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr.build());
59         byte firstByte = packet.get(0);
60         assertHexEquals((byte) 0x28, firstByte);
61
62         mr.setProbe(false);
63         mr.setEchoNonceEnabled(true);
64
65         packet = MapReplySerializer.getInstance().serialize(mr.build());
66         firstByte = packet.get(0);
67         assertHexEquals((byte) 0x24, firstByte);
68     }
69
70     @Test
71     public void deserialize__SomeFlags() throws Exception {
72
73         MapReply mr = MapReplySerializer.getInstance().deserialize(hexToByteBuffer("2A 00 00 00 00 00 "
74                 + "00 00 00 00 00 00"));
75         assertEquals(true, mr.getProbe());
76         assertEquals(false, mr.getEchoNonceEnabled());
77         assertEquals(true, mr.getSecurityEnabled());
78     }
79
80     @Test
81     public void deserialize__All() throws Exception {
82         MapReply mr = MapReplySerializer.getInstance().deserialize(hexToByteBuffer("20 00 00 02 00 00 "
83                 + "00 00 00 00 00 02 00 00 "
84                 + "00 02 02 20 00 00 00 00 "
85                 + "00 01 01 02 03 04 01 02 "
86                 + "03 04 00 06 00 01 0a 0a "
87                 + "0a 0a 04 03 02 01 00 01 "
88                 + "00 02 00 01 00 02 00 03 "
89                 + "00 04 00 05 00 06 00 07 00 08 00 00 00 00 00 10 30 00 00 02 00 01 04 03 00 00"));
90         assertEquals(2, mr.getNonce().longValue());
91         assertArrayEquals(new byte[] {1, 2, 3, 4}, ((Ipv4PrefixBinary) mr.getMappingRecordItem().get(0)
92                 .getMappingRecord().getEid().getAddress()).getIpv4AddressBinary().getValue());
93         // XXX Why here normalized and other cases not normalized?
94         assertArrayEquals(new byte[] {4, 3, 0, 0}, ((Ipv4PrefixBinary) mr.getMappingRecordItem().get(1)
95                 .getMappingRecord().getEid().getAddress()).getIpv4AddressBinary().getValue());
96         assertEquals(false, mr.getMappingRecordItem().get(0).getMappingRecord().getAuthoritative());
97         assertEquals(true, mr.getMappingRecordItem().get(1).getMappingRecord().getAuthoritative());
98         assertEquals(Action.NoAction, mr.getMappingRecordItem().get(0).getMappingRecord().getAction());
99         assertEquals(Action.NativelyForward, mr.getMappingRecordItem().get(1).getMappingRecord().getAction());
100         assertEquals(0, mr.getMappingRecordItem().get(0).getMappingRecord().getMapVersion().shortValue());
101         assertEquals(2, mr.getMappingRecordItem().get(1).getMappingRecord().getMapVersion().shortValue());
102         assertEquals(32, MaskUtil.getMaskForAddress(mr.getMappingRecordItem().get(0).getMappingRecord()
103                 .getEid().getAddress()));
104         assertEquals(16, MaskUtil.getMaskForAddress(mr.getMappingRecordItem().get(1).getMappingRecord()
105                 .getEid().getAddress()));
106         assertEquals(2, mr.getMappingRecordItem().get(0).getMappingRecord().getRecordTtl().byteValue());
107         assertEquals(0, mr.getMappingRecordItem().get(1).getMappingRecord().getRecordTtl().byteValue());
108         assertArrayEquals(new byte[] {0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8},
109                 ((Ipv6Binary) mr.getMappingRecordItem().get(0).getMappingRecord()
110                 .getLocatorRecord().get(1).getRloc().getAddress()).getIpv6Binary().getValue());
111         assertArrayEquals(new byte[] {10, 10, 10, 10},
112                 ((Ipv4Binary) mr.getMappingRecordItem().get(0).getMappingRecord()
113                 .getLocatorRecord().get(0).getRloc().getAddress()).getIpv4Binary().getValue());
114         assertEquals(1, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
115                 .getPriority().byteValue());
116         assertEquals(2, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
117                 .getWeight().byteValue());
118         assertEquals(3, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
119                 .getMulticastPriority().byteValue());
120         assertEquals(4, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
121                 .getMulticastWeight().byteValue());
122         assertEquals(4, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
123                 .getPriority().byteValue());
124         assertEquals(3, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
125                 .getWeight().byteValue());
126         assertEquals(2, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
127                 .getMulticastPriority().byteValue());
128         assertEquals(1, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
129                 .getMulticastWeight().byteValue());
130         assertEquals(true, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
131                 .getLocalLocator());
132         assertEquals(true, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
133                 .getRlocProbed());
134         assertEquals(false, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(0)
135                 .getRouted());
136         assertEquals(false, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
137                 .getLocalLocator());
138         assertEquals(false, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1)
139                 .getRlocProbed());
140         assertEquals(true, mr.getMappingRecordItem().get(0).getMappingRecord().getLocatorRecord().get(1).getRouted());
141     }
142
143     @Test
144     public void serialize__MultipleRecordsWithoutRLOCs() throws Exception {
145         MapReplyBuilder mrBuilder = new MapReplyBuilder();
146         mrBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
147         MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
148         recordBuilder.setEid(LispAddressUtil.asIpv6PrefixEid("0:0:0:0:0:0:0:8/128"));
149         mrBuilder.getMappingRecordItem().add(
150                 new MappingRecordItemBuilder().setMappingRecord(recordBuilder.build()).setMappingRecordItemId("xyzzy")
151                     .build());
152
153         recordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("8.2.4.5/32"));
154         mrBuilder.getMappingRecordItem().add(
155                 new MappingRecordItemBuilder().setMappingRecord(recordBuilder.build()).setMappingRecordItemId("xyzzy")
156                     .build());
157
158         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mrBuilder.build());
159         assertEquals(2, packet.get(3));
160
161         packet.position(24); // EID in first record
162         byte[] expected = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163             0x08 };
164         byte[] actual = new byte[16];
165         packet.get(actual);
166         assertArrayEquals(expected, actual);
167
168         packet.position(packet.position() + 12); // EID in second record
169         assertEquals(0x08020405, packet.getInt());
170     }
171
172     @Test
173     public void deserialize__MultipleRecordsWithoutRLOCs() throws Exception {
174         MapReply mr = MapReplySerializer.getInstance().deserialize(hexToByteBuffer("20 00 00 02 00 00 "
175                 + "00 00 00 00 00 00 00 00 00 01 00 20 00 00 00 00 "
176                 + "00 01 01 02 03 04 00 00 00 00 00 10 30 00 00 02 00 01 04 03 00 00"));
177         assertArrayEquals(new byte[] {1, 2, 3, 4}, ((Ipv4PrefixBinary) mr.getMappingRecordItem().get(0)
178                 .getMappingRecord().getEid().getAddress()).getIpv4AddressBinary().getValue());
179         // XXX Why here normalized and other cases not normalized?
180         assertArrayEquals(new byte[] {4, 3, 0, 0}, ((Ipv4PrefixBinary) mr.getMappingRecordItem().get(1)
181                 .getMappingRecord().getEid().getAddress()).getIpv4AddressBinary().getValue());
182         assertEquals(false, mr.getMappingRecordItem().get(0).getMappingRecord().getAuthoritative());
183         assertEquals(true, mr.getMappingRecordItem().get(1).getMappingRecord().getAuthoritative());
184         assertEquals(Action.NoAction, mr.getMappingRecordItem().get(0).getMappingRecord().getAction());
185         assertEquals(Action.NativelyForward, mr.getMappingRecordItem().get(1).getMappingRecord().getAction());
186         assertEquals(0, mr.getMappingRecordItem().get(0).getMappingRecord().getMapVersion().shortValue());
187         assertEquals(2, mr.getMappingRecordItem().get(1).getMappingRecord().getMapVersion().shortValue());
188         assertEquals(32, MaskUtil.getMaskForAddress(mr.getMappingRecordItem().get(0).getMappingRecord()
189                 .getEid().getAddress()));
190         assertEquals(16, MaskUtil.getMaskForAddress(mr.getMappingRecordItem().get(1).getMappingRecord()
191                 .getEid().getAddress()));
192         assertEquals(1, mr.getMappingRecordItem().get(0).getMappingRecord().getRecordTtl().byteValue());
193         assertEquals(0, mr.getMappingRecordItem().get(1).getMappingRecord().getRecordTtl().byteValue());
194     }
195
196     @Test
197     public void serialize__EidRecordDefaultAction() throws Exception {
198         MapReplyBuilder mrBuilder = new MapReplyBuilder();
199         mrBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
200         MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
201         recordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/32"));
202         mrBuilder.getMappingRecordItem().add(
203                 new MappingRecordItemBuilder().setMappingRecord(recordBuilder.build())
204                     .setMappingRecordItemId("xyzzy").build());
205
206         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mrBuilder.build());
207
208         packet.position(18);
209         assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
210     }
211
212     @Test
213     public void serialize__EidRecordNullActionShouldTranslateToDefault() throws Exception {
214         MapReplyBuilder mrBuilder = new MapReplyBuilder();
215         mrBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
216         MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
217         recordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/32"));
218         recordBuilder.setAction(null);
219         mrBuilder.getMappingRecordItem().add(
220                 new MappingRecordItemBuilder().setMappingRecord(recordBuilder.build())
221                     .setMappingRecordItemId("xyzzy").build());
222
223         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mrBuilder.build());
224
225         packet.position(18);
226         assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
227     }
228
229     @Test
230     public void serialize__EidRecordFields() throws Exception {
231         MapReplyBuilder mrBuilder = new MapReplyBuilder();
232         mrBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
233
234         MappingRecordBuilder eidToLocator1 = new MappingRecordBuilder();
235         eidToLocator1.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/32"));
236         eidToLocator1.setRecordTtl(7);
237         eidToLocator1.setAction(Action.SendMapRequest);
238         eidToLocator1.setAuthoritative(true);
239         eidToLocator1.setMapVersion((short) 3072);
240         mrBuilder.getMappingRecordItem().add(
241                 new MappingRecordItemBuilder().setMappingRecord(eidToLocator1.build()).setMappingRecordItemId("xyzzy")
242                     .build());
243
244         MappingRecordBuilder eidToLocator2 = new MappingRecordBuilder();
245         eidToLocator2.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.7/32"));
246         eidToLocator2.setRecordTtl(1000000);
247         eidToLocator2.setAction(Action.Drop);
248         eidToLocator2.setAuthoritative(false);
249         eidToLocator2.setMapVersion((short) 29);
250         mrBuilder.getMappingRecordItem().add(
251                 new MappingRecordItemBuilder().setMappingRecord(eidToLocator2.build()).setMappingRecordItemId("xyzzy")
252                     .build());
253
254         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mrBuilder.build());
255
256         packet.position(12); // First record
257         assertEquals(7, packet.getInt());
258         packet.position(packet.position() + 2); // skip Locator Count & Mask-len
259
260         assertHexEquals((byte) 0x50, packet.get());
261         packet.position(packet.position() + 1); // skip Reserved byte
262         assertEquals((short) 3072, packet.getShort());
263
264         packet.position(packet.position() + 6); // Second record
265         assertEquals(1000000, packet.getInt());
266         packet.position(packet.position() + 2); // skip Locator Count & Mask-len
267
268         assertHexEquals((byte) 0x60, packet.get());
269         packet.position(packet.position() + 1); // skip Reserved byte
270         assertEquals((short) 29, packet.getShort());
271     }
272
273     @Test
274     public void serialize__LocatorRecordFields() throws Exception {
275         MapReplyBuilder mrBuilder = new MapReplyBuilder();
276         mrBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
277
278         MappingRecordBuilder eidToLocatorBuilder = new MappingRecordBuilder();
279         eidToLocatorBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("0.0.0.1/32"));
280         eidToLocatorBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
281
282         LocatorRecordBuilder locatorBuilder1 = new LocatorRecordBuilder();
283         locatorBuilder1.setPriority(Uint8.valueOf(0xF3));
284         locatorBuilder1.setWeight(Uint8.valueOf(0xF6));
285         locatorBuilder1.setMulticastPriority(Uint8.valueOf(0xA3));
286         locatorBuilder1.setMulticastWeight(Uint8.valueOf(0x06));
287         locatorBuilder1.setRloc(LispAddressUtil.asIpv4Rloc("0.0.0.1"));
288         locatorBuilder1.setLocalLocator(true);
289         locatorBuilder1.setRlocProbed(true);
290         locatorBuilder1.setRouted(true);
291         locatorBuilder1.setLocatorId("someId");
292         eidToLocatorBuilder.getLocatorRecord().add(locatorBuilder1.build());
293
294         LocatorRecordBuilder locatorBuilder2 = new LocatorRecordBuilder();
295         locatorBuilder2.setPriority(Uint8.valueOf(0x03));
296         locatorBuilder2.setWeight(Uint8.valueOf(0x06));
297         locatorBuilder2.setMulticastPriority(Uint8.valueOf(0x03));
298         locatorBuilder2.setMulticastWeight(Uint8.valueOf(0xF1));
299         locatorBuilder2.setRloc(LispAddressUtil.asIpv4Rloc("0.0.0.2"));
300         locatorBuilder2.setLocalLocator(false);
301         locatorBuilder2.setRlocProbed(false);
302         locatorBuilder2.setRouted(false);
303         locatorBuilder2.setLocatorId("otherId");
304         eidToLocatorBuilder.getLocatorRecord().add(locatorBuilder2.build());
305
306         mrBuilder.getMappingRecordItem().add(
307                 new MappingRecordItemBuilder().setMappingRecord(eidToLocatorBuilder.build())
308                     .setMappingRecordItemId("xyzzy").build());
309
310         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mrBuilder.build());
311
312         packet.position(12 + 16); // First locator record
313         assertHexEquals((byte) 0xF3, packet.get());
314         assertHexEquals((byte) 0xF6, packet.get());
315         assertHexEquals((byte) 0xA3, packet.get());
316         assertHexEquals((byte) 0x06, packet.get());
317         packet.position(packet.position() + 1);
318         assertHexEquals((byte) 0x07, packet.get());
319
320         packet.position(packet.position() + 6); // Second locator record
321         assertHexEquals((byte) 0x03, packet.get());
322         assertHexEquals((byte) 0x06, packet.get());
323         assertHexEquals((byte) 0x03, packet.get());
324         assertHexEquals((byte) 0xF1, packet.get());
325         packet.position(packet.position() + 1);
326         assertHexEquals((byte) 0x00, packet.get());
327     }
328 }