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