849a9cbb5d85803695eec0738811624ea7091ccc
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / serializer / MapRequestSerializationTest.java
1 /*
2  * Copyright (c) 2013 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.southbound.serializer;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import org.junit.Test;
16 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
17 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
18 import org.opendaylight.lispflowmapping.type.lisp.EidRecord;
19 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
20 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
21 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
22 import org.opendaylight.lispflowmapping.type.lisp.address.LispNoAddress;
23
24 public class MapRequestSerializationTest extends BaseTestCase {
25
26     @Test
27     public void prefix__NoPrefix() throws Exception {
28         MapRequest mr = new MapRequest();
29         mr.addEidRecord(new EidRecord((byte) 0, null));
30         mr.addEidRecord(new EidRecord((byte) 0, new LispNoAddress()));
31
32         assertEquals(AddressFamilyNumberEnum.RESERVED, mr.getEids().get(0).getPrefix().getAfi());
33         assertEquals(AddressFamilyNumberEnum.RESERVED, mr.getEids().get(1).getPrefix().getAfi());
34     }
35
36     @Test
37     public void deserialize__FlagsInFirstByte() throws Exception {
38         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 00 00 01 3d 8d " //
39                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
40                 + "00 01 01 02 03 04"));
41         assertFalse(mr.isAuthoritative());
42         assertTrue(mr.isMapDataPresent());
43         assertTrue(mr.isProbe());
44         assertFalse(mr.isSmr());
45
46         mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 00 00 01 3d 8d " //
47                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
48                 + "00 01 01 02 03 04"));
49         assertTrue(mr.isAuthoritative());
50         assertFalse(mr.isMapDataPresent());
51         assertFalse(mr.isProbe());
52         assertTrue(mr.isSmr());
53
54         mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("1C 00 00 01 3d 8d " //
55                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
56                 + "00 01 01 02 03 04"));
57         assertTrue(mr.isAuthoritative());
58         assertTrue(mr.isMapDataPresent());
59         assertFalse(mr.isProbe());
60         assertFalse(mr.isSmr());
61     }
62
63     @Test
64     public void deserialize__FlagsInSecondByte() throws Exception {
65         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 01 3d 8d " //
66                 + "2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a 00 20 " //
67                 + "00 01 01 02 03 04"));
68         assertTrue(mr.isPitr());
69         assertFalse(mr.isSmrInvoked());
70
71         mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("19 40 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         assertFalse(mr.isPitr());
75         assertTrue(mr.isSmrInvoked());
76     }
77
78     @Test
79     public void deserialize__SingleEidRecord() throws Exception {
80         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 " //
81                 + "01 " // single record
82                 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a " //
83                 + "00 20 00 01 01 02 03 04"));
84
85         assertEquals(1, mr.getEids().size());
86         EidRecord eid = mr.getEids().get(0);
87         assertEquals(0x20, eid.getMaskLength());
88         assertEquals(new LispIpv4Address(0x01020304), eid.getPrefix());
89     }
90
91     @Test
92     public void deserialize__MultipleEidRecord() throws Exception {
93         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("16 80 00 " //
94                 + "02 " // 2 records
95                 + "3d 8d 2a cd 39 c8 d6 08 00 00 00 01 c0 a8 88 0a " //
96                 + "00 20 00 01 01 02 03 04 " //
97                 + "00 80 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05"));
98
99         assertEquals(2, mr.getEids().size());
100
101         EidRecord eid = mr.getEids().get(0);
102         assertEquals(0x20, eid.getMaskLength());
103         assertEquals(new LispIpv4Address(0x01020304), eid.getPrefix());
104
105         eid = mr.getEids().get(1);
106         assertEquals(0x80, eid.getMaskLength());
107         assertEquals(new LispIpv6Address("::5"), eid.getPrefix());
108     }
109
110     @Test
111     public void deserialize__SingleItrRloc() throws Exception {
112         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 " //
113                 + "00 " // This means 1 ITR-RLOC
114                 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
115                 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 1)
116                 + "00 20 00 01 01 02 03 04"));
117
118         assertEquals(1, mr.getItrRlocs().size());
119         assertEquals(new LispIpv4Address(0xC0A8880A), mr.getItrRlocs().get(0));
120     }
121
122     @Test
123     public void deserialize__MultipleItrRlocs() throws Exception {
124         MapRequest mr = MapRequestSerializer.getInstance().deserialize(hexToByteBuffer("10 00 " //
125                 + "02 " // This means 3 ITR - RLOCs
126                 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
127                 + "00 01 c0 a8 88 0a " // IPv4 (ITR-RLOC #1 of 3)
128                 // IPv6 (ITR-RLOC #2 of 3)
129                 + "00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 " //
130                 + "00 01 11 22 34 56 " // IPv4 (ITR-RLOC #3 of 3)
131                 + "00 20 00 01 01 02 03 04"));
132
133         assertEquals(3, mr.getItrRlocs().size());
134         assertEquals(new LispIpv4Address(0xC0A8880A), mr.getItrRlocs().get(0));
135         assertEquals(new LispIpv6Address("::1"), mr.getItrRlocs().get(1));
136         assertEquals(new LispIpv4Address(0x11223456), mr.getItrRlocs().get(2));
137     }
138 }