Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PcepEROSubobjectParserTest.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 java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.parser.BaseParserExtensionActivator;
22 import org.opendaylight.protocol.pcep.parser.subobject.EROAsNumberSubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.EROExplicitExclusionRouteSubobjectParser;
24 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv4PrefixSubobjectParser;
25 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv6PrefixSubobjectParser;
26 import org.opendaylight.protocol.pcep.parser.subobject.EROLabelSubobjectParser;
27 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey128SubobjectParser;
28 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey32SubobjectParser;
29 import org.opendaylight.protocol.pcep.parser.subobject.EROUnnumberedInterfaceSubobjectParser;
30 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.protocol.util.Ipv6Util;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
53 import org.opendaylight.yangtools.yang.common.Uint16;
54 import org.opendaylight.yangtools.yang.common.Uint32;
55
56 public class PcepEROSubobjectParserTest {
57     private static final byte[] IP4_PREFIX_BYTES = {
58         (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
59     };
60     private static final byte[] IP6_PREFIX_BYTES = {
61         (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
62         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
63         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00
64     };
65     private static final byte[] AS_NUMBER_BYTES = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
66     private static final byte[] UNNUMBERED_BYTES = {
67         (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
68         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
69     };
70     private static final byte[] PATH_KEY32_BYTES = {
71         (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
72     };
73     private static final byte[] PATH_KEY128_BYTES = {
74         (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
75         (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
76         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
77     };
78     private static final byte[] LABEL_BYTES = {
79         (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF
80     };
81     private static final byte[] EXRS_BYTES = {
82         (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64
83     };
84
85     private SimplePCEPExtensionProviderContext ctx;
86     private BaseParserExtensionActivator act;
87
88     @Before
89     public void setUp() {
90         this.ctx = new SimplePCEPExtensionProviderContext();
91         this.act = new BaseParserExtensionActivator();
92         this.act.start(this.ctx);
93     }
94
95     @Test
96     public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
97         final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
98         final SubobjectBuilder subs = new SubobjectBuilder()
99                 .setLoose(true)
100                 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
101                     new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build())
102                     .build());
103         assertEquals(subs.build(),
104             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), true));
105         final ByteBuf buff = Unpooled.buffer();
106         parser.serializeSubobject(subs.build(), buff);
107         assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
108
109         try {
110             parser.parseSubobject(null, true);
111             fail();
112         } catch (final IllegalArgumentException e) {
113             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
114         }
115         try {
116             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
117             fail();
118         } catch (final IllegalArgumentException e) {
119             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
120         }
121     }
122
123     @Test
124     public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
125         final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
126         final SubobjectBuilder subs = new SubobjectBuilder()
127                 .setSubobjectType(new IpPrefixCaseBuilder()
128                     .setIpPrefix(new IpPrefixBuilder()
129                         .setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
130                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
131                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
132                             (byte) 0xFF, (byte) 0xFF
133                         }, 22)))
134                         .build())
135                     .build())
136                 .setLoose(false);
137         assertEquals(subs.build(),
138             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), false));
139         final ByteBuf buff = Unpooled.buffer();
140         parser.serializeSubobject(subs.build(), buff);
141         assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
142
143         try {
144             parser.parseSubobject(null, true);
145             fail();
146         } catch (final IllegalArgumentException e) {
147             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
148         }
149         try {
150             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
151             fail();
152         } catch (final IllegalArgumentException e) {
153             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
154         }
155     }
156
157     @Test
158     public void testEROAsNumberSubobject() throws PCEPDeserializerException {
159         final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
160         final SubobjectBuilder subs = new SubobjectBuilder()
161                 .setLoose(true)
162                 .setSubobjectType(new AsNumberCaseBuilder()
163                     .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
164                     .build());
165         assertEquals(subs.build(),
166             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
167         final ByteBuf buff = Unpooled.buffer();
168         parser.serializeSubobject(subs.build(), buff);
169         assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
170
171         try {
172             parser.parseSubobject(null, true);
173             fail();
174         } catch (final IllegalArgumentException e) {
175             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
176         }
177         try {
178             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
179             fail();
180         } catch (final IllegalArgumentException e) {
181             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
182         }
183     }
184
185     @Test
186     public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
187         final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
188         final SubobjectBuilder subs = new SubobjectBuilder()
189                 .setLoose(true)
190                 .setSubobjectType(new UnnumberedCaseBuilder()
191                     .setUnnumbered(new UnnumberedBuilder()
192                         .setRouterId(Uint32.valueOf(0x12345000L))
193                         .setInterfaceId(Uint32.valueOf(0xffffffffL))
194                         .build())
195                     .build());
196         assertEquals(subs.build(),
197             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
198         final ByteBuf buff = Unpooled.buffer();
199         parser.serializeSubobject(subs.build(), buff);
200         assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
201
202         try {
203             parser.parseSubobject(null, true);
204             fail();
205         } catch (final IllegalArgumentException e) {
206             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
207         }
208         try {
209             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
210             fail();
211         } catch (final IllegalArgumentException e) {
212             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
213         }
214     }
215
216     @Test
217     public void testEROPathKey32Subobject() throws PCEPDeserializerException {
218         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
219         final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true);
220         final PathKeyBuilder pBuilder = new PathKeyBuilder()
221                 .setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }))
222                 .setPathKey(new PathKey(Uint16.valueOf(4660)));
223         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
224         assertEquals(subs.build(),
225             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
226         final ByteBuf buff = Unpooled.buffer();
227         parser.serializeSubobject(subs.build(), buff);
228         assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
229
230         try {
231             parser.parseSubobject(null, true);
232             fail();
233         } catch (final IllegalArgumentException e) {
234             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
235         }
236         try {
237             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
238             fail();
239         } catch (final IllegalArgumentException e) {
240             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
241         }
242     }
243
244     @Test
245     public void testEROPathKey128Subobject() throws PCEPDeserializerException {
246         final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
247         final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true);
248         final PathKeyBuilder pBuilder = new PathKeyBuilder()
249                 .setPceId(new PceId(new byte[] {
250                     (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
251                     (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
252                     (byte) 0x00, (byte) 0x00
253                 }))
254                 .setPathKey(new PathKey(Uint16.valueOf(4660)));
255         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
256         assertEquals(subs.build(),
257             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
258         final ByteBuf buff = Unpooled.buffer();
259         parser.serializeSubobject(subs.build(), buff);
260         assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
261
262         try {
263             parser.parseSubobject(null, true);
264             fail();
265         } catch (final IllegalArgumentException e) {
266             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
267         }
268         try {
269             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
270             fail();
271         } catch (final IllegalArgumentException e) {
272             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
273         }
274     }
275
276     @Test
277     public void testEROLabelSubobject() throws Exception {
278         final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
279
280         final SubobjectBuilder subs = new SubobjectBuilder()
281                 .setLoose(true)
282                 .setSubobjectType(new LabelCaseBuilder()
283                     .setLabel(new LabelBuilder()
284                         .setUniDirectional(true)
285                         .setLabelType(new GeneralizedLabelCaseBuilder()
286                             .setGeneralizedLabel(new GeneralizedLabelBuilder()
287                                 .setGeneralizedLabel(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
288                                 .build())
289                             .build())
290                         .build())
291                     .build());
292         assertEquals(subs.build(),
293             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2)), true));
294         final ByteBuf buff = Unpooled.buffer();
295         parser.serializeSubobject(subs.build(), buff);
296         assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
297
298         try {
299             parser.parseSubobject(null, true);
300             fail();
301         } catch (final IllegalArgumentException e) {
302             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
303         }
304         try {
305             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
306             fail();
307         } catch (final IllegalArgumentException e) {
308             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
309         }
310     }
311
312     @Test
313     public void testERO_EXRSSubobject() throws Exception {
314         final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
315             this.ctx.getXROSubobjectHandlerRegistry());
316         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
317             .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
318         list.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
319             .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
320             .setMandatory(true)
321             .setSubobjectType(new AsNumberCaseBuilder()
322                 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
323                 .build())
324             .build());
325         final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true)
326                 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
327         assertEquals(subs.build(),
328             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(EXRS_BYTES, 2)), true));
329         final ByteBuf buff = Unpooled.buffer();
330         parser.serializeSubobject(subs.build(), buff);
331         assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
332
333         try {
334             parser.parseSubobject(null, true);
335             fail();
336         } catch (final IllegalArgumentException e) {
337             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
338         }
339         try {
340             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
341             fail();
342         } catch (final IllegalArgumentException e) {
343             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
344         }
345     }
346 }