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