Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPSubobjectParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, 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.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.junit.Test;
20 import org.opendaylight.protocol.concepts.IPv4Address;
21 import org.opendaylight.protocol.concepts.IPv4Prefix;
22 import org.opendaylight.protocol.concepts.IPv6Address;
23 import org.opendaylight.protocol.concepts.IPv6Prefix;
24 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
25 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
26 import org.opendaylight.protocol.pcep.concepts.UnnumberedInterfaceIdentifier;
27 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
28 import org.opendaylight.protocol.pcep.impl.subobject.EROIPv4PrefixSubobjectParser;
29 import org.opendaylight.protocol.pcep.impl.subobject.EROIPv6PrefixSubobjectParser;
30 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
31 import org.opendaylight.protocol.pcep.subobject.EROAsNumberSubobject;
32 import org.opendaylight.protocol.pcep.subobject.EROExplicitExclusionRouteSubobject;
33 import org.opendaylight.protocol.pcep.subobject.EROGeneralizedLabelSubobject;
34 import org.opendaylight.protocol.pcep.subobject.EROIPPrefixSubobject;
35 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith128PCEIDSubobject;
36 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith32PCEIDSubobject;
37 import org.opendaylight.protocol.pcep.subobject.EROProtectionType1Subobject;
38 import org.opendaylight.protocol.pcep.subobject.EROProtectionType2Subobject;
39 import org.opendaylight.protocol.pcep.subobject.EROType1LabelSubobject;
40 import org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject;
41 import org.opendaylight.protocol.pcep.subobject.EROWavebandSwitchingLabelSubobject;
42 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
43 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
44 import org.opendaylight.protocol.pcep.subobject.RROAttributesSubobject;
45 import org.opendaylight.protocol.pcep.subobject.RROGeneralizedLabelSubobject;
46 import org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject;
47 import org.opendaylight.protocol.pcep.subobject.RROPathKeyWith128PCEIDSubobject;
48 import org.opendaylight.protocol.pcep.subobject.RROPathKeyWith32PCEIDSubobject;
49 import org.opendaylight.protocol.pcep.subobject.RROProtectionType1Subobject;
50 import org.opendaylight.protocol.pcep.subobject.RROProtectionType2Subobject;
51 import org.opendaylight.protocol.pcep.subobject.RROType1LabelSubobject;
52 import org.opendaylight.protocol.pcep.subobject.RROWavebandSwitchingLabelSubobject;
53 import org.opendaylight.protocol.pcep.subobject.ReportedRouteSubobject;
54 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
55 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
56 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
57 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
58 import org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject;
59 import org.opendaylight.protocol.util.ByteArray;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
61
62 /**
63  * Tests for subobjects
64  */
65 public class PCEPSubobjectParserTest {
66         final byte[] ipv6bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
67                         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
68         final byte[] ipv6bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
69                         (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
70
71         final byte[] ipv4bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
72         final byte[] ipv4bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 };
73
74         @Test
75         public void testSerDeser() throws PCEPDeserializerException, IOException {
76                 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfSubobjects.bin");
77                 final List<ExplicitRouteSubobject> objsToTest = PCEPEROSubobjectParser.parse(bytesFromFile);
78
79                 assertEquals(8, objsToTest.size());
80
81                 assertEquals(objsToTest.get(0), new EROAsNumberSubobject(new AsNumber(0xFFFFL), true));
82                 assertEquals(objsToTest.get(1), new EROAsNumberSubobject(new AsNumber(0x0010L), false));
83                 assertEquals(objsToTest.get(2), new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(this.ipv4bytes1), 0x20), true));
84
85                 assertEquals(objsToTest.get(3), new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(this.ipv4bytes2), 0x15), false));
86                 assertEquals(objsToTest.get(4), new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(this.ipv6bytes1), 0x80), true));
87
88                 assertEquals(objsToTest.get(5), new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(this.ipv6bytes2), 0x16), false));
89                 final byte[] addr1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
90                 assertEquals(objsToTest.get(6),
91                                 new EROUnnumberedInterfaceSubobject(new IPv4Address(addr1), new UnnumberedInterfaceIdentifier(0xFFFFFFFFL), true));
92
93                 final byte[] addr2 = { (byte) 0x01, (byte) 0x24, (byte) 0x56, (byte) 0x78 };
94                 assertEquals(objsToTest.get(7),
95                                 new EROUnnumberedInterfaceSubobject(new IPv4Address(addr2), new UnnumberedInterfaceIdentifier(0x9ABCDEF0L), false));
96
97                 assertArrayEquals(bytesFromFile, PCEPEROSubobjectParser.put(objsToTest));
98
99         }
100
101         @Test
102         public void testEROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
103                 final List<ExplicitRouteSubobject> objsToTest = new ArrayList<ExplicitRouteSubobject>();
104                 objsToTest.add(new EROType1LabelSubobject(0xFFFF51F2L, true, false));
105                 objsToTest.add(new EROType1LabelSubobject(0x12345648L, false, true));
106                 objsToTest.add(new EROGeneralizedLabelSubobject(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }, true, true));
107                 objsToTest.add(new EROWavebandSwitchingLabelSubobject(0x12345678L, 0x87654321L, 0xFFFFFFFFL, false, false));
108                 objsToTest.add(new EROProtectionType1Subobject(true, (byte) 0x05, true));
109                 objsToTest.add(new EROProtectionType2Subobject(true, false, true, true, (byte) 0x06, (byte) 0x3f, true, false, (byte) 0x00, false));
110                 objsToTest.add(new EROPathKeyWith32PCEIDSubobject(0x1235, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }, true));
111                 objsToTest.add(new EROPathKeyWith128PCEIDSubobject(0x5432, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
112                                 (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
113                                 (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }, true));
114                 objsToTest.add(new EROExplicitExclusionRouteSubobject(Arrays.asList((ExcludeRouteSubobject) new XROAsNumberSubobject(new AsNumber((long) 2588), true))));
115
116                 assertEquals(objsToTest, PCEPEROSubobjectParser.parse(PCEPEROSubobjectParser.put(objsToTest)));
117         }
118
119         @Test
120         public void testRROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
121                 final List<ReportedRouteSubobject> objsToTest = new ArrayList<ReportedRouteSubobject>();
122                 objsToTest.add(new RROIPAddressSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(this.ipv6bytes2), 0x16), true, false));
123                 objsToTest.add(new RROIPAddressSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(this.ipv4bytes1), 0x16), true, false));
124                 objsToTest.add(new RROType1LabelSubobject(0xFFFF51F2L, true));
125                 objsToTest.add(new RROType1LabelSubobject(0x12345648L, false));
126                 objsToTest.add(new RROGeneralizedLabelSubobject(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }, true));
127                 objsToTest.add(new RROWavebandSwitchingLabelSubobject(0x12345678L, 0x87654321L, 0xFFFFFFFFL, false));
128                 objsToTest.add(new RROProtectionType1Subobject(true, (byte) 0x05));
129                 objsToTest.add(new RROProtectionType2Subobject(true, false, true, true, (byte) 0x06, (byte) 0x3f, true, false, (byte) 0x00));
130                 objsToTest.add(new RROPathKeyWith32PCEIDSubobject(0x1235, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }));
131                 objsToTest.add(new RROPathKeyWith128PCEIDSubobject(0x5432, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
132                                 (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
133                                 (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }));
134                 objsToTest.add(new RROAttributesSubobject(new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
135                                 (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55,
136                                 (byte) 0xFF, (byte) 0xF1 }));
137
138                 assertEquals(objsToTest, PCEPRROSubobjectParser.parse(PCEPRROSubobjectParser.put(objsToTest)));
139         }
140
141         @Test
142         public void testXROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
143                 final List<ExcludeRouteSubobject> objsToTest = new ArrayList<ExcludeRouteSubobject>();
144                 objsToTest.add(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(this.ipv6bytes2), 0x16), true, XROSubobjectAttribute.INTERFACE));
145                 objsToTest.add(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(this.ipv4bytes1), 0x16), false, XROSubobjectAttribute.INTERFACE));
146                 objsToTest.add(new XROAsNumberSubobject(new AsNumber((long) 0x1234), true));
147                 objsToTest.add(new XROUnnumberedInterfaceSubobject(new IPv4Address(this.ipv4bytes1), new UnnumberedInterfaceIdentifier(0xFFFFFFFFL), true, XROSubobjectAttribute.SRLG));
148                 objsToTest.add(new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
149
150                 assertEquals(objsToTest, PCEPXROSubobjectParser.parse(PCEPXROSubobjectParser.put(objsToTest)));
151         }
152
153         @Test
154         public void testDifferentLengthExceptions() {
155                 final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data
156                 // for parsing subobjects
157
158                 try {
159                         EROAsNumberSubobjectParser.parse(bytes, true);
160                         fail("");
161                 } catch (final PCEPDeserializerException e) {
162                 }
163
164                 try {
165                         EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
166                         fail("");
167                 } catch (final PCEPDeserializerException e) {
168                 }
169
170                 try {
171                         EROIPv4PrefixSubobjectParser.parse(bytes, true);
172                         fail("");
173                 } catch (final PCEPDeserializerException e) {
174                 }
175
176                 try {
177                         EROIPv6PrefixSubobjectParser.parse(bytes, true);
178                         fail("");
179                 } catch (final PCEPDeserializerException e) {
180                 }
181         }
182
183         @Test
184         public void testNullExceptions() throws PCEPDeserializerException {
185                 final byte[] bytes = null; // not empty but not enought data for parsing
186                 // subobjects
187
188                 try {
189                         EROAsNumberSubobjectParser.parse(bytes, true);
190                         fail("");
191                 } catch (final IllegalArgumentException e) {
192                 }
193
194                 try {
195                         EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
196                         fail("");
197                 } catch (final IllegalArgumentException e) {
198                 }
199
200                 try {
201                         EROIPv4PrefixSubobjectParser.parse(bytes, true);
202                         fail("");
203                 } catch (final IllegalArgumentException e) {
204                 }
205
206                 try {
207                         EROIPv6PrefixSubobjectParser.parse(bytes, true);
208                         fail("");
209                 } catch (final IllegalArgumentException e) {
210                 }
211         }
212
213         @Test
214         public void testUnknownInstanceExceptions() {
215
216                 final ExplicitRouteSubobject instance = new ExplicitRouteSubobject() {
217                 };
218
219                 try {
220                         EROAsNumberSubobjectParser.put(instance);
221                         fail("");
222                 } catch (final IllegalArgumentException e) {
223                 }
224
225                 try {
226                         EROUnnumberedInterfaceSubobjectParser.put(instance);
227                         fail("");
228                 } catch (final IllegalArgumentException e) {
229                 }
230
231                 try {
232                         EROIPv4PrefixSubobjectParser.put(instance);
233                         fail("");
234                 } catch (final IllegalArgumentException e) {
235                 }
236
237                 try {
238                         final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
239                                         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
240                         EROIPv4PrefixSubobjectParser.put(new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr), 1), false));
241                         fail("");
242                 } catch (final IllegalArgumentException e) {
243                 }
244
245                 try {
246                         EROIPv6PrefixSubobjectParser.put(instance);
247                         fail("");
248                 } catch (final IllegalArgumentException e) {
249                 }
250
251                 try {
252                         final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
253                         EROIPv6PrefixSubobjectParser.put(new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(ipv4addr), 1), false));
254                         fail("");
255                 } catch (final IllegalArgumentException e) {
256                 }
257
258         }
259
260         @Test
261         public void testEmptyExceptions() throws PCEPDeserializerException {
262                 final byte[] bytes = {}; // not empty but not enought data for parsing
263                 // subobjects
264
265                 try {
266                         EROAsNumberSubobjectParser.parse(bytes, true);
267                         fail("");
268                 } catch (final IllegalArgumentException e) {
269                 }
270
271                 try {
272                         EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
273                         fail("");
274                 } catch (final IllegalArgumentException e) {
275                 }
276
277                 try {
278                         EROIPv4PrefixSubobjectParser.parse(bytes, true);
279                         fail("");
280                 } catch (final IllegalArgumentException e) {
281                 }
282
283                 try {
284                         EROIPv6PrefixSubobjectParser.parse(bytes, true);
285                         fail("");
286                 } catch (final IllegalArgumentException e) {
287                 }
288         }
289
290 }