Add ByteBufUtils
[bgpcep.git] / rsvp / impl / src / test / java / org / opendaylight / protocol / rsvp / parser / impl / RROSubobjectParserTest.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROIpv4PrefixSubobjectParser;
17 import org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROIpv6PrefixSubobjectParser;
18 import org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROLabelSubobjectParser;
19 import org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROPathKey128SubobjectParser;
20 import org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROPathKey32SubobjectParser;
21 import org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROUnnumberedInterfaceSubobjectParser;
22 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
23 import org.opendaylight.protocol.rsvp.parser.spi.pojo.SimpleRSVPExtensionProviderContext;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
41
42 public class RROSubobjectParserTest {
43
44     private static final byte[] IP_4_PREFIX_BYTES = {(byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
45         (byte) 0xFF, (byte) 0x16, (byte) 0x01};
46     private static final byte[] IP_6_PREFIX_BYTES = {(byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
47         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
48         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02};
49     private static final byte[] UNNUMBERED_BYTES = {(byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12,
50         (byte) 0x34, (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
51     private static final byte[] PATH_KEY_32_BYTES = {(byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12,
52         (byte) 0x34, (byte) 0x50, (byte) 0x00};
53     private static final byte[] PATH_KEY_128_BYTES = {(byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12,
54         (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34,
55         (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
56     private static final byte[] LABEL_BYTES = {0x03, 0x08, (byte) 0x80, 0x02, 0x12, 0x00, 0x25, (byte) 0xFF};
57
58     @Test
59     public void testRROIp4PrefixSubobject() throws RSVPParsingException {
60         final RROIpv4PrefixSubobjectParser parser = new RROIpv4PrefixSubobjectParser();
61         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
62         subs.setProtectionAvailable(true);
63         subs.setProtectionInUse(false);
64         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
65             new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
66         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP_4_PREFIX_BYTES, 2)
67         )));
68         final ByteBuf buff = Unpooled.buffer();
69         parser.serializeSubobject(subs.build(), buff);
70         Assert.assertArrayEquals(IP_4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
71
72         try {
73             parser.parseSubobject(null);
74             Assert.fail();
75         } catch (final IllegalArgumentException e) {
76             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
77         }
78         try {
79             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
80             Assert.fail();
81         } catch (final IllegalArgumentException e) {
82             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
83         }
84     }
85
86     @Test
87     public void testRROIp6PrefixSubobject() throws RSVPParsingException {
88         final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
89         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
90         subs.setProtectionAvailable(false);
91         subs.setProtectionInUse(true);
92         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
93             new IpPrefixBuilder().setIpPrefix(
94                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
95                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
96                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, 22))).build()).build());
97         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
98             ByteArray.cutBytes(IP_6_PREFIX_BYTES, 2))));
99         final ByteBuf buff = Unpooled.buffer();
100         parser.serializeSubobject(subs.build(), buff);
101         Assert.assertArrayEquals(IP_6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
102
103         try {
104             parser.parseSubobject(null);
105             Assert.fail();
106         } catch (final IllegalArgumentException e) {
107             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
108         }
109         try {
110             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
111             Assert.fail();
112         } catch (final IllegalArgumentException e) {
113             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
114         }
115     }
116
117     @Test
118     public void testRROUnnumberedSubobject() throws RSVPParsingException {
119         final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
120         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
121         subs.setProtectionAvailable(false);
122         subs.setProtectionInUse(true);
123         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
124             new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
125         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES,
126             2))));
127         final ByteBuf buff = Unpooled.buffer();
128         parser.serializeSubobject(subs.build(), buff);
129         Assert.assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
130
131         try {
132             parser.parseSubobject(null);
133             Assert.fail();
134         } catch (final IllegalArgumentException e) {
135             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
136         }
137         try {
138             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
139             Assert.fail();
140         } catch (final IllegalArgumentException e) {
141             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
142         }
143     }
144
145     @Test
146     public void testRROPathKey32Subobject() throws RSVPParsingException {
147         final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
148         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
149         final PathKeyBuilder pBuilder = new PathKeyBuilder();
150         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00}));
151         pBuilder.setPathKey(new PathKey(4660));
152         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
153         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY_32_BYTES, 2)
154         )));
155         final ByteBuf buff = Unpooled.buffer();
156         parser.serializeSubobject(subs.build(), buff);
157         Assert.assertArrayEquals(PATH_KEY_32_BYTES, ByteArray.getAllBytes(buff));
158
159         try {
160             parser.parseSubobject(null);
161             Assert.fail();
162         } catch (final IllegalArgumentException e) {
163             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
164         }
165         try {
166             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
167             Assert.fail();
168         } catch (final IllegalArgumentException e) {
169             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
170         }
171     }
172
173     @Test
174     public void testRROPathKey128Subobject() throws RSVPParsingException {
175         final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
176         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
177         final PathKeyBuilder pBuilder = new PathKeyBuilder();
178         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
179             (byte) 0xBC, (byte) 0xDE,
180             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
181             (byte) 0x00}));
182         pBuilder.setPathKey(new PathKey(4660));
183         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
184         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY_128_BYTES,
185             2))));
186         final ByteBuf buff = Unpooled.buffer();
187         RROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
188         Assert.assertArrayEquals(PATH_KEY_128_BYTES, ByteArray.getAllBytes(buff));
189
190         try {
191             parser.parseSubobject(null);
192             Assert.fail();
193         } catch (final IllegalArgumentException e) {
194             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
195         }
196         try {
197             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
198             Assert.fail();
199         } catch (final IllegalArgumentException e) {
200             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
201         }
202     }
203
204     @Test
205     public void testRROLabelSubobject() throws Exception {
206         final SimpleRSVPExtensionProviderContext ctx = new SimpleRSVPExtensionProviderContext();
207         try (RSVPActivator a = new RSVPActivator()) {
208             a.start(ctx);
209             final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
210             final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
211             subs.setSubobjectType(new LabelCaseBuilder().setLabel(
212                 new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
213                     new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
214                         new GeneralizedLabelBuilder().setGeneralizedLabel(
215                             new byte[]{(byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF}).build()).build()).build()
216             ).build());
217             assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2)
218             )));
219             final ByteBuf buff = Unpooled.buffer();
220             parser.serializeSubobject(subs.build(), buff);
221             Assert.assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
222
223             try {
224                 parser.parseSubobject(null);
225                 Assert.fail();
226             } catch (final IllegalArgumentException e) {
227                 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
228             }
229             try {
230                 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
231                 Assert.fail();
232             } catch (final IllegalArgumentException e) {
233                 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
234             }
235         }
236     }
237 }