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;
18 import org.opendaylight.protocol.concepts.IPv4Address;
19 import org.opendaylight.protocol.concepts.IPv4Prefix;
20 import org.opendaylight.protocol.concepts.IPv6Address;
21 import org.opendaylight.protocol.concepts.IPv6Prefix;
22 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
23 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
24 import org.opendaylight.protocol.pcep.concepts.UnnumberedInterfaceIdentifier;
25 import org.opendaylight.protocol.pcep.impl.subobject.XROAsNumberSubobjectParser;
26 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv4PrefixSubobjectParser;
27 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv6PrefixSubobjectParser;
28 import org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser;
29 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
30 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
31 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
32 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
33 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
34 import org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject;
35 import org.opendaylight.protocol.util.ByteArray;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
38 public class PCEPXROSubobjectParserTest {
41 public void testSerDeser() throws PCEPDeserializerException, IOException {
42 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfXROSubobjects.bin");
43 final List<ExcludeRouteSubobject> objsToTest = PCEPXROSubobjectParser.parse(bytesFromFile);
45 assertEquals(5, objsToTest.size());
47 assertEquals(objsToTest.get(0), new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(new byte[] { (byte) 192,
48 (byte) 168, (byte) 0, (byte) 0 }), 16), true, XROSubobjectAttribute.NODE));
49 assertEquals(objsToTest.get(1), new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(new byte[] { (byte) 0x12,
50 (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90,
51 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0, (byte) 0 }), 112), true, XROSubobjectAttribute.INTERFACE));
52 assertEquals(objsToTest.get(2), new XROUnnumberedInterfaceSubobject(new IPv4Address(new byte[] { (byte) 0, (byte) 0, (byte) 0,
53 (byte) 0x20 }), new UnnumberedInterfaceIdentifier(0x1234L), false, XROSubobjectAttribute.SRLG));
54 assertEquals(objsToTest.get(3), new XROAsNumberSubobject(new AsNumber((long) 0x1234), false));
55 assertEquals(objsToTest.get(4), new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
57 assertArrayEquals(bytesFromFile, PCEPXROSubobjectParser.put(objsToTest));
62 public void testDifferentLengthExceptions() {
63 final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data for parsing subobjects
66 XROAsNumberSubobjectParser.parse(bytes, true);
68 } catch (final PCEPDeserializerException e) {
72 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
74 } catch (final PCEPDeserializerException e) {
78 XROIPv4PrefixSubobjectParser.parse(bytes, true);
80 } catch (final PCEPDeserializerException e) {
84 XROIPv6PrefixSubobjectParser.parse(bytes, true);
86 } catch (final PCEPDeserializerException e) {
91 public void testNullExceptions() throws PCEPDeserializerException {
92 final byte[] bytes = null; // not empty but not enought data for parsing subobjects
95 XROAsNumberSubobjectParser.parse(bytes, true);
97 } catch (final IllegalArgumentException e) {
101 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
103 } catch (final IllegalArgumentException e) {
107 XROIPv4PrefixSubobjectParser.parse(bytes, true);
109 } catch (final IllegalArgumentException e) {
113 XROIPv6PrefixSubobjectParser.parse(bytes, true);
115 } catch (final IllegalArgumentException e) {
120 public void testUnknownInstanceExceptions() {
122 final ExcludeRouteSubobject instance = new ExcludeRouteSubobject(true) {
126 XROAsNumberSubobjectParser.put(instance);
128 } catch (final IllegalArgumentException e) {
132 XROUnnumberedInterfaceSubobjectParser.put(instance);
134 } catch (final IllegalArgumentException e) {
138 XROIPv4PrefixSubobjectParser.put(instance);
140 } catch (final IllegalArgumentException e) {
144 final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
145 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
146 XROIPv4PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr), 1), false, XROSubobjectAttribute.INTERFACE));
148 } catch (final IllegalArgumentException e) {
152 XROIPv6PrefixSubobjectParser.put(instance);
154 } catch (final IllegalArgumentException e) {
158 final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
159 XROIPv6PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(ipv4addr), 1), false, XROSubobjectAttribute.INTERFACE));
161 } catch (final IllegalArgumentException e) {
167 public void testEmptyExceptions() throws PCEPDeserializerException {
168 final byte[] bytes = {}; // not empty but not enought data for parsing subobjects
171 XROAsNumberSubobjectParser.parse(bytes, true);
173 } catch (final IllegalArgumentException e) {
177 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
179 } catch (final IllegalArgumentException e) {
183 XROIPv4PrefixSubobjectParser.parse(bytes, true);
185 } catch (final IllegalArgumentException e) {
189 XROIPv6PrefixSubobjectParser.parse(bytes, true);
191 } catch (final IllegalArgumentException e) {