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.NoPathVectorTlv;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.db.version.tlv.LspDbVersion;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.db.version.tlv.LspDbVersionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.error.code.tlv.LspErrorCode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.error.code.tlv.LspErrorCodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.LspIdentifiers;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.LspIdentifiersBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv4Builder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv6Builder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
51 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;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.predundancy.group.id.tlv.PredundancyGroupId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.predundancy.group.id.tlv.PredundancyGroupIdBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.RsvpErrorSpec;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.RsvpErrorSpecBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.rsvp.error.spec.error.type.RsvpBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.rsvp.error.spec.error.type.UserBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.rsvp.error.spec.error.type.rsvp.RsvpErrorBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.rsvp.error.spec.error.type.user.UserErrorBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.stateful.capability.tlv.Stateful;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.stateful.capability.tlv.Stateful.Flags;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.stateful.capability.tlv.StatefulBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathName;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathNameBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv4ExtendedTunnelId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
72 import com.google.common.collect.Lists;
74 public class PCEPTlvParserTest {
76 private static final byte[] statefulBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 };
77 private static final byte[] DbVersionBytes = { (byte) 0xff, (byte) 0x00, (byte) 0xff, (byte) 0xaa, (byte) 0xb2, (byte) 0xf5,
78 (byte) 0xf2, (byte) 0xcf };
79 private static final byte[] noPathVectorBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa7 };
80 private static final byte[] overloadedBytes = { (byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
81 private static final byte[] symbolicNameBytes = { (byte) 0x4d, (byte) 0x65, (byte) 0x64, (byte) 0x20, (byte) 0x74, (byte) 0x65,
82 (byte) 0x73, (byte) 0x74, (byte) 0x20, (byte) 0x6f, (byte) 0x66, (byte) 0x20, (byte) 0x73, (byte) 0x79, (byte) 0x6d,
83 (byte) 0x62, (byte) 0x6f, (byte) 0x6c, (byte) 0x69, (byte) 0x63, (byte) 0x20, (byte) 0x6e, (byte) 0x61, (byte) 0x6d,
85 private static final byte[] lspUpdateErrorBytes = { (byte) 0x25, (byte) 0x68, (byte) 0x95, (byte) 0x03 };
86 private static final byte[] lspIdentifiers4Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xFF, (byte) 0xFF,
87 (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
88 private static final byte[] lspIdentifiers6Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC,
89 (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
90 (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0xFF, (byte) 0xFF, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
91 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x01,
92 (byte) 0x23, (byte) 0x45, (byte) 0x67 };
93 private static final byte[] rsvpErrorBytes = { (byte) 0x06, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
94 (byte) 0x02, (byte) 0x92, (byte) 0x16, (byte) 0x02 };
95 private static final byte[] rsvpError6Bytes = { (byte) 0x06, (byte) 0x02, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
96 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
97 (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x02, (byte) 0xd5, (byte) 0xc5, (byte) 0xd9 };
98 private static final byte[] userErrorBytes = { (byte) 0xc2, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x30, (byte) 0x39,
99 (byte) 0x05, (byte) 0x09, (byte) 0x00, (byte) 0x26, (byte) 0x75, (byte) 0x73, (byte) 0x65, (byte) 0x72, (byte) 0x20,
100 (byte) 0x64, (byte) 0x65, (byte) 0x73, (byte) 0x63 };
101 private static final byte[] reqMissingBytes = { (byte) 0xF7, (byte) 0x82, (byte) 0x35, (byte) 0x17 };
102 private static final byte[] orderBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00,
104 private static final byte[] ofListBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
105 private static final byte[] predundancyBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
108 public void testStatefulTlv() throws PCEPDeserializerException {
109 final PCEStatefulCapabilityTlvParser parser = new PCEStatefulCapabilityTlvParser();
110 final Stateful tlv = new StatefulBuilder().setFlags(new Flags(true, false, false)).build();
111 assertEquals(tlv, parser.parseTlv(statefulBytes));
112 assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
116 public void testStateDbVersionTlv() throws PCEPDeserializerException {
117 final LspDbVersionTlvParser parser = new LspDbVersionTlvParser();
118 final LspDbVersion tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(0xFF00FFAAB2F5F2CFL)).build();
119 assertEquals(tlv, parser.parseTlv(DbVersionBytes));
120 assertArrayEquals(DbVersionBytes, parser.serializeTlv(tlv));
124 public void testNoPathVectorTlv() throws PCEPDeserializerException {
125 final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
126 final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
127 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();
128 assertEquals(tlv, parser.parseTlv(noPathVectorBytes));
129 assertArrayEquals(noPathVectorBytes, parser.serializeTlv(tlv));
133 public void testOverloadedDurationTlv() throws PCEPDeserializerException {
134 final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
135 final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
136 assertEquals(tlv, parser.parseTlv(overloadedBytes));
137 assertArrayEquals(overloadedBytes, parser.serializeTlv(tlv));
141 public void testSymbolicNameTlv() throws PCEPDeserializerException {
142 final LspSymbolicNameTlvParser parser = new LspSymbolicNameTlvParser();
143 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
144 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SymbolicPathName("Med test of symbolic name".getBytes())).build();
145 assertEquals(tlv, parser.parseTlv(symbolicNameBytes));
146 assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
150 public void testLspErrorCodeTlv() throws PCEPDeserializerException {
151 final LspUpdateErrorTlvParser parser = new LspUpdateErrorTlvParser();
152 final LspErrorCode tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
153 assertEquals(tlv, parser.parseTlv(lspUpdateErrorBytes));
154 assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
158 public void testLspIdentifiers4Tlv() throws PCEPDeserializerException {
159 final LSPIdentifierTlvParser parser = new LSPIdentifierTlvParser();
160 final Ipv4Builder afi = new Ipv4Builder();
161 afi.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
162 afi.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
164 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(afi.build()).setLspId(new LspId(65535L)).setTunnelId(
165 new TunnelId(4660)).build();
166 assertEquals(tlv, parser.parseTlv(lspIdentifiers4Bytes));
167 assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
171 public void testLspIdentifiers6Tlv() throws PCEPDeserializerException {
172 final LSPIdentifierTlvParser parser = new LSPIdentifierTlvParser();
173 final Ipv6Builder afi = new Ipv6Builder();
174 afi.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
175 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
176 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
177 afi.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
178 (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
179 (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 })));
180 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(afi.build()).setLspId(new LspId(4660L)).setTunnelId(
181 new TunnelId(65535)).build();
182 assertEquals(tlv, parser.parseTlv(lspIdentifiers6Bytes));
183 assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
187 public void testRSVPError4SpecTlv() throws PCEPDeserializerException {
188 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
189 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
190 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 })));
191 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
192 builder.setCode((short) 146);
193 builder.setValue(5634);
194 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpBuilder().setRsvpError(builder.build()).build()).build();
195 assertEquals(tlv, parser.parseTlv(rsvpErrorBytes));
196 assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
200 public void testRSVPError6SpecTlv() throws PCEPDeserializerException {
201 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
202 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
203 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
204 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
205 (byte) 0xbc, (byte) 0xde, (byte) 0xf0 })));
206 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
207 builder.setCode((short) 213);
208 builder.setValue(50649);
209 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpBuilder().setRsvpError(builder.build()).build()).build();
210 assertEquals(tlv, parser.parseTlv(rsvpError6Bytes));
211 assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
215 public void testUserErrorSpecTlv() throws PCEPDeserializerException {
216 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
217 final UserErrorBuilder builder = new UserErrorBuilder();
218 builder.setEnterprise(new EnterpriseNumber(12345L));
219 builder.setSubOrg((short) 5);
220 builder.setValue(38);
221 builder.setDescription("user desc");
222 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new UserBuilder().setUserError(builder.build()).build()).build();
223 assertEquals(tlv, parser.parseTlv(userErrorBytes));
224 assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
228 public void testReqMissingTlv() throws PCEPDeserializerException {
229 final ReqMissingTlvParser parser = new ReqMissingTlvParser();
230 final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
231 assertEquals(tlv, parser.parseTlv(reqMissingBytes));
232 assertArrayEquals(reqMissingBytes, parser.serializeTlv(tlv));
236 public void testOrderTlv() throws PCEPDeserializerException {
237 final OrderTlvParser parser = new OrderTlvParser();
238 final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
239 assertEquals(tlv, parser.parseTlv(orderBytes));
240 assertArrayEquals(orderBytes, parser.serializeTlv(tlv));
244 public void testOFListTlv() throws PCEPDeserializerException {
245 final OFListTlvParser parser = new OFListTlvParser();
246 final List<OfId> ids = Lists.newArrayList();
247 ids.add(new OfId(0x1234));
248 ids.add(new OfId(0x5678));
249 final OfList tlv = new OfListBuilder().setCodes(ids).build();
250 assertEquals(tlv, parser.parseTlv(ofListBytes));
251 assertArrayEquals(ofListBytes, parser.serializeTlv(tlv));
255 public void testPredundancyTlv() throws PCEPDeserializerException {
256 final PredundancyGroupTlvParser parser = new PredundancyGroupTlvParser();
257 final PredundancyGroupId tlv = new PredundancyGroupIdBuilder().setIdentifier(predundancyBytes).build();
258 assertEquals(tlv, parser.parseTlv(predundancyBytes));
259 assertArrayEquals(predundancyBytes, parser.serializeTlv(tlv));