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.List;
18 import org.junit.Test;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.concepts.IPv4Address;
22 import org.opendaylight.protocol.concepts.IPv6Address;
23 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
24 import org.opendaylight.protocol.pcep.PCEPOFCodes;
25 import org.opendaylight.protocol.pcep.PCEPTlv;
26 import org.opendaylight.protocol.pcep.concepts.IPv4ExtendedTunnelIdentifier;
27 import org.opendaylight.protocol.pcep.concepts.IPv6ExtendedTunnelIdentifier;
28 import org.opendaylight.protocol.pcep.concepts.LSPIdentifier;
29 import org.opendaylight.protocol.pcep.concepts.LSPSymbolicName;
30 import org.opendaylight.protocol.pcep.concepts.TunnelIdentifier;
31 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIPv4TlvParser;
32 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIPv6TlvParser;
33 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
34 import org.opendaylight.protocol.pcep.impl.tlv.PCEStatefulCapabilityTlvParser;
35 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecIPv4TlvParser;
36 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecIPv6TlvParser;
37 import org.opendaylight.protocol.pcep.tlv.IPv4LSPIdentifiersTlv;
38 import org.opendaylight.protocol.pcep.tlv.IPv6LSPIdentifiersTlv;
39 import org.opendaylight.protocol.pcep.tlv.LSPStateDBVersionTlv;
40 import org.opendaylight.protocol.pcep.tlv.LSPSymbolicNameTlv;
41 import org.opendaylight.protocol.pcep.tlv.LSPUpdateErrorTlv;
42 import org.opendaylight.protocol.pcep.tlv.NoPathVectorTlv;
43 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
44 import org.opendaylight.protocol.pcep.tlv.OFListTlv;
45 import org.opendaylight.protocol.pcep.tlv.OrderTlv;
46 import org.opendaylight.protocol.pcep.tlv.OverloadedDurationTlv;
47 import org.opendaylight.protocol.pcep.tlv.P2MPCapabilityTlv;
48 import org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv;
49 import org.opendaylight.protocol.pcep.tlv.RSVPErrorSpecTlv;
50 import org.opendaylight.protocol.pcep.tlv.ReqMissingTlv;
53 * Tests of PCEPTlvParser
55 public class PCEPTlvParserTest {
58 public void testDeserialization() throws PCEPDeserializerException, IOException {
59 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfTlvs.bin");
60 final List<PCEPTlv> tlvsToTest = PCEPTlvParser.parse(bytesFromFile);
62 assertEquals(17, tlvsToTest.size());
63 assertEquals(tlvsToTest.get(0), new PCEStatefulCapabilityTlv(false, false, true));
64 assertEquals(tlvsToTest.get(1), new LSPStateDBVersionTlv(0xFF00FFAAB2F5F2CFL));
65 assertEquals(tlvsToTest.get(2), new PCEStatefulCapabilityTlv(false, true, true));
66 assertEquals(tlvsToTest.get(3), new LSPStateDBVersionTlv(0xFFFFFFFFFFFFFFFFL));
67 assertEquals(tlvsToTest.get(4), new NoPathVectorTlv(true, true, true, false, true, true));
68 assertEquals(tlvsToTest.get(5), new OverloadedDurationTlv(0x7FFFFFFF));
69 assertEquals(tlvsToTest.get(6), new LSPSymbolicNameTlv(new LSPSymbolicName(new String("Med test of symbolic name").getBytes())));
70 final byte[] errorCode = { (byte) 0x25, (byte) 0x68, (byte) 0x95, (byte) 0x03 };
71 assertEquals(tlvsToTest.get(7), new LSPUpdateErrorTlv(errorCode));
72 final byte[] ipv4Address = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
73 final byte[] tunnelId1 = { (byte) 0x12, (byte) 0x34 };
74 final byte[] extendedTunnelID1 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
75 final byte[] lspId1 = { (byte) 0xFF, (byte) 0xFF };
76 assertEquals(tlvsToTest.get(8), new IPv4LSPIdentifiersTlv(new IPv4Address(ipv4Address),
77 new LSPIdentifier(lspId1), new TunnelIdentifier(tunnelId1), new IPv4ExtendedTunnelIdentifier(new IPv4Address(extendedTunnelID1))));
78 final byte[] ipv6Address = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12,
79 (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 };
80 final byte[] tunnelId2 = { (byte) 0xFF, (byte) 0xFF };
81 final byte[] extendedTunnelID2 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01,
82 (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 };
83 final byte[] lspId2 = { (byte) 0x12, (byte) 0x34 };
84 assertEquals(tlvsToTest.get(9), new IPv6LSPIdentifiersTlv(new IPv6Address(ipv6Address),
85 new LSPIdentifier(lspId2), new TunnelIdentifier(tunnelId2), new IPv6ExtendedTunnelIdentifier(new IPv6Address(extendedTunnelID2))));
86 assertEquals(tlvsToTest.get(10), new RSVPErrorSpecTlv<IPv4Address>(new IPv4Address(ipv4Address), false, true, 0x92, 0x1602));
87 assertEquals(tlvsToTest.get(11), new RSVPErrorSpecTlv<IPv6Address>(new IPv6Address(ipv6Address), true, false, 0xD5, 0xC5D9));
88 assertEquals(tlvsToTest.get(12), new ReqMissingTlv(0xF7823517L));
89 final byte[] valueBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
90 assertEquals(tlvsToTest.get(13), new NodeIdentifierTlv(valueBytes));
91 assertEquals(tlvsToTest.get(14), new OrderTlv(0xFFFFFFFFL, 0x00000001L));
92 assertEquals(tlvsToTest.get(15), new OFListTlv(new ArrayList<PCEPOFCodes>() {
93 private static final long serialVersionUID = 1L;
96 this.add(PCEPOFCodes.MCC);
97 this.add(PCEPOFCodes.MCP);
98 this.add(PCEPOFCodes.MLL);
101 assertEquals(tlvsToTest.get(16), new P2MPCapabilityTlv(2));
103 assertArrayEquals(bytesFromFile, PCEPTlvParser.put(tlvsToTest));
107 public void testDifferentLengthExceptions() {
108 final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data
109 // for parsing subobjects
112 LSPIdentifierIPv4TlvParser.parse(bytes);
114 } catch (final PCEPDeserializerException e) {
118 LSPIdentifierIPv6TlvParser.parse(bytes);
120 } catch (final PCEPDeserializerException e) {
124 PCEStatefulCapabilityTlvParser.deserializeValueField(bytes);
126 } catch (final PCEPDeserializerException e) {
130 RSVPErrorSpecIPv4TlvParser.parse(bytes);
132 } catch (final PCEPDeserializerException e) {
136 RSVPErrorSpecIPv6TlvParser.parse(bytes);
138 } catch (final PCEPDeserializerException e) {
142 OFListTlvParser.parse(bytes);
144 } catch (final PCEPDeserializerException e) {
149 public void testUnknownInstanceExceptions() {
151 LSPIdentifierIPv4TlvParser.put(null);
153 } catch (final IllegalArgumentException e) {
157 LSPIdentifierIPv6TlvParser.put(null);
159 } catch (final IllegalArgumentException e) {
163 PCEStatefulCapabilityTlvParser.serializeValueField(null);
165 } catch (final IllegalArgumentException e) {
169 RSVPErrorSpecIPv4TlvParser.put(null);
171 } catch (final IllegalArgumentException e) {
175 RSVPErrorSpecIPv6TlvParser.put(null);
177 } catch (final IllegalArgumentException e) {
181 OFListTlvParser.put(null);
183 } catch (final IllegalArgumentException e) {
189 public void testEmptyExceptions() throws PCEPDeserializerException {
190 final byte[] bytes = {}; // empty
193 LSPIdentifierIPv4TlvParser.parse(bytes);
195 } catch (final IllegalArgumentException e) {
199 LSPIdentifierIPv6TlvParser.parse(bytes);
201 } catch (final IllegalArgumentException e) {
205 PCEStatefulCapabilityTlvParser.deserializeValueField(bytes);
207 } catch (final IllegalArgumentException e) {
211 RSVPErrorSpecIPv4TlvParser.parse(bytes);
213 } catch (final IllegalArgumentException e) {
217 RSVPErrorSpecIPv6TlvParser.parse(bytes);
219 } catch (final IllegalArgumentException e) {
223 OFListTlvParser.parse(bytes);
225 } catch (final IllegalArgumentException e) {