Removed obsolete methods from Ipv4/Ipv6Util.
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPObjectParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.collect.Lists;
18 import com.google.common.primitives.UnsignedBytes;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.io.IOException;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.protocol.pcep.impl.TestVendorInformationTlvParser.TestEnterpriseSpecificInformation;
26 import org.opendaylight.protocol.pcep.impl.object.PCEPBandwidthObjectParser;
27 import org.opendaylight.protocol.pcep.impl.object.PCEPClassTypeObjectParser;
28 import org.opendaylight.protocol.pcep.impl.object.PCEPCloseObjectParser;
29 import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsIpv4ObjectParser;
30 import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsIpv6ObjectParser;
31 import org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser;
32 import org.opendaylight.protocol.pcep.impl.object.PCEPExcludeRouteObjectParser;
33 import org.opendaylight.protocol.pcep.impl.object.PCEPExistingBandwidthObjectParser;
34 import org.opendaylight.protocol.pcep.impl.object.PCEPExplicitRouteObjectParser;
35 import org.opendaylight.protocol.pcep.impl.object.PCEPGlobalConstraintsObjectParser;
36 import org.opendaylight.protocol.pcep.impl.object.PCEPIncludeRouteObjectParser;
37 import org.opendaylight.protocol.pcep.impl.object.PCEPLoadBalancingObjectParser;
38 import org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
39 import org.opendaylight.protocol.pcep.impl.object.PCEPMetricObjectParser;
40 import org.opendaylight.protocol.pcep.impl.object.PCEPNoPathObjectParser;
41 import org.opendaylight.protocol.pcep.impl.object.PCEPNotificationObjectParser;
42 import org.opendaylight.protocol.pcep.impl.object.PCEPObjectiveFunctionObjectParser;
43 import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
44 import org.opendaylight.protocol.pcep.impl.object.PCEPPathKeyObjectParser;
45 import org.opendaylight.protocol.pcep.impl.object.PCEPReportedRouteObjectParser;
46 import org.opendaylight.protocol.pcep.impl.object.PCEPRequestParameterObjectParser;
47 import org.opendaylight.protocol.pcep.impl.object.PCEPSvecObjectParser;
48 import org.opendaylight.protocol.pcep.spi.ObjectHeaderImpl;
49 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
50 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
51 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
52 import org.opendaylight.protocol.pcep.spi.UnknownObject;
53 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
54 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
55 import org.opendaylight.protocol.util.ByteArray;
56 import org.opendaylight.protocol.util.Ipv4Util;
57 import org.opendaylight.protocol.util.Ipv6Util;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClassType;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ProtocolVersion;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.BandwidthBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassTypeBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CCloseBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6CaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6Builder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.XroBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobject;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.GcBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.IroBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancingBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.MetricBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotificationBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.OfBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.PathKeyBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.path.key.PathKeys;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.key.object.path.key.PathKeysBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVectorBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.RroBuilder;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.RpBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.SvecBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObjectBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlvBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AttributeFilter;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
116
117 public class PCEPObjectParserTest {
118
119     private TlvRegistry tlvRegistry;
120
121     private VendorInformationTlvRegistry viTlvRegistry;
122
123     private SimplePCEPExtensionProviderContext ctx;
124     private Activator act;
125     private TestVendorInformationActivator viAct;
126
127     @Before
128     public void setUp() {
129         this.ctx = new SimplePCEPExtensionProviderContext();
130         this.act = new Activator();
131         this.viAct = new TestVendorInformationActivator();
132         this.act.start(this.ctx);
133         this.viAct.start(this.ctx);
134         this.tlvRegistry = this.ctx.getTlvHandlerRegistry();
135         this.viTlvRegistry = this.ctx.getVendorInformationTlvRegistry();
136     }
137
138     @Test
139     public void testOpenObjectWOTLV() throws PCEPDeserializerException, IOException {
140         final PCEPOpenObjectParser parser = new PCEPOpenObjectParser(this.tlvRegistry, this.viTlvRegistry);
141         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPOpenObject1.bin"));
142
143         final OpenBuilder builder = new OpenBuilder();
144         builder.setProcessingRule(false);
145         builder.setIgnore(false);
146         builder.setVersion(new ProtocolVersion((short) 1));
147         builder.setKeepalive((short) 30);
148         builder.setDeadTimer((short) 120);
149         builder.setSessionId((short) 1);
150
151         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder().build());
152
153         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
154         final ByteBuf buf = Unpooled.buffer();
155         parser.serializeObject(builder.build(), buf);
156         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
157
158         try {
159             parser.parseObject(new ObjectHeaderImpl(true, true), null);
160             fail();
161         } catch (final IllegalArgumentException e) {
162             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
163         }
164         try {
165             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
166             fail();
167         } catch (final IllegalArgumentException e) {
168             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
169         }
170     }
171
172     @Test
173     public void testCloseObject() throws IOException, PCEPDeserializerException {
174         final PCEPCloseObjectParser parser = new PCEPCloseObjectParser(this.tlvRegistry, this.viTlvRegistry);
175         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPCloseObject1.bin"));
176
177         final CCloseBuilder builder = new CCloseBuilder();
178         builder.setProcessingRule(false);
179         builder.setIgnore(false);
180         builder.setReason((short) 5);
181         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.c.close.TlvsBuilder().build());
182
183         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
184         final ByteBuf buf = Unpooled.buffer();
185         parser.serializeObject(builder.build(), buf);
186         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
187
188         try {
189             parser.parseObject(new ObjectHeaderImpl(true, true), null);
190             fail();
191         } catch (final IllegalArgumentException e) {
192             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
193         }
194         try {
195             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
196             fail();
197         } catch (final IllegalArgumentException e) {
198             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
199         }
200     }
201
202     @Test
203     public void testLoadBalancingObject() throws IOException, PCEPDeserializerException {
204         final PCEPLoadBalancingObjectParser parser = new PCEPLoadBalancingObjectParser();
205         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLoadBalancingObject1.bin"));
206
207         final LoadBalancingBuilder builder = new LoadBalancingBuilder();
208         builder.setProcessingRule(true);
209         builder.setIgnore(false);
210         builder.setMaxLsp((short) UnsignedBytes.toInt((byte) 0xf1));
211         builder.setMinBandwidth(new Bandwidth(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }));
212
213         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
214         final ByteBuf buf = Unpooled.buffer();
215         parser.serializeObject(builder.build(), buf);
216         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
217
218         try {
219             parser.parseObject(new ObjectHeaderImpl(true, true), null);
220             fail();
221         } catch (final IllegalArgumentException e) {
222             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
223         }
224         try {
225             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
226             fail();
227         } catch (final IllegalArgumentException e) {
228             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
229         }
230     }
231
232     @Test
233     public void testERObject() throws Exception {
234         final PCEPExplicitRouteObjectParser parser = new PCEPExplicitRouteObjectParser(this.ctx.getEROSubobjectHandlerRegistry());
235         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPExplicitRouteObject1PackOfSubobjects.bin"));
236
237         final EroBuilder builder = new EroBuilder();
238         builder.setProcessingRule(false);
239         builder.setIgnore(false);
240         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject> subs = Lists.newArrayList();
241         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
242                 true).setSubobjectType(
243                         new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0xffffL)).build()).build()).build());
244         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
245                 true).setSubobjectType(
246                         new IpPrefixCaseBuilder().setIpPrefix(
247                                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).build());
248         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder().setLoose(
249                 true).setSubobjectType(
250                         new UnnumberedCaseBuilder().setUnnumbered(
251                                 new UnnumberedBuilder().setRouterId(0xffffffffL).setInterfaceId(0xffffffffL).build()).build()).build());
252         builder.setSubobject(subs);
253
254         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
255         final ByteBuf buf = Unpooled.buffer();
256         parser.serializeObject(builder.build(), buf);
257         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
258
259         try {
260             parser.parseObject(new ObjectHeaderImpl(true, true), null);
261             fail();
262         } catch (final IllegalArgumentException e) {
263             assertEquals("Array of bytes is mandatory. Can't be null.", e.getMessage());
264         }
265     }
266
267     @Test
268     public void testIRObject() throws Exception {
269         final PCEPIncludeRouteObjectParser parser = new PCEPIncludeRouteObjectParser(this.ctx.getEROSubobjectHandlerRegistry());
270         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPIncludeRouteObject1PackOfSubobjects.bin"));
271         final byte[] ip6PrefixBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
272             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
273
274         final IroBuilder builder = new IroBuilder();
275         builder.setProcessingRule(false);
276         builder.setIgnore(false);
277         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.Subobject> subs = Lists.newArrayList();
278         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectBuilder().setSubobjectType(
279                 new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x10L)).build()).build()).build());
280         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectBuilder().setSubobjectType(
281                 new IpPrefixCaseBuilder().setIpPrefix(
282                         new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("18.52.80.0/21"))).build()).build()).build());
283         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectBuilder().setSubobjectType(
284                 new IpPrefixCaseBuilder().setIpPrefix(
285                         new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ip6PrefixBytes, 22))).build()).build()).build());
286         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.iro.SubobjectBuilder().setSubobjectType(
287                 new UnnumberedCaseBuilder().setUnnumbered(
288                         new UnnumberedBuilder().setRouterId(0x1245678L).setInterfaceId(0x9abcdef0L).build()).build()).build());
289         builder.setSubobject(subs);
290
291         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
292         final ByteBuf buf = Unpooled.buffer();
293         parser.serializeObject(builder.build(), buf);
294         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
295
296         try {
297             parser.parseObject(new ObjectHeaderImpl(true, true), null);
298             fail();
299         } catch (final IllegalArgumentException e) {
300             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
301         }
302         try {
303             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
304             fail();
305         } catch (final IllegalArgumentException e) {
306             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
307         }
308     }
309
310     @Test
311     public void testRRObject() throws Exception {
312         final PCEPReportedRouteObjectParser parser = new PCEPReportedRouteObjectParser(this.ctx.getRROSubobjectHandlerRegistry());
313         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPReportedRouteObject1PackOfSubobjects.bin"));
314         final byte[] ip6PrefixBytes = { (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
315             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
316
317         final RroBuilder builder = new RroBuilder();
318         builder.setProcessingRule(false);
319         builder.setIgnore(false);
320         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject> subs = Lists.newArrayList();
321         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
322                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder().setIpPrefix(
323                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder().setIpPrefix(
324                                 new IpPrefix(new Ipv4Prefix("255.255.255.255/32"))).build()).build()).setProtectionAvailable(false).setProtectionInUse(
325                                         false).build());
326         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
327                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder().setIpPrefix(
328                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder().setIpPrefix(
329                                 new IpPrefix(Ipv6Util.prefixForBytes(ip6PrefixBytes, 22))).build()).build()).setProtectionAvailable(false).setProtectionInUse(
330                                         false).build());
331         subs.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder().setSubobjectType(
332                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder().setUnnumbered(
333                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder().setRouterId(
334                                 0x1245678L).setInterfaceId(0x9abcdef0L).build()).build()).setProtectionAvailable(false).setProtectionInUse(
335                                         false).build());
336         builder.setSubobject(subs);
337
338         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
339         final ByteBuf buf = Unpooled.buffer();
340         parser.serializeObject(builder.build(), buf);
341         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
342
343         try {
344             parser.parseObject(new ObjectHeaderImpl(true, true), null);
345             fail();
346         } catch (final IllegalArgumentException e) {
347             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
348         }
349         try {
350             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
351             fail();
352         } catch (final IllegalArgumentException e) {
353             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
354         }
355     }
356
357     @Test
358     public void testBandwidthObject() throws IOException, PCEPDeserializerException {
359         final PCEPBandwidthObjectParser parser = new PCEPBandwidthObjectParser();
360         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPBandwidthObject1LowerBounds.bin"));
361
362         final BandwidthBuilder builder = new BandwidthBuilder();
363         builder.setProcessingRule(true);
364         builder.setIgnore(true);
365         builder.setBandwidth(new Bandwidth(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
366
367         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
368         final ByteBuf buf = Unpooled.buffer();
369         parser.serializeObject(builder.build(), buf);
370         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
371
372         try {
373             parser.parseObject(new ObjectHeaderImpl(true, true), null);
374             fail();
375         } catch (final IllegalArgumentException e) {
376             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
377         }
378         try {
379             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
380             fail();
381         } catch (final IllegalArgumentException e) {
382             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
383         }
384     }
385
386     @Test
387     public void testExistingBandwidthObject() throws IOException, PCEPDeserializerException {
388         final PCEPExistingBandwidthObjectParser parser = new PCEPExistingBandwidthObjectParser();
389         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPBandwidthObject2UpperBounds.bin"));
390
391         final BandwidthBuilder builder = new BandwidthBuilder();
392         builder.setProcessingRule(true);
393         builder.setIgnore(true);
394         builder.setBandwidth(new Bandwidth(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }));
395
396         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
397         final ByteBuf buf = Unpooled.buffer();
398         parser.serializeObject(builder.build(), buf);
399         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
400
401         try {
402             parser.parseObject(new ObjectHeaderImpl(true, true), null);
403             fail();
404         } catch (final IllegalArgumentException e) {
405             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
406         }
407         try {
408             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
409             fail();
410         } catch (final IllegalArgumentException e) {
411             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
412         }
413     }
414
415     @Test
416     public void testEndPointsObjectIPv4() throws IOException, PCEPDeserializerException {
417         final byte[] srcIPBytes = { (byte) 0xA2, (byte) 0xF5, (byte) 0x11, (byte) 0x0E };
418         final byte[] destIPBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
419
420         final PCEPEndPointsIpv4ObjectParser parser = new PCEPEndPointsIpv4ObjectParser();
421         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPEndPointsObject1IPv4.bin"));
422
423         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
424         builder.setProcessingRule(true);
425         builder.setIgnore(false);
426         builder.setAddressFamily(new Ipv4CaseBuilder().setIpv4(
427                 new Ipv4Builder().setSourceIpv4Address(Ipv4Util.addressForByteBuf(Unpooled.wrappedBuffer(srcIPBytes))).setDestinationIpv4Address(
428                         Ipv4Util.addressForByteBuf(Unpooled.wrappedBuffer(destIPBytes))).build()).build());
429
430         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
431         final ByteBuf buf = Unpooled.buffer();
432         parser.serializeObject(builder.build(), buf);
433         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
434
435         try {
436             parser.parseObject(new ObjectHeaderImpl(true, true), null);
437             fail();
438         } catch (final IllegalArgumentException e) {
439             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
440         }
441         try {
442             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
443             fail();
444         } catch (final IllegalArgumentException e) {
445             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
446         }
447     }
448
449     @Test
450     public void testEndPointsObjectIPv6() throws IOException, PCEPDeserializerException {
451         final byte[] destIPBytes = { (byte) 0x00, (byte) 0x02, (byte) 0x5D, (byte) 0xD2, (byte) 0xFF, (byte) 0xEC, (byte) 0xA1,
452             (byte) 0xB6, (byte) 0x58, (byte) 0x1E, (byte) 0x9F, (byte) 0x50, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, };
453         final byte[] srcIPBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
454             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
455
456         final PCEPEndPointsIpv6ObjectParser parser = new PCEPEndPointsIpv6ObjectParser();
457         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPEndPointsObject2IPv6.bin"));
458
459         final EndpointsObjBuilder builder = new EndpointsObjBuilder();
460         builder.setProcessingRule(true);
461         builder.setIgnore(false);
462         builder.setAddressFamily(new Ipv6CaseBuilder().setIpv6(
463                 new Ipv6Builder().setSourceIpv6Address(Ipv6Util.addressForByteBuf(Unpooled.wrappedBuffer(srcIPBytes))).setDestinationIpv6Address(
464                         Ipv6Util.addressForByteBuf(Unpooled.wrappedBuffer(destIPBytes))).build()).build());
465
466         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
467         final ByteBuf buf = Unpooled.buffer();
468         parser.serializeObject(builder.build(), buf);
469         assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
470
471         try {
472             parser.parseObject(new ObjectHeaderImpl(true, true), null);
473             fail();
474         } catch (final IllegalArgumentException e) {
475             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
476         }
477         try {
478             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
479             fail();
480         } catch (final IllegalArgumentException e) {
481             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
482         }
483     }
484
485     @Test
486     public void testErrorObjectWithTlv() throws PCEPDeserializerException, IOException {
487         final PCEPErrorObjectParser parser = new PCEPErrorObjectParser(this.tlvRegistry, this.viTlvRegistry);
488         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPErrorObject1.bin"));
489
490         final ErrorObjectBuilder builder = new ErrorObjectBuilder();
491         builder.setProcessingRule(true);
492         builder.setIgnore(true);
493         builder.setType((short) 1);
494         builder.setValue((short) 1);
495
496         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
497         ByteBuf buf = Unpooled.buffer();
498         parser.serializeObject(builder.build(), buf);
499         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
500
501         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPErrorObject3.bin"));
502
503         builder.setType((short) 7);
504         builder.setValue((short) 0);
505         builder.setTlvs(new TlvsBuilder().setReqMissing(new ReqMissingBuilder().setRequestId(new RequestId(0x00001155L)).build()).build());
506
507         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
508         buf = Unpooled.buffer();
509         parser.serializeObject(builder.build(), buf);
510         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
511
512         try {
513             parser.parseObject(new ObjectHeaderImpl(true, true), null);
514             fail();
515         } catch (final IllegalArgumentException e) {
516             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
517         }
518         try {
519             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
520             fail();
521         } catch (final IllegalArgumentException e) {
522             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
523         }
524     }
525
526     @Test
527     public void testLspaObject() throws IOException, PCEPDeserializerException {
528         final PCEPLspaObjectParser parser = new PCEPLspaObjectParser(this.tlvRegistry, this.viTlvRegistry);
529         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspaObject1LowerBounds.bin"));
530
531         final LspaBuilder builder = new LspaBuilder();
532         builder.setProcessingRule(true);
533         builder.setIgnore(true);
534         builder.setExcludeAny(new AttributeFilter(0L));
535         builder.setIncludeAny(new AttributeFilter(0L));
536         builder.setIncludeAll(new AttributeFilter(0L));
537         builder.setHoldPriority((short) 0);
538         builder.setSetupPriority((short) 0);
539         builder.setLocalProtectionDesired(false);
540         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder().build());
541
542         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
543         ByteBuf buf = Unpooled.buffer();
544         parser.serializeObject(builder.build(), buf);
545         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
546
547         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspaObject2UpperBounds.bin"));
548
549         builder.setExcludeAny(new AttributeFilter(0xFFFFFFFFL));
550         builder.setIncludeAny(new AttributeFilter(0xFFFFFFFFL));
551         builder.setIncludeAll(new AttributeFilter(0xFFFFFFFFL));
552         builder.setHoldPriority((short) 0xFF);
553         builder.setSetupPriority((short) 0xFF);
554         builder.setLocalProtectionDesired(true);
555
556         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
557         buf = Unpooled.buffer();
558         parser.serializeObject(builder.build(), buf);
559         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
560
561         try {
562             parser.parseObject(new ObjectHeaderImpl(true, true), null);
563             fail();
564         } catch (final IllegalArgumentException e) {
565             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
566         }
567         try {
568             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
569             fail();
570         } catch (final IllegalArgumentException e) {
571             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
572         }
573     }
574
575     @Test
576     public void testMetricObject() throws IOException, PCEPDeserializerException {
577         final PCEPMetricObjectParser parser = new PCEPMetricObjectParser();
578         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPMetricObject1LowerBounds.bin"));
579
580         final MetricBuilder builder = new MetricBuilder();
581         builder.setProcessingRule(true);
582         builder.setIgnore(true);
583         builder.setComputed(false);
584         builder.setBound(false);
585         builder.setMetricType((short) 1);
586         builder.setValue(new Float32(new byte[] { 0, 0, 0, 0 }));
587
588         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
589         ByteBuf buf = Unpooled.buffer();
590         parser.serializeObject(builder.build(), buf);
591         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
592
593         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPMetricObject2UpperBounds.bin"));
594
595         builder.setComputed(true);
596         builder.setBound(false);
597         builder.setMetricType((short) 2);
598         builder.setValue(new Float32(new byte[] { (byte) 0x4f, (byte) 0x70, (byte) 0x00, (byte) 0x00 }));
599
600         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
601         buf = Unpooled.buffer();
602         parser.serializeObject(builder.build(), buf);
603         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
604
605         try {
606             parser.parseObject(new ObjectHeaderImpl(true, true), null);
607             fail();
608         } catch (final IllegalArgumentException e) {
609             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
610         }
611         try {
612             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
613             fail();
614         } catch (final IllegalArgumentException e) {
615             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
616         }
617     }
618
619     @Test
620     public void testNoPathObjectWithTlv() throws PCEPDeserializerException, IOException {
621         final PCEPNoPathObjectParser parser = new PCEPNoPathObjectParser(this.tlvRegistry, this.viTlvRegistry);
622         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNoPathObject1WithoutTLV.bin"));
623
624         final NoPathBuilder builder = new NoPathBuilder();
625         builder.setProcessingRule(true);
626         builder.setIgnore(true);
627         builder.setNatureOfIssue((short) 1);
628         builder.setUnsatisfiedConstraints(true);
629         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.TlvsBuilder().build());
630
631         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
632         ByteBuf buf = Unpooled.buffer();
633         parser.serializeObject(builder.build(), buf);
634         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
635
636         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNoPathObject2WithTLV.bin"));
637
638         builder.setNatureOfIssue((short) 0);
639         builder.setUnsatisfiedConstraints(false);
640
641         final NoPathVectorBuilder b = new NoPathVectorBuilder();
642         b.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true));
643         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.TlvsBuilder().setNoPathVector(
644                 b.build()).build());
645
646         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
647         buf = Unpooled.buffer();
648         parser.serializeObject(builder.build(), buf);
649         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
650
651         try {
652             parser.parseObject(new ObjectHeaderImpl(true, true), null);
653             fail();
654         } catch (final IllegalArgumentException e) {
655             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
656         }
657         try {
658             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
659             fail();
660         } catch (final IllegalArgumentException e) {
661             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
662         }
663     }
664
665     @Test
666     public void testNotifyObjectWithTlv() throws PCEPDeserializerException, IOException {
667         final PCEPNotificationObjectParser parser = new PCEPNotificationObjectParser(this.tlvRegistry, this.viTlvRegistry);
668         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNotificationObject2WithoutTlv.bin"));
669
670         final CNotificationBuilder builder = new CNotificationBuilder();
671         builder.setProcessingRule(true);
672         builder.setIgnore(true);
673         builder.setType((short) 0xff);
674         builder.setValue((short) 0xff);
675
676         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
677         ByteBuf buf = Unpooled.buffer();
678         parser.serializeObject(builder.build(), buf);
679         assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
680
681         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPNotificationObject1WithTlv.bin"));
682
683         builder.setType((short) 2);
684         builder.setValue((short) 1);
685         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.c.notification.TlvsBuilder().setOverloadDuration(
686                 new OverloadDurationBuilder().setDuration(0xff0000a2L).build()).build());
687
688         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
689         buf = Unpooled.buffer();
690         parser.serializeObject(builder.build(), buf);
691         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
692
693         try {
694             parser.parseObject(new ObjectHeaderImpl(true, true), null);
695             fail();
696         } catch (final IllegalArgumentException e) {
697             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
698         }
699         try {
700             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
701             fail();
702         } catch (final IllegalArgumentException e) {
703             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
704         }
705     }
706
707     @Test
708     public void testRPObjectWithTlv() throws PCEPDeserializerException, IOException {
709         final PCEPRequestParameterObjectParser parser = new PCEPRequestParameterObjectParser(this.tlvRegistry, this.viTlvRegistry);
710         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPRPObject1.bin"));
711
712         final RpBuilder builder = new RpBuilder();
713         builder.setProcessingRule(true);
714         builder.setIgnore(true);
715         builder.setReoptimization(true);
716         builder.setBiDirectional(false);
717         builder.setLoose(true);
718         builder.setMakeBeforeBreak(true);
719         builder.setOrder(false);
720         builder.setPathKey(false);
721         builder.setSupplyOf(false);
722         builder.setFragmentation(false);
723         builder.setP2mp(false);
724         builder.setEroCompression(false);
725         builder.setPriority((short) 5);
726         builder.setRequestId(new RequestId(0xdeadbeefL));
727         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder().build());
728
729         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
730         ByteBuf buf = Unpooled.buffer();
731         parser.serializeObject(builder.build(), buf);
732         assertArrayEquals(result.array(),ByteArray.getAllBytes(buf));
733
734         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPRPObject2.bin"));
735
736         builder.setReoptimization(false);
737         builder.setFragmentation(true);
738         builder.setEroCompression(true);
739
740         final OrderBuilder b = new OrderBuilder();
741         b.setDelete(0xffffffffL);
742         b.setSetup(1L);
743
744         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.rp.TlvsBuilder().setOrder(
745                 b.build()).build());
746
747         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, true), result.slice(4, result.readableBytes() - 4)));
748         buf = Unpooled.buffer();
749         parser.serializeObject(builder.build(), buf);
750         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
751
752         try {
753             parser.parseObject(new ObjectHeaderImpl(true, true), null);
754             fail();
755         } catch (final IllegalArgumentException e) {
756             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
757         }
758         try {
759             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
760             fail();
761         } catch (final IllegalArgumentException e) {
762             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
763         }
764     }
765
766     @Test
767     public void testSvecObject() throws IOException, PCEPDeserializerException {
768         final PCEPSvecObjectParser parser = new PCEPSvecObjectParser();
769         ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPSvecObject2.bin"));
770
771         final SvecBuilder builder = new SvecBuilder();
772         builder.setProcessingRule(false);
773         builder.setIgnore(false);
774         builder.setLinkDiverse(false);
775         builder.setNodeDiverse(false);
776         builder.setSrlgDiverse(false);
777         builder.setRequestsIds(Lists.newArrayList(new RequestId(0xFFL)));
778
779         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
780         ByteBuf buf = Unpooled.buffer();
781         parser.serializeObject(builder.build(), buf);
782         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
783
784         result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPSvecObject1_10ReqIDs.bin"));
785
786         builder.setProcessingRule(true);
787         builder.setLinkDiverse(true);
788         builder.setSrlgDiverse(true);
789
790         final List<RequestId> requestIDs = Lists.newArrayList();
791         requestIDs.add(new RequestId(0xFFFFFFFFL));
792         requestIDs.add(new RequestId(0x00000001L));
793         requestIDs.add(new RequestId(0x01234567L));
794         requestIDs.add(new RequestId(0x89ABCDEFL));
795         requestIDs.add(new RequestId(0xFEDCBA98L));
796         requestIDs.add(new RequestId(0x76543210L));
797         requestIDs.add(new RequestId(0x15825266L));
798         requestIDs.add(new RequestId(0x48120BBEL));
799         requestIDs.add(new RequestId(0x25FB7E52L));
800         requestIDs.add(new RequestId(0xB2F2546BL));
801
802         builder.setRequestsIds(requestIDs);
803
804         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
805         buf = Unpooled.buffer();
806         parser.serializeObject(builder.build(), buf);
807         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
808
809         try {
810             parser.parseObject(new ObjectHeaderImpl(true, true), null);
811             fail();
812         } catch (final IllegalArgumentException e) {
813             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
814         }
815         try {
816             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
817             fail();
818         } catch (final IllegalArgumentException e) {
819             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
820         }
821     }
822
823     @Test
824     public void testClassTypeObject() throws PCEPDeserializerException {
825         final PCEPClassTypeObjectParser parser = new PCEPClassTypeObjectParser();
826         final ByteBuf result = Unpooled.wrappedBuffer(new byte[] { (byte) 0x16, (byte) 0x12, (byte) 0x00, (byte) 0x08, 0, 0, 0, (byte) 0x04 });
827
828         final ClassTypeBuilder builder = new ClassTypeBuilder();
829         builder.setProcessingRule(true);
830         builder.setIgnore(false);
831         builder.setClassType(new ClassType((short) 4));
832
833         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
834         final ByteBuf buf = Unpooled.buffer();
835         parser.serializeObject(builder.build(), buf);
836         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
837
838         try {
839             parser.parseObject(new ObjectHeaderImpl(true, true), null);
840             fail();
841         } catch (final IllegalArgumentException e) {
842             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
843         }
844         try {
845             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
846             fail();
847         } catch (final IllegalArgumentException e) {
848             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
849         }
850     }
851
852     @Test
853     public void testExcludeRouteObject() throws Exception {
854         final PCEPExcludeRouteObjectParser parser = new PCEPExcludeRouteObjectParser(this.ctx.getXROSubobjectHandlerRegistry());
855         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPExcludeRouteObject.1.bin"));
856
857         final XroBuilder builder = new XroBuilder();
858         builder.setProcessingRule(false);
859         builder.setIgnore(false);
860         builder.setFlags(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Xro.Flags(true));
861         final List<Subobject> subs = Lists.newArrayList();
862         subs.add(new SubobjectBuilder().setMandatory(true).setSubobjectType(
863                 new IpPrefixCaseBuilder().setIpPrefix(
864                         new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("192.168.0.0/16"))).build()).build()).setAttribute(
865                                 Attribute.Node).build());
866         subs.add(new SubobjectBuilder().setMandatory(false).setSubobjectType(
867                 new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x1234L)).build()).build()).build());
868         builder.setSubobject(subs);
869
870         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
871         final ByteBuf buf = Unpooled.buffer();
872         parser.serializeObject(builder.build(), buf);
873         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
874
875         try {
876             parser.parseObject(new ObjectHeaderImpl(true, true), null);
877             fail();
878         } catch (final IllegalArgumentException e) {
879             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
880         }
881         try {
882             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
883             fail();
884         } catch (final IllegalArgumentException e) {
885             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
886         }
887     }
888
889     @Test
890     public void testPathKeyObject() throws Exception {
891         final PCEPPathKeyObjectParser parser = new PCEPPathKeyObjectParser(this.ctx.getEROSubobjectHandlerRegistry());
892         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPPathKeyObject.bin"));
893
894         final PathKeyBuilder builder = new PathKeyBuilder();
895         builder.setProcessingRule(true);
896         builder.setIgnore(false);
897         final List<PathKeys> list = Lists.newArrayList();
898         list.add(new PathKeysBuilder().setLoose(true).setPathKey(new PathKey(0x1234)).setPceId(
899                 new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 })).build());
900         builder.setPathKeys(list);
901
902         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
903         final ByteBuf buf = Unpooled.buffer();
904         parser.serializeObject(builder.build(), buf);
905         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
906
907         try {
908             parser.parseObject(new ObjectHeaderImpl(true, true), null);
909             fail();
910         } catch (final IllegalArgumentException e) {
911             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
912         }
913         try {
914             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
915             fail();
916         } catch (final IllegalArgumentException e) {
917             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
918         }
919     }
920
921     @Test
922     public void testObjectiveFunctionObject() throws IOException, PCEPDeserializerException {
923         final PCEPObjectiveFunctionObjectParser parser = new PCEPObjectiveFunctionObjectParser(this.tlvRegistry, this.viTlvRegistry);
924         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPObjectiveFunctionObject.1.bin"));
925
926         final OfBuilder builder = new OfBuilder();
927         builder.setProcessingRule(true);
928         builder.setIgnore(false);
929         builder.setCode(new OfId(4));
930         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.of.TlvsBuilder().build());
931
932         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
933         final ByteBuf buf = Unpooled.buffer();
934         parser.serializeObject(builder.build(), buf);
935         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
936
937         try {
938             parser.parseObject(new ObjectHeaderImpl(true, true), null);
939             fail();
940         } catch (final IllegalArgumentException e) {
941             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
942         }
943         try {
944             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
945             fail();
946         } catch (final IllegalArgumentException e) {
947             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
948         }
949     }
950
951     @Test
952     public void testGlobalConstraintsObject() throws IOException, PCEPDeserializerException {
953         final PCEPGlobalConstraintsObjectParser parser = new PCEPGlobalConstraintsObjectParser(this.tlvRegistry, this.viTlvRegistry);
954         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPGlobalConstraintsObject.1.bin"));
955
956         final GcBuilder builder = new GcBuilder();
957         builder.setProcessingRule(true);
958         builder.setIgnore(false);
959         builder.setMaxHop((short) 1);
960         builder.setMaxUtilization((short) 0);
961         builder.setMinUtilization((short) 100);
962         builder.setOverBookingFactor((short) 99);
963         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.gc.object.gc.TlvsBuilder().build());
964
965         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(true, false), result.slice(4, result.readableBytes() - 4)));
966         final ByteBuf buf = Unpooled.buffer();
967         parser.serializeObject(builder.build(), buf);
968         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
969
970         try {
971             parser.parseObject(new ObjectHeaderImpl(true, true), null);
972             fail();
973         } catch (final IllegalArgumentException e) {
974             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
975         }
976         try {
977             parser.parseObject(new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
978             fail();
979         } catch (final IllegalArgumentException e) {
980             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
981         }
982     }
983
984     @Test
985     public void testIgnoreUknownObject() throws PCEPDeserializerException {
986         final Object object = this.ctx.getObjectHandlerRegistry().parseObject(35, 1, new ObjectHeaderImpl(false, false), null);
987         assertNull(object);
988     }
989
990     @Test
991     public void testUnrecognizedObjectType() throws PCEPDeserializerException {
992         final Object object = this.ctx.getObjectHandlerRegistry().parseObject(2, 2, new ObjectHeaderImpl(true, true), null);
993         assertNotNull(object);
994         assertTrue(object instanceof UnknownObject);
995         assertEquals(PCEPErrors.UNRECOGNIZED_OBJ_TYPE, ((UnknownObject) object).getError());
996     }
997
998     @Test
999     public void testUnrecognizedObjectClass() throws PCEPDeserializerException {
1000         final Object object = this.ctx.getObjectHandlerRegistry().parseObject(35, 1, new ObjectHeaderImpl(true, true), null);
1001         assertNotNull(object);
1002         assertTrue(object instanceof UnknownObject);
1003         assertEquals(PCEPErrors.UNRECOGNIZED_OBJ_CLASS, ((UnknownObject) object).getError());
1004     }
1005
1006     @Test
1007     public void testLspaObjectSerializerDefence() throws IOException, PCEPDeserializerException {
1008         final PCEPLspaObjectParser parser = new PCEPLspaObjectParser(this.tlvRegistry, this.viTlvRegistry);
1009         final ByteBuf result = Unpooled.wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPLspaObject1LowerBounds.bin"));
1010
1011         final LspaBuilder builder = new LspaBuilder();
1012         builder.setProcessingRule(true);
1013         builder.setIgnore(true);
1014         builder.setLocalProtectionDesired(false);
1015
1016         final ByteBuf buf = Unpooled.buffer(result.readableBytes());
1017         parser.serializeObject(builder.build(), buf);
1018         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
1019     }
1020
1021     @Test
1022     public void testEmptyEroObject() throws PCEPDeserializerException {
1023         final Object object = this.ctx.getObjectHandlerRegistry().parseObject(PCEPExplicitRouteObjectParser.CLASS, PCEPExplicitRouteObjectParser.TYPE, new ObjectHeaderImpl(true, true), Unpooled.EMPTY_BUFFER);
1024         assertNotNull(object);
1025         assertTrue(object instanceof Ero);
1026         final Ero eroObject = (Ero) object;
1027         assertTrue(eroObject.getSubobject().isEmpty());
1028
1029         final ByteBuf buffer = Unpooled.buffer();
1030         this.ctx.getObjectHandlerRegistry().serializeObject(eroObject, buffer);
1031         final byte[] expected = {0x07, 0x13, 0x00, 0x04};
1032         assertArrayEquals(expected, ByteArray.getAllBytes(buffer));
1033     }
1034
1035     @Test
1036     public void testCloseObjectWithVendorInformationTlv() throws IOException, PCEPDeserializerException {
1037         final byte[] closeBytes = {
1038             0x0f, 0x10, 0x00, 0x14,
1039             0x00, 0x00, 0x00, 0x05,
1040             /* vendor-information TLV */
1041             0x00, 0x07, 0x00, 0x08,
1042             /* enterprise number */
1043             0x00, 0x00, 0x00, 0x00,
1044             /* enterprise specific information */
1045             0x00, 0x00, 0x00, 0x05
1046         };
1047         final PCEPCloseObjectParser parser = new PCEPCloseObjectParser(this.tlvRegistry, this.viTlvRegistry);
1048         final ByteBuf result = Unpooled.wrappedBuffer(closeBytes);
1049
1050         final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
1051         final VendorInformationTlv viTlv = new VendorInformationTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
1052                 .setEnterpriseSpecificInformation(esInfo).build();
1053         final CCloseBuilder builder = new CCloseBuilder();
1054         builder.setProcessingRule(false);
1055         builder.setIgnore(false);
1056         builder.setReason((short) 5);
1057         builder.setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.c.close.TlvsBuilder()
1058             .setVendorInformationTlv(Lists.newArrayList(viTlv)).build());
1059
1060         assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
1061
1062         final ByteBuf buf = Unpooled.buffer();
1063         parser.serializeObject(builder.build(), buf);
1064         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
1065     }
1066
1067     @Test
1068     public void testVendorInformationObject() throws PCEPDeserializerException {
1069         final byte[] viObjBytes = {
1070             /* vendor-information object */
1071             0x22, 0x10, 0x00, 0x0C,
1072             /* enterprise number */
1073             0x00, 0x00, 0x00, 0x00,
1074             /* enterprise specific information */
1075             0x00, 0x00, 0x00, 0x05
1076         };
1077         final TestVendorInformationObjectParser parser = new TestVendorInformationObjectParser();
1078         final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
1079         final VendorInformationObject viObj = new VendorInformationObjectBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
1080                 .setEnterpriseSpecificInformation(esInfo).build();
1081         final ByteBuf result = Unpooled.wrappedBuffer(viObjBytes);
1082
1083         assertEquals(viObj, parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4)));
1084
1085         final ByteBuf buf = Unpooled.buffer(viObjBytes.length);
1086         parser.serializeObject(viObj, buf);
1087         assertArrayEquals(result.array(), ByteArray.getAllBytes(buf));
1088     }
1089 }