Convert rsvp-spi to OSGi DS
[bgpcep.git] / rsvp / impl / src / test / java / org / opendaylight / protocol / rsvp / parser / impl / EROSubobjectParserTest.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROAsNumberSubobjectParser;
20 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROExplicitExclusionRouteSubobjectParser;
21 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv4PrefixSubobjectParser;
22 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv6PrefixSubobjectParser;
23 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROLabelSubobjectParser;
24 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROPathKey128SubobjectParser;
25 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROPathKey32SubobjectParser;
26 import org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROUnnumberedInterfaceSubobjectParser;
27 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
28 import org.opendaylight.protocol.rsvp.parser.spi.pojo.SimpleRSVPExtensionProviderContext;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.opendaylight.protocol.util.Ipv6Util;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
51 import org.opendaylight.yangtools.yang.common.Uint16;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53
54 public class EROSubobjectParserTest {
55     private static final byte[] IP_4_PREFIX_BYTES = {(byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
56         (byte) 0xFF, (byte) 0x16, (byte) 0x00};
57     private static final byte[] IP_6_PREFIX_BYTES = {(byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
58         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
59         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00};
60     private static final byte[] AS_NUMBER_BYTES = {(byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64};
61     private static final byte[] UNNUMBERED_BYTES = {(byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12,
62         (byte) 0x34, (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
63     private static final byte[] PATH_KEY_32_BYTES = {(byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12,
64         (byte) 0x34, (byte) 0x50, (byte) 0x00};
65     private static final byte[] PATH_KEY_128_BYTES = {(byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12,
66         (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34,
67         (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
68     private static final byte[] LABEL_BYTES = {(byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12,
69         (byte) 0x00, (byte) 0x25, (byte) 0xFF};
70     private static final byte[] EXRS_BYTES = {(byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00,
71         (byte) 0x64};
72
73     private SimpleRSVPExtensionProviderContext ctx;
74     private RSVPActivator act;
75
76     @Before
77     public void setUp() {
78         this.ctx = new SimpleRSVPExtensionProviderContext();
79         this.act = new RSVPActivator();
80         this.act.start(this.ctx);
81     }
82
83     @Test
84     public void testEROIp4PrefixSubobject() throws RSVPParsingException {
85         final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
86         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
87         subs.setLoose(true);
88         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(new IpPrefixBuilder().setIpPrefix(
89             new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
90         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
91             ByteArray.cutBytes(IP_4_PREFIX_BYTES, 2)), true));
92         final ByteBuf buff = Unpooled.buffer();
93         parser.serializeSubobject(subs.build(), buff);
94         Assert.assertArrayEquals(IP_4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
95
96         try {
97             parser.parseSubobject(null, true);
98             Assert.fail();
99         } catch (final IllegalArgumentException e) {
100             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
101         }
102         try {
103             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
104             Assert.fail();
105         } catch (final IllegalArgumentException e) {
106             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
107         }
108     }
109
110     @Test
111     public void testEROIp6PrefixSubobject() throws RSVPParsingException {
112         final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
113         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
114         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
115             new IpPrefixBuilder().setIpPrefix(
116                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
117                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
118                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, 22))).build()).build());
119         subs.setLoose(false);
120         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP_6_PREFIX_BYTES,
121             2)), false));
122         final ByteBuf buff = Unpooled.buffer();
123         parser.serializeSubobject(subs.build(), buff);
124         Assert.assertArrayEquals(IP_6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
125
126         try {
127             parser.parseSubobject(null, true);
128             Assert.fail();
129         } catch (final IllegalArgumentException e) {
130             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
131         }
132         try {
133             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
134             Assert.fail();
135         } catch (final IllegalArgumentException e) {
136             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
137         }
138     }
139
140     @Test
141     public void testEROAsNumberSubobject() throws RSVPParsingException {
142         final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
143         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder()
144                 .setLoose(true)
145                 .setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(
146                     new AsNumber(Uint32.valueOf(0x64))).build()).build());
147         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
148             ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
149         final ByteBuf buff = Unpooled.buffer();
150         parser.serializeSubobject(subs.build(), buff);
151         Assert.assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
152
153         try {
154             parser.parseSubobject(null, true);
155             Assert.fail();
156         } catch (final IllegalArgumentException e) {
157             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
158         }
159         try {
160             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
161             Assert.fail();
162         } catch (final IllegalArgumentException e) {
163             Assert.assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
164         }
165     }
166
167     @Test
168     public void testEROUnnumberedSubobject() throws RSVPParsingException {
169         final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
170         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder()
171                 .setLoose(true)
172                 .setSubobjectType(new UnnumberedCaseBuilder()
173                     .setUnnumbered(new UnnumberedBuilder()
174                         .setRouterId(Uint32.valueOf(0x12345000L))
175                         .setInterfaceId(Uint32.valueOf(0xffffffffL))
176                         .build())
177                     .build());
178         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES,
179             2)), true));
180         final ByteBuf buff = Unpooled.buffer();
181         parser.serializeSubobject(subs.build(), buff);
182         Assert.assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
183
184         try {
185             parser.parseSubobject(null, true);
186             Assert.fail();
187         } catch (final IllegalArgumentException e) {
188             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
189         }
190         try {
191             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
192             Assert.fail();
193         } catch (final IllegalArgumentException e) {
194             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
195         }
196     }
197
198     @Test
199     public void testEROPathKey32Subobject() throws RSVPParsingException {
200         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
201         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
202         subs.setLoose(true);
203         final PathKeyBuilder pBuilder = new PathKeyBuilder();
204         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00}));
205         pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
206         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
207         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY_32_BYTES,
208             2)), true));
209         final ByteBuf buff = Unpooled.buffer();
210         parser.serializeSubobject(subs.build(), buff);
211         Assert.assertArrayEquals(PATH_KEY_32_BYTES, ByteArray.getAllBytes(buff));
212
213         try {
214             parser.parseSubobject(null, true);
215             Assert.fail();
216         } catch (final IllegalArgumentException e) {
217             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
218         }
219         try {
220             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
221             Assert.fail();
222         } catch (final IllegalArgumentException e) {
223             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
224         }
225     }
226
227     @Test
228     public void testEROPathKey128Subobject() throws RSVPParsingException {
229         final EROPathKey128SubobjectParser parser128 = new EROPathKey128SubobjectParser();
230         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
231         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
232         subs.setLoose(true);
233         final PathKeyBuilder pBuilder = new PathKeyBuilder();
234         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A,
235             (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00,
236             (byte) 0x00, (byte) 0x00, (byte) 0x00}));
237         pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
238         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
239         assertEquals(subs.build(), parser128.parseSubobject(Unpooled.wrappedBuffer(
240             ByteArray.cutBytes(PATH_KEY_128_BYTES, 2)), true));
241         final ByteBuf buff = Unpooled.buffer();
242         parser.serializeSubobject(subs.build(), buff);
243         Assert.assertArrayEquals(PATH_KEY_128_BYTES, ByteArray.getAllBytes(buff));
244
245         try {
246             parser128.parseSubobject(null, true);
247             Assert.fail();
248         } catch (final IllegalArgumentException e) {
249             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
250         }
251         try {
252             parser128.parseSubobject(Unpooled.EMPTY_BUFFER, true);
253             Assert.fail();
254         } catch (final IllegalArgumentException e) {
255             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
256         }
257     }
258
259     @Test
260     public void testEROLabelSubobject() throws Exception {
261         final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
262
263         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
264         subs.setLoose(true);
265         subs.setSubobjectType(new LabelCaseBuilder().setLabel(
266             new LabelBuilder().setUniDirectional(true).setLabelType(
267                 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
268                     new GeneralizedLabelBuilder().setGeneralizedLabel(
269                         new byte[]{(byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF}).build()).build()).build())
270             .build());
271         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
272             ByteArray.cutBytes(LABEL_BYTES, 2)), true));
273         final ByteBuf buff = Unpooled.buffer();
274         parser.serializeSubobject(subs.build(), buff);
275         Assert.assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
276
277         try {
278             parser.parseSubobject(null, true);
279             Assert.fail();
280         } catch (final IllegalArgumentException e) {
281             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
282         }
283         try {
284             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
285             Assert.fail();
286         } catch (final IllegalArgumentException e) {
287             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
288         }
289     }
290
291     @Test
292     public void testEROExrsSubobject() throws Exception {
293         final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
294             this.ctx.getXROSubobjectHandlerRegistry());
295         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
296         subs.setLoose(true);
297         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
298             .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
299         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
300             .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder builder =
301             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp
302             .rev150820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
303         builder.setMandatory(true);
304         builder.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(
305             new AsNumber(Uint32.valueOf(0x64))).build()).build());
306         list.add(builder.build());
307         subs.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
308         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(
309             ByteArray.cutBytes(EXRS_BYTES, 2)), true));
310         final ByteBuf buff = Unpooled.buffer();
311         parser.serializeSubobject(subs.build(), buff);
312         Assert.assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
313
314         try {
315             parser.parseSubobject(null, true);
316             Assert.fail();
317         } catch (final IllegalArgumentException e) {
318             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
319         }
320         try {
321             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
322             Assert.fail();
323         } catch (final IllegalArgumentException e) {
324             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
325         }
326     }
327 }