Migrate Path-Key
[bgpcep.git] / rsvp / impl / src / test / java / org / opendaylight / protocol / rsvp / parser / impl / XROSubobjectParserTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.rsvp.parser.impl;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROAsNumberSubobjectParser;
18 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv4PrefixSubobjectParser;
19 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv6PrefixSubobjectParser;
20 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROPathKey128SubobjectParser;
21 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROPathKey32SubobjectParser;
22 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROSRLGSubobjectParser;
23 import org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROUnnumberedInterfaceSubobjectParser;
24 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
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.rev100924.AsNumber;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PathKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.PceId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.SrlgCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.srlg._case.SrlgBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
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 XROSubobjectParserTest {
47     private static final byte[] ip4PrefixBytes = {(byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
48         (byte) 0x16, (byte) 0x00};
49     private static final byte[] ip6PrefixBytes = {(byte) 0x82, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
50         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
51         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x01};
52     private static final byte[] srlgBytes = {(byte) 0xa2, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x00, (byte) 0x02};
53     private static final byte[] unnumberedBytes = {(byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x12, (byte) 0x34,
54         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
55     private static final byte[] asNumberBytes = {(byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64};
56     private static final byte[] pathKey32Bytes = {(byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
57         (byte) 0x50, (byte) 0x00};
58     private static final byte[] pathKey128Bytes = {(byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
59         (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
60         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
61
62     @Test
63     public void testXROIp4PrefixSubobject() throws RSVPParsingException {
64         final XROIpv4PrefixSubobjectParser parser = new XROIpv4PrefixSubobjectParser();
65         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
66         subs.setMandatory(false);
67         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Interface);
68         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
69             new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
70         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), false));
71         final ByteBuf buff = Unpooled.buffer();
72         parser.serializeSubobject(subs.build(), buff);
73         Assert.assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
74
75         try {
76             parser.parseSubobject(null, true);
77             Assert.fail();
78         } catch (final IllegalArgumentException e) {
79             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
80         }
81         try {
82             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
83             Assert.fail();
84         } catch (final IllegalArgumentException e) {
85             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
86         }
87     }
88
89     @Test
90     public void testXROIp6PrefixSubobject() throws RSVPParsingException {
91         final XROIpv6PrefixSubobjectParser parser = new XROIpv6PrefixSubobjectParser();
92         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
93         subs.setMandatory(true);
94         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Node);
95         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
96             new IpPrefixBuilder().setIpPrefix(
97                 new IpPrefix(Ipv6Util.prefixForBytes(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
98                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
99                     (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, 22))).build()).build());
100         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), true));
101         final ByteBuf buff = Unpooled.buffer();
102         parser.serializeSubobject(subs.build(), buff);
103         Assert.assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
104
105         try {
106             parser.parseSubobject(null, true);
107             Assert.fail();
108         } catch (final IllegalArgumentException e) {
109             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
110         }
111         try {
112             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
113             Assert.fail();
114         } catch (final IllegalArgumentException e) {
115             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
116         }
117     }
118
119     @Test
120     public void testXROSrlgSubobject() throws RSVPParsingException {
121         final XROSRLGSubobjectParser parser = new XROSRLGSubobjectParser();
122         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
123         subs.setMandatory(true);
124         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Srlg);
125         subs.setSubobjectType(new SrlgCaseBuilder().setSrlg(new SrlgBuilder().setSrlgId(new SrlgId(0x12345678L)).build()).build());
126         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(srlgBytes, 2)), true));
127         final ByteBuf buff = Unpooled.buffer();
128         parser.serializeSubobject(subs.build(), buff);
129         Assert.assertArrayEquals(srlgBytes, ByteArray.getAllBytes(buff));
130
131         try {
132             parser.parseSubobject(null, true);
133             Assert.fail();
134         } catch (final IllegalArgumentException e) {
135             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
136         }
137         try {
138             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
139             Assert.fail();
140         } catch (final IllegalArgumentException e) {
141             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
142         }
143     }
144
145     @Test
146     public void testXROUnnumberedSubobject() throws RSVPParsingException {
147         final XROUnnumberedInterfaceSubobjectParser parser = new XROUnnumberedInterfaceSubobjectParser();
148         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
149         subs.setMandatory(true);
150         subs.setAttribute(ExcludeRouteSubobjects.Attribute.Node);
151         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
152             new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
153         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
154         final ByteBuf buff = Unpooled.buffer();
155         parser.serializeSubobject(subs.build(), buff);
156         Assert.assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
157
158         try {
159             parser.parseSubobject(null, true);
160             Assert.fail();
161         } catch (final IllegalArgumentException e) {
162             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
163         }
164         try {
165             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
166             Assert.fail();
167         } catch (final IllegalArgumentException e) {
168             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
169         }
170     }
171
172     @Test
173     public void testXROAsNumberSubobject() throws RSVPParsingException {
174         final XROAsNumberSubobjectParser parser = new XROAsNumberSubobjectParser();
175         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
176         subs.setMandatory(true);
177         subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
178         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
179         final ByteBuf buff = Unpooled.buffer();
180         parser.serializeSubobject(subs.build(), buff);
181         Assert.assertArrayEquals(asNumberBytes, ByteArray.getAllBytes(buff));
182
183         try {
184             parser.parseSubobject(null, true);
185             Assert.fail();
186         } catch (final IllegalArgumentException e) {
187             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
188         }
189         try {
190             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
191             Assert.fail();
192         } catch (final IllegalArgumentException e) {
193             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
194         }
195     }
196
197     @Test
198     public void testXROPathKey32Subobject() throws RSVPParsingException {
199         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
200         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
201         subs.setMandatory(true);
202         final PathKeyBuilder pBuilder = new PathKeyBuilder();
203         pBuilder.setPceId(new PceId(new byte[]{(byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00}));
204         pBuilder.setPathKey(new PathKey(4660));
205         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
206         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
207         final ByteBuf buff = Unpooled.buffer();
208         parser.serializeSubobject(subs.build(), buff);
209         Assert.assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
210
211         try {
212             parser.parseSubobject(null, true);
213             Assert.fail();
214         } catch (final IllegalArgumentException e) {
215             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
216         }
217         try {
218             parser.parseSubobject(Unpooled.EMPTY_BUFFER, true);
219             Assert.fail();
220         } catch (final IllegalArgumentException e) {
221             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
222         }
223     }
224
225     @Test
226     public void testXROPathKey128Subobject() throws RSVPParsingException {
227         final XROPathKey128SubobjectParser parser128 = new XROPathKey128SubobjectParser();
228         final XROPathKey32SubobjectParser parser = new XROPathKey32SubobjectParser();
229         final SubobjectContainerBuilder subs = new SubobjectContainerBuilder();
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(), parser128.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
237         final ByteBuf buff = Unpooled.buffer();
238         parser.serializeSubobject(subs.build(), buff);
239         Assert.assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
240
241         try {
242             parser128.parseSubobject(null, true);
243             Assert.fail();
244         } catch (final IllegalArgumentException e) {
245             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
246         }
247         try {
248             parser128.parseSubobject(Unpooled.EMPTY_BUFFER, true);
249             Assert.fail();
250         } catch (final IllegalArgumentException e) {
251             Assert.assertEquals("Array of bytes is mandatory. Can't be null or empty.", e.getMessage());
252         }
253     }
254 }