enforce check-style for rsvp impl
[bgpcep.git] / rsvp / impl / src / test / java / org / opendaylight / protocol / rsvp / parser / impl / XROSubobjectParserTest.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
9 package org.opendaylight.protocol.rsvp.parser.impl;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROAsNumberSubobjectParser;
18 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv4PrefixSubobjectParser;
19 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv6PrefixSubobjectParser;
20 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROPathKey128SubobjectParser;
21 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROPathKey32SubobjectParser;
22 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROSrlgSubobjectParser;
23 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROUnnumberedInterfaceSubobjectParser;
24 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.protocol.util.Ipv6Util;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
45
46 public class XROSubobjectParserTest {
47     private static final byte[] IP_4_PREFIX_BYTES = {(byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
48         (byte) 0xFF, (byte) 0x16, (byte) 0x00};
49     private static final byte[] IP_6_PREFIX_BYTES = {(byte) 0x82, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
50         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
51         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01};
52     private static final byte[] SRLG_BYTES = {(byte) 0xa2, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56,
53         (byte) 0x78, (byte) 0x00, (byte) 0x02};
54     private static final byte[] UNNUMBERED_BYTES = {(byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12,
55         (byte) 0x34, (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
56     private static final byte[] AS_NUMBER_BYTES = {(byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64};
57     private static final byte[] PATH_KEY_32_BYTES = {(byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12,
58         (byte) 0x34, (byte) 0x50, (byte) 0x00};
59     private static final byte[] PATH_KEY_128_BYTES = {(byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12,
60         (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34,
61         (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
62
63     @Test
64     public void testXROIp4PrefixSubobject() throws RSVPParsingException {
65         final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
66         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
67         subs.setMandatory(false);
68         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Interface);
69         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
70             new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22")))
71                 .build()).build());
72         assertEquals(subs.build(), parser.parseSubobject(
73             Unpooled.wrappedBuffer(ByteArray.cutBytes(IP_4_PREFIX_BYTES, 2)), false));
74         final ByteBuf buff = Unpooled.buffer();
75         parser.serializeSubobject(subs.build(), buff);
76         Assert.assertArrayEquals(IP_4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
77
78         try {
79             parser.parseSubobject(null, true);
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         try {
85             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
86             Assert.fail();
87         } catch (final IllegalArgumentException e) {
88             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
89         }
90     }
91
92     @Test
93     public void testXROIp6PrefixSubobject() throws RSVPParsingException {
94         final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
95         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
96         subs.setMandatory(true);
97         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Node);
98         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
99             new IpPrefixBuilder().setIpPrefix(
100                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
101                     (byte) 0xFF,
102                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
103                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, 22))).build()).build());
104         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP_6_PREFIX_BYTES, 2)
105         ), true));
106         final ByteBuf buff = Unpooled.buffer();
107         parser.serializeSubobject(subs.build(), buff);
108         Assert.assertArrayEquals(IP_6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
109
110         try {
111             parser.parseSubobject(null, true);
112             Assert.fail();
113         } catch (final IllegalArgumentException e) {
114             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
115         }
116         try {
117             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
118             Assert.fail();
119         } catch (final IllegalArgumentException e) {
120             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
121         }
122     }
123
124     @Test
125     public void testXROSrlgSubobject() throws RSVPParsingException {
126         final XROSrlgSubobjectParser parser = new XROSrlgSubobjectParser();
127         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
128         subs.setMandatory(true);
129         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Srlg);
130         subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L))
131             .build()).build());
132         assertEquals(subs.build(), parser.parseSubobject(
133             Unpooled.wrappedBuffer(ByteArray.cutBytes(SRLG_BYTES, 2)), true));
134         final ByteBuf buff = Unpooled.buffer();
135         parser.serializeSubobject(subs.build(), buff);
136         Assert.assertArrayEquals(SRLG_BYTES, ByteArray.getAllBytes(buff));
137
138         try {
139             parser.parseSubobject(null, true);
140             Assert.fail();
141         } catch (final IllegalArgumentException e) {
142             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
143         }
144         try {
145             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
146             Assert.fail();
147         } catch (final IllegalArgumentException e) {
148             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
149         }
150     }
151
152     @Test
153     public void testXROUnnumberedSubobject() throws RSVPParsingException {
154         final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
155         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
156         subs.setMandatory(true);
157         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Node);
158         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
159             new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
160         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES,
161             2)), true));
162         final ByteBuf buff = Unpooled.buffer();
163         parser.serializeSubobject(subs.build(), buff);
164         Assert.assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
165
166         try {
167             parser.parseSubobject(null, true);
168             Assert.fail();
169         } catch (final IllegalArgumentException e) {
170             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
171         }
172         try {
173             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
174             Assert.fail();
175         } catch (final IllegalArgumentException e) {
176             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
177         }
178     }
179
180     @Test
181     public void testXROAsNumberSubobject() throws RSVPParsingException {
182         final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
183         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
184         subs.setMandatory(true);
185         subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(
186             new AsNumber(0x64L)).build()).build());
187         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
188             ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
189         final ByteBuf buff = Unpooled.buffer();
190         parser.serializeSubobject(subs.build(), buff);
191         Assert.assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
192
193         try {
194             parser.parseSubobject(null, true);
195             Assert.fail();
196         } catch (final IllegalArgumentException e) {
197             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
198         }
199         try {
200             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
201             Assert.fail();
202         } catch (final IllegalArgumentException e) {
203             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
204         }
205     }
206
207     @Test
208     public void testXROPathKey32Subobject() throws RSVPParsingException {
209         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
210         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
211         subs.setMandatory(true);
212         final PathKeyBuilder pBuilder = new PathKeyBuilder();
213         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00}));
214         pBuilder.setPathKey(new PathKey(4660));
215         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
216         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY_32_BYTES, 2)
217         ), true));
218         final ByteBuf buff = Unpooled.buffer();
219         parser.serializeSubobject(subs.build(), buff);
220         Assert.assertArrayEquals(PATH_KEY_32_BYTES, ByteArray.getAllBytes(buff));
221
222         try {
223             parser.parseSubobject(null, true);
224             Assert.fail();
225         } catch (final IllegalArgumentException e) {
226             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
227         }
228         try {
229             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
230             Assert.fail();
231         } catch (final IllegalArgumentException e) {
232             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
233         }
234     }
235
236     @Test
237     public void testXROPathKey128Subobject() throws RSVPParsingException {
238         final XROPathKey128SubobjectParser parser128 = new XROPathKey128SubobjectParser();
239         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
240         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
241         subs.setMandatory(true);
242         final PathKeyBuilder pBuilder = new PathKeyBuilder();
243         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
244             (byte) 0xBC, (byte) 0xDE,
245             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
246             (byte) 0x00}));
247         pBuilder.setPathKey(new PathKey(4660));
248         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
249         assertEquals(subs.build(), parser128.parseSubobject(Unpooled.wrappedBuffer(
250             ByteArray.cutBytes(PATH_KEY_128_BYTES, 2)), true));
251         final ByteBuf buff = Unpooled.buffer();
252         parser.serializeSubobject(subs.build(), buff);
253         Assert.assertArrayEquals(PATH_KEY_128_BYTES, ByteArray.getAllBytes(buff));
254
255         try {
256             parser128.parseSubobject(null, true);
257             Assert.fail();
258         } catch (final IllegalArgumentException e) {
259             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
260         }
261         try {
262             parser128.parseSubobject(Unpooled.EMPTY_BUFFER, true);
263             Assert.fail();
264         } catch (final IllegalArgumentException e) {
265             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
266         }
267     }
268 }