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