f11ccd1904b861b410df42872bb80b8d1424d844
[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.parser.subobject.XROAsNumberSubobjectParser;
18 import org.opendaylight.protocol.pcep.parser.subobject.XROIpv4PrefixSubobjectParser;
19 import org.opendaylight.protocol.pcep.parser.subobject.XROIpv6PrefixSubobjectParser;
20 import org.opendaylight.protocol.pcep.parser.subobject.XROPathKey128SubobjectParser;
21 import org.opendaylight.protocol.pcep.parser.subobject.XROPathKey32SubobjectParser;
22 import org.opendaylight.protocol.pcep.parser.subobject.XROSRLGSubobjectParser;
23 import org.opendaylight.protocol.pcep.parser.subobject.XROUnnumberedInterfaceSubobjectParser;
24 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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
46 public class PCEPXROSubobjectParserTest {
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) 0x00
50     };
51     private static final byte[] IP6_PREFIX_BYTES = {
52         (byte) 0x82, (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) 0x01
55     };
56     private static final byte[] SRLG_BYTES = {
57         (byte) 0xa2, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x00, (byte) 0x02
58     };
59     private static final byte[] UNNUMBERED_BYTES = {
60         (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00,
61         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
62     };
63     private static final byte[] AS_NUMBER_BYTES = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
64     private static final byte[] PATH_KEY32_BYTES = {
65         (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00
66     };
67     private static final byte[] PATH_KEY128_BYTES = {
68         (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
69         (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
70         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
71     };
72
73     @Test
74     public void testXROIp4PrefixSubobject() throws PCEPDeserializerException {
75         final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
76         final SubobjectBuilder subs = new SubobjectBuilder();
77         subs.setMandatory(false);
78         subs.setAttribute(Attribute.Interface);
79         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
80             new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
81         assertEquals(subs.build(), parser.parseSubobject(
82             Unpooled.wrappedBuffer(ByteArray.cutBytes(IP4_PREFIX_BYTES, 2)), false));
83         final ByteBuf buff = Unpooled.buffer();
84         parser.serializeSubobject(subs.build(), buff);
85         assertArrayEquals(IP4_PREFIX_BYTES, ByteArray.getAllBytes(buff));
86
87         try {
88             parser.parseSubobject(null, true);
89             fail();
90         } catch (final IllegalArgumentException e) {
91             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
92         }
93         try {
94             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
95             fail();
96         } catch (final IllegalArgumentException e) {
97             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
98         }
99     }
100
101     @Test
102     public void testXROIp6PrefixSubobject() throws PCEPDeserializerException {
103         final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
104         final SubobjectBuilder subs = new SubobjectBuilder();
105         subs.setMandatory(true);
106         subs.setAttribute(Attribute.Node);
107         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
108             new IpPrefixBuilder().setIpPrefix(
109                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[] {
110                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
111                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
112                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
113                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
114                 }, 22)))
115             .build()).build());
116         assertEquals(
117             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(IP6_PREFIX_BYTES, 2)), true));
118         final ByteBuf buff = Unpooled.buffer();
119         parser.serializeSubobject(subs.build(), buff);
120         assertArrayEquals(IP6_PREFIX_BYTES, ByteArray.getAllBytes(buff));
121
122         try {
123             parser.parseSubobject(null, true);
124             fail();
125         } catch (final IllegalArgumentException e) {
126             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
127         }
128         try {
129             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
130             fail();
131         } catch (final IllegalArgumentException e) {
132             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
133         }
134     }
135
136     @Test
137     public void testXROSrlgSubobject() throws PCEPDeserializerException {
138         final XROSRLGSubobjectParser parser = new XROSRLGSubobjectParser();
139         final SubobjectBuilder subs = new SubobjectBuilder();
140         subs.setMandatory(true);
141         subs.setAttribute(Attribute.Srlg);
142         subs.setSubobjectType(
143             new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build());
144         assertEquals(subs.build(),
145             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(SRLG_BYTES, 2)), true));
146         final ByteBuf buff = Unpooled.buffer();
147         parser.serializeSubobject(subs.build(), buff);
148         assertArrayEquals(SRLG_BYTES, ByteArray.getAllBytes(buff));
149
150         try {
151             parser.parseSubobject(null, true);
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, true);
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 testXROUnnumberedSubobject() throws PCEPDeserializerException {
166         final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
167         final SubobjectBuilder subs = new SubobjectBuilder();
168         subs.setMandatory(true);
169         subs.setAttribute(Attribute.Node);
170         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
171                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
172         assertEquals(
173             subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(UNNUMBERED_BYTES, 2)), true));
174         final ByteBuf buff = Unpooled.buffer();
175         parser.serializeSubobject(subs.build(), buff);
176         assertArrayEquals(UNNUMBERED_BYTES, ByteArray.getAllBytes(buff));
177
178         try {
179             parser.parseSubobject(null, true);
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, true);
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 testXROAsNumberSubobject() throws PCEPDeserializerException {
194         final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
195         final SubobjectBuilder subs = new SubobjectBuilder();
196         subs.setMandatory(true);
197         subs.setSubobjectType(
198             new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build())
199                 .build());
200         assertEquals(subs.build(),
201             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(AS_NUMBER_BYTES, 2)), true));
202         final ByteBuf buff = Unpooled.buffer();
203         parser.serializeSubobject(subs.build(), buff);
204         assertArrayEquals(AS_NUMBER_BYTES, ByteArray.getAllBytes(buff));
205
206         try {
207             parser.parseSubobject(null, true);
208             fail();
209         } catch (final IllegalArgumentException e) {
210             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
211         }
212         try {
213             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
214             fail();
215         } catch (final IllegalArgumentException e) {
216             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
217         }
218     }
219
220     @Test
221     public void testXROPathKey32Subobject() throws PCEPDeserializerException {
222         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
223         final SubobjectBuilder subs = new SubobjectBuilder();
224         subs.setMandatory(true);
225         final PathKeyBuilder pBuilder = new PathKeyBuilder();
226         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
227         pBuilder.setPathKey(new PathKey(4660));
228         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
229         assertEquals(subs.build(),
230             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY32_BYTES, 2)), true));
231         final ByteBuf buff = Unpooled.buffer();
232         parser.serializeSubobject(subs.build(), buff);
233         assertArrayEquals(PATH_KEY32_BYTES, ByteArray.getAllBytes(buff));
234
235         try {
236             parser.parseSubobject(null, true);
237             fail();
238         } catch (final IllegalArgumentException e) {
239             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
240         }
241         try {
242             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
243             fail();
244         } catch (final IllegalArgumentException e) {
245             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
246         }
247     }
248
249     @Test
250     public void testXROPathKey128Subobject() throws PCEPDeserializerException {
251         final XROPathKey128SubobjectParser parser = new XROPathKey128SubobjectParser();
252         final SubobjectBuilder subs = new SubobjectBuilder();
253         subs.setMandatory(true);
254         final PathKeyBuilder pBuilder = new PathKeyBuilder();
255         pBuilder.setPceId(new PceId(new byte[] {
256             (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12,
257             (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
258         }));
259         pBuilder.setPathKey(new PathKey(4660));
260         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
261         assertEquals(subs.build(),
262             parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(PATH_KEY128_BYTES, 2)), true));
263         final ByteBuf buff = Unpooled.buffer();
264         XROPathKey128SubobjectParser.serializeSubobject(subs.build(), buff);
265         assertArrayEquals(PATH_KEY128_BYTES, ByteArray.getAllBytes(buff));
266
267         try {
268             parser.parseSubobject(null, true);
269             fail();
270         } catch (final IllegalArgumentException e) {
271             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
272         }
273         try {
274             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
275             fail();
276         } catch (final IllegalArgumentException e) {
277             assertEquals("Array of bytes is mandatory. Cannot be null or empty.", e.getMessage());
278         }
279     }
280 }