Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PcepXROSubobjectParserTest.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.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.parser.subobject.XROAsNumberSubobjectParser;
19 import org.opendaylight.protocol.pcep.parser.subobject.XROIpv4PrefixSubobjectParser;
20 import org.opendaylight.protocol.pcep.parser.subobject.XROIpv6PrefixSubobjectParser;
21 import org.opendaylight.protocol.pcep.parser.subobject.XROPathKey128SubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.XROPathKey32SubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.XROSrlgSubobjectParser;
24 import org.opendaylight.protocol.pcep.parser.subobject.XROUnnumberedInterfaceSubobjectParser;
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.AsNumber;
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.exclude.route.object.xro.SubobjectBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
45 import org.opendaylight.yangtools.yang.common.Uint16;
46 import org.opendaylight.yangtools.yang.common.Uint32;
47
48 public class PcepXROSubobjectParserTest {
49
50     private static final byte[] IP4_PREFIX_BYTES = {
51         (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
52     };
53     private static final byte[] IP6_PREFIX_BYTES = {
54         (byte) 0x82, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
55         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
56         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01
57     };
58     private static final byte[] SRLG_BYTES = {
59         (byte) 0xa2, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x00, (byte) 0x02
60     };
61     private static final byte[] UNNUMBERED_BYTES = {
62         (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
63         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
64     };
65     private static final byte[] AS_NUMBER_BYTES = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
66     private static final byte[] PATH_KEY32_BYTES = {
67         (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
68     };
69     private static final byte[] PATH_KEY128_BYTES = {
70         (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
71         (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
72         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
73     };
74
75     @Test
76     public void testXROIp4PrefixSubobject() throws PCEPDeserializerException {
77         final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
78         final SubobjectBuilder subs = new SubobjectBuilder()
79                 .setMandatory(false)
80                 .setAttribute(Attribute.Interface)
81                 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
82                     new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build())
83                     .build());
84         assertEquals(subs.build(), parser.parseSubobject(
85             Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), false));
86         final ByteBuf buff = Unpooled.buffer();
87         parser.serializeSubobject(subs.build(), buff);
88         assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
89
90         try {
91             parser.parseSubobject(null, true);
92             fail();
93         } catch (final IllegalArgumentException e) {
94             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
95         }
96         try {
97             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
98             fail();
99         } catch (final IllegalArgumentException e) {
100             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
101         }
102     }
103
104     @Test
105     public void testXROIp6PrefixSubobject() throws PCEPDeserializerException {
106         final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
107         final SubobjectBuilder subs = new SubobjectBuilder()
108                 .setMandatory(true)
109                 .setAttribute(Attribute.Node)
110                 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
111                     new IpPrefixBuilder().setIpPrefix(
112                         new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
113                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
114                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
115                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
116                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
117                         }, 22)))
118                 .build()).build());
119         assertEquals(
120             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), true));
121         final ByteBuf buff = Unpooled.buffer();
122         parser.serializeSubobject(subs.build(), buff);
123         assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
124
125         try {
126             parser.parseSubobject(null, true);
127             fail();
128         } catch (final IllegalArgumentException e) {
129             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
130         }
131         try {
132             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
133             fail();
134         } catch (final IllegalArgumentException e) {
135             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
136         }
137     }
138
139     @Test
140     public void testXROSrlgSubobject() throws PCEPDeserializerException {
141         final XROSrlgSubobjectParser parser = new XROSrlgSubobjectParser();
142         final SubobjectBuilder subs = new SubobjectBuilder()
143                 .setMandatory(true)
144                 .setAttribute(Attribute.Srlg)
145                 .setSubobjectType(new SrlgCaseBuilder()
146                     .setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(Uint32.valueOf(0x12345678L))).build())
147                     .build());
148         assertEquals(subs.build(),
149             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(SRLG_BYTES, 2)), true));
150         final ByteBuf buff = Unpooled.buffer();
151         parser.serializeSubobject(subs.build(), buff);
152         assertArrayEquals(SRLG_BYTES, ByteArray.getAllBytes(buff));
153
154         try {
155             parser.parseSubobject(null, true);
156             fail();
157         } catch (final IllegalArgumentException e) {
158             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
159         }
160         try {
161             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
162             fail();
163         } catch (final IllegalArgumentException e) {
164             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
165         }
166     }
167
168     @Test
169     public void testXROUnnumberedSubobject() throws PCEPDeserializerException {
170         final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
171         final SubobjectBuilder subs = new SubobjectBuilder()
172                 .setMandatory(true)
173                 .setAttribute(Attribute.Node)
174                 .setSubobjectType(new UnnumberedCaseBuilder()
175                     .setUnnumbered(new UnnumberedBuilder()
176                         .setRouterId(Uint32.valueOf(0x12345000L))
177                         .setInterfaceId(Uint32.valueOf(0xffffffffL))
178                         .build())
179                     .build());
180         assertEquals(
181             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
182         final ByteBuf buff = Unpooled.buffer();
183         parser.serializeSubobject(subs.build(), buff);
184         assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
185
186         try {
187             parser.parseSubobject(null, true);
188             fail();
189         } catch (final IllegalArgumentException e) {
190             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
191         }
192         try {
193             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
194             fail();
195         } catch (final IllegalArgumentException e) {
196             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
197         }
198     }
199
200     @Test
201     public void testXROAsNumberSubobject() throws PCEPDeserializerException {
202         final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
203         final SubobjectBuilder subs = new SubobjectBuilder()
204                 .setMandatory(true)
205                 .setSubobjectType(new AsNumberCaseBuilder()
206                     .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
207                     .build());
208         assertEquals(subs.build(),
209             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
210         final ByteBuf buff = Unpooled.buffer();
211         parser.serializeSubobject(subs.build(), buff);
212         assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
213
214         try {
215             parser.parseSubobject(null, true);
216             fail();
217         } catch (final IllegalArgumentException e) {
218             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
219         }
220         try {
221             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
222             fail();
223         } catch (final IllegalArgumentException e) {
224             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
225         }
226     }
227
228     @Test
229     public void testXROPathKey32Subobject() throws PCEPDeserializerException {
230         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
231         final PathKeyBuilder pBuilder = new PathKeyBuilder()
232                 .setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }))
233                 .setPathKey(new PathKey(Uint16.valueOf(4660)));
234         final SubobjectBuilder subs = new SubobjectBuilder()
235                 .setMandatory(true)
236                 .setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
237         assertEquals(
238             subs.build(),parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
239         final ByteBuf buff = Unpooled.buffer();
240         parser.serializeSubobject(subs.build(), buff);
241         assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
242
243         try {
244             parser.parseSubobject(null, true);
245             fail();
246         } catch (final IllegalArgumentException e) {
247             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
248         }
249         try {
250             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
251             fail();
252         } catch (final IllegalArgumentException e) {
253             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
254         }
255     }
256
257     @Test
258     public void testXROPathKey128Subobject() throws PCEPDeserializerException {
259         final XROPathKey128SubobjectParser parser = new XROPathKey128SubobjectParser();
260         final PathKeyBuilder pBuilder = new PathKeyBuilder()
261                 .setPceId(new PceId(new byte[] {
262                     (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
263                     (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
264                     (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
265                     (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
266                 }))
267                 .setPathKey(new PathKey(Uint16.valueOf(4660)));
268         final SubobjectBuilder subs = new SubobjectBuilder()
269                 .setMandatory(true)
270                 .setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
271         assertEquals(
272             subs.build(),
273             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
274         final ByteBuf buff = Unpooled.buffer();
275         XROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
276         assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
277
278         try {
279             parser.parseSubobject(null, true);
280             fail();
281         } catch (final IllegalArgumentException e) {
282             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
283         }
284         try {
285             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
286             fail();
287         } catch (final IllegalArgumentException e) {
288             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
289         }
290     }
291 }