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 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
101 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build())
103 assertEquals(subs.build(),
104 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), true));
105 final ByteBuf buff = Unpooled.buffer();
106 parser.serializeSubobject(subs.build(), buff);
107 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
110 parser.parseSubobject(null, true);
112 } catch (final IllegalArgumentException e) {
113 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
116 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
118 } catch (final IllegalArgumentException e) {
119 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
124 public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
125 final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
126 final SubobjectBuilder subs = new SubobjectBuilder()
127 .setSubobjectType(new IpPrefixCaseBuilder()
128 .setIpPrefix(new IpPrefixBuilder()
129 .setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
130 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
131 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
132 (byte) 0xFF, (byte) 0xFF
137 assertEquals(subs.build(),
138 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), false));
139 final ByteBuf buff = Unpooled.buffer();
140 parser.serializeSubobject(subs.build(), buff);
141 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
144 parser.parseSubobject(null, true);
146 } catch (final IllegalArgumentException e) {
147 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
150 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
152 } catch (final IllegalArgumentException e) {
153 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
158 public void testEROAsNumberSubobject() throws PCEPDeserializerException {
159 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
160 final SubobjectBuilder subs = new SubobjectBuilder()
162 .setSubobjectType(new AsNumberCaseBuilder()
163 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
165 assertEquals(subs.build(),
166 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
167 final ByteBuf buff = Unpooled.buffer();
168 parser.serializeSubobject(subs.build(), buff);
169 assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
172 parser.parseSubobject(null, true);
174 } catch (final IllegalArgumentException e) {
175 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
178 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
180 } catch (final IllegalArgumentException e) {
181 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
186 public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
187 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
188 final SubobjectBuilder subs = new SubobjectBuilder()
190 .setSubobjectType(new UnnumberedCaseBuilder()
191 .setUnnumbered(new UnnumberedBuilder()
192 .setRouterId(Uint32.valueOf(0x12345000L))
193 .setInterfaceId(Uint32.valueOf(0xffffffffL))
196 assertEquals(subs.build(),
197 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
198 final ByteBuf buff = Unpooled.buffer();
199 parser.serializeSubobject(subs.build(), buff);
200 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
203 parser.parseSubobject(null, true);
205 } catch (final IllegalArgumentException e) {
206 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
209 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
211 } catch (final IllegalArgumentException e) {
212 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
217 public void testEROPathKey32Subobject() throws PCEPDeserializerException {
218 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
219 final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true);
220 final PathKeyBuilder pBuilder = new PathKeyBuilder()
221 .setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }))
222 .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().setLoose(true);
248 final PathKeyBuilder pBuilder = new PathKeyBuilder()
249 .setPceId(new PceId(new byte[] {
250 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
251 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
252 (byte) 0x00, (byte) 0x00
254 .setPathKey(new PathKey(Uint16.valueOf(4660)));
255 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
256 assertEquals(subs.build(),
257 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
258 final ByteBuf buff = Unpooled.buffer();
259 parser.serializeSubobject(subs.build(), buff);
260 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
263 parser.parseSubobject(null, true);
265 } catch (final IllegalArgumentException e) {
266 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
269 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
271 } catch (final IllegalArgumentException e) {
272 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
277 public void testEROLabelSubobject() throws Exception {
278 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
280 final SubobjectBuilder subs = new SubobjectBuilder()
282 .setSubobjectType(new LabelCaseBuilder()
283 .setLabel(new LabelBuilder()
284 .setUniDirectional(true)
285 .setLabelType(new GeneralizedLabelCaseBuilder()
286 .setGeneralizedLabel(new GeneralizedLabelBuilder()
287 .setGeneralizedLabel(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
292 assertEquals(subs.build(),
293 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2)), true));
294 final ByteBuf buff = Unpooled.buffer();
295 parser.serializeSubobject(subs.build(), buff);
296 assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
299 parser.parseSubobject(null, true);
301 } catch (final IllegalArgumentException e) {
302 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
305 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
307 } catch (final IllegalArgumentException e) {
308 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
313 public void testERO_EXRSSubobject() throws Exception {
314 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
315 this.ctx.getXROSubobjectHandlerRegistry());
316 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
317 .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
318 list.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
319 .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
321 .setSubobjectType(new AsNumberCaseBuilder()
322 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
325 final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true)
326 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
327 assertEquals(subs.build(),
328 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(EXRS_BYTES, 2)), true));
329 final ByteBuf buff = Unpooled.buffer();
330 parser.serializeSubobject(subs.build(), buff);
331 assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
334 parser.parseSubobject(null, true);
336 } catch (final IllegalArgumentException e) {
337 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
340 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
342 } catch (final IllegalArgumentException e) {
343 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());