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.assertEquals;
11 import static org.junit.Assert.fail;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.concepts.IPv4Address;
20 import org.opendaylight.protocol.concepts.IPv4Prefix;
21 import org.opendaylight.protocol.concepts.IPv6Address;
22 import org.opendaylight.protocol.concepts.IPv6Prefix;
23 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
24 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
25 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
26 import org.opendaylight.protocol.pcep.impl.subobject.EROIpPrefixSubobjectParser;
27 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
28 import org.opendaylight.protocol.pcep.subobject.EROExplicitExclusionRouteSubobject;
29 import org.opendaylight.protocol.pcep.subobject.EROGeneralizedLabelSubobject;
30 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith128PCEIDSubobject;
31 import org.opendaylight.protocol.pcep.subobject.EROPathKeyWith32PCEIDSubobject;
32 import org.opendaylight.protocol.pcep.subobject.EROProtectionType1Subobject;
33 import org.opendaylight.protocol.pcep.subobject.EROProtectionType2Subobject;
34 import org.opendaylight.protocol.pcep.subobject.EROType1LabelSubobject;
35 import org.opendaylight.protocol.pcep.subobject.EROWavebandSwitchingLabelSubobject;
36 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
37 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
38 import org.opendaylight.protocol.pcep.subobject.RROAttributesSubobject;
39 import org.opendaylight.protocol.pcep.subobject.RROGeneralizedLabelSubobject;
40 import org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject;
41 import org.opendaylight.protocol.pcep.subobject.RROPathKeyWith128PCEIDSubobject;
42 import org.opendaylight.protocol.pcep.subobject.RROPathKeyWith32PCEIDSubobject;
43 import org.opendaylight.protocol.pcep.subobject.RROProtectionType1Subobject;
44 import org.opendaylight.protocol.pcep.subobject.RROProtectionType2Subobject;
45 import org.opendaylight.protocol.pcep.subobject.RROType1LabelSubobject;
46 import org.opendaylight.protocol.pcep.subobject.RROWavebandSwitchingLabelSubobject;
47 import org.opendaylight.protocol.pcep.subobject.ReportedRouteSubobject;
48 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
49 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
50 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
51 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
52 import org.opendaylight.protocol.util.ByteArray;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CSubobject;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.route.subobjects.subobject.type.AsNumberBuilder;
58 * Tests for subobjects
60 public class PCEPSubobjectParserTest {
61 final byte[] ipv6bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
62 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
63 final byte[] ipv6bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
64 (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
66 final byte[] ipv4bytes1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
67 final byte[] ipv4bytes2 = { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 };
69 final byte[] asnumber = { (byte) 0xA0, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64 };
72 public void testSerDeser() throws PCEPDeserializerException, IOException {
73 // final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfSubobjects.bin");
74 // final List<ExplicitRouteSubobject> objsToTest = PCEPEROSubobjectParser.parse(bytesFromFile);
76 // assertEquals(8, objsToTest.size());
78 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
79 final CSubobject s = parser.parseSubobject(ByteArray.cutBytes(this.asnumber, 2));
81 assertEquals(s, new AsNumberBuilder().setAsNumber(new AsNumber((long) 0x64)).build());
82 // assertEquals(objsToTest.get(1), new AsNumberBuilder().setAsNumber(new AsNumber(0x0010L)).build());
84 // assertEquals(objsToTest.get(2), new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new
85 // IPv4Address(this.ipv4bytes1), 0x20), true));
87 // assertEquals(objsToTest.get(3), new EROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new
88 // IPv4Address(this.ipv4bytes2), 0x15), false));
89 // assertEquals(objsToTest.get(4), new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new
90 // IPv6Address(this.ipv6bytes1), 0x80), true));
92 // assertEquals(objsToTest.get(5), new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new
93 // IPv6Address(this.ipv6bytes2), 0x16), false));
94 // final byte[] addr1 = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
95 // assertEquals(objsToTest.get(6),
96 // new EROUnnumberedInterfaceSubobject(new IPv4Address(addr1), new UnnumberedInterfaceIdentifier(0xFFFFFFFFL),
99 // final byte[] addr2 = { (byte) 0x01, (byte) 0x24, (byte) 0x56, (byte) 0x78 };
100 // assertEquals(objsToTest.get(7),
101 // new EROUnnumberedInterfaceSubobject(new IPv4Address(addr2), new UnnumberedInterfaceIdentifier(0x9ABCDEF0L),
104 // assertArrayEquals(bytesFromFile, PCEPEROSubobjectParser.put(objsToTest));
109 public void testEROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
110 final List<ExplicitRouteSubobject> objsToTest = new ArrayList<ExplicitRouteSubobject>();
111 objsToTest.add(new EROType1LabelSubobject(0xFFFF51F2L, true, false));
112 objsToTest.add(new EROType1LabelSubobject(0x12345648L, false, true));
113 objsToTest.add(new EROGeneralizedLabelSubobject(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }, true, true));
114 objsToTest.add(new EROWavebandSwitchingLabelSubobject(0x12345678L, 0x87654321L, 0xFFFFFFFFL, false, false));
115 objsToTest.add(new EROProtectionType1Subobject(true, (byte) 0x05, true));
116 objsToTest.add(new EROProtectionType2Subobject(true, false, true, true, (byte) 0x06, (byte) 0x3f, true, false, (byte) 0x00, false));
117 objsToTest.add(new EROPathKeyWith32PCEIDSubobject(0x1235, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }, true));
118 objsToTest.add(new EROPathKeyWith128PCEIDSubobject(0x5432, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
119 (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
120 (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }, true));
121 objsToTest.add(new EROExplicitExclusionRouteSubobject(Arrays.asList((ExcludeRouteSubobject) new XROAsNumberSubobject(new AsNumber((long) 2588), true))));
123 assertEquals(objsToTest, PCEPEROSubobjectParser.parse(PCEPEROSubobjectParser.put(objsToTest)));
127 public void testRROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
128 final List<ReportedRouteSubobject> objsToTest = new ArrayList<ReportedRouteSubobject>();
129 objsToTest.add(new RROIPAddressSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(this.ipv6bytes2), 0x16), true, false));
130 objsToTest.add(new RROIPAddressSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(this.ipv4bytes1), 0x16), true, false));
131 objsToTest.add(new RROType1LabelSubobject(0xFFFF51F2L, true));
132 objsToTest.add(new RROType1LabelSubobject(0x12345648L, false));
133 objsToTest.add(new RROGeneralizedLabelSubobject(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }, true));
134 objsToTest.add(new RROWavebandSwitchingLabelSubobject(0x12345678L, 0x87654321L, 0xFFFFFFFFL, false));
135 objsToTest.add(new RROProtectionType1Subobject(true, (byte) 0x05));
136 objsToTest.add(new RROProtectionType2Subobject(true, false, true, true, (byte) 0x06, (byte) 0x3f, true, false, (byte) 0x00));
137 objsToTest.add(new RROPathKeyWith32PCEIDSubobject(0x1235, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }));
138 objsToTest.add(new RROPathKeyWith128PCEIDSubobject(0x5432, new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1,
139 (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
140 (byte) 0x55, (byte) 0xFF, (byte) 0xF1 }));
141 objsToTest.add(new RROAttributesSubobject(new byte[] { (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00,
142 (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55, (byte) 0xFF, (byte) 0xF1, (byte) 0x00, (byte) 0x55,
143 (byte) 0xFF, (byte) 0xF1 }));
145 assertEquals(objsToTest, PCEPRROSubobjectParser.parse(PCEPRROSubobjectParser.put(objsToTest)));
149 public void testXROSubojectsSerDeserWithoutBin() throws PCEPDeserializerException {
150 final List<ExcludeRouteSubobject> objsToTest = new ArrayList<ExcludeRouteSubobject>();
151 objsToTest.add(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(this.ipv6bytes2), 0x16), true, XROSubobjectAttribute.INTERFACE));
152 objsToTest.add(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(this.ipv4bytes1), 0x16), false, XROSubobjectAttribute.INTERFACE));
153 objsToTest.add(new XROAsNumberSubobject(new AsNumber((long) 0x1234), true));
154 // objsToTest.add(new XROUnnumberedInterfaceSubobject(new IPv4Address(this.ipv4bytes1), new
155 // UnnumberedInterfaceIdentifier(0xFFFFFFFFL), true, XROSubobjectAttribute.SRLG));
156 objsToTest.add(new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
158 assertEquals(objsToTest, PCEPXROSubobjectParser.parse(PCEPXROSubobjectParser.put(objsToTest)));
163 // public void testDifferentLengthExceptions() {
164 // final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data
165 // // for parsing subobjects
168 // EROAsNumberSubobjectParser.parse(bytes, true);
170 // } catch (final PCEPDeserializerException e) {
174 // EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
176 // } catch (final PCEPDeserializerException e) {
180 // EROIpPrefixSubobjectParser.parse(bytes, true);
182 // } catch (final PCEPDeserializerException e) {
187 public void testNullExceptions() throws PCEPDeserializerException {
188 final byte[] bytes = null; // not empty but not enought data for parsing
192 new EROAsNumberSubobjectParser().parseSubobject(bytes);
194 } catch (final IllegalArgumentException e) {
198 EROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
200 } catch (final IllegalArgumentException e) {
204 new EROIpPrefixSubobjectParser().parseSubobject(bytes);
206 } catch (final IllegalArgumentException e) {
211 public void testUnknownInstanceExceptions() {
213 final ExplicitRouteSubobject instance = new ExplicitRouteSubobject() {
217 // new EROAsNumberSubobjectParser().serializeSubobject(instance);
219 // } catch (final IllegalArgumentException e) {
223 EROUnnumberedInterfaceSubobjectParser.put(instance);
225 } catch (final IllegalArgumentException e) {
229 // EROIpPrefixSubobjectParser.put(instance);
231 // } catch (final IllegalArgumentException e) {
235 // final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
237 // (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
239 // EROIpPrefixSubobjectParser.put(new EROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr),
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
254 // IPv4Address(ipv4addr), 1), false));
256 // } catch (final IllegalArgumentException e) {