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