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.List;
16 import org.junit.Test;
17 import org.opendaylight.protocol.concepts.IPv4Address;
18 import org.opendaylight.protocol.concepts.IPv4Prefix;
19 import org.opendaylight.protocol.concepts.IPv6Address;
20 import org.opendaylight.protocol.concepts.IPv6Prefix;
21 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
22 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.impl.subobject.XROAsNumberSubobjectParser;
24 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv4PrefixSubobjectParser;
25 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv6PrefixSubobjectParser;
26 import org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser;
27 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
28 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
29 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
30 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
31 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
32 import org.opendaylight.protocol.util.ByteArray;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
35 public class PCEPXROSubobjectParserTest {
38 public void testSerDeser() throws PCEPDeserializerException, IOException {
39 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfXROSubobjects.bin");
40 final List<ExcludeRouteSubobject> objsToTest = PCEPXROSubobjectParser.parse(bytesFromFile);
42 assertEquals(5, objsToTest.size());
44 assertEquals(objsToTest.get(0), new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(new byte[] { (byte) 192,
45 (byte) 168, (byte) 0, (byte) 0 }), 16), true, XROSubobjectAttribute.NODE));
46 assertEquals(objsToTest.get(1), new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(new byte[] { (byte) 0x12,
47 (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90,
48 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0, (byte) 0 }), 112), true, XROSubobjectAttribute.INTERFACE));
49 // assertEquals(objsToTest.get(2), new XROUnnumberedInterfaceSubobject(new IPv4Address(new byte[] { (byte) 0,
50 // (byte) 0, (byte) 0,
51 // (byte) 0x20 }), new UnnumberedInterfaceIdentifier(0x1234L), false, XROSubobjectAttribute.SRLG));
52 assertEquals(objsToTest.get(3), new XROAsNumberSubobject(new AsNumber((long) 0x1234), false));
53 assertEquals(objsToTest.get(4), new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
55 // assertArrayEquals(bytesFromFile, PCEPXROSubobjectParser.put(objsToTest));
60 public void testDifferentLengthExceptions() {
61 final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data for parsing subobjects
64 XROAsNumberSubobjectParser.parse(bytes, true);
66 } catch (final PCEPDeserializerException e) {
70 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
72 } catch (final PCEPDeserializerException e) {
76 XROIPv4PrefixSubobjectParser.parse(bytes, true);
78 } catch (final PCEPDeserializerException e) {
82 XROIPv6PrefixSubobjectParser.parse(bytes, true);
84 } catch (final PCEPDeserializerException e) {
89 public void testNullExceptions() throws PCEPDeserializerException {
90 final byte[] bytes = null; // not empty but not enought data for parsing subobjects
93 XROAsNumberSubobjectParser.parse(bytes, true);
95 } catch (final IllegalArgumentException e) {
99 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
101 } catch (final IllegalArgumentException e) {
105 XROIPv4PrefixSubobjectParser.parse(bytes, true);
107 } catch (final IllegalArgumentException e) {
111 XROIPv6PrefixSubobjectParser.parse(bytes, true);
113 } catch (final IllegalArgumentException e) {
118 public void testUnknownInstanceExceptions() {
120 final ExcludeRouteSubobject instance = new ExcludeRouteSubobject(true) {
124 XROAsNumberSubobjectParser.put(instance);
126 } catch (final IllegalArgumentException e) {
130 XROUnnumberedInterfaceSubobjectParser.put(instance);
132 } catch (final IllegalArgumentException e) {
136 XROIPv4PrefixSubobjectParser.put(instance);
138 } catch (final IllegalArgumentException e) {
142 final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
143 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
144 XROIPv4PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr), 1), false, XROSubobjectAttribute.INTERFACE));
146 } catch (final IllegalArgumentException e) {
150 XROIPv6PrefixSubobjectParser.put(instance);
152 } catch (final IllegalArgumentException e) {
156 final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
157 XROIPv6PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(ipv4addr), 1), false, XROSubobjectAttribute.INTERFACE));
159 } catch (final IllegalArgumentException e) {
165 public void testEmptyExceptions() throws PCEPDeserializerException {
166 final byte[] bytes = {}; // not empty but not enought data for parsing subobjects
169 XROAsNumberSubobjectParser.parse(bytes, true);
171 } catch (final IllegalArgumentException e) {
175 XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
177 } catch (final IllegalArgumentException e) {
181 XROIPv4PrefixSubobjectParser.parse(bytes, true);
183 } catch (final IllegalArgumentException e) {
187 XROIPv6PrefixSubobjectParser.parse(bytes, true);
189 } catch (final IllegalArgumentException e) {