0ce2bf7dd93174a6334ec861a80cbe9bb6f933ba
[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.parser.BaseParserExtensionActivator;
21 import org.opendaylight.protocol.pcep.parser.subobject.EROAsNumberSubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.EROExplicitExclusionRouteSubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv4PrefixSubobjectParser;
24 import org.opendaylight.protocol.pcep.parser.subobject.EROIpv6PrefixSubobjectParser;
25 import org.opendaylight.protocol.pcep.parser.subobject.EROLabelSubobjectParser;
26 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey128SubobjectParser;
27 import org.opendaylight.protocol.pcep.parser.subobject.EROPathKey32SubobjectParser;
28 import org.opendaylight.protocol.pcep.parser.subobject.EROUnnumberedInterfaceSubobjectParser;
29 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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         subs.setLoose(true);
100         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
101                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
102         assertEquals(subs.build(),
103             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), true));
104         final ByteBuf buff = Unpooled.buffer();
105         parser.serializeSubobject(subs.build(), buff);
106         assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
107
108         try {
109             parser.parseSubobject(null, true);
110             fail();
111         } catch (final IllegalArgumentException e) {
112             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
113         }
114         try {
115             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
116             fail();
117         } catch (final IllegalArgumentException e) {
118             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
119         }
120     }
121
122     @Test
123     public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
124         final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
125         final SubobjectBuilder subs = new SubobjectBuilder()
126                 .setSubobjectType(new IpPrefixCaseBuilder()
127                     .setIpPrefix(new IpPrefixBuilder()
128                         .setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
129                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
130                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
131                             (byte) 0xFF, (byte) 0xFF
132                         }, 22)))
133                         .build())
134                     .build())
135                 .setLoose(false);
136         assertEquals(subs.build(),
137             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), false));
138         final ByteBuf buff = Unpooled.buffer();
139         parser.serializeSubobject(subs.build(), buff);
140         assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
141
142         try {
143             parser.parseSubobject(null, true);
144             fail();
145         } catch (final IllegalArgumentException e) {
146             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
147         }
148         try {
149             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
150             fail();
151         } catch (final IllegalArgumentException e) {
152             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
153         }
154     }
155
156     @Test
157     public void testEROAsNumberSubobject() throws PCEPDeserializerException {
158         final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
159         final SubobjectBuilder subs = new SubobjectBuilder()
160                 .setLoose(true)
161                 .setSubobjectType(new AsNumberCaseBuilder()
162                     .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
163                     .build());
164         assertEquals(subs.build(),
165             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
166         final ByteBuf buff = Unpooled.buffer();
167         parser.serializeSubobject(subs.build(), buff);
168         assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
169
170         try {
171             parser.parseSubobject(null, true);
172             fail();
173         } catch (final IllegalArgumentException e) {
174             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
175         }
176         try {
177             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
178             fail();
179         } catch (final IllegalArgumentException e) {
180             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
181         }
182     }
183
184     @Test
185     public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
186         final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
187         final SubobjectBuilder subs = new SubobjectBuilder()
188                 .setLoose(true)
189                 .setSubobjectType(new UnnumberedCaseBuilder()
190                     .setUnnumbered(new UnnumberedBuilder()
191                         .setRouterId(Uint32.valueOf(0x12345000L))
192                         .setInterfaceId(Uint32.valueOf(0xffffffffL))
193                         .build())
194                     .build());
195         assertEquals(subs.build(),
196             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
197         final ByteBuf buff = Unpooled.buffer();
198         parser.serializeSubobject(subs.build(), buff);
199         assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
200
201         try {
202             parser.parseSubobject(null, true);
203             fail();
204         } catch (final IllegalArgumentException e) {
205             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
206         }
207         try {
208             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
209             fail();
210         } catch (final IllegalArgumentException e) {
211             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
212         }
213     }
214
215     @Test
216     public void testEROPathKey32Subobject() throws PCEPDeserializerException {
217         final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
218         final SubobjectBuilder subs = new SubobjectBuilder();
219         subs.setLoose(true);
220         final PathKeyBuilder pBuilder = new PathKeyBuilder();
221         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
222         pBuilder.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();
248         subs.setLoose(true);
249         final PathKeyBuilder pBuilder = new PathKeyBuilder();
250         pBuilder.setPceId(new PceId(new byte[] {
251             (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
252             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
253             (byte) 0x00, (byte) 0x00
254         }));
255         pBuilder.setPathKey(new PathKey(Uint16.valueOf(4660)));
256         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
257         assertEquals(subs.build(),
258             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
259         final ByteBuf buff = Unpooled.buffer();
260         parser.serializeSubobject(subs.build(), buff);
261         assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
262
263         try {
264             parser.parseSubobject(null, true);
265             fail();
266         } catch (final IllegalArgumentException e) {
267             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
268         }
269         try {
270             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
271             fail();
272         } catch (final IllegalArgumentException e) {
273             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
274         }
275     }
276
277     @Test
278     public void testEROLabelSubobject() throws Exception {
279         final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
280
281         final SubobjectBuilder subs = new SubobjectBuilder()
282                 .setLoose(true)
283                 .setSubobjectType(new LabelCaseBuilder()
284                     .setLabel(new LabelBuilder()
285                         .setUniDirectional(true)
286                         .setLabelType(new GeneralizedLabelCaseBuilder()
287                             .setGeneralizedLabel(new GeneralizedLabelBuilder()
288                                 .setGeneralizedLabel(new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF })
289                                 .build())
290                             .build())
291                         .build())
292                     .build());
293         assertEquals(subs.build(),
294             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(LABEL_BYTES, 2)), true));
295         final ByteBuf buff = Unpooled.buffer();
296         parser.serializeSubobject(subs.build(), buff);
297         assertArrayEquals(LABEL_BYTES, ByteArray.getAllBytes(buff));
298
299         try {
300             parser.parseSubobject(null, true);
301             fail();
302         } catch (final IllegalArgumentException e) {
303             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
304         }
305         try {
306             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
307             fail();
308         } catch (final IllegalArgumentException e) {
309             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
310         }
311     }
312
313     @Test
314     public void testEROEXRSSubobject() throws Exception {
315         final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(
316             this.ctx.getXROSubobjectHandlerRegistry());
317         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
318             .subobjects.subobject.type.exrs._case.exrs.Exrs> list = new ArrayList<>();
319         list.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
320             .subobjects.subobject.type.exrs._case.exrs.ExrsBuilder()
321             .setMandatory(true)
322             .setSubobjectType(new AsNumberCaseBuilder()
323                 .setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(Uint32.valueOf(0x64))).build())
324                 .build())
325             .build());
326         final SubobjectBuilder subs = new SubobjectBuilder().setLoose(true)
327                 .setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
328         assertEquals(subs.build(),
329             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(EXRS_BYTES, 2)), true));
330         final ByteBuf buff = Unpooled.buffer();
331         parser.serializeSubobject(subs.build(), buff);
332         assertArrayEquals(EXRS_BYTES, ByteArray.getAllBytes(buff));
333
334         try {
335             parser.parseSubobject(null, true);
336             fail();
337         } catch (final IllegalArgumentException e) {
338             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
339         }
340         try {
341             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
342             fail();
343         } catch (final IllegalArgumentException e) {
344             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
345         }
346     }
347 }