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.List;
17 import org.junit.Test;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.concepts.ASNumber;
21 import org.opendaylight.protocol.concepts.IPv4Address;
22 import org.opendaylight.protocol.concepts.IPv4Prefix;
23 import org.opendaylight.protocol.concepts.IPv6Address;
24 import org.opendaylight.protocol.concepts.IPv6Prefix;
25 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
26 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
27 import org.opendaylight.protocol.pcep.concepts.UnnumberedInterfaceIdentifier;
28 import org.opendaylight.protocol.pcep.impl.subobject.XROAsNumberSubobjectParser;
29 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv4PrefixSubobjectParser;
30 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv6PrefixSubobjectParser;
31 import org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser;
32 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
33 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
34 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
35 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
36 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
37 import org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject;
39 public class PCEPXROSubobjectParserTest {
42 public void testSerDeser() throws PCEPDeserializerException, IOException {
43 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfXROSubobjects.bin");
44 final List<ExcludeRouteSubobject> objsToTest = PCEPXROSubobjectParser.parse(bytesFromFile);
46 assertEquals(5, objsToTest.size());
50 new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(
51 new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 0 }), 16), true, XROSubobjectAttribute.NODE));
54 new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(
55 new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
56 (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0, (byte) 0 }), 112), true,
57 XROSubobjectAttribute.INTERFACE));
60 new XROUnnumberedInterfaceSubobject(new IPv4Address(
61 new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0x20 }), new UnnumberedInterfaceIdentifier(0x1234L), false,
62 XROSubobjectAttribute.SRLG));
63 assertEquals(objsToTest.get(3), new XROAsNumberSubobject(new ASNumber(0x1234), false));
64 assertEquals(objsToTest.get(4), new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
66 assertArrayEquals(bytesFromFile, PCEPXROSubobjectParser.put(objsToTest));
71 public void testDifferentLengthExceptions() {
72 final byte[] bytes = { (byte) 0x00 }; //not empty but not enought data for parsing subobjects
75 XROAsNumberSubobjectParser.parse(bytes, true);
77 } catch (final PCEPDeserializerException e) {
81 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
83 } catch (final PCEPDeserializerException e) {
87 XROIPv4PrefixSubobjectParser.parse(bytes, true);
89 } catch (final PCEPDeserializerException e) {
93 XROIPv6PrefixSubobjectParser.parse(bytes, true);
95 } catch (final PCEPDeserializerException e) {
100 public void testNullExceptions() throws PCEPDeserializerException {
101 final byte[] bytes = null; //not empty but not enought data for parsing subobjects
104 XROAsNumberSubobjectParser.parse(bytes, true);
106 } catch (final IllegalArgumentException e) {
110 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
112 } catch (final IllegalArgumentException e) {
116 XROIPv4PrefixSubobjectParser.parse(bytes, true);
118 } catch (final IllegalArgumentException e) {
122 XROIPv6PrefixSubobjectParser.parse(bytes, true);
124 } catch (final IllegalArgumentException e) {
129 public void testUnknownInstanceExceptions() {
131 final ExcludeRouteSubobject instance = new ExcludeRouteSubobject(true) {
135 XROAsNumberSubobjectParser.put(instance);
137 } catch (final IllegalArgumentException e) {
141 XROUnnumberedInterfaceSubobjectParser.put(instance);
143 } catch (final IllegalArgumentException e) {
147 XROIPv4PrefixSubobjectParser.put(instance);
149 } catch (final IllegalArgumentException e) {
153 final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
154 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
155 XROIPv4PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr), 1), false, XROSubobjectAttribute.INTERFACE));
157 } catch (final IllegalArgumentException e) {
161 XROIPv6PrefixSubobjectParser.put(instance);
163 } catch (final IllegalArgumentException e) {
167 final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
168 XROIPv6PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(ipv4addr), 1), false, XROSubobjectAttribute.INTERFACE));
170 } catch (final IllegalArgumentException e) {
176 public void testEmptyExceptions() throws PCEPDeserializerException {
177 final byte[] bytes = {}; //not empty but not enought data for parsing subobjects
180 XROAsNumberSubobjectParser.parse(bytes, true);
182 } catch (final IllegalArgumentException e) {
186 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
188 } catch (final IllegalArgumentException e) {
192 XROIPv4PrefixSubobjectParser.parse(bytes, true);
194 } catch (final IllegalArgumentException e) {
198 XROIPv6PrefixSubobjectParser.parse(bytes, true);
200 } catch (final IllegalArgumentException e) {