2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.rsvp.parser.impl;
11 import static org.junit.Assert.assertEquals;
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;
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};
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")))
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));
79 parser.parseSubobject(null, true);
81 } catch (final IllegalArgumentException e) {
82 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
85 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
87 } catch (final IllegalArgumentException e) {
88 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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,
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)
106 final ByteBuf buff = Unpooled.buffer();
107 parser.serializeSubobject(subs.build(), buff);
108 Assert.assertArrayEquals(IP_6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
111 parser.parseSubobject(null, true);
113 } catch (final IllegalArgumentException e) {
114 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
117 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
119 } catch (final IllegalArgumentException e) {
120 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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))
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));
139 parser.parseSubobject(null, true);
141 } catch (final IllegalArgumentException e) {
142 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
145 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
147 } catch (final IllegalArgumentException e) {
148 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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,
162 final ByteBuf buff = Unpooled.buffer();
163 parser.serializeSubobject(subs.build(), buff);
164 Assert.assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
167 parser.parseSubobject(null, true);
169 } catch (final IllegalArgumentException e) {
170 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
173 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
175 } catch (final IllegalArgumentException e) {
176 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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));
194 parser.parseSubobject(null, true);
196 } catch (final IllegalArgumentException e) {
197 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
200 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
202 } catch (final IllegalArgumentException e) {
203 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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)
218 final ByteBuf buff = Unpooled.buffer();
219 parser.serializeSubobject(subs.build(), buff);
220 Assert.assertArrayEquals(PATH_KEY_32_BYTES, ByteArray.getAllBytes(buff));
223 parser.parseSubobject(null, true);
225 } catch (final IllegalArgumentException e) {
226 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
229 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
231 } catch (final IllegalArgumentException e) {
232 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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,
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));
256 parser128.parseSubobject(null, true);
258 } catch (final IllegalArgumentException e) {
259 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
262 parser128.parseSubobject(Unpooled.EMPTY_BUFFER, true);
264 } catch (final IllegalArgumentException e) {
265 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());