Move pcep base parser Activator to its own bundle
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPRROSubobjectParserTest.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 io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Test;
17 import org.opendaylight.protocol.pcep.parser.BaseParserExtensionActivator;
18 import org.opendaylight.protocol.pcep.parser.subobject.RROIpv4PrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.parser.subobject.RROIpv6PrefixSubobjectParser;
20 import org.opendaylight.protocol.pcep.parser.subobject.RROLabelSubobjectParser;
21 import org.opendaylight.protocol.pcep.parser.subobject.RROPathKey128SubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.RROPathKey32SubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.RROUnnumberedInterfaceSubobjectParser;
24 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
25 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.protocol.util.Ipv6Util;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.PathKeyCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
43
44 public class PCEPRROSubobjectParserTest {
45
46     private static final byte[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
47         (byte) 0x16, (byte) 0x01 };
48     private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
49         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
50         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02 };
51     private static final byte[] unnumberedBytes = { (byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34,
52         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
53     private static final byte[] pathKey32Bytes = { (byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
54         (byte) 0x50, (byte) 0x00 };
55     private static final byte[] pathKey128Bytes = { (byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
56         (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
57         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
58     private static final byte[] labelBytes = { 0x03, 0x08, (byte) 0x80, 0x02, 0x12, 0x00, 0x25, (byte) 0xFF };
59
60     @Test
61     public void testRROIp4PrefixSubobject() throws PCEPDeserializerException {
62         final RROIpv4PrefixSubobjectParser parser = new RROIpv4PrefixSubobjectParser();
63         final SubobjectBuilder subs = new SubobjectBuilder();
64         subs.setProtectionAvailable(true);
65         subs.setProtectionInUse(false);
66         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
67                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
68         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2))));
69         final ByteBuf buff = Unpooled.buffer();
70         parser.serializeSubobject(subs.build(), buff);
71         assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
72
73         try {
74             parser.parseSubobject(null);
75             fail();
76         } catch (final IllegalArgumentException e) {
77             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
78         }
79         try {
80             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
81             fail();
82         } catch (final IllegalArgumentException e) {
83             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
84         }
85     }
86
87     @Test
88     public void testRROIp6PrefixSubobject() throws PCEPDeserializerException {
89         final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
90         final SubobjectBuilder subs = new SubobjectBuilder();
91         subs.setProtectionAvailable(false);
92         subs.setProtectionInUse(true);
93         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
94                 new IpPrefixBuilder().setIpPrefix(
95                         new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
96                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
97                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
98         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2))));
99         final ByteBuf buff = Unpooled.buffer();
100         parser.serializeSubobject(subs.build(), buff);
101         assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
102
103         try {
104             parser.parseSubobject(null);
105             fail();
106         } catch (final IllegalArgumentException e) {
107             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
108         }
109         try {
110             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
111             fail();
112         } catch (final IllegalArgumentException e) {
113             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
114         }
115     }
116
117     @Test
118     public void testRROUnnumberedSubobject() throws PCEPDeserializerException {
119         final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
120         final SubobjectBuilder subs = new SubobjectBuilder();
121         subs.setProtectionAvailable(false);
122         subs.setProtectionInUse(true);
123         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
124                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
125         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2))));
126         final ByteBuf buff = Unpooled.buffer();
127         parser.serializeSubobject(subs.build(), buff);
128         assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
129
130         try {
131             parser.parseSubobject(null);
132             fail();
133         } catch (final IllegalArgumentException e) {
134             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
135         }
136         try {
137             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
138             fail();
139         } catch (final IllegalArgumentException e) {
140             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
141         }
142     }
143
144     @Test
145     public void testRROPathKey32Subobject() throws PCEPDeserializerException {
146         final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
147         final SubobjectBuilder subs = new SubobjectBuilder();
148         final PathKeyBuilder pBuilder = new PathKeyBuilder();
149         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
150         pBuilder.setPathKey(new PathKey(4660));
151         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
152         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2))));
153         final ByteBuf buff = Unpooled.buffer();
154         parser.serializeSubobject(subs.build(), buff);
155         assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
156
157         try {
158             parser.parseSubobject(null);
159             fail();
160         } catch (final IllegalArgumentException e) {
161             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
162         }
163         try {
164             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
165             fail();
166         } catch (final IllegalArgumentException e) {
167             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
168         }
169     }
170
171     @Test
172     public void testRROPathKey128Subobject() throws PCEPDeserializerException {
173         final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
174         final SubobjectBuilder subs = new SubobjectBuilder();
175         final PathKeyBuilder pBuilder = new PathKeyBuilder();
176         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
177             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
178         pBuilder.setPathKey(new PathKey(4660));
179         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
180         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2))));
181         final ByteBuf buff = Unpooled.buffer();
182         parser.serializeSubobject(subs.build(), buff);
183         assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
184
185         try {
186             parser.parseSubobject(null);
187             fail();
188         } catch (final IllegalArgumentException e) {
189             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
190         }
191         try {
192             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
193             fail();
194         } catch (final IllegalArgumentException e) {
195             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
196         }
197     }
198
199     @Test
200     public void testRROLabelSubobject() throws Exception {
201         final SimplePCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
202         try (BaseParserExtensionActivator a = new BaseParserExtensionActivator()) {
203             a.start(ctx);
204             final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
205             final SubobjectBuilder subs = new SubobjectBuilder();
206             subs.setSubobjectType(new LabelCaseBuilder().setLabel(
207                     new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
208                             new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
209                                     new GeneralizedLabelBuilder().setGeneralizedLabel(
210                                             new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
211             assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(labelBytes, 2))));
212             final ByteBuf buff = Unpooled.buffer();
213             parser.serializeSubobject(subs.build(), buff);
214             assertArrayEquals(labelBytes, ByteArray.getAllBytes(buff));
215
216             try {
217                 parser.parseSubobject(null);
218                 fail();
219             } catch (final IllegalArgumentException e) {
220                 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
221             }
222             try {
223                 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
224                 fail();
225             } catch (final IllegalArgumentException e) {
226                 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
227             }
228         }
229     }
230 }