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.subobject.XROAsNumberSubobjectParser;
18 import org.opendaylight.protocol.pcep.parser.subobject.XROIpv4PrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.parser.subobject.XROIpv6PrefixSubobjectParser;
20 import org.opendaylight.protocol.pcep.parser.subobject.XROPathKey128SubobjectParser;
21 import org.opendaylight.protocol.pcep.parser.subobject.XROPathKey32SubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.XROSRLGSubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.XROUnnumberedInterfaceSubobjectParser;
24 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.rev130715.AsNumber;
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.exclude.route.object.xro.SubobjectBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
45 import org.opendaylight.yangtools.yang.common.Uint16;
46 import org.opendaylight.yangtools.yang.common.Uint32;
48 public class PCEPXROSubobjectParserTest {
50 private static final byte[] IP4_PREFIX_BYTES = {
51 (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
53 private static final byte[] IP6_PREFIX_BYTES = {
54 (byte) 0x82, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
55 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
56 (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01
58 private static final byte[] SRLG_BYTES = {
59 (byte) 0xa2, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x00, (byte) 0x02
61 private static final byte[] UNNUMBERED_BYTES = {
62 (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
63 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
65 private static final byte[] AS_NUMBER_BYTES = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
66 private static final byte[] PATH_KEY32_BYTES = {
67 (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
69 private static final byte[] PATH_KEY128_BYTES = {
70 (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
71 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
72 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
76 public void testXROIp4PrefixSubobject() throws PCEPDeserializerException {
77 final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
78 final SubobjectBuilder subs = new SubobjectBuilder();
79 subs.setMandatory(false);
80 subs.setAttribute(Attribute.Interface);
81 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
82 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
83 assertEquals(subs.build(), parser.parseSubobject(
84 Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), false));
85 final ByteBuf buff = Unpooled.buffer();
86 parser.serializeSubobject(subs.build(), buff);
87 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
90 parser.parseSubobject(null, true);
92 } catch (final IllegalArgumentException e) {
93 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
96 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
98 } catch (final IllegalArgumentException e) {
99 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
104 public void testXROIp6PrefixSubobject() throws PCEPDeserializerException {
105 final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
106 final SubobjectBuilder subs = new SubobjectBuilder();
107 subs.setMandatory(true);
108 subs.setAttribute(Attribute.Node);
109 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
110 new IpPrefixBuilder().setIpPrefix(
111 new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
112 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
113 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
114 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
115 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
119 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), true));
120 final ByteBuf buff = Unpooled.buffer();
121 parser.serializeSubobject(subs.build(), buff);
122 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
125 parser.parseSubobject(null, true);
127 } catch (final IllegalArgumentException e) {
128 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
131 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
133 } catch (final IllegalArgumentException e) {
134 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
139 public void testXROSrlgSubobject() throws PCEPDeserializerException {
140 final XROSRLGSubobjectParser parser = new XROSRLGSubobjectParser();
141 final SubobjectBuilder subs = new SubobjectBuilder()
143 .setAttribute(Attribute.Srlg)
144 .setSubobjectType(new SrlgCaseBuilder()
145 .setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(Uint32.valueOf(0x12345678L))).build())
147 assertEquals(subs.build(),
148 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(SRLG_BYTES, 2)), true));
149 final ByteBuf buff = Unpooled.buffer();
150 parser.serializeSubobject(subs.build(), buff);
151 assertArrayEquals(SRLG_BYTES, ByteArray.getAllBytes(buff));
154 parser.parseSubobject(null, true);
156 } catch (final IllegalArgumentException e) {
157 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
160 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
162 } catch (final IllegalArgumentException e) {
163 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
168 public void testXROUnnumberedSubobject() throws PCEPDeserializerException {
169 final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
170 final SubobjectBuilder subs = new SubobjectBuilder()
172 .setAttribute(Attribute.Node)
173 .setSubobjectType(new UnnumberedCaseBuilder()
174 .setUnnumbered(new UnnumberedBuilder()
175 .setRouterId(Uint32.valueOf(0x12345000L))
176 .setInterfaceId(Uint32.valueOf(0xffffffffL))
180 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
181 final ByteBuf buff = Unpooled.buffer();
182 parser.serializeSubobject(subs.build(), buff);
183 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
186 parser.parseSubobject(null, true);
188 } catch (final IllegalArgumentException e) {
189 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
192 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
194 } catch (final IllegalArgumentException e) {
195 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
200 public void testXROAsNumberSubobject() throws PCEPDeserializerException {
201 final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
202 final SubobjectBuilder subs = new SubobjectBuilder()
204 .setSubobjectType(new AsNumberCaseBuilder()
205 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
207 assertEquals(subs.build(),
208 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
209 final ByteBuf buff = Unpooled.buffer();
210 parser.serializeSubobject(subs.build(), buff);
211 assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
214 parser.parseSubobject(null, true);
216 } catch (final IllegalArgumentException e) {
217 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
220 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
222 } catch (final IllegalArgumentException e) {
223 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
228 public void testXROPathKey32Subobject() throws PCEPDeserializerException {
229 final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
230 final SubobjectBuilder subs = new SubobjectBuilder();
231 subs.setMandatory(true);
232 final PathKeyBuilder pBuilder = new PathKeyBuilder();
233 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
234 pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
235 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
236 assertEquals(subs.build(),
237 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
238 final ByteBuf buff = Unpooled.buffer();
239 parser.serializeSubobject(subs.build(), buff);
240 assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
243 parser.parseSubobject(null, true);
245 } catch (final IllegalArgumentException e) {
246 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
249 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
251 } catch (final IllegalArgumentException e) {
252 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
257 public void testXROPathKey128Subobject() throws PCEPDeserializerException {
258 final XROPathKey128SubobjectParser parser = new XROPathKey128SubobjectParser();
259 final SubobjectBuilder subs = new SubobjectBuilder();
260 subs.setMandatory(true);
261 final PathKeyBuilder pBuilder = new PathKeyBuilder();
262 pBuilder.setPceId(new PceId(new byte[] {
263 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
264 (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
266 pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
267 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
268 assertEquals(subs.build(),
269 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
270 final ByteBuf buff = Unpooled.buffer();
271 XROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
272 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
275 parser.parseSubobject(null, true);
277 } catch (final IllegalArgumentException e) {
278 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
281 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
283 } catch (final IllegalArgumentException e) {
284 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());