15e2673d4324994b69cffdbd457c0488f880c843
[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.rev181109.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 import org.opendaylight.yangtools.yang.common.Uint16;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45
46 public class PCEPRROSubobjectParserTest {
47
48     private static final byte[] IP4_PREFIX_BYTES = {
49         (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01
50     };
51     private static final byte[] IP6_PREFIX_BYTES = {
52         (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
53         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
54         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02
55     };
56     private static final byte[] UNNUMBERED_BYTES = {
57         (byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34,
58         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
59     };
60     private static final byte[] PATH_KEY32_BYTES = {
61         (byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
62     };
63     private static final byte[] PATH_KEY128_BYTES = {
64         (byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
65         (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     };
68     private static final byte[] LABEL_BYTES = { 0x03, 0x08, (byte) 0x80, 0x02, 0x12, 0x00, 0x25, (byte) 0xFF };
69
70     @Test
71     public void testRROIp4PrefixSubobject() throws PCEPDeserializerException {
72         final RROIpv4PrefixSubobjectParser parser = new RROIpv4PrefixSubobjectParser();
73         final SubobjectBuilder subs = new SubobjectBuilder();
74         subs.setProtectionAvailable(true);
75         subs.setProtectionInUse(false);
76         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
77                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
78         assertEquals(
79             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2))));
80         final ByteBuf buff = Unpooled.buffer();
81         parser.serializeSubobject(subs.build(), buff);
82         assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
83
84         try {
85             parser.parseSubobject(null);
86             fail();
87         } catch (final IllegalArgumentException e) {
88             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
89         }
90         try {
91             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
92             fail();
93         } catch (final IllegalArgumentException e) {
94             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
95         }
96     }
97
98     @Test
99     public void testRROIp6PrefixSubobject() throws PCEPDeserializerException {
100         final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
101         final SubobjectBuilder subs = new SubobjectBuilder();
102         subs.setProtectionAvailable(false);
103         subs.setProtectionInUse(true);
104         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
105             new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
106                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
107                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
108             },
109             22))).build()).build());
110         assertEquals(
111             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2))));
112         final ByteBuf buff = Unpooled.buffer();
113         parser.serializeSubobject(subs.build(), buff);
114         assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
115
116         try {
117             parser.parseSubobject(null);
118             fail();
119         } catch (final IllegalArgumentException e) {
120             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
121         }
122         try {
123             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
124             fail();
125         } catch (final IllegalArgumentException e) {
126             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
127         }
128     }
129
130     @Test
131     public void testRROUnnumberedSubobject() throws PCEPDeserializerException {
132         final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
133         final SubobjectBuilder subs = new SubobjectBuilder()
134                 .setProtectionAvailable(false)
135                 .setProtectionInUse(true)
136                 .setSubobjectType(new UnnumberedCaseBuilder()
137                     .setUnnumbered(new UnnumberedBuilder()
138                         .setRouterId(Uint32.valueOf(0x12345000L)).setInterfaceId(Uint32.valueOf(0xffffffffL))
139                         .build())
140                     .build());
141         assertEquals(
142             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2))));
143         final ByteBuf buff = Unpooled.buffer();
144         parser.serializeSubobject(subs.build(), buff);
145         assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
146
147         try {
148             parser.parseSubobject(null);
149             fail();
150         } catch (final IllegalArgumentException e) {
151             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
152         }
153         try {
154             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
155             fail();
156         } catch (final IllegalArgumentException e) {
157             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
158         }
159     }
160
161     @Test
162     public void testRROPathKey32Subobject() throws PCEPDeserializerException {
163         final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
164         final SubobjectBuilder subs = new SubobjectBuilder();
165         final PathKeyBuilder pBuilder = new PathKeyBuilder();
166         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
167         pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
168         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
169         assertEquals(
170             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2))));
171         final ByteBuf buff = Unpooled.buffer();
172         parser.serializeSubobject(subs.build(), buff);
173         assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
174
175         try {
176             parser.parseSubobject(null);
177             fail();
178         } catch (final IllegalArgumentException e) {
179             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
180         }
181         try {
182             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
183             fail();
184         } catch (final IllegalArgumentException e) {
185             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
186         }
187     }
188
189     @Test
190     public void testRROPathKey128Subobject() throws PCEPDeserializerException {
191         final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
192         final SubobjectBuilder subs = new SubobjectBuilder();
193         final PathKeyBuilder pBuilder = new PathKeyBuilder();
194         pBuilder.setPceId(new PceId(new byte[] {
195             (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
196             (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
197         }));
198         pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
199         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
200         assertEquals(
201             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2))));
202         final ByteBuf buff = Unpooled.buffer();
203         RROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
204         assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
205
206         try {
207             parser.parseSubobject(null);
208             fail();
209         } catch (final IllegalArgumentException e) {
210             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
211         }
212         try {
213             parser.parseSubobject(Unpooled.EMPTY_BUFFER);
214             fail();
215         } catch (final IllegalArgumentException e) {
216             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
217         }
218     }
219
220     @Test
221     public void testRROLabelSubobject() throws Exception {
222         final SimplePCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
223         try (BaseParserExtensionActivator a = new BaseParserExtensionActivator()) {
224             a.start(ctx);
225             final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
226             final SubobjectBuilder subs = new SubobjectBuilder();
227             subs.setSubobjectType(new LabelCaseBuilder().setLabel(
228                 new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
229                     new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
230                        new GeneralizedLabelBuilder().setGeneralizedLabel(
231                             new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
232                        .build()).build()).build()).build());
233             assertEquals(
234                 subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2))));
235             final ByteBuf buff = Unpooled.buffer();
236             parser.serializeSubobject(subs.build(), buff);
237             assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
238
239             try {
240                 parser.parseSubobject(null);
241                 fail();
242             } catch (final IllegalArgumentException e) {
243                 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
244             }
245             try {
246                 parser.parseSubobject(Unpooled.EMPTY_BUFFER);
247                 fail();
248             } catch (final IllegalArgumentException e) {
249                 assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
250             }
251         }
252     }
253 }