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.math.BigInteger;
14 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.protocol.concepts.Ipv4Util;
18 import org.opendaylight.protocol.concepts.Ipv6Util;
19 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierTlvParser;
21 import org.opendaylight.protocol.pcep.impl.tlv.LspDbVersionTlvParser;
22 import org.opendaylight.protocol.pcep.impl.tlv.LspSymbolicNameTlvParser;
23 import org.opendaylight.protocol.pcep.impl.tlv.LspUpdateErrorTlvParser;
24 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
25 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
26 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
27 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
28 import org.opendaylight.protocol.pcep.impl.tlv.PCEStatefulCapabilityTlvParser;
29 import org.opendaylight.protocol.pcep.impl.tlv.PredundancyGroupTlvParser;
30 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecTlvParser;
31 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspDbVersionTlv;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspErrorCodeTlv;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspIdentifiersTlv;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfListTlv;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OrderTlv;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PredundancyGroupIdTlv;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RsvpErrorSpecTlv;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv.Flags;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SymbolicPathName;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SymbolicPathNameTlv;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.address.family.Ipv4Builder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.address.family.Ipv6Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.tlvs.LspErrorCodeBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.tlvs.LspIdentifiersBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.tlvs.RsvpErrorSpecBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.tlvs.SymbolicPathNameBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.LspDbVersionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.OfListBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.PredundancyGroupIdBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.tlvs.StatefulBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure.no.path.tlvs.NoPathVectorBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.tlvs.OrderBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.error.type.RsvpBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.error.type.UserBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.error.type.rsvp.RsvpErrorBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.error.type.user.UserErrorBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv4ExtendedTunnelId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
73 import com.google.common.collect.Lists;
75 public class PCEPTlvParserTest {
77 private static final byte[] statefulBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 };
78 private static final byte[] DbVersionBytes = { (byte) 0xff, (byte) 0x00, (byte) 0xff, (byte) 0xaa, (byte) 0xb2, (byte) 0xf5,
79 (byte) 0xf2, (byte) 0xcf };
80 private static final byte[] noPathVectorBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa7 };
81 private static final byte[] overloadedBytes = { (byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
82 private static final byte[] symbolicNameBytes = { (byte) 0x4d, (byte) 0x65, (byte) 0x64, (byte) 0x20, (byte) 0x74, (byte) 0x65,
83 (byte) 0x73, (byte) 0x74, (byte) 0x20, (byte) 0x6f, (byte) 0x66, (byte) 0x20, (byte) 0x73, (byte) 0x79, (byte) 0x6d,
84 (byte) 0x62, (byte) 0x6f, (byte) 0x6c, (byte) 0x69, (byte) 0x63, (byte) 0x20, (byte) 0x6e, (byte) 0x61, (byte) 0x6d,
86 private static final byte[] lspUpdateErrorBytes = { (byte) 0x25, (byte) 0x68, (byte) 0x95, (byte) 0x03 };
87 private static final byte[] lspIdentifiers4Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xFF, (byte) 0xFF,
88 (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
89 private static final byte[] lspIdentifiers6Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC,
90 (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
91 (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0xFF, (byte) 0xFF, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
92 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x01,
93 (byte) 0x23, (byte) 0x45, (byte) 0x67 };
94 private static final byte[] rsvpErrorBytes = { (byte) 0x06, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
95 (byte) 0x02, (byte) 0x92, (byte) 0x16, (byte) 0x02 };
96 private static final byte[] rsvpError6Bytes = { (byte) 0x06, (byte) 0x02, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
97 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
98 (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x02, (byte) 0xd5, (byte) 0xc5, (byte) 0xd9 };
99 private static final byte[] userErrorBytes = { (byte) 0xc2, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x30, (byte) 0x39,
100 (byte) 0x05, (byte) 0x09, (byte) 0x00, (byte) 0x26, (byte) 0x75, (byte) 0x73, (byte) 0x65, (byte) 0x72, (byte) 0x20,
101 (byte) 0x64, (byte) 0x65, (byte) 0x73, (byte) 0x63 };
102 private static final byte[] reqMissingBytes = { (byte) 0xF7, (byte) 0x82, (byte) 0x35, (byte) 0x17 };
103 private static final byte[] orderBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00,
105 private static final byte[] ofListBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
106 private static final byte[] predundancyBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
109 public void testStatefulTlv() throws PCEPDeserializerException {
110 final PCEStatefulCapabilityTlvParser parser = new PCEStatefulCapabilityTlvParser();
111 final StatefulCapabilityTlv tlv = new StatefulBuilder().setFlags(new Flags(true, false, false)).build();
112 assertEquals(tlv, parser.parseTlv(statefulBytes));
113 assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
117 public void testStateDbVersionTlv() throws PCEPDeserializerException {
118 final LspDbVersionTlvParser parser = new LspDbVersionTlvParser();
119 final LspDbVersionTlv tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(0xFF00FFAAB2F5F2CFL)).build();
120 assertEquals(tlv, parser.parseTlv(DbVersionBytes));
121 assertArrayEquals(DbVersionBytes, parser.serializeTlv(tlv));
125 public void testNoPathVectorTlv() throws PCEPDeserializerException {
126 final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
127 final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
128 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();
129 assertEquals(tlv, parser.parseTlv(noPathVectorBytes));
130 assertArrayEquals(noPathVectorBytes, parser.serializeTlv(tlv));
134 public void testOverloadedDurationTlv() throws PCEPDeserializerException {
135 final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
136 final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
137 assertEquals(tlv, parser.parseTlv(overloadedBytes));
138 assertArrayEquals(overloadedBytes, parser.serializeTlv(tlv));
142 public void testSymbolicNameTlv() throws PCEPDeserializerException {
143 final LspSymbolicNameTlvParser parser = new LspSymbolicNameTlvParser();
144 final SymbolicPathNameTlv tlv = new SymbolicPathNameBuilder().setPathName(
145 new SymbolicPathName("Med test of symbolic name".getBytes())).build();
146 assertEquals(tlv, parser.parseTlv(symbolicNameBytes));
147 assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
151 public void testLspErrorCodeTlv() throws PCEPDeserializerException {
152 final LspUpdateErrorTlvParser parser = new LspUpdateErrorTlvParser();
153 final LspErrorCodeTlv tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
154 assertEquals(tlv, parser.parseTlv(lspUpdateErrorBytes));
155 assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
159 public void testLspIdentifiers4Tlv() throws PCEPDeserializerException {
160 final LSPIdentifierTlvParser parser = new LSPIdentifierTlvParser();
161 final Ipv4Builder afi = new Ipv4Builder();
162 afi.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
163 afi.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
165 final LspIdentifiersTlv tlv = new LspIdentifiersBuilder().setAddressFamily(afi.build()).setLspId(new LspId(65535L)).setTunnelId(
166 new TunnelId(4660)).build();
167 assertEquals(tlv, parser.parseTlv(lspIdentifiers4Bytes));
168 assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
172 public void testLspIdentifiers6Tlv() throws PCEPDeserializerException {
173 final LSPIdentifierTlvParser parser = new LSPIdentifierTlvParser();
174 final Ipv6Builder afi = new Ipv6Builder();
175 afi.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
176 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
177 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
178 afi.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
179 (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
180 (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 })));
181 final LspIdentifiersTlv tlv = new LspIdentifiersBuilder().setAddressFamily(afi.build()).setLspId(new LspId(4660L)).setTunnelId(
182 new TunnelId(65535)).build();
183 assertEquals(tlv, parser.parseTlv(lspIdentifiers6Bytes));
184 assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
188 public void testRSVPError4SpecTlv() throws PCEPDeserializerException {
189 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
190 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
191 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 })));
192 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
193 builder.setCode((short) 146);
194 builder.setValue(5634);
195 final RsvpErrorSpecTlv tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpBuilder().setRsvpError(builder.build()).build()).build();
196 assertEquals(tlv, parser.parseTlv(rsvpErrorBytes));
197 assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
201 public void testRSVPError6SpecTlv() throws PCEPDeserializerException {
202 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
203 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
204 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
205 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
206 (byte) 0xbc, (byte) 0xde, (byte) 0xf0 })));
207 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
208 builder.setCode((short) 213);
209 builder.setValue(50649);
210 final RsvpErrorSpecTlv tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpBuilder().setRsvpError(builder.build()).build()).build();
211 assertEquals(tlv, parser.parseTlv(rsvpError6Bytes));
212 assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
216 public void testUserErrorSpecTlv() throws PCEPDeserializerException {
217 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
218 final UserErrorBuilder builder = new UserErrorBuilder();
219 builder.setEnterprise(new EnterpriseNumber(12345L));
220 builder.setSubOrg((short) 5);
221 builder.setValue(38);
222 builder.setDescription("user desc");
223 final RsvpErrorSpecTlv tlv = new RsvpErrorSpecBuilder().setErrorType(new UserBuilder().setUserError(builder.build()).build()).build();
224 assertEquals(tlv, parser.parseTlv(userErrorBytes));
225 assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
229 public void testReqMissingTlv() throws PCEPDeserializerException {
230 final ReqMissingTlvParser parser = new ReqMissingTlvParser();
231 final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
232 assertEquals(tlv, parser.parseTlv(reqMissingBytes));
233 assertArrayEquals(reqMissingBytes, parser.serializeTlv(tlv));
237 public void testOrderTlv() throws PCEPDeserializerException {
238 final OrderTlvParser parser = new OrderTlvParser();
239 final OrderTlv tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
240 assertEquals(tlv, parser.parseTlv(orderBytes));
241 assertArrayEquals(orderBytes, parser.serializeTlv(tlv));
245 public void testOFListTlv() throws PCEPDeserializerException {
246 final OFListTlvParser parser = new OFListTlvParser();
247 final List<OfId> ids = Lists.newArrayList();
248 ids.add(new OfId(0x1234));
249 ids.add(new OfId(0x5678));
250 final OfListTlv tlv = new OfListBuilder().setCodes(ids).build();
251 assertEquals(tlv, parser.parseTlv(ofListBytes));
252 assertArrayEquals(ofListBytes, parser.serializeTlv(tlv));
256 public void testPredundancyTlv() throws PCEPDeserializerException {
257 final PredundancyGroupTlvParser parser = new PredundancyGroupTlvParser();
258 final PredundancyGroupIdTlv tlv = new PredundancyGroupIdBuilder().setIdentifier(predundancyBytes).build();
259 assertEquals(tlv, parser.parseTlv(predundancyBytes));
260 assertArrayEquals(predundancyBytes, parser.serializeTlv(tlv));