2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
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;
63 * Tests for subobjects
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 };
71 final byte[] ipv4bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
72 final byte[] ipv4bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 };
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);
79 assertEquals(8, objsToTest.size());
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));
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));
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));
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));
97 assertArrayEquals(bytesFromFile, PCEPEROSubobjectParser.put(objsToTest));
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))));
116 assertEquals(objsToTest, PCEPEROSubobjectParser.parse(PCEPEROSubobjectParser.put(objsToTest)));
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 }));
138 assertEquals(objsToTest, PCEPRROSubobjectParser.parse(PCEPRROSubobjectParser.put(objsToTest)));
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));
150 assertEquals(objsToTest, PCEPXROSubobjectParser.parse(PCEPXROSubobjectParser.put(objsToTest)));
154 public void testDifferentLengthExceptions() {
155 final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data
156 // for parsing subobjects
159 EROAsNumberSubobjectParser.parse(bytes, true);
161 } catch (final PCEPDeserializerException e) {
165 EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
167 } catch (final PCEPDeserializerException e) {
171 EROIPv4PrefixSubobjectParser.parse(bytes, true);
173 } catch (final PCEPDeserializerException e) {
177 EROIPv6PrefixSubobjectParser.parse(bytes, true);
179 } catch (final PCEPDeserializerException e) {
184 public void testNullExceptions() throws PCEPDeserializerException {
185 final byte[] bytes = null; // not empty but not enought data for parsing
189 EROAsNumberSubobjectParser.parse(bytes, true);
191 } catch (final IllegalArgumentException e) {
195 EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
197 } catch (final IllegalArgumentException e) {
201 EROIPv4PrefixSubobjectParser.parse(bytes, true);
203 } catch (final IllegalArgumentException e) {
207 EROIPv6PrefixSubobjectParser.parse(bytes, true);
209 } catch (final IllegalArgumentException e) {
214 public void testUnknownInstanceExceptions() {
216 final ExplicitRouteSubobject instance = new ExplicitRouteSubobject() {
220 EROAsNumberSubobjectParser.put(instance);
222 } catch (final IllegalArgumentException e) {
226 EROUnnumberedInterfaceSubobjectParser.put(instance);
228 } catch (final IllegalArgumentException e) {
232 EROIPv4PrefixSubobjectParser.put(instance);
234 } catch (final IllegalArgumentException e) {
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));
242 } catch (final IllegalArgumentException e) {
246 EROIPv6PrefixSubobjectParser.put(instance);
248 } catch (final IllegalArgumentException e) {
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));
255 } catch (final IllegalArgumentException e) {
261 public void testEmptyExceptions() throws PCEPDeserializerException {
262 final byte[] bytes = {}; // not empty but not enought data for parsing
266 EROAsNumberSubobjectParser.parse(bytes, true);
268 } catch (final IllegalArgumentException e) {
272 EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
274 } catch (final IllegalArgumentException e) {
278 EROIPv4PrefixSubobjectParser.parse(bytes, true);
280 } catch (final IllegalArgumentException e) {
284 EROIPv6PrefixSubobjectParser.parse(bytes, true);
286 } catch (final IllegalArgumentException e) {