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.impl.tlv.LSPIdentifierTlvParser;
20 import org.opendaylight.protocol.pcep.impl.tlv.LspDbVersionTlvParser;
21 import org.opendaylight.protocol.pcep.impl.tlv.LspSymbolicNameTlvParser;
22 import org.opendaylight.protocol.pcep.impl.tlv.LspUpdateErrorTlvParser;
23 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
24 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
25 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
26 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
27 import org.opendaylight.protocol.pcep.impl.tlv.PCEStatefulCapabilityTlvParser;
28 import org.opendaylight.protocol.pcep.impl.tlv.PredundancyGroupTlvParser;
29 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecTlvParser;
30 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
31 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.StatefulBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathName;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathNameBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv4ExtendedTunnelId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
71 import com.google.common.collect.Lists;
73 public class PCEPTlvParserTest {
75 private static final byte[] statefulBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02 };
76 private static final byte[] DbVersionBytes = { (byte) 0xff, (byte) 0x00, (byte) 0xff, (byte) 0xaa, (byte) 0xb2, (byte) 0xf5,
77 (byte) 0xf2, (byte) 0xcf };
78 private static final byte[] noPathVectorBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa7 };
79 private static final byte[] overloadedBytes = { (byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
80 private static final byte[] symbolicNameBytes = { (byte) 0x4d, (byte) 0x65, (byte) 0x64, (byte) 0x20, (byte) 0x74, (byte) 0x65,
81 (byte) 0x73, (byte) 0x74, (byte) 0x20, (byte) 0x6f, (byte) 0x66, (byte) 0x20, (byte) 0x73, (byte) 0x79, (byte) 0x6d,
82 (byte) 0x62, (byte) 0x6f, (byte) 0x6c, (byte) 0x69, (byte) 0x63, (byte) 0x20, (byte) 0x6e, (byte) 0x61, (byte) 0x6d,
84 private static final byte[] lspUpdateErrorBytes = { (byte) 0x25, (byte) 0x68, (byte) 0x95, (byte) 0x03 };
85 private static final byte[] lspIdentifiers4Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0xFF, (byte) 0xFF,
86 (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
87 private static final byte[] lspIdentifiers6Bytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC,
88 (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
89 (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0xFF, (byte) 0xFF, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
90 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x01,
91 (byte) 0x23, (byte) 0x45, (byte) 0x67 };
92 private static final byte[] rsvpErrorBytes = { (byte) 0x06, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
93 (byte) 0x02, (byte) 0x92, (byte) 0x16, (byte) 0x02 };
94 private static final byte[] rsvpError6Bytes = { (byte) 0x06, (byte) 0x02, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
95 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
96 (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x02, (byte) 0xd5, (byte) 0xc5, (byte) 0xd9 };
97 private static final byte[] userErrorBytes = { (byte) 0xc2, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x30, (byte) 0x39,
98 (byte) 0x05, (byte) 0x09, (byte) 0x00, (byte) 0x26, (byte) 0x75, (byte) 0x73, (byte) 0x65, (byte) 0x72, (byte) 0x20,
99 (byte) 0x64, (byte) 0x65, (byte) 0x73, (byte) 0x63 };
100 private static final byte[] reqMissingBytes = { (byte) 0xF7, (byte) 0x82, (byte) 0x35, (byte) 0x17 };
101 private static final byte[] orderBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00,
103 private static final byte[] ofListBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
104 private static final byte[] predundancyBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
107 public void testStatefulTlv() throws PCEPDeserializerException {
108 final PCEStatefulCapabilityTlvParser parser = new PCEStatefulCapabilityTlvParser();
109 final Stateful tlv = new StatefulBuilder().setLspUpdateCapability(Boolean.FALSE).setIncludeDbVersion(Boolean.TRUE).build();
110 assertEquals(tlv, parser.parseTlv(statefulBytes));
111 assertArrayEquals(statefulBytes, parser.serializeTlv(tlv));
115 public void testStateDbVersionTlv() throws PCEPDeserializerException {
116 final LspDbVersionTlvParser parser = new LspDbVersionTlvParser();
117 final LspDbVersion tlv = new LspDbVersionBuilder().setVersion(BigInteger.valueOf(0xFF00FFAAB2F5F2CFL)).build();
118 assertEquals(tlv, parser.parseTlv(DbVersionBytes));
119 assertArrayEquals(DbVersionBytes, parser.serializeTlv(tlv));
123 public void testNoPathVectorTlv() throws PCEPDeserializerException {
124 final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
125 final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
126 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();
127 assertEquals(tlv, parser.parseTlv(noPathVectorBytes));
128 assertArrayEquals(noPathVectorBytes, parser.serializeTlv(tlv));
132 public void testOverloadedDurationTlv() throws PCEPDeserializerException {
133 final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
134 final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
135 assertEquals(tlv, parser.parseTlv(overloadedBytes));
136 assertArrayEquals(overloadedBytes, parser.serializeTlv(tlv));
140 public void testSymbolicNameTlv() throws PCEPDeserializerException {
141 final LspSymbolicNameTlvParser parser = new LspSymbolicNameTlvParser();
142 final SymbolicPathName tlv = new SymbolicPathNameBuilder().setPathName(
143 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SymbolicPathName("Med test of symbolic name".getBytes())).build();
144 assertEquals(tlv, parser.parseTlv(symbolicNameBytes));
145 assertArrayEquals(symbolicNameBytes, parser.serializeTlv(tlv));
149 public void testLspErrorCodeTlv() throws PCEPDeserializerException {
150 final LspUpdateErrorTlvParser parser = new LspUpdateErrorTlvParser();
151 final LspErrorCode tlv = new LspErrorCodeBuilder().setErrorCode(627610883L).build();
152 assertEquals(tlv, parser.parseTlv(lspUpdateErrorBytes));
153 assertArrayEquals(lspUpdateErrorBytes, parser.serializeTlv(tlv));
157 public void testLspIdentifiers4Tlv() throws PCEPDeserializerException {
158 final LSPIdentifierTlvParser parser = new LSPIdentifierTlvParser();
159 final Ipv4Builder afi = new Ipv4Builder();
160 afi.setIpv4TunnelSenderAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }));
161 afi.setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
163 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(afi.build()).setLspId(new LspId(65535L)).setTunnelId(
164 new TunnelId(4660)).build();
165 assertEquals(tlv, parser.parseTlv(lspIdentifiers4Bytes));
166 assertArrayEquals(lspIdentifiers4Bytes, parser.serializeTlv(tlv));
170 public void testLspIdentifiers6Tlv() throws PCEPDeserializerException {
171 final LSPIdentifierTlvParser parser = new LSPIdentifierTlvParser();
172 final Ipv6Builder afi = new Ipv6Builder();
173 afi.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
174 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
175 (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 }));
176 afi.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56,
177 (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
178 (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 })));
179 final LspIdentifiers tlv = new LspIdentifiersBuilder().setAddressFamily(afi.build()).setLspId(new LspId(4660L)).setTunnelId(
180 new TunnelId(65535)).build();
181 assertEquals(tlv, parser.parseTlv(lspIdentifiers6Bytes));
182 assertArrayEquals(lspIdentifiers6Bytes, parser.serializeTlv(tlv));
186 public void testRSVPError4SpecTlv() throws PCEPDeserializerException {
187 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
188 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
189 builder.setNode(new IpAddress(Ipv4Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 })));
190 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
191 builder.setCode((short) 146);
192 builder.setValue(5634);
193 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpBuilder().setRsvpError(builder.build()).build()).build();
194 assertEquals(tlv, parser.parseTlv(rsvpErrorBytes));
195 assertArrayEquals(rsvpErrorBytes, parser.serializeTlv(tlv));
199 public void testRSVPError6SpecTlv() throws PCEPDeserializerException {
200 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
201 final RsvpErrorBuilder builder = new RsvpErrorBuilder();
202 builder.setNode(new IpAddress(Ipv6Util.addressForBytes(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
203 (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a,
204 (byte) 0xbc, (byte) 0xde, (byte) 0xf0 })));
205 builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ErrorSpec.Flags(false, true));
206 builder.setCode((short) 213);
207 builder.setValue(50649);
208 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new RsvpBuilder().setRsvpError(builder.build()).build()).build();
209 assertEquals(tlv, parser.parseTlv(rsvpError6Bytes));
210 assertArrayEquals(rsvpError6Bytes, parser.serializeTlv(tlv));
214 public void testUserErrorSpecTlv() throws PCEPDeserializerException {
215 final RSVPErrorSpecTlvParser parser = new RSVPErrorSpecTlvParser();
216 final UserErrorBuilder builder = new UserErrorBuilder();
217 builder.setEnterprise(new EnterpriseNumber(12345L));
218 builder.setSubOrg((short) 5);
219 builder.setValue(38);
220 builder.setDescription("user desc");
221 final RsvpErrorSpec tlv = new RsvpErrorSpecBuilder().setErrorType(new UserBuilder().setUserError(builder.build()).build()).build();
222 assertEquals(tlv, parser.parseTlv(userErrorBytes));
223 assertArrayEquals(userErrorBytes, parser.serializeTlv(tlv));
227 public void testReqMissingTlv() throws PCEPDeserializerException {
228 final ReqMissingTlvParser parser = new ReqMissingTlvParser();
229 final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
230 assertEquals(tlv, parser.parseTlv(reqMissingBytes));
231 assertArrayEquals(reqMissingBytes, parser.serializeTlv(tlv));
235 public void testOrderTlv() throws PCEPDeserializerException {
236 final OrderTlvParser parser = new OrderTlvParser();
237 final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
238 assertEquals(tlv, parser.parseTlv(orderBytes));
239 assertArrayEquals(orderBytes, parser.serializeTlv(tlv));
243 public void testOFListTlv() throws PCEPDeserializerException {
244 final OFListTlvParser parser = new OFListTlvParser();
245 final List<OfId> ids = Lists.newArrayList();
246 ids.add(new OfId(0x1234));
247 ids.add(new OfId(0x5678));
248 final OfList tlv = new OfListBuilder().setCodes(ids).build();
249 assertEquals(tlv, parser.parseTlv(ofListBytes));
250 assertArrayEquals(ofListBytes, parser.serializeTlv(tlv));
254 public void testPredundancyTlv() throws PCEPDeserializerException {
255 final PredundancyGroupTlvParser parser = new PredundancyGroupTlvParser();
256 final PredundancyGroupId tlv = new PredundancyGroupIdBuilder().setIdentifier(predundancyBytes).build();
257 assertEquals(tlv, parser.parseTlv(predundancyBytes));
258 assertArrayEquals(predundancyBytes, parser.serializeTlv(tlv));