80a680fef947700ca541c876a91ebda86fcd57a2
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPEROSubobjectParserTest.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.fail;
13
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;
52
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,
68         (byte) 0xFF };
69     private static final byte[] exrsBytes = { (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
70
71     private SimplePCEPExtensionProviderContext ctx;
72     private Activator act;
73
74     @Before
75     public void setUp() {
76         this.ctx = new SimplePCEPExtensionProviderContext();
77         this.act = new Activator();
78         this.act.start(this.ctx);
79     }
80
81     @Test
82     public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
83         final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
84         final SubobjectBuilder subs = new SubobjectBuilder();
85         subs.setLoose(true);
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));
92
93         try {
94             parser.parseSubobject(null, true);
95             fail();
96         } catch (final IllegalArgumentException e) {
97             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
98         }
99         try {
100             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
101             fail();
102         } catch (final IllegalArgumentException e) {
103             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
104         }
105     }
106
107     @Test
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));
121
122         try {
123             parser.parseSubobject(null, true);
124             fail();
125         } catch (final IllegalArgumentException e) {
126             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
127         }
128         try {
129             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
130             fail();
131         } catch (final IllegalArgumentException e) {
132             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
133         }
134     }
135
136     @Test
137     public void testEROAsNumberSubobject() throws PCEPDeserializerException {
138         final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
139         final SubobjectBuilder subs = new SubobjectBuilder();
140         subs.setLoose(true);
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));
146
147         try {
148             parser.parseSubobject(null, true);
149             fail();
150         } catch (final IllegalArgumentException e) {
151             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
152         }
153         try {
154             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
155             fail();
156         } catch (final IllegalArgumentException e) {
157             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
158         }
159     }
160
161     @Test
162     public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
163         final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
164         final SubobjectBuilder subs = new SubobjectBuilder();
165         subs.setLoose(true);
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));
172
173         try {
174             parser.parseSubobject(null, true);
175             fail();
176         } catch (final IllegalArgumentException e) {
177             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
178         }
179         try {
180             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
181             fail();
182         } catch (final IllegalArgumentException e) {
183             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
184         }
185     }
186
187     @Test
188     public void testEROPathKey32Subobject() throws PCEPDeserializerException {
189         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
190         final SubobjectBuilder subs = new SubobjectBuilder();
191         subs.setLoose(true);
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));
200
201         try {
202             parser.parseSubobject(null, true);
203             fail();
204         } catch (final IllegalArgumentException e) {
205             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
206         }
207         try {
208             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
209             fail();
210         } catch (final IllegalArgumentException e) {
211             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
212         }
213     }
214
215     @Test
216     public void testEROPathKey128Subobject() throws PCEPDeserializerException {
217         final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
218         final SubobjectBuilder subs = new SubobjectBuilder();
219         subs.setLoose(true);
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));
229
230         try {
231             parser.parseSubobject(null, true);
232             fail();
233         } catch (final IllegalArgumentException e) {
234             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
235         }
236         try {
237             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
238             fail();
239         } catch (final IllegalArgumentException e) {
240             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
241         }
242     }
243
244     @Test
245     public void testEROLabelSubobject() throws Exception {
246         final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
247
248         final SubobjectBuilder subs = new SubobjectBuilder();
249         subs.setLoose(true);
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));
259
260         try {
261             parser.parseSubobject(null, true);
262             fail();
263         } catch (final IllegalArgumentException e) {
264             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
265         }
266         try {
267             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
268             fail();
269         } catch (final IllegalArgumentException e) {
270             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
271         }
272     }
273
274     @Test
275     public void testEROEXRSSubobject() throws Exception {
276         final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(this.ctx.getXROSubobjectHandlerRegistry());
277         final SubobjectBuilder subs = new SubobjectBuilder();
278         subs.setLoose(true);
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));
289
290         try {
291             parser.parseSubobject(null, true);
292             fail();
293         } catch (final IllegalArgumentException e) {
294             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
295         }
296         try {
297             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
298             fail();
299         } catch (final IllegalArgumentException e) {
300             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
301         }
302     }
303 }