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 .setProtectionAvailable(true)
75 .setProtectionInUse(false)
76 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
77 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build())
80 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2))));
81 final ByteBuf buff = Unpooled.buffer();
82 parser.serializeSubobject(subs.build(), buff);
83 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
86 parser.parseSubobject(null);
88 } catch (final IllegalArgumentException e) {
89 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
92 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
94 } catch (final IllegalArgumentException e) {
95 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
100 public void testRROIp6PrefixSubobject() throws PCEPDeserializerException {
101 final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
102 final SubobjectBuilder subs = new SubobjectBuilder()
103 .setProtectionAvailable(false)
104 .setProtectionInUse(true)
105 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
106 new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
107 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
108 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
109 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
110 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
112 22))).build()).build());
114 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2))));
115 final ByteBuf buff = Unpooled.buffer();
116 parser.serializeSubobject(subs.build(), buff);
117 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
120 parser.parseSubobject(null);
122 } catch (final IllegalArgumentException e) {
123 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
126 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
128 } catch (final IllegalArgumentException e) {
129 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
134 public void testRROUnnumberedSubobject() throws PCEPDeserializerException {
135 final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
136 final SubobjectBuilder subs = new SubobjectBuilder()
137 .setProtectionAvailable(false)
138 .setProtectionInUse(true)
139 .setSubobjectType(new UnnumberedCaseBuilder()
140 .setUnnumbered(new UnnumberedBuilder()
141 .setRouterId(Uint32.valueOf(0x12345000L)).setInterfaceId(Uint32.valueOf(0xffffffffL))
145 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2))));
146 final ByteBuf buff = Unpooled.buffer();
147 parser.serializeSubobject(subs.build(), buff);
148 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
151 parser.parseSubobject(null);
153 } catch (final IllegalArgumentException e) {
154 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
157 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
159 } catch (final IllegalArgumentException e) {
160 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
165 public void testRROPathKey32Subobject() throws PCEPDeserializerException {
166 final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
167 final SubobjectBuilder subs = new SubobjectBuilder();
168 final PathKeyBuilder pBuilder = new PathKeyBuilder()
169 .setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }))
170 .setPathKey(new PathKey(Uint16.valueOf(4660)));
171 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
173 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2))));
174 final ByteBuf buff = Unpooled.buffer();
175 parser.serializeSubobject(subs.build(), buff);
176 assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
179 parser.parseSubobject(null);
181 } catch (final IllegalArgumentException e) {
182 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
185 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
187 } catch (final IllegalArgumentException e) {
188 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
193 public void testRROPathKey128Subobject() throws PCEPDeserializerException {
194 final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
195 final SubobjectBuilder subs = new SubobjectBuilder();
196 final PathKeyBuilder pBuilder = new PathKeyBuilder()
197 .setPceId(new PceId(new byte[] {
198 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
199 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
200 (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
201 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
203 .setPathKey(new PathKey(Uint16.valueOf(4660)));
204 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
206 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2))));
207 final ByteBuf buff = Unpooled.buffer();
208 RROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
209 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
212 parser.parseSubobject(null);
214 } catch (final IllegalArgumentException e) {
215 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
218 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
220 } catch (final IllegalArgumentException e) {
221 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
226 public void testRROLabelSubobject() throws Exception {
227 final SimplePCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
228 try (BaseParserExtensionActivator a = new BaseParserExtensionActivator()) {
230 final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
231 final SubobjectBuilder subs = new SubobjectBuilder()
232 .setSubobjectType(new LabelCaseBuilder().setLabel(
233 new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
234 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
235 new GeneralizedLabelBuilder().setGeneralizedLabel(
236 new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
237 .build()).build()).build()).build());
239 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2))));
240 final ByteBuf buff = Unpooled.buffer();
241 parser.serializeSubobject(subs.build(), buff);
242 assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
245 parser.parseSubobject(null);
247 } catch (final IllegalArgumentException e) {
248 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
251 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
253 } catch (final IllegalArgumentException e) {
254 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());