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.impl.subobject.RROIpv4PrefixSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.RROIpv6PrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.RROLabelSubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.RROPathKey128SubobjectParser;
21 import org.opendaylight.protocol.pcep.impl.subobject.RROPathKey32SubobjectParser;
22 import org.opendaylight.protocol.pcep.impl.subobject.RROUnnumberedInterfaceSubobjectParser;
23 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
24 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.protocol.util.Ipv6Util;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.path.key._case.PathKeyBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.LabelCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
43 public class PCEPRROSubobjectParserTest {
45 private static final byte[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
46 (byte) 0x16, (byte) 0x01 };
47 private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
48 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
49 (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02 };
50 private static final byte[] unnumberedBytes = { (byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34,
51 (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
52 private static final byte[] pathKey32Bytes = { (byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
53 (byte) 0x50, (byte) 0x00 };
54 private static final byte[] pathKey128Bytes = { (byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
55 (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
56 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
57 private static final byte[] labelBytes = { 0x03, 0x08, (byte) 0x80, 0x02, 0x12, 0x00, 0x25, (byte) 0xFF };
60 public void testRROIp4PrefixSubobject() throws PCEPDeserializerException {
61 final RROIpv4PrefixSubobjectParser parser = new RROIpv4PrefixSubobjectParser();
62 final SubobjectBuilder subs = new SubobjectBuilder();
63 subs.setProtectionAvailable(true);
64 subs.setProtectionInUse(false);
65 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
66 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
67 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2))));
68 final ByteBuf buff = Unpooled.buffer();
69 parser.serializeSubobject(subs.build(), buff);
70 assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
73 parser.parseSubobject(null);
75 } catch (final IllegalArgumentException e) {
76 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
79 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
81 } catch (final IllegalArgumentException e) {
82 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
87 public void testRROIp6PrefixSubobject() throws PCEPDeserializerException {
88 final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
89 final SubobjectBuilder subs = new SubobjectBuilder();
90 subs.setProtectionAvailable(false);
91 subs.setProtectionInUse(true);
92 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
93 new IpPrefixBuilder().setIpPrefix(
94 new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
95 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
96 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
97 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2))));
98 final ByteBuf buff = Unpooled.buffer();
99 parser.serializeSubobject(subs.build(), buff);
100 assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
103 parser.parseSubobject(null);
105 } catch (final IllegalArgumentException e) {
106 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
109 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
111 } catch (final IllegalArgumentException e) {
112 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
117 public void testRROUnnumberedSubobject() throws PCEPDeserializerException {
118 final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
119 final SubobjectBuilder subs = new SubobjectBuilder();
120 subs.setProtectionAvailable(false);
121 subs.setProtectionInUse(true);
122 subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
123 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
124 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2))));
125 final ByteBuf buff = Unpooled.buffer();
126 parser.serializeSubobject(subs.build(), buff);
127 assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
130 parser.parseSubobject(null);
132 } catch (final IllegalArgumentException e) {
133 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
136 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
138 } catch (final IllegalArgumentException e) {
139 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
144 public void testRROPathKey32Subobject() throws PCEPDeserializerException {
145 final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
146 final SubobjectBuilder subs = new SubobjectBuilder();
147 final PathKeyBuilder pBuilder = new PathKeyBuilder();
148 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
149 pBuilder.setPathKey(new PathKey(4660));
150 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
151 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2))));
152 final ByteBuf buff = Unpooled.buffer();
153 parser.serializeSubobject(subs.build(), buff);
154 assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
157 parser.parseSubobject(null);
159 } catch (final IllegalArgumentException e) {
160 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
163 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
165 } catch (final IllegalArgumentException e) {
166 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
171 public void testRROPathKey128Subobject() throws PCEPDeserializerException {
172 final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
173 final SubobjectBuilder subs = new SubobjectBuilder();
174 final PathKeyBuilder pBuilder = new PathKeyBuilder();
175 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
176 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
177 pBuilder.setPathKey(new PathKey(4660));
178 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
179 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2))));
180 final ByteBuf buff = Unpooled.buffer();
181 parser.serializeSubobject(subs.build(), buff);
182 assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
185 parser.parseSubobject(null);
187 } catch (final IllegalArgumentException e) {
188 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
191 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
193 } catch (final IllegalArgumentException e) {
194 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
199 public void testRROLabelSubobject() throws Exception {
200 final SimplePCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
201 try (Activator a = new Activator()) {
203 final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
204 final SubobjectBuilder subs = new SubobjectBuilder();
205 subs.setSubobjectType(new LabelCaseBuilder().setLabel(
206 new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
207 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
208 new GeneralizedLabelBuilder().setGeneralizedLabel(
209 new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
210 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(labelBytes, 2))));
211 final ByteBuf buff = Unpooled.buffer();
212 parser.serializeSubobject(subs.build(), buff);
213 assertArrayEquals(labelBytes, ByteArray.getAllBytes(buff));
216 parser.parseSubobject(null);
218 } catch (final IllegalArgumentException e) {
219 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
222 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
224 } catch (final IllegalArgumentException e) {
225 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());