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 com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
21 import org.opendaylight.protocol.pcep.impl.subobject.EROExplicitExclusionRouteSubobjectParser;
22 import org.opendaylight.protocol.pcep.impl.subobject.EROIpv4PrefixSubobjectParser;
23 import org.opendaylight.protocol.pcep.impl.subobject.EROIpv6PrefixSubobjectParser;
24 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
25 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKey128SubobjectParser;
26 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKey32SubobjectParser;
27 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
28 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
29 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.protocol.util.Ipv6Util;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
53 public class PCEPEROSubobjectParserTest {
54 private static final byte[] ip4PrefixBytes = { (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
55 (byte) 0x16, (byte) 0x00 };
56 private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
57 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
58 (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00 };
59 private static final byte[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
60 private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34,
61 (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
62 private static final byte[] pathKey32Bytes = { (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
63 (byte) 0x50, (byte) 0x00 };
64 private static final byte[] pathKey128Bytes = { (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
65 (byte) 0x56, (byte) 0x78, (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 };
67 private static final byte[] labelBytes = { (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25,
69 private static final byte[] exrsBytes = { (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
71 private SimplePCEPExtensionProviderContext ctx;
72 private Activator act;
76 this.ctx = new SimplePCEPExtensionProviderContext();
77 this.act = new Activator();
78 this.act.start(this.ctx);
82 public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
83 final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
84 final SubobjectBuilder subs = new SubobjectBuilder();
86 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
87 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
88 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), true));
89 final ByteBuf buff = Unpooled.buffer();
90 parser.serializeSubobject(subs.build(), buff);
91 assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
94 parser.parseSubobject(null, true);
96 } catch (final IllegalArgumentException e) {
97 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
100 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
102 } catch (final IllegalArgumentException e) {
103 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
108 public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
109 final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
110 final SubobjectBuilder subs = new SubobjectBuilder();
111 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
112 new IpPrefixBuilder().setIpPrefix(
113 new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
114 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
115 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
116 subs.setLoose(false);
117 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), false));
118 final ByteBuf buff = Unpooled.buffer();
119 parser.serializeSubobject(subs.build(), buff);
120 assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
123 parser.parseSubobject(null, true);
125 } catch (final IllegalArgumentException e) {
126 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
129 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
131 } catch (final IllegalArgumentException e) {
132 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
137 public void testEROAsNumberSubobject() throws PCEPDeserializerException {
138 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
139 final SubobjectBuilder subs = new SubobjectBuilder();
141 subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
142 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
143 final ByteBuf buff = Unpooled.buffer();
144 parser.serializeSubobject(subs.build(), buff);
145 assertArrayEquals(asNumberBytes, ByteArray.getAllBytes(buff));
148 parser.parseSubobject(null, true);
150 } catch (final IllegalArgumentException e) {
151 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
154 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
156 } catch (final IllegalArgumentException e) {
157 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
162 public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
163 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
164 final SubobjectBuilder subs = new SubobjectBuilder();
166 subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
167 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
168 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
169 final ByteBuf buff = Unpooled.buffer();
170 parser.serializeSubobject(subs.build(), buff);
171 assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
174 parser.parseSubobject(null, true);
176 } catch (final IllegalArgumentException e) {
177 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
180 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
182 } catch (final IllegalArgumentException e) {
183 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
188 public void testEROPathKey32Subobject() throws PCEPDeserializerException {
189 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
190 final SubobjectBuilder subs = new SubobjectBuilder();
192 final PathKeyBuilder pBuilder = new PathKeyBuilder();
193 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
194 pBuilder.setPathKey(new PathKey(4660));
195 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
196 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
197 final ByteBuf buff = Unpooled.buffer();
198 parser.serializeSubobject(subs.build(), buff);
199 assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
202 parser.parseSubobject(null, true);
204 } catch (final IllegalArgumentException e) {
205 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
208 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
210 } catch (final IllegalArgumentException e) {
211 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
216 public void testEROPathKey128Subobject() throws PCEPDeserializerException {
217 final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
218 final SubobjectBuilder subs = new SubobjectBuilder();
220 final PathKeyBuilder pBuilder = new PathKeyBuilder();
221 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
222 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
223 pBuilder.setPathKey(new PathKey(4660));
224 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
225 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
226 final ByteBuf buff = Unpooled.buffer();
227 parser.serializeSubobject(subs.build(), buff);
228 assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
231 parser.parseSubobject(null, true);
233 } catch (final IllegalArgumentException e) {
234 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
237 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
239 } catch (final IllegalArgumentException e) {
240 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
245 public void testEROLabelSubobject() throws Exception {
246 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
248 final SubobjectBuilder subs = new SubobjectBuilder();
250 subs.setSubobjectType(new LabelCaseBuilder().setLabel(
251 new LabelBuilder().setUniDirectional(true).setLabelType(
252 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
253 new GeneralizedLabelBuilder().setGeneralizedLabel(
254 new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
255 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(labelBytes, 2)), true));
256 final ByteBuf buff = Unpooled.buffer();
257 parser.serializeSubobject(subs.build(), buff);
258 assertArrayEquals(labelBytes, ByteArray.getAllBytes(buff));
261 parser.parseSubobject(null, true);
263 } catch (final IllegalArgumentException e) {
264 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
267 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
269 } catch (final IllegalArgumentException e) {
270 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
275 public void testEROEXRSSubobject() throws Exception {
276 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(this.ctx.getXROSubobjectHandlerRegistry());
277 final SubobjectBuilder subs = new SubobjectBuilder();
279 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs> list = Lists.newArrayList();
280 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
281 builder.setMandatory(true);
282 builder.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
283 list.add(builder.build());
284 subs.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
285 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(exrsBytes, 2)), true));
286 final ByteBuf buff = Unpooled.buffer();
287 parser.serializeSubobject(subs.build(), buff);
288 assertArrayEquals(exrsBytes, ByteArray.getAllBytes(buff));
291 parser.parseSubobject(null, true);
293 } catch (final IllegalArgumentException e) {
294 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
297 parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
299 } catch (final IllegalArgumentException e) {
300 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());