Add ByteBufUtils
[bgpcep.git] / rsvp / impl / src / test / java / org / opendaylight / protocol / rsvp / parser / impl / EROSubobjectParserTest.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 java.util.ArrayList;
15 import java.util.List;
16 import org.junit.After;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROAsNumberSubobjectParser;
21 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROExplicitExclusionRouteSubobjectParser;
22 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv4PrefixSubobjectParser;
23 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv6PrefixSubobjectParser;
24 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROLabelSubobjectParser;
25 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROPathKey128SubobjectParser;
26 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROPathKey32SubobjectParser;
27 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROUnnumberedInterfaceSubobjectParser;
28 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
29 import org.opendaylight.protocol.rsvp.parser.spi.pojo.SimpleRSVPExtensionProviderContext;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.protocol.util.Ipv6Util;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
52
53 public class EROSubobjectParserTest {
54     private static final byte[] IP_4_PREFIX_BYTES = {(byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
55         (byte) 0xFF, (byte) 0x16, (byte) 0x00};
56     private static final byte[] IP_6_PREFIX_BYTES = {(byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
57         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
58         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00};
59     private static final byte[] AS_NUMBER_BYTES = {(byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64};
60     private static final byte[] UNNUMBERED_BYTES = {(byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12,
61         (byte) 0x34, (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
62     private static final byte[] PATH_KEY_32_BYTES = {(byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12,
63         (byte) 0x34, (byte) 0x50, (byte) 0x00};
64     private static final byte[] PATH_KEY_128_BYTES = {(byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12,
65         (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34,
66         (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
67     private static final byte[] LABEL_BYTES = {(byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12,
68         (byte) 0x00, (byte) 0x25, (byte) 0xFF};
69     private static final byte[] EXRS_BYTES = {(byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00,
70         (byte) 0x64};
71
72     private SimpleRSVPExtensionProviderContext ctx;
73     private RSVPActivator act;
74
75     @Before
76     public void setUp() {
77         this.ctx = new SimpleRSVPExtensionProviderContext();
78         this.act = new RSVPActivator();
79         this.act.start(this.ctx);
80     }
81
82     @After
83     public void tearDown() {
84         this.act.close();
85     }
86
87     @Test
88     public void testEROIp4PrefixSubobject() throws RSVPParsingException {
89         final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
90         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
91         subs.setLoose(true);
92         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder().setIpPrefix(
93             new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
94         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
95             ByteArray.cutBytes(IP_4_PREFIX_BYTES, 2)), true));
96         final ByteBuf buff = Unpooled.buffer();
97         parser.serializeSubobject(subs.build(), buff);
98         Assert.assertArrayEquals(IP_4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
99
100         try {
101             parser.parseSubobject(null, true);
102             Assert.fail();
103         } catch (final IllegalArgumentException e) {
104             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
105         }
106         try {
107             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
108             Assert.fail();
109         } catch (final IllegalArgumentException e) {
110             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
111         }
112     }
113
114     @Test
115     public void testEROIp6PrefixSubobject() throws RSVPParsingException {
116         final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
117         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
118         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
119             new IpPrefixBuilder().setIpPrefix(
120                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
121                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
122                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, 22))).build()).build());
123         subs.setLoose(false);
124         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP_6_PREFIX_BYTES,
125             2)), false));
126         final ByteBuf buff = Unpooled.buffer();
127         parser.serializeSubobject(subs.build(), buff);
128         Assert.assertArrayEquals(IP_6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
129
130         try {
131             parser.parseSubobject(null, true);
132             Assert.fail();
133         } catch (final IllegalArgumentException e) {
134             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
135         }
136         try {
137             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
138             Assert.fail();
139         } catch (final IllegalArgumentException e) {
140             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
141         }
142     }
143
144     @Test
145     public void testEROAsNumberSubobject() throws RSVPParsingException {
146         final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
147         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
148         subs.setLoose(true);
149         subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(
150             new AsNumber(0x64L)).build()).build());
151         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
152             ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
153         final ByteBuf buff = Unpooled.buffer();
154         parser.serializeSubobject(subs.build(), buff);
155         Assert.assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
156
157         try {
158             parser.parseSubobject(null, true);
159             Assert.fail();
160         } catch (final IllegalArgumentException e) {
161             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
162         }
163         try {
164             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
165             Assert.fail();
166         } catch (final IllegalArgumentException e) {
167             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
168         }
169     }
170
171     @Test
172     public void testEROUnnumberedSubobject() throws RSVPParsingException {
173         final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
174         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
175         subs.setLoose(true);
176         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
177             new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
178         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES,
179             2)), true));
180         final ByteBuf buff = Unpooled.buffer();
181         parser.serializeSubobject(subs.build(), buff);
182         Assert.assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
183
184         try {
185             parser.parseSubobject(null, true);
186             Assert.fail();
187         } catch (final IllegalArgumentException e) {
188             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
189         }
190         try {
191             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
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     }
197
198     @Test
199     public void testEROPathKey32Subobject() throws RSVPParsingException {
200         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
201         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
202         subs.setLoose(true);
203         final PathKeyBuilder pBuilder = new PathKeyBuilder();
204         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00}));
205         pBuilder.setPathKey(new PathKey(4660));
206         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
207         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY_32_BYTES,
208             2)), true));
209         final ByteBuf buff = Unpooled.buffer();
210         parser.serializeSubobject(subs.build(), buff);
211         Assert.assertArrayEquals(PATH_KEY_32_BYTES, ByteArray.getAllBytes(buff));
212
213         try {
214             parser.parseSubobject(null, true);
215             Assert.fail();
216         } catch (final IllegalArgumentException e) {
217             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
218         }
219         try {
220             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
221             Assert.fail();
222         } catch (final IllegalArgumentException e) {
223             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
224         }
225     }
226
227     @Test
228     public void testEROPathKey128Subobject() throws RSVPParsingException {
229         final EROPathKey128SubobjectParser parser128 = new EROPathKey128SubobjectParser();
230         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
231         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
232         subs.setLoose(true);
233         final PathKeyBuilder pBuilder = new PathKeyBuilder();
234         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
235             (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00,
236             (byte) 0x00, (byte) 0x00, (byte) 0x00}));
237         pBuilder.setPathKey(new PathKey(4660));
238         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
239         assertEquals(subs.build(), parser128.parseSubobject(Unpooled.wrappedBuffer(
240             ByteArray.cutBytes(PATH_KEY_128_BYTES, 2)), true));
241         final ByteBuf buff = Unpooled.buffer();
242         parser.serializeSubobject(subs.build(), buff);
243         Assert.assertArrayEquals(PATH_KEY_128_BYTES, ByteArray.getAllBytes(buff));
244
245         try {
246             parser128.parseSubobject(null, true);
247             Assert.fail();
248         } catch (final IllegalArgumentException e) {
249             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
250         }
251         try {
252             parser128.parseSubobject(Unpooled.EMPTY_BUFFER, true);
253             Assert.fail();
254         } catch (final IllegalArgumentException e) {
255             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
256         }
257     }
258
259     @Test
260     public void testEROLabelSubobject() throws Exception {
261         final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
262
263         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
264         subs.setLoose(true);
265         subs.setSubobjectType(new LabelCaseBuilder().setLabel(
266             new LabelBuilder().setUniDirectional(true).setLabelType(
267                 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
268                     new GeneralizedLabelBuilder().setGeneralizedLabel(
269                         new byte[]{(byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF}).build()).build()).build())
270             .build());
271         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
272             ByteArray.cutBytes(LABEL_BYTES, 2)), true));
273         final ByteBuf buff = Unpooled.buffer();
274         parser.serializeSubobject(subs.build(), buff);
275         Assert.assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
276
277         try {
278             parser.parseSubobject(null, true);
279             Assert.fail();
280         } catch (final IllegalArgumentException e) {
281             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
282         }
283         try {
284             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
285             Assert.fail();
286         } catch (final IllegalArgumentException e) {
287             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
288         }
289     }
290
291     @Test
292     public void testEROExrsSubobject() throws Exception {
293         final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
294             this.ctx.getXROSubobjectHandlerRegistry());
295         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
296         subs.setLoose(true);
297         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
298             .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
299         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
300             .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder builder =
301             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp
302             .rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
303         builder.setMandatory(true);
304         builder.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(
305             new AsNumber(0x64L)).build()).build());
306         list.add(builder.build());
307         subs.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
308         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
309             ByteArray.cutBytes(EXRS_BYTES, 2)), true));
310         final ByteBuf buff = Unpooled.buffer();
311         parser.serializeSubobject(subs.build(), buff);
312         Assert.assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
313
314         try {
315             parser.parseSubobject(null, true);
316             Assert.fail();
317         } catch (final IllegalArgumentException e) {
318             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
319         }
320         try {
321             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
322             Assert.fail();
323         } catch (final IllegalArgumentException e) {
324             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
325         }
326     }
327 }