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 io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
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.parser.tlv.NoPathVectorTlvParser;
21 import org.opendaylight.protocol.pcep.parser.tlv.OFListTlvParser;
22 import org.opendaylight.protocol.pcep.parser.tlv.OrderTlvParser;
23 import org.opendaylight.protocol.pcep.parser.tlv.OverloadedDurationTlvParser;
24 import org.opendaylight.protocol.pcep.parser.tlv.PathSetupTypeTlvParser;
25 import org.opendaylight.protocol.pcep.parser.tlv.ReqMissingTlvParser;
26 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.NoPathVectorTlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.OfId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.RequestId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.of.list.tlv.OfListBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.order.tlv.Order;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.order.tlv.OrderBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.duration.tlv.OverloadDuration;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.duration.tlv.OverloadDurationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.setup.type.tlv.PathSetupType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.setup.type.tlv.PathSetupTypeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVectorBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.req.missing.tlv.ReqMissing;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.req.missing.tlv.ReqMissingBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlvBuilder;
45 import org.opendaylight.yangtools.yang.common.Uint16;
46 import org.opendaylight.yangtools.yang.common.Uint32;
47 import org.opendaylight.yangtools.yang.common.Uint8;
49 public class PCEPTlvParserTest {
50 private static final byte[] NO_PATH_VECTOR_BYTES = {
51 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0xa7
53 private static final byte[] OVERLOADED_BYTES = {
54 0x00, 0x02, 0x00, 0x04, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff
56 private static final byte[] REQ_MISSING_BYTES = {
57 0x00, 0x03, 0x00, 0x04, (byte) 0xF7, (byte) 0x82, 0x35, 0x17
59 private static final byte[] ORDER_BYTES = {
60 0x00, 0x05, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00, 0x00, 0x01
62 private static final byte[] OF_LIST_BYTES = { 0x00, 0x04, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78 };
63 private static final byte[] VENDOR_INFO_BYTES = {
64 0x00, 0x07, 0x00, 0x08,
65 /* Enterprise number */
66 0x00, 0x00, 0x00, 0x00,
67 /* Enterprise specific information */
68 0x00, 0x00, 0x00, 0x05
71 private static final byte[] PST_TLV_BYTES = { 0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0 };
73 private static final byte[] PST_TLV_BYTES_UNSUPPORTED = { 0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
76 public void testNoPathVectorTlv() throws PCEPDeserializerException {
77 final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
78 final NoPathVectorTlv tlv = new NoPathVectorBuilder()
79 .setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
80 .NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true))
82 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(NO_PATH_VECTOR_BYTES, 4))));
83 final ByteBuf buff = Unpooled.buffer();
84 parser.serializeTlv(tlv, buff);
85 assertArrayEquals(NO_PATH_VECTOR_BYTES, ByteArray.getAllBytes(buff));
86 assertNull(parser.parseTlv(null));
90 public void testOverloadedDurationTlv() throws PCEPDeserializerException {
91 final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
92 final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(Uint32.valueOf(0x7FFFFFFFL)).build();
93 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(OVERLOADED_BYTES, 4))));
94 final ByteBuf buff = Unpooled.buffer();
95 parser.serializeTlv(tlv, buff);
96 assertArrayEquals(OVERLOADED_BYTES, ByteArray.getAllBytes(buff));
97 assertNull(parser.parseTlv(null));
101 public void testReqMissingTlv() throws PCEPDeserializerException {
102 final ReqMissingTlvParser parser = new ReqMissingTlvParser();
103 final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(Uint32.valueOf(0xF7823517L))).build();
104 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(REQ_MISSING_BYTES, 4))));
105 final ByteBuf buff = Unpooled.buffer();
106 parser.serializeTlv(tlv, buff);
107 assertArrayEquals(REQ_MISSING_BYTES, ByteArray.getAllBytes(buff));
108 assertNull(parser.parseTlv(null));
112 public void testOrderTlv() throws PCEPDeserializerException {
113 final OrderTlvParser parser = new OrderTlvParser();
114 final Order tlv = new OrderBuilder()
115 .setDelete(Uint32.valueOf(0xFFFFFFFFL))
116 .setSetup(Uint32.valueOf(0x00000001L))
118 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ORDER_BYTES, 4))));
119 final ByteBuf buff = Unpooled.buffer();
120 parser.serializeTlv(tlv, buff);
121 assertArrayEquals(ORDER_BYTES, ByteArray.getAllBytes(buff));
122 assertNull(parser.parseTlv(null));
126 public void testOFListTlv() throws PCEPDeserializerException {
127 final OFListTlvParser parser = new OFListTlvParser();
128 final List<OfId> ids = new ArrayList<>();
129 ids.add(new OfId(Uint16.valueOf(0x1234)));
130 ids.add(new OfId(Uint16.valueOf(0x5678)));
131 final OfList tlv = new OfListBuilder().setCodes(ids).build();
132 assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(OF_LIST_BYTES, 4))));
133 final ByteBuf buff = Unpooled.buffer();
134 parser.serializeTlv(tlv, buff);
135 assertArrayEquals(OF_LIST_BYTES, ByteArray.getAllBytes(buff));
136 assertNull(parser.parseTlv(null));
140 public void testVendorInformationTlv() throws PCEPDeserializerException {
141 final TestVendorInformationTlvParser parser = new TestVendorInformationTlvParser();
142 final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
143 final VendorInformationTlv viTlv = new VendorInformationTlvBuilder()
144 .setEnterpriseNumber(new EnterpriseNumber(Uint32.ZERO))
145 .setEnterpriseSpecificInformation(esInfo)
148 final VendorInformationTlv parsedTlv = parser.parseTlv(
149 Unpooled.wrappedBuffer(ByteArray.cutBytes(VENDOR_INFO_BYTES, 8)));
150 assertEquals(viTlv, parsedTlv);
152 final ByteBuf buff = Unpooled.buffer(VENDOR_INFO_BYTES.length);
153 parser.serializeTlv(viTlv, buff);
154 assertArrayEquals(VENDOR_INFO_BYTES, ByteArray.getAllBytes(buff));
155 assertNull(parser.parseTlv(null));
159 public void testPathSetupTypeTlvParser() throws PCEPDeserializerException {
160 final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
161 final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst(Uint8.ZERO).build();
162 assertEquals(pstTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(PST_TLV_BYTES, 4))));
163 final ByteBuf buff = Unpooled.buffer();
164 parser.serializeTlv(pstTlv, buff);
165 assertArrayEquals(PST_TLV_BYTES, ByteArray.getAllBytes(buff));
168 @Test(expected = PCEPDeserializerException.class)
169 public void testUnsupportedPSTParser() throws PCEPDeserializerException {
170 final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
171 parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(PST_TLV_BYTES_UNSUPPORTED, 4)));
174 @Test(expected = IllegalArgumentException.class)
175 public void testUnsupportedPSTSerializer() {
176 final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
177 final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst(Uint8.ONE).build();
178 final ByteBuf buff = Unpooled.buffer();
179 parser.serializeTlv(pstTlv, buff);