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()
80 .setAttribute(Attribute.Interface)
81 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
82 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build())
84 assertEquals(subs.build(), parser.parseSubobject(
85 Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), false));
86 final ByteBuf buff = Unpooled.buffer();
87 parser.serializeSubobject(subs.build(), buff);
88 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
91 parser.parseSubobject(null, true);
93 } catch (final IllegalArgumentException e) {
94 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
97 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
99 } catch (final IllegalArgumentException e) {
100 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
105 public void testXROIp6PrefixSubobject() throws PCEPDeserializerException {
106 final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
107 final SubobjectBuilder subs = new SubobjectBuilder()
109 .setAttribute(Attribute.Node)
110 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
111 new IpPrefixBuilder().setIpPrefix(
112 new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
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,
116 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
120 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), true));
121 final ByteBuf buff = Unpooled.buffer();
122 parser.serializeSubobject(subs.build(), buff);
123 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
126 parser.parseSubobject(null, true);
128 } catch (final IllegalArgumentException e) {
129 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
132 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
134 } catch (final IllegalArgumentException e) {
135 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
140 public void testXROSrlgSubobject() throws PCEPDeserializerException {
141 final XROSrlgSubobjectParser parser = new XROSrlgSubobjectParser();
142 final SubobjectBuilder subs = new SubobjectBuilder()
144 .setAttribute(Attribute.Srlg)
145 .setSubobjectType(new SrlgCaseBuilder()
146 .setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(Uint32.valueOf(0x12345678L))).build())
148 assertEquals(subs.build(),
149 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(SRLG_BYTES, 2)), true));
150 final ByteBuf buff = Unpooled.buffer();
151 parser.serializeSubobject(subs.build(), buff);
152 assertArrayEquals(SRLG_BYTES, ByteArray.getAllBytes(buff));
155 parser.parseSubobject(null, true);
157 } catch (final IllegalArgumentException e) {
158 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
161 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
163 } catch (final IllegalArgumentException e) {
164 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
169 public void testXROUnnumberedSubobject() throws PCEPDeserializerException {
170 final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
171 final SubobjectBuilder subs = new SubobjectBuilder()
173 .setAttribute(Attribute.Node)
174 .setSubobjectType(new UnnumberedCaseBuilder()
175 .setUnnumbered(new UnnumberedBuilder()
176 .setRouterId(Uint32.valueOf(0x12345000L))
177 .setInterfaceId(Uint32.valueOf(0xffffffffL))
181 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
182 final ByteBuf buff = Unpooled.buffer();
183 parser.serializeSubobject(subs.build(), buff);
184 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
187 parser.parseSubobject(null, true);
189 } catch (final IllegalArgumentException e) {
190 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
193 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
195 } catch (final IllegalArgumentException e) {
196 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
201 public void testXROAsNumberSubobject() throws PCEPDeserializerException {
202 final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
203 final SubobjectBuilder subs = new SubobjectBuilder()
205 .setSubobjectType(new AsNumberCaseBuilder()
206 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
208 assertEquals(subs.build(),
209 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
210 final ByteBuf buff = Unpooled.buffer();
211 parser.serializeSubobject(subs.build(), buff);
212 assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
215 parser.parseSubobject(null, true);
217 } catch (final IllegalArgumentException e) {
218 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
221 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
223 } catch (final IllegalArgumentException e) {
224 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
229 public void testXROPathKey32Subobject() throws PCEPDeserializerException {
230 final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
231 final PathKeyBuilder pBuilder = new PathKeyBuilder()
232 .setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }))
233 .setPathKey(new PathKey(Uint16.valueOf(4660)));
234 final SubobjectBuilder subs = new SubobjectBuilder()
236 .setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
238 subs.build(),parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
239 final ByteBuf buff = Unpooled.buffer();
240 parser.serializeSubobject(subs.build(), buff);
241 assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
244 parser.parseSubobject(null, true);
246 } catch (final IllegalArgumentException e) {
247 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
250 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
252 } catch (final IllegalArgumentException e) {
253 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
258 public void testXROPathKey128Subobject() throws PCEPDeserializerException {
259 final XROPathKey128SubobjectParser parser = new XROPathKey128SubobjectParser();
260 final PathKeyBuilder pBuilder = new PathKeyBuilder()
261 .setPceId(new PceId(new byte[] {
262 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
263 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
264 (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
265 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
267 .setPathKey(new PathKey(Uint16.valueOf(4660)));
268 final SubobjectBuilder subs = new SubobjectBuilder()
270 .setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
273 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
274 final ByteBuf buff = Unpooled.buffer();
275 XROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
276 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
279 parser.parseSubobject(null, true);
281 } catch (final IllegalArgumentException e) {
282 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
285 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
287 } catch (final IllegalArgumentException e) {
288 assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());