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 java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.parser.BaseParserExtensionActivator;
21 import org.opendaylight.protocol.pcep.parser.subobject.EROAsNumberSubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.EROExplicitExclusionRouteSubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv4PrefixSubobjectParser;
24 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv6PrefixSubobjectParser;
25 import org.opendaylight.protocol.pcep.parser.subobject.EROLabelSubobjectParser;
26 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey128SubobjectParser;
27 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey32SubobjectParser;
28 import org.opendaylight.protocol.pcep.parser.subobject.EROUnnumberedInterfaceSubobjectParser;
29 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
30 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.protocol.util.Ipv6Util;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
54 public class PCEPEROSubobjectParserTest {
55 private static final byte[] IP4_PREFIX_BYTES = {
56 (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
58 private static final byte[] IP6_PREFIX_BYTES = {
59 (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
60 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
61 (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
63 private static final byte[] AS_NUMBER_BYTES = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
64 private static final byte[] UNNUMBERED_BYTES = {
65 (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
66 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
68 private static final byte[] PATH_KEY32_BYTES = {
69 (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
71 private static final byte[] PATH_KEY128_BYTES = {
72 (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
73 (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
74 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
76 private static final byte[] LABEL_BYTES = {
77 (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF
79 private static final byte[] EXRS_BYTES = {
80 (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64
83 private SimplePCEPExtensionProviderContext ctx;
84 private BaseParserExtensionActivator act;
88 this.ctx = new SimplePCEPExtensionProviderContext();
89 this.act = new BaseParserExtensionActivator();
90 this.act.start(this.ctx);
94 public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
95 final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
96 final SubobjectBuilder subs = new SubobjectBuilder();
98 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
99 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
100 assertEquals(subs.build(),
101 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), true));
102 final ByteBuf buff = Unpooled.buffer();
103 parser.serializeSubobject(subs.build(), buff);
104 assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
107 parser.parseSubobject(null, true);
109 } catch (final IllegalArgumentException e) {
110 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
113 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
115 } catch (final IllegalArgumentException e) {
116 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
121 public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
122 final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
123 final SubobjectBuilder subs = new SubobjectBuilder()
124 .setSubobjectType(new IpPrefixCaseBuilder()
125 .setIpPrefix(new IpPrefixBuilder()
126 .setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
127 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
128 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
129 (byte) 0xFF, (byte) 0xFF
134 assertEquals(subs.build(),
135 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), false));
136 final ByteBuf buff = Unpooled.buffer();
137 parser.serializeSubobject(subs.build(), buff);
138 assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
141 parser.parseSubobject(null, true);
143 } catch (final IllegalArgumentException e) {
144 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
147 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
149 } catch (final IllegalArgumentException e) {
150 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
155 public void testEROAsNumberSubobject() throws PCEPDeserializerException {
156 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
157 final SubobjectBuilder subs = new SubobjectBuilder()
159 .setSubobjectType(new AsNumberCaseBuilder()
160 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build())
162 assertEquals(subs.build(),
163 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
164 final ByteBuf buff = Unpooled.buffer();
165 parser.serializeSubobject(subs.build(), buff);
166 assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
169 parser.parseSubobject(null, true);
171 } catch (final IllegalArgumentException e) {
172 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
175 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
177 } catch (final IllegalArgumentException e) {
178 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
183 public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
184 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
185 final SubobjectBuilder subs = new SubobjectBuilder();
187 subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
188 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
189 assertEquals(subs.build(),
190 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
191 final ByteBuf buff = Unpooled.buffer();
192 parser.serializeSubobject(subs.build(), buff);
193 assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
196 parser.parseSubobject(null, true);
198 } catch (final IllegalArgumentException e) {
199 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
202 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
204 } catch (final IllegalArgumentException e) {
205 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
210 public void testEROPathKey32Subobject() throws PCEPDeserializerException {
211 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
212 final SubobjectBuilder subs = new SubobjectBuilder();
214 final PathKeyBuilder pBuilder = new PathKeyBuilder();
215 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
216 pBuilder.setPathKey(new PathKey(4660));
217 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
218 assertEquals(subs.build(),
219 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
220 final ByteBuf buff = Unpooled.buffer();
221 parser.serializeSubobject(subs.build(), buff);
222 assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
225 parser.parseSubobject(null, true);
227 } catch (final IllegalArgumentException e) {
228 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
231 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
233 } catch (final IllegalArgumentException e) {
234 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
239 public void testEROPathKey128Subobject() throws PCEPDeserializerException {
240 final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
241 final SubobjectBuilder subs = new SubobjectBuilder();
243 final PathKeyBuilder pBuilder = new PathKeyBuilder();
244 pBuilder.setPceId(new PceId(new byte[] {
245 (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
246 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
247 (byte) 0x00, (byte) 0x00
249 pBuilder.setPathKey(new PathKey(4660));
250 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
251 assertEquals(subs.build(),
252 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
253 final ByteBuf buff = Unpooled.buffer();
254 parser.serializeSubobject(subs.build(), buff);
255 assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
258 parser.parseSubobject(null, true);
260 } catch (final IllegalArgumentException e) {
261 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
264 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
266 } catch (final IllegalArgumentException e) {
267 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
272 public void testEROLabelSubobject() throws Exception {
273 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
275 final SubobjectBuilder subs = new SubobjectBuilder()
277 .setSubobjectType(new LabelCaseBuilder()
278 .setLabel(new LabelBuilder()
279 .setUniDirectional(true)
280 .setLabelType(new GeneralizedLabelCaseBuilder()
281 .setGeneralizedLabel(new GeneralizedLabelBuilder()
282 .setGeneralizedLabel(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
287 assertEquals(subs.build(),
288 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2)), true));
289 final ByteBuf buff = Unpooled.buffer();
290 parser.serializeSubobject(subs.build(), buff);
291 assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
294 parser.parseSubobject(null, true);
296 } catch (final IllegalArgumentException e) {
297 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
300 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
302 } catch (final IllegalArgumentException e) {
303 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
308 public void testEROEXRSSubobject() throws Exception {
309 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
310 this.ctx.getXROSubobjectHandlerRegistry());
311 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
312 .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
313 list.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
314 .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
316 .setSubobjectType(new AsNumberCaseBuilder()
317 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build())
320 final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true)
321 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
322 assertEquals(subs.build(),
323 parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(EXRS_BYTES, 2)), true));
324 final ByteBuf buff = Unpooled.buffer();
325 parser.serializeSubobject(subs.build(), buff);
326 assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
329 parser.parseSubobject(null, true);
331 } catch (final IllegalArgumentException e) {
332 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
335 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
337 } catch (final IllegalArgumentException e) {
338 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());