2 * Copyright (c) 2013 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.pcep.impl;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.parser.BaseParserExtensionActivator;
21 import org.opendaylight.protocol.pcep.parser.subobject.EROAsNumberSubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.EROExplicitExclusionRouteSubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv4PrefixSubobjectParser;
24 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv6PrefixSubobjectParser;
25 import org.opendaylight.protocol.pcep.parser.subobject.EROLabelSubobjectParser;
26 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey128SubobjectParser;
27 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey32SubobjectParser;
28 import org.opendaylight.protocol.pcep.parser.subobject.EROUnnumberedInterfaceSubobjectParser;
29 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
30 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.protocol.util.Ipv6Util;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
53 import org.opendaylight.yangtools.yang.common.Uint16;
54 import org.opendaylight.yangtools.yang.common.Uint32;
56 public class PCEPEROSubobjectParserTest {
57 private static final byte[] IP4_PREFIX_BYTES = {
58 (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
60 private static final byte[] IP6_PREFIX_BYTES = {
61 (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
62 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
63 (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
65 private static final byte[] AS_NUMBER_BYTES = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
66 private static final byte[] UNNUMBERED_BYTES = {
67 (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
68 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
70 private static final byte[] PATH_KEY32_BYTES = {
71 (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
73 private static final byte[] PATH_KEY128_BYTES = {
74 (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
75 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
76 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
78 private static final byte[] LABEL_BYTES = {
79 (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF
81 private static final byte[] EXRS_BYTES = {
82 (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64
85 private SimplePCEPExtensionProviderContext ctx;
86 private BaseParserExtensionActivator act;
90 this.ctx = new SimplePCEPExtensionProviderContext();
91 this.act = new BaseParserExtensionActivator();
92 this.act.start(this.ctx);
96 public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
97 final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
98 final SubobjectBuilder subs = new SubobjectBuilder();
100 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
101 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
102 assertEquals(subs.build(),
103 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), true));
104 final ByteBuf buff = Unpooled.buffer();
105 parser.serializeSubobject(subs.build(), buff);
106 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
109 parser.parseSubobject(null, true);
111 } catch (final IllegalArgumentException e) {
112 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
115 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
117 } catch (final IllegalArgumentException e) {
118 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
123 public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
124 final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
125 final SubobjectBuilder subs = new SubobjectBuilder()
126 .setSubobjectType(new IpPrefixCaseBuilder()
127 .setIpPrefix(new IpPrefixBuilder()
128 .setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
129 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
130 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
131 (byte) 0xFF, (byte) 0xFF
136 assertEquals(subs.build(),
137 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), false));
138 final ByteBuf buff = Unpooled.buffer();
139 parser.serializeSubobject(subs.build(), buff);
140 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
143 parser.parseSubobject(null, true);
145 } catch (final IllegalArgumentException e) {
146 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
149 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
151 } catch (final IllegalArgumentException e) {
152 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
157 public void testEROAsNumberSubobject() throws PCEPDeserializerException {
158 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
159 final SubobjectBuilder subs = new SubobjectBuilder()
161 .setSubobjectType(new AsNumberCaseBuilder()
162 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
164 assertEquals(subs.build(),
165 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
166 final ByteBuf buff = Unpooled.buffer();
167 parser.serializeSubobject(subs.build(), buff);
168 assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
171 parser.parseSubobject(null, true);
173 } catch (final IllegalArgumentException e) {
174 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
177 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
179 } catch (final IllegalArgumentException e) {
180 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
185 public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
186 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
187 final SubobjectBuilder subs = new SubobjectBuilder()
189 .setSubobjectType(new UnnumberedCaseBuilder()
190 .setUnnumbered(new UnnumberedBuilder()
191 .setRouterId(Uint32.valueOf(0x12345000L))
192 .setInterfaceId(Uint32.valueOf(0xffffffffL))
195 assertEquals(subs.build(),
196 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
197 final ByteBuf buff = Unpooled.buffer();
198 parser.serializeSubobject(subs.build(), buff);
199 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
202 parser.parseSubobject(null, true);
204 } catch (final IllegalArgumentException e) {
205 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
208 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
210 } catch (final IllegalArgumentException e) {
211 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
216 public void testEROPathKey32Subobject() throws PCEPDeserializerException {
217 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
218 final SubobjectBuilder subs = new SubobjectBuilder();
220 final PathKeyBuilder pBuilder = new PathKeyBuilder();
221 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
222 pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
223 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
224 assertEquals(subs.build(),
225 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
226 final ByteBuf buff = Unpooled.buffer();
227 parser.serializeSubobject(subs.build(), buff);
228 assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
231 parser.parseSubobject(null, true);
233 } catch (final IllegalArgumentException e) {
234 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
237 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
239 } catch (final IllegalArgumentException e) {
240 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
245 public void testEROPathKey128Subobject() throws PCEPDeserializerException {
246 final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
247 final SubobjectBuilder subs = new SubobjectBuilder();
249 final PathKeyBuilder pBuilder = new PathKeyBuilder();
250 pBuilder.setPceId(new PceId(new byte[] {
251 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
252 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
253 (byte) 0x00, (byte) 0x00
255 pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
256 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
257 assertEquals(subs.build(),
258 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
259 final ByteBuf buff = Unpooled.buffer();
260 parser.serializeSubobject(subs.build(), buff);
261 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
264 parser.parseSubobject(null, true);
266 } catch (final IllegalArgumentException e) {
267 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
270 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
272 } catch (final IllegalArgumentException e) {
273 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
278 public void testEROLabelSubobject() throws Exception {
279 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
281 final SubobjectBuilder subs = new SubobjectBuilder()
283 .setSubobjectType(new LabelCaseBuilder()
284 .setLabel(new LabelBuilder()
285 .setUniDirectional(true)
286 .setLabelType(new GeneralizedLabelCaseBuilder()
287 .setGeneralizedLabel(new GeneralizedLabelBuilder()
288 .setGeneralizedLabel(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
293 assertEquals(subs.build(),
294 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2)), true));
295 final ByteBuf buff = Unpooled.buffer();
296 parser.serializeSubobject(subs.build(), buff);
297 assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
300 parser.parseSubobject(null, true);
302 } catch (final IllegalArgumentException e) {
303 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
306 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
308 } catch (final IllegalArgumentException e) {
309 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
314 public void testEROEXRSSubobject() throws Exception {
315 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
316 this.ctx.getXROSubobjectHandlerRegistry());
317 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
318 .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
319 list.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
320 .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
322 .setSubobjectType(new AsNumberCaseBuilder()
323 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
326 final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true)
327 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
328 assertEquals(subs.build(),
329 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(EXRS_BYTES, 2)), true));
330 final ByteBuf buff = Unpooled.buffer();
331 parser.serializeSubobject(subs.build(), buff);
332 assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
335 parser.parseSubobject(null, true);
337 } catch (final IllegalArgumentException e) {
338 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
341 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
343 } catch (final IllegalArgumentException e) {
344 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());