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;
13 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.protocol.concepts.Ipv4Util;
17 import org.opendaylight.protocol.concepts.Ipv6Util;
18 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIpv4TlvParser;
19 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIpv6TlvParser;
20 import org.opendaylight.protocol.pcep.impl.tlv.LspSymbolicNameTlvParser;
21 import org.opendaylight.protocol.pcep.impl.tlv.LspUpdateErrorTlvParser;
22 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
23 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
24 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
25 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
26 import org.opendaylight.protocol.pcep.impl.tlv.PCEStatefulCapabilityTlvParser;
27 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecTlvParser;
28 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
29 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCodeBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv4CaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv6CaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv4._case.Ipv4Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv6._case.Ipv6Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp._case.RsvpErrorBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user._case.UserErrorBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVectorBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv4ExtendedTunnelId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
67 import com.google.common.collect.Lists;
69 public class PCEPTlvParserTest {
71 private static final byte[] statefulBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 };
72 private static final byte[] noPathVectorBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa7 };
73 private static final byte[] overloadedBytes = { (byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
74 private static final byte[] symbolicNameBytes = { (byte) 0x4d, (byte) 0x65, (byte) 0x64, (byte) 0x20, (byte) 0x74, (byte) 0x65,
75 (byte) 0x73, (byte) 0x74, (byte) 0x20, (byte) 0x6f, (byte) 0x66, (byte) 0x20, (byte) 0x73, (byte) 0x79, (byte) 0x6d,
76 (byte) 0x62, (byte) 0x6f, (byte) 0x6c, (byte) 0x69, (byte) 0x63, (byte) 0x20, (byte) 0x6e, (byte) 0x61, (byte) 0x6d,
78 private static final byte[] lspUpdateErrorBytes = { (byte) 0x25, (byte) 0x68, (byte) 0x95, (byte) 0x03 };
79 private static final byte[] lspIdentifiers4Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xFF, (byte) 0xFF,
80 (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56,
82 private static final byte[] lspIdentifiers6Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC,
83 (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
84 (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0xFF, (byte) 0xFF, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
85 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x01,
86 (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC,
87 (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
89 private static final byte[] rsvpErrorBytes = { (byte) 0x06, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
90 (byte) 0x02, (byte) 0x92, (byte) 0x16, (byte) 0x02 };
91 private static final byte[] rsvpError6Bytes = { (byte) 0x06, (byte) 0x02, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
92 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
93 (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x02, (byte) 0xd5, (byte) 0xc5, (byte) 0xd9 };
94 private static final byte[] userErrorBytes = { (byte) 0xc2, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x30, (byte) 0x39,
95 (byte) 0x05, (byte) 0x09, (byte) 0x00, (byte) 0x26, (byte) 0x75, (byte) 0x73, (byte) 0x65, (byte) 0x72, (byte) 0x20,
96 (byte) 0x64, (byte) 0x65, (byte) 0x73, (byte) 0x63 };
97 private static final byte[] reqMissingBytes = { (byte) 0xF7, (byte) 0x82, (byte) 0x35, (byte) 0x17 };
98 private static final byte[] orderBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00,
100 private static final byte[] ofListBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
103 public void testStatefulTlv() throws PCEPDeserializerException {
104 final PCEStatefulCapabilityTlvParser parser = new PCEStatefulCapabilityTlvParser();
105 final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE).build();
106 assertEquals(tlv, parser.parseTlv(statefulBytes));
107 assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
111 public void testNoPathVectorTlv() throws PCEPDeserializerException {
112 final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
113 final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
114 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true)).build();
115 assertEquals(tlv, parser.parseTlv(noPathVectorBytes));
116 assertArrayEquals(noPathVectorBytes, parser.serializeTlv(tlv));
120 public void testOverloadedDurationTlv() throws PCEPDeserializerException {
121 final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
122 final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
123 assertEquals(tlv, parser.parseTlv(overloadedBytes));
124 assertArrayEquals(overloadedBytes, parser.serializeTlv(tlv));
128 public void testSymbolicNameTlv() throws PCEPDeserializerException {
129 final LspSymbolicNameTlvParser parser = new LspSymbolicNameTlvParser();
130 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
131 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName("Med test of symbolic name".getBytes())).build();
132 assertEquals(tlv, parser.parseTlv(symbolicNameBytes));
133 assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
137 public void testLspErrorCodeTlv() throws PCEPDeserializerException {
138 final LspUpdateErrorTlvParser parser = new LspUpdateErrorTlvParser();
139 final LspErrorCode tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
140 assertEquals(tlv, parser.parseTlv(lspUpdateErrorBytes));
141 assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
145 public void testLspIdentifiers4Tlv() throws PCEPDeserializerException {
146 final LSPIdentifierIpv4TlvParser parser = new LSPIdentifierIpv4TlvParser();
147 final Ipv4Builder afi = new Ipv4Builder();
148 afi.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
149 afi.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
151 afi.setIpv4TunnelEndpointAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
152 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv4CaseBuilder().setIpv4(afi.build()).build()).setLspId(
153 new LspId(65535L)).setTunnelId(new TunnelId(4660)).build();
154 assertEquals(tlv, parser.parseTlv(lspIdentifiers4Bytes));
155 assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
159 public void testLspIdentifiers6Tlv() throws PCEPDeserializerException {
160 final LSPIdentifierIpv6TlvParser parser = new LSPIdentifierIpv6TlvParser();
161 final Ipv6Builder afi = new Ipv6Builder();
162 afi.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
163 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
164 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
165 afi.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
166 (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
167 (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 })));
168 afi.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
169 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
170 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
171 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(new Ipv6CaseBuilder().setIpv6(afi.build()).build()).setLspId(
172 new LspId(4660L)).setTunnelId(new TunnelId(65535)).build();
173 assertEquals(tlv, parser.parseTlv(lspIdentifiers6Bytes));
174 assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
178 public void testRSVPError4SpecTlv() throws PCEPDeserializerException {
179 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
180 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
181 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 })));
182 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
183 builder.setCode((short) 146);
184 builder.setValue(5634);
185 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
186 assertEquals(tlv, parser.parseTlv(rsvpErrorBytes));
187 assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
191 public void testRSVPError6SpecTlv() throws PCEPDeserializerException {
192 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
193 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
194 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
195 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
196 (byte) 0xbc, (byte) 0xde, (byte) 0xf0 })));
197 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
198 builder.setCode((short) 213);
199 builder.setValue(50649);
200 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpCaseBuilder().setRsvpError(builder.build()).build()).build();
201 assertEquals(tlv, parser.parseTlv(rsvpError6Bytes));
202 assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
206 public void testUserErrorSpecTlv() throws PCEPDeserializerException {
207 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
208 final UserErrorBuilder builder = new UserErrorBuilder();
209 builder.setEnterprise(new EnterpriseNumber(12345L));
210 builder.setSubOrg((short) 5);
211 builder.setValue(38);
212 builder.setDescription("user desc");
213 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new UserCaseBuilder().setUserError(builder.build()).build()).build();
214 assertEquals(tlv, parser.parseTlv(userErrorBytes));
215 assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
219 public void testReqMissingTlv() throws PCEPDeserializerException {
220 final ReqMissingTlvParser parser = new ReqMissingTlvParser();
221 final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
222 assertEquals(tlv, parser.parseTlv(reqMissingBytes));
223 assertArrayEquals(reqMissingBytes, parser.serializeTlv(tlv));
227 public void testOrderTlv() throws PCEPDeserializerException {
228 final OrderTlvParser parser = new OrderTlvParser();
229 final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
230 assertEquals(tlv, parser.parseTlv(orderBytes));
231 assertArrayEquals(orderBytes, parser.serializeTlv(tlv));
235 public void testOFListTlv() throws PCEPDeserializerException {
236 final OFListTlvParser parser = new OFListTlvParser();
237 final List<OfId> ids = Lists.newArrayList();
238 ids.add(new OfId(0x1234));
239 ids.add(new OfId(0x5678));
240 final OfList tlv = new OfListBuilder().setCodes(ids).build();
241 assertEquals(tlv, parser.parseTlv(ofListBytes));
242 assertArrayEquals(ofListBytes, parser.serializeTlv(tlv));