Add new revision for pcep types model
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPTlvParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13
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.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
46 public class PCEPTlvParserTest {
47
48     private static final byte[] noPathVectorBytes = { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0xa7 };
49     private static final byte[] overloadedBytes = { 0x00, 0x02, 0x00, 0x04, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
50     private static final byte[] reqMissingBytes = { 0x00, 0x03, 0x00, 0x04, (byte) 0xF7, (byte) 0x82, 0x35, 0x17 };
51     private static final byte[] orderBytes = { 0x00, 0x05, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00,
52         0x00, 0x01 };
53     private static final byte[] ofListBytes = { 0x00, 0x04, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78 };
54     private static final byte[] VENDOR_INFO_BYTES = {
55         0x00, 0x07, 0x00, 0x08,
56         /* Enterprise number */
57         0x00, 0x00, 0x00, 0x00,
58         /* Enterprise specific information */
59         0x00, 0x00, 0x00, 0x05
60     };
61
62     private static final byte[] PST_TLV_BYTES = { 0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0 };
63
64     private static final byte[] PST_TLV_BYTES_UNSUPPORTED = { 0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
65
66     @Test
67     public void testNoPathVectorTlv() throws PCEPDeserializerException {
68         final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
69         final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
70                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true)).build();
71         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(noPathVectorBytes, 4))));
72         final ByteBuf buff = Unpooled.buffer();
73         parser.serializeTlv(tlv, buff);
74         assertArrayEquals(noPathVectorBytes, ByteArray.getAllBytes(buff));
75         assertNull(parser.parseTlv(null));
76     }
77
78     @Test
79     public void testOverloadedDurationTlv() throws PCEPDeserializerException {
80         final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
81         final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
82         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(overloadedBytes, 4))));
83         final ByteBuf buff = Unpooled.buffer();
84         parser.serializeTlv(tlv, buff);
85         assertArrayEquals(overloadedBytes, ByteArray.getAllBytes(buff));
86         assertNull(parser.parseTlv(null));
87     }
88
89     @Test
90     public void testReqMissingTlv() throws PCEPDeserializerException {
91         final ReqMissingTlvParser parser = new ReqMissingTlvParser();
92         final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
93         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(reqMissingBytes, 4))));
94         final ByteBuf buff = Unpooled.buffer();
95         parser.serializeTlv(tlv, buff);
96         assertArrayEquals(reqMissingBytes, ByteArray.getAllBytes(buff));
97         assertNull(parser.parseTlv(null));
98     }
99
100     @Test
101     public void testOrderTlv() throws PCEPDeserializerException {
102         final OrderTlvParser parser = new OrderTlvParser();
103         final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
104         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(orderBytes, 4))));
105         final ByteBuf buff = Unpooled.buffer();
106         parser.serializeTlv(tlv, buff);
107         assertArrayEquals(orderBytes, ByteArray.getAllBytes(buff));
108         assertNull(parser.parseTlv(null));
109     }
110
111     @Test
112     public void testOFListTlv() throws PCEPDeserializerException {
113         final OFListTlvParser parser = new OFListTlvParser();
114         final List<OfId> ids = Lists.newArrayList();
115         ids.add(new OfId(0x1234));
116         ids.add(new OfId(0x5678));
117         final OfList tlv = new OfListBuilder().setCodes(ids).build();
118         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ofListBytes, 4))));
119         final ByteBuf buff = Unpooled.buffer();
120         parser.serializeTlv(tlv, buff);
121         assertArrayEquals(ofListBytes, ByteArray.getAllBytes(buff));
122         assertNull(parser.parseTlv(null));
123     }
124
125     @Test
126     public void testVendorInformationTlv() throws PCEPDeserializerException {
127         final TestVendorInformationTlvParser parser = new TestVendorInformationTlvParser();
128         final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
129         final VendorInformationTlv viTlv = new VendorInformationTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
130                 .setEnterpriseSpecificInformation(esInfo).build();
131
132         final VendorInformationTlv parsedTlv = parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(VENDOR_INFO_BYTES, 8)));
133         assertEquals(viTlv, parsedTlv);
134
135         final ByteBuf buff = Unpooled.buffer(VENDOR_INFO_BYTES.length);
136         parser.serializeTlv(viTlv, buff);
137         assertArrayEquals(VENDOR_INFO_BYTES, ByteArray.getAllBytes(buff));
138         assertNull(parser.parseTlv(null));
139     }
140
141     @Test
142     public void testPathSetupTypeTlvParser() throws PCEPDeserializerException {
143         final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
144         final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst((short) 0).build();
145         assertEquals(pstTlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(PST_TLV_BYTES, 4))));
146         final ByteBuf buff = Unpooled.buffer();
147         parser.serializeTlv(pstTlv, buff);
148         assertArrayEquals(PST_TLV_BYTES, ByteArray.getAllBytes(buff));
149     }
150
151     @Test(expected=PCEPDeserializerException.class)
152     public void testUnsupportedPSTParser() throws PCEPDeserializerException {
153         final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
154         parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(PST_TLV_BYTES_UNSUPPORTED, 4)));
155     }
156
157     @Test(expected=IllegalArgumentException.class)
158     public void testUnsupportedPSTSerializer() {
159         final PathSetupTypeTlvParser parser = new PathSetupTypeTlvParser();
160         final PathSetupType pstTlv = new PathSetupTypeBuilder().setPst((short) 1).build();
161         final ByteBuf buff = Unpooled.buffer();
162         parser.serializeTlv(pstTlv, buff);
163     }
164 }