Move pcep base parser Activator to its own bundle
[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.rev131005.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[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
49         (byte) 0x16, (byte) 0x00 };
50     private static final byte[] ip6PrefixBytes = { (byte) 0x82, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
51         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
52         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01 };
53     private static final byte[] srlgBytes = { (byte) 0xa2, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x00, (byte) 0x02 };
54     private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12, (byte) 0x34,
55         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
56     private static final byte[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
57     private static final byte[] pathKey32Bytes = { (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
58         (byte) 0x50, (byte) 0x00 };
59     private static final byte[] pathKey128Bytes = { (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
60         (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
61         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
62
63     @Test
64     public void testXROIp4PrefixSubobject() throws PCEPDeserializerException {
65         final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
66         final SubobjectBuilder subs = new SubobjectBuilder();
67         subs.setMandatory(false);
68         subs.setAttribute(Attribute.Interface);
69         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
70                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
71         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), false));
72         final ByteBuf buff = Unpooled.buffer();
73         parser.serializeSubobject(subs.build(), buff);
74         assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
75
76         try {
77             parser.parseSubobject(null, true);
78             fail();
79         } catch (final IllegalArgumentException e) {
80             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
81         }
82         try {
83             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
84             fail();
85         } catch (final IllegalArgumentException e) {
86             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
87         }
88     }
89
90     @Test
91     public void testXROIp6PrefixSubobject() throws PCEPDeserializerException {
92         final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
93         final SubobjectBuilder subs = new SubobjectBuilder();
94         subs.setMandatory(true);
95         subs.setAttribute(Attribute.Node);
96         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
97                 new IpPrefixBuilder().setIpPrefix(
98                         new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
99                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
100                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
101         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), true));
102         final ByteBuf buff = Unpooled.buffer();
103         parser.serializeSubobject(subs.build(), buff);
104         assertArrayEquals(ip6PrefixBytes, 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 testXROSrlgSubobject() throws PCEPDeserializerException {
122         final XROSRLGSubobjectParser parser = new XROSRLGSubobjectParser();
123         final SubobjectBuilder subs = new SubobjectBuilder();
124         subs.setMandatory(true);
125         subs.setAttribute(Attribute.Srlg);
126         subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build());
127         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srlgBytes, 2)), true));
128         final ByteBuf buff = Unpooled.buffer();
129         parser.serializeSubobject(subs.build(), buff);
130         assertArrayEquals(srlgBytes, ByteArray.getAllBytes(buff));
131
132         try {
133             parser.parseSubobject(null, true);
134             fail();
135         } catch (final IllegalArgumentException e) {
136             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
137         }
138         try {
139             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
140             fail();
141         } catch (final IllegalArgumentException e) {
142             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
143         }
144     }
145
146     @Test
147     public void testXROUnnumberedSubobject() throws PCEPDeserializerException {
148         final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
149         final SubobjectBuilder subs = new SubobjectBuilder();
150         subs.setMandatory(true);
151         subs.setAttribute(Attribute.Node);
152         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
153                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
154         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
155         final ByteBuf buff = Unpooled.buffer();
156         parser.serializeSubobject(subs.build(), buff);
157         assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
158
159         try {
160             parser.parseSubobject(null, true);
161             fail();
162         } catch (final IllegalArgumentException e) {
163             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
164         }
165         try {
166             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
167             fail();
168         } catch (final IllegalArgumentException e) {
169             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
170         }
171     }
172
173     @Test
174     public void testXROAsNumberSubobject() throws PCEPDeserializerException {
175         final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
176         final SubobjectBuilder subs = new SubobjectBuilder();
177         subs.setMandatory(true);
178         subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
179         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
180         final ByteBuf buff = Unpooled.buffer();
181         parser.serializeSubobject(subs.build(), buff);
182         assertArrayEquals(asNumberBytes, ByteArray.getAllBytes(buff));
183
184         try {
185             parser.parseSubobject(null, true);
186             fail();
187         } catch (final IllegalArgumentException e) {
188             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
189         }
190         try {
191             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
192             fail();
193         } catch (final IllegalArgumentException e) {
194             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
195         }
196     }
197
198     @Test
199     public void testXROPathKey32Subobject() throws PCEPDeserializerException {
200         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
201         final SubobjectBuilder subs = new SubobjectBuilder();
202         subs.setMandatory(true);
203         final PathKeyBuilder pBuilder = new PathKeyBuilder();
204         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
205         pBuilder.setPathKey(new PathKey(4660));
206         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
207         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
208         final ByteBuf buff = Unpooled.buffer();
209         parser.serializeSubobject(subs.build(), buff);
210         assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
211
212         try {
213             parser.parseSubobject(null, true);
214             fail();
215         } catch (final IllegalArgumentException e) {
216             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
217         }
218         try {
219             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
220             fail();
221         } catch (final IllegalArgumentException e) {
222             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
223         }
224     }
225
226     @Test
227     public void testXROPathKey128Subobject() throws PCEPDeserializerException {
228         final XROPathKey128SubobjectParser parser = new XROPathKey128SubobjectParser();
229         final SubobjectBuilder subs = new SubobjectBuilder();
230         subs.setMandatory(true);
231         final PathKeyBuilder pBuilder = new PathKeyBuilder();
232         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
233             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
234         pBuilder.setPathKey(new PathKey(4660));
235         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
236         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
237         final ByteBuf buff = Unpooled.buffer();
238         parser.serializeSubobject(subs.build(), buff);
239         assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
240
241         try {
242             parser.parseSubobject(null, true);
243             fail();
244         } catch (final IllegalArgumentException e) {
245             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
246         }
247         try {
248             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
249             fail();
250         } catch (final IllegalArgumentException e) {
251             assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
252         }
253     }
254 }