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
8 package org.opendaylight.protocol.rsvp.parser.impl;
10 import static org.junit.Assert.assertEquals;
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;
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,
72 private SimpleRSVPExtensionProviderContext ctx;
73 private RSVPActivator act;
77 this.ctx = new SimpleRSVPExtensionProviderContext();
78 this.act = new RSVPActivator();
79 this.act.start(this.ctx);
83 public void tearDown() {
88 public void testEROIp4PrefixSubobject() throws RSVPParsingException {
89 final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
90 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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));
101 parser.parseSubobject(null, true);
103 } catch (final IllegalArgumentException e) {
104 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
107 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
109 } catch (final IllegalArgumentException e) {
110 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
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,
126 final ByteBuf buff = Unpooled.buffer();
127 parser.serializeSubobject(subs.build(), buff);
128 Assert.assertArrayEquals(IP_6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
131 parser.parseSubobject(null, true);
133 } catch (final IllegalArgumentException e) {
134 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
137 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
139 } catch (final IllegalArgumentException e) {
140 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
145 public void testEROAsNumberSubobject() throws RSVPParsingException {
146 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
147 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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));
158 parser.parseSubobject(null, true);
160 } catch (final IllegalArgumentException e) {
161 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
164 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
166 } catch (final IllegalArgumentException e) {
167 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
172 public void testEROUnnumberedSubobject() throws RSVPParsingException {
173 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
174 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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,
180 final ByteBuf buff = Unpooled.buffer();
181 parser.serializeSubobject(subs.build(), buff);
182 Assert.assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
185 parser.parseSubobject(null, true);
187 } catch (final IllegalArgumentException e) {
188 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
191 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
193 } catch (final IllegalArgumentException e) {
194 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
199 public void testEROPathKey32Subobject() throws RSVPParsingException {
200 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
201 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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,
209 final ByteBuf buff = Unpooled.buffer();
210 parser.serializeSubobject(subs.build(), buff);
211 Assert.assertArrayEquals(PATH_KEY_32_BYTES, ByteArray.getAllBytes(buff));
214 parser.parseSubobject(null, true);
216 } catch (final IllegalArgumentException e) {
217 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
220 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
222 } catch (final IllegalArgumentException e) {
223 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
228 public void testEROPathKey128Subobject() throws RSVPParsingException {
229 final EROPathKey128SubobjectParser parser128 = new EROPathKey128SubobjectParser();
230 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
231 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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));
246 parser128.parseSubobject(null, true);
248 } catch (final IllegalArgumentException e) {
249 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
252 parser128.parseSubobject(Unpooled.EMPTY_BUFFER, true);
254 } catch (final IllegalArgumentException e) {
255 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
260 public void testEROLabelSubobject() throws Exception {
261 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
263 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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())
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));
278 parser.parseSubobject(null, true);
280 } catch (final IllegalArgumentException e) {
281 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
284 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
286 } catch (final IllegalArgumentException e) {
287 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
292 public void testEROExrsSubobject() throws Exception {
293 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
294 this.ctx.getXROSubobjectHandlerRegistry());
295 final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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));
315 parser.parseSubobject(null, true);
317 } catch (final IllegalArgumentException e) {
318 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
321 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
323 } catch (final IllegalArgumentException e) {
324 Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());