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.ietf;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
15 import java.math.BigInteger;
17 import org.junit.Test;
18 import org.opendaylight.protocol.concepts.Ipv4Util;
19 import org.opendaylight.protocol.concepts.Ipv6Util;
20 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02LspDbVersionTlvParser;
21 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02LspSymbolicNameTlvParser;
22 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02NodeIdentifierTlvParser;
23 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02RSVPErrorSpecTlvParser;
24 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02StatefulCapabilityTlvParser;
25 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifierBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.rsvp.error.spec.RsvpErrorBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.StatefulBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathNameBuilder;
40 public class PCEPTlvParserTest {
42 private static final byte[] statefulBytes = { 0x00, 0x10, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01 };
43 private static final byte[] symbolicNameBytes = { 0x00, 0x11, 0x00, 0x19, 0x4d, 0x65, 0x64, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6f,
44 0x66, 0x20, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x69, 0x63, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x00, 0x00 };
45 private static final byte[] nodeIdentifierBytes = { 0x00, 0x18, 0x00, 0x19, 0x4d, 0x65, 0x64, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6f,
46 0x66, 0x20, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x69, 0x63, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x00, 0x00 };
47 private static final byte[] rsvpErrorBytes = { 0x00, 0x15, 0x00, 0x08, 0x12, 0x34, 0x56, 0x78, 0x02, (byte) 0x92, 0x16, 0x02 };
48 private static final byte[] rsvpError6Bytes = { 0x00, 0x15, 0x00, 0x14, 0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde,
49 (byte) 0xf0, 0x12, 0x34, 0x56, 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, 0x02, (byte) 0xd5, (byte) 0xc5,
51 private static final byte[] lspDbBytes = { 0x00, 0x17, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xb4 };
54 public void testStatefulTlv() throws PCEPDeserializerException {
55 final Stateful02StatefulCapabilityTlvParser parser = new Stateful02StatefulCapabilityTlvParser();
56 final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).setIncludeDbVersion(false).build();
57 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(statefulBytes, 4))));
58 final ByteBuf buff = Unpooled.buffer();
59 parser.serializeTlv(tlv, buff);
60 assertArrayEquals(statefulBytes, ByteArray.getAllBytes(buff));
64 public void testSymbolicNameTlv() throws PCEPDeserializerException {
65 final Stateful02LspSymbolicNameTlvParser parser = new Stateful02LspSymbolicNameTlvParser();
66 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
67 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.SymbolicPathName("Med test of symbolic name".getBytes())).build();
68 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(symbolicNameBytes, 4, 25))));
69 final ByteBuf buff = Unpooled.buffer();
70 parser.serializeTlv(tlv, buff);
71 assertArrayEquals(symbolicNameBytes, ByteArray.getAllBytes(buff));
75 public void testNodeIdentifier() throws PCEPDeserializerException {
76 final Stateful02NodeIdentifierTlvParser parser = new Stateful02NodeIdentifierTlvParser();
77 NodeIdentifier tlv = new NodeIdentifierBuilder().setNodeId(
78 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.NodeIdentifier(ByteArray.subByte(
79 nodeIdentifierBytes, 4, 25))).build();
80 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(nodeIdentifierBytes, 4, 25))));
81 final ByteBuf buff = Unpooled.buffer();
82 parser.serializeTlv(tlv, buff);
83 assertArrayEquals(nodeIdentifierBytes, ByteArray.getAllBytes(buff));
87 public void testRSVPError4SpecTlv() throws PCEPDeserializerException {
88 final Stateful02RSVPErrorSpecTlvParser parser = new Stateful02RSVPErrorSpecTlvParser();
89 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
90 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 })));
91 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
92 builder.setCode((short) 146);
93 builder.setValue(5634);
94 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
95 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(rsvpErrorBytes, 4, 8))));
96 final ByteBuf buff = Unpooled.buffer();
97 parser.serializeTlv(tlv, buff);
98 assertArrayEquals(rsvpErrorBytes, ByteArray.getAllBytes(buff));
102 public void testRSVPError6SpecTlv() throws PCEPDeserializerException {
103 final Stateful02RSVPErrorSpecTlvParser parser = new Stateful02RSVPErrorSpecTlvParser();
104 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
105 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
106 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
107 (byte) 0xbc, (byte) 0xde, (byte) 0xf0 })));
108 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
109 builder.setCode((short) 213);
110 builder.setValue(50649);
111 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setRsvpError(builder.build()).build();
112 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.subByte(rsvpError6Bytes, 4, 20))));
113 final ByteBuf buff = Unpooled.buffer();
114 parser.serializeTlv(tlv, buff);
115 assertArrayEquals(rsvpError6Bytes, ByteArray.getAllBytes(buff));
119 public void testLspDbVersionTlv() throws PCEPDeserializerException {
120 final Stateful02LspDbVersionTlvParser parser = new Stateful02LspDbVersionTlvParser();
121 final LspDbVersion tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(180L)).build();
122 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(lspDbBytes, 4))));
123 final ByteBuf buff = Unpooled.buffer();
124 parser.serializeTlv(tlv, buff);
125 assertArrayEquals(lspDbBytes, ByteArray.getAllBytes(buff));