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