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 org.junit.Test;
17 import org.opendaylight.protocol.pcep.parser.BaseParserExtensionActivator;
18 import org.opendaylight.protocol.pcep.parser.subobject.RROIpv4PrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.parser.subobject.RROIpv6PrefixSubobjectParser;
20 import org.opendaylight.protocol.pcep.parser.subobject.RROLabelSubobjectParser;
21 import org.opendaylight.protocol.pcep.parser.subobject.RROPathKey128SubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.RROPathKey32SubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.RROUnnumberedInterfaceSubobjectParser;
24 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
25 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.protocol.util.Ipv6Util;
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.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
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.label.subobject.label.type.GeneralizedLabelCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
43 import org.opendaylight.yangtools.yang.common.Uint16;
44 import org.opendaylight.yangtools.yang.common.Uint32;
46 public class PCEPRROSubobjectParserTest {
48 private static final byte[] IP4_PREFIX_BYTES = {
49 (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01
51 private static final byte[] IP6_PREFIX_BYTES = {
52 (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
53 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
54 (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02
56 private static final byte[] UNNUMBERED_BYTES = {
57 (byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34,
58 (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
60 private static final byte[] PATH_KEY32_BYTES = {
61 (byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
63 private static final byte[] PATH_KEY128_BYTES = {
64 (byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
65 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
66 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
68 private static final byte[] LABEL_BYTES = { 0x03, 0x08, (byte) 0x80, 0x02, 0x12, 0x00, 0x25, (byte) 0xFF };
71 public void testRROIp4PrefixSubobject() throws PCEPDeserializerException {
72 final RROIpv4PrefixSubobjectParser parser = new RROIpv4PrefixSubobjectParser();
73 final SubobjectBuilder subs = new SubobjectBuilder();
74 subs.setProtectionAvailable(true);
75 subs.setProtectionInUse(false);
76 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
77 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
79 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2))));
80 final ByteBuf buff = Unpooled.buffer();
81 parser.serializeSubobject(subs.build(), buff);
82 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
85 parser.parseSubobject(null);
87 } catch (final IllegalArgumentException e) {
88 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
91 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
93 } catch (final IllegalArgumentException e) {
94 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
99 public void testRROIp6PrefixSubobject() throws PCEPDeserializerException {
100 final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
101 final SubobjectBuilder subs = new SubobjectBuilder();
102 subs.setProtectionAvailable(false);
103 subs.setProtectionInUse(true);
104 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
105 new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
106 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
107 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
109 22))).build()).build());
111 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2))));
112 final ByteBuf buff = Unpooled.buffer();
113 parser.serializeSubobject(subs.build(), buff);
114 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
117 parser.parseSubobject(null);
119 } catch (final IllegalArgumentException e) {
120 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
123 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
125 } catch (final IllegalArgumentException e) {
126 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
131 public void testRROUnnumberedSubobject() throws PCEPDeserializerException {
132 final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
133 final SubobjectBuilder subs = new SubobjectBuilder()
134 .setProtectionAvailable(false)
135 .setProtectionInUse(true)
136 .setSubobjectType(new UnnumberedCaseBuilder()
137 .setUnnumbered(new UnnumberedBuilder()
138 .setRouterId(Uint32.valueOf(0x12345000L)).setInterfaceId(Uint32.valueOf(0xffffffffL))
142 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2))));
143 final ByteBuf buff = Unpooled.buffer();
144 parser.serializeSubobject(subs.build(), buff);
145 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
148 parser.parseSubobject(null);
150 } catch (final IllegalArgumentException e) {
151 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
154 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
156 } catch (final IllegalArgumentException e) {
157 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
162 public void testRROPathKey32Subobject() throws PCEPDeserializerException {
163 final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
164 final SubobjectBuilder subs = new SubobjectBuilder();
165 final PathKeyBuilder pBuilder = new PathKeyBuilder();
166 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
167 pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
168 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
170 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2))));
171 final ByteBuf buff = Unpooled.buffer();
172 parser.serializeSubobject(subs.build(), buff);
173 assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
176 parser.parseSubobject(null);
178 } catch (final IllegalArgumentException e) {
179 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
182 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
184 } catch (final IllegalArgumentException e) {
185 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
190 public void testRROPathKey128Subobject() throws PCEPDeserializerException {
191 final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
192 final SubobjectBuilder subs = new SubobjectBuilder();
193 final PathKeyBuilder pBuilder = new PathKeyBuilder();
194 pBuilder.setPceId(new PceId(new byte[] {
195 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
196 (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
198 pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
199 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
201 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2))));
202 final ByteBuf buff = Unpooled.buffer();
203 RROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
204 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
207 parser.parseSubobject(null);
209 } catch (final IllegalArgumentException e) {
210 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
213 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
215 } catch (final IllegalArgumentException e) {
216 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
221 public void testRROLabelSubobject() throws Exception {
222 final SimplePCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
223 try (BaseParserExtensionActivator a = new BaseParserExtensionActivator()) {
225 final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
226 final SubobjectBuilder subs = new SubobjectBuilder();
227 subs.setSubobjectType(new LabelCaseBuilder().setLabel(
228 new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
229 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
230 new GeneralizedLabelBuilder().setGeneralizedLabel(
231 new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
232 .build()).build()).build()).build());
234 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2))));
235 final ByteBuf buff = Unpooled.buffer();
236 parser.serializeSubobject(subs.build(), buff);
237 assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
240 parser.parseSubobject(null);
242 } catch (final IllegalArgumentException e) {
243 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
246 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
248 } catch (final IllegalArgumentException e) {
249 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());