e9fdd2e73fee5876a868eb2cc2a20deb5d8ce736
[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
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.protocol.pcep.impl.TestVendorInformationTlvParser.TestEnterpriseSpecificInformation;
19 import org.opendaylight.protocol.pcep.impl.tlv.AbstractVendorSpecificTlvParser;
20 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
21 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
22 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
23 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
24 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
25 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
37 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;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlvBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
45 import org.opendaylight.yangtools.yang.binding.DataContainer;
46
47 public class PCEPTlvParserTest {
48
49     private static final byte[] noPathVectorBytes = { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0xa7 };
50     private static final byte[] overloadedBytes = { 0x00, 0x02, 0x00, 0x04, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
51     private static final byte[] reqMissingBytes = { 0x00, 0x03, 0x00, 0x04, (byte) 0xF7, (byte) 0x82, 0x35, 0x17 };
52     private static final byte[] orderBytes = { 0x00, 0x05, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00,
53         0x00, 0x01 };
54     private static final byte[] ofListBytes = { 0x00, 0x04, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78 };
55     private static final byte[] vsTlvBytes = { 0x00, 0x1b, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
56         0x05 };
57     private static final byte[] VENDOR_INFO_BYTES = {
58         0x00, 0x07, 0x00, 0x08,
59         /* Enterprise number */
60         0x00, 0x00, 0x00, 0x00,
61         /* Enterprise specific information */
62         0x00, 0x00, 0x00, 0x05
63     };
64
65     private final AbstractVendorSpecificTlvParser vsParser = new AbstractVendorSpecificTlvParser() {
66
67         @Override
68         protected void serializeVendorPayload(final VendorPayload payload, final ByteBuf buffer) {
69             buffer.writeBytes(new byte[] { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05 });
70         }
71
72         @Override
73         protected VendorPayload parseVendorPayload(final ByteBuf payloadBytes) throws PCEPDeserializerException {
74             return PCEPTlvParserTest.this.vp;
75         }
76
77         @Override
78         protected long getEnterpriseNumber() {
79             return 9;
80         }
81     };
82
83     private final VendorPayload vp = new VendorPayload() {
84
85         @Override
86         public Class<? extends DataContainer> getImplementedInterface() {
87             return null;
88         }
89     };
90
91     @Test
92     public void testNoPathVectorTlv() throws PCEPDeserializerException {
93         final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
94         final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
95                 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();
96         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(noPathVectorBytes, 4))));
97         final ByteBuf buff = Unpooled.buffer();
98         parser.serializeTlv(tlv, buff);
99         assertArrayEquals(noPathVectorBytes, ByteArray.getAllBytes(buff));
100     }
101
102     @Test
103     public void testOverloadedDurationTlv() throws PCEPDeserializerException {
104         final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
105         final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
106         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(overloadedBytes, 4))));
107         final ByteBuf buff = Unpooled.buffer();
108         parser.serializeTlv(tlv, buff);
109         assertArrayEquals(overloadedBytes, ByteArray.getAllBytes(buff));
110     }
111
112     @Test
113     public void testReqMissingTlv() throws PCEPDeserializerException {
114         final ReqMissingTlvParser parser = new ReqMissingTlvParser();
115         final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
116         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(reqMissingBytes, 4))));
117         final ByteBuf buff = Unpooled.buffer();
118         parser.serializeTlv(tlv, buff);
119         assertArrayEquals(reqMissingBytes, ByteArray.getAllBytes(buff));
120     }
121
122     @Test
123     public void testOrderTlv() throws PCEPDeserializerException {
124         final OrderTlvParser parser = new OrderTlvParser();
125         final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
126         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(orderBytes, 4))));
127         final ByteBuf buff = Unpooled.buffer();
128         parser.serializeTlv(tlv, buff);
129         assertArrayEquals(orderBytes, ByteArray.getAllBytes(buff));
130     }
131
132     @Test
133     public void testOFListTlv() throws PCEPDeserializerException {
134         final OFListTlvParser parser = new OFListTlvParser();
135         final List<OfId> ids = Lists.newArrayList();
136         ids.add(new OfId(0x1234));
137         ids.add(new OfId(0x5678));
138         final OfList tlv = new OfListBuilder().setCodes(ids).build();
139         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ofListBytes, 4))));
140         final ByteBuf buff = Unpooled.buffer();
141         parser.serializeTlv(tlv, buff);
142         assertArrayEquals(ofListBytes, ByteArray.getAllBytes(buff));
143     }
144
145     @Test
146     public void testVendorSpecificTlv() throws PCEPDeserializerException {
147         final VsTlv tlv = new VsTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(9L)).setVendorPayload(this.vp).build();
148         assertEquals(tlv, this.vsParser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(vsTlvBytes, 4))));
149         final ByteBuf buff = Unpooled.buffer();
150         this.vsParser.serializeTlv(tlv, buff);
151         assertArrayEquals(vsTlvBytes, ByteArray.getAllBytes(buff));
152     }
153
154     @Test
155     public void testVendorInformationTlv() throws PCEPDeserializerException {
156         final TestVendorInformationTlvParser parser = new TestVendorInformationTlvParser();
157         final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
158         final VendorInformationTlv viTlv = new VendorInformationTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
159                 .setEnterpriseSpecificInformation(esInfo).build();
160
161         final VendorInformationTlv parsedTlv = parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(VENDOR_INFO_BYTES, 8)));
162         assertEquals(viTlv, parsedTlv);
163
164         final ByteBuf buff = Unpooled.buffer(VENDOR_INFO_BYTES.length);
165         parser.serializeTlv(viTlv, buff);
166         assertArrayEquals(VENDOR_INFO_BYTES, ByteArray.getAllBytes(buff));
167     }
168 }