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