Further migration of test code from legacy setters
[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 import org.opendaylight.yangtools.yang.common.Uint16;
46 import org.opendaylight.yangtools.yang.common.Uint32;
47 import org.opendaylight.yangtools.yang.common.Uint8;
48
49 public class PCEPTlvParserTest {
50     private static final byte[] NO_PATH_VECTOR_BYTES = {
51         0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0xa7
52     };
53     private static final byte[] OVERLOADED_BYTES = {
54         0x00, 0x02, 0x00, 0x04, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff
55     };
56     private static final byte[] REQ_MISSING_BYTES = {
57         0x00, 0x03, 0x00, 0x04, (byte) 0xF7, (byte) 0x82, 0x35, 0x17
58     };
59     private static final byte[] ORDER_BYTES = {
60         0x00, 0x05, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00, 0x00, 0x01
61     };
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
69     };
70
71     private static final byte[] PST_TLV_BYTES = { 0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0 };
72
73     private static final byte[] PST_TLV_BYTES_UNSUPPORTED = { 0x0, 0x1C, 0x0, 0x4, 0x0, 0x0, 0x0, 0x1 };
74
75     @Test
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))
81                 .build();
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));
87     }
88
89     @Test
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));
98     }
99
100     @Test
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));
109     }
110
111     @Test
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))
117                 .build();
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));
123     }
124
125     @Test
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));
137     }
138
139     @Test
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)
146                 .build();
147
148         final VendorInformationTlv parsedTlv = parser.parseTlv(
149             Unpooled.wrappedBuffer(ByteArray.cutBytes(VENDOR_INFO_BYTES, 8)));
150         assertEquals(viTlv, parsedTlv);
151
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));
156     }
157
158     @Test
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));
166     }
167
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)));
172     }
173
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);
180     }
181 }