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;
12 import static org.junit.Assert.assertNull;
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.pcep.impl.TestVendorInformationTlvParser.TestEnterpriseSpecificInformation;
20 import org.opendaylight.protocol.pcep.impl.tlv.AbstractVendorSpecificTlvParser;
21 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
22 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
23 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
24 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
25 import org.opendaylight.protocol.pcep.impl.tlv.PathSetupTypeTlvParser;
26 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
27 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupTypeBuilder;
41 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;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlvBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
49 import org.opendaylight.yangtools.yang.binding.DataContainer;
51 public class PCEPTlvParserTest {
53 private static final byte[] noPathVectorBytes = { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0xa7 };
54 private static final byte[] overloadedBytes = { 0x00, 0x02, 0x00, 0x04, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
55 private static final byte[] reqMissingBytes = { 0x00, 0x03, 0x00, 0x04, (byte) 0xF7, (byte) 0x82, 0x35, 0x17 };
56 private static final byte[] orderBytes = { 0x00, 0x05, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00,
58 private static final byte[] ofListBytes = { 0x00, 0x04, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78 };
59 private static final byte[] vsTlvBytes = { 0x00, 0x1b, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
61 private static final byte[] VENDOR_INFO_BYTES = {
62 0x00, 0x07, 0x00, 0x08,
63 /* Enterprise number */
64 0x00, 0x00, 0x00, 0x00,
65 /* Enterprise specific information */
66 0x00, 0x00, 0x00, 0x05
69 private static final byte[] PST_TLV_BYTES = { 0x0, 0x1b, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0 };
71 private static final byte[] PST_TLV_BYTES_UNSUPPORTED = { 0x0, 0x1b, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
73 private final AbstractVendorSpecificTlvParser vsParser = new AbstractVendorSpecificTlvParser() {
76 protected void serializeVendorPayload(final VendorPayload payload, final ByteBuf buffer) {
77 buffer.writeBytes(new byte[] { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05 });
81 protected VendorPayload parseVendorPayload(final ByteBuf payloadBytes) throws PCEPDeserializerException {
82 return PCEPTlvParserTest.this.vp;
86 protected long getEnterpriseNumber() {
91 private final VendorPayload vp = new VendorPayload() {
94 public Class<? extends DataContainer> getImplementedInterface() {
100 public void testNoPathVectorTlv() throws PCEPDeserializerException {
101 final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
102 final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
103 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();
104 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(noPathVectorBytes, 4))));
105 final ByteBuf buff = Unpooled.buffer();
106 parser.serializeTlv(tlv, buff);
107 assertArrayEquals(noPathVectorBytes, ByteArray.getAllBytes(buff));
108 assertNull(parser.parseTlv(null));
112 public void testOverloadedDurationTlv() throws PCEPDeserializerException {
113 final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
114 final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
115 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(overloadedBytes, 4))));
116 final ByteBuf buff = Unpooled.buffer();
117 parser.serializeTlv(tlv, buff);
118 assertArrayEquals(overloadedBytes, ByteArray.getAllBytes(buff));
119 assertNull(parser.parseTlv(null));
123 public void testReqMissingTlv() throws PCEPDeserializerException {
124 final ReqMissingTlvParser parser = new ReqMissingTlvParser();
125 final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
126 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(reqMissingBytes, 4))));
127 final ByteBuf buff = Unpooled.buffer();
128 parser.serializeTlv(tlv, buff);
129 assertArrayEquals(reqMissingBytes, ByteArray.getAllBytes(buff));
130 assertNull(parser.parseTlv(null));
134 public void testOrderTlv() throws PCEPDeserializerException {
135 final OrderTlvParser parser = new OrderTlvParser();
136 final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
137 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(orderBytes, 4))));
138 final ByteBuf buff = Unpooled.buffer();
139 parser.serializeTlv(tlv, buff);
140 assertArrayEquals(orderBytes, ByteArray.getAllBytes(buff));
141 assertNull(parser.parseTlv(null));
145 public void testOFListTlv() throws PCEPDeserializerException {
146 final OFListTlvParser parser = new OFListTlvParser();
147 final List<OfId> ids = Lists.newArrayList();
148 ids.add(new OfId(0x1234));
149 ids.add(new OfId(0x5678));
150 final OfList tlv = new OfListBuilder().setCodes(ids).build();
151 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ofListBytes, 4))));
152 final ByteBuf buff = Unpooled.buffer();
153 parser.serializeTlv(tlv, buff);
154 assertArrayEquals(ofListBytes, ByteArray.getAllBytes(buff));
155 assertNull(parser.parseTlv(null));
159 public void testVendorSpecificTlv() throws PCEPDeserializerException {
160 final VsTlv tlv = new VsTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(9L)).setVendorPayload(this.vp).build();
161 assertEquals(tlv, this.vsParser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(vsTlvBytes, 4))));
162 final ByteBuf buff = Unpooled.buffer();
163 this.vsParser.serializeTlv(tlv, buff);
164 assertArrayEquals(vsTlvBytes, ByteArray.getAllBytes(buff));
165 assertNull(this.vsParser.parseTlv(null));
169 public void testVendorInformationTlv() throws PCEPDeserializerException {
170 final TestVendorInformationTlvParser parser = new TestVendorInformationTlvParser();
171 final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
172 final VendorInformationTlv viTlv = new VendorInformationTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
173 .setEnterpriseSpecificInformation(esInfo).build();
175 final VendorInformationTlv parsedTlv = parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(VENDOR_INFO_BYTES, 8)));
176 assertEquals(viTlv, parsedTlv);
178 final ByteBuf buff = Unpooled.buffer(VENDOR_INFO_BYTES.length);
179 parser.serializeTlv(viTlv, buff);
180 assertArrayEquals(VENDOR_INFO_BYTES, ByteArray.getAllBytes(buff));
181 assertNull(parser.parseTlv(null));
185 public void testPathSetupTypeTlvParser() throws PCEPDeserializerException {
186 final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
187 final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst((short) 0).build();
188 assertEquals(pstTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(PST_TLV_BYTES, 4))));
189 final ByteBuf buff = Unpooled.buffer();
190 parser.serializeTlv(pstTlv, buff);
191 assertArrayEquals(PST_TLV_BYTES, ByteArray.getAllBytes(buff));
194 @Test(expected=PCEPDeserializerException.class)
195 public void testUnsupportedPSTParser() throws PCEPDeserializerException {
196 final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
197 parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(PST_TLV_BYTES_UNSUPPORTED, 4)));
200 @Test(expected=IllegalArgumentException.class)
201 public void testUnsupportedPSTSerializer() {
202 final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
203 final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst((short) 1).build();
204 final ByteBuf buff = Unpooled.buffer();
205 parser.serializeTlv(pstTlv, buff);