BUG-612 : switched ERO to ByteBuf.
[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 io.netty.buffer.Unpooled;
13
14 import java.util.List;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.protocol.concepts.Ipv6Util;
19 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.EROExplicitExclusionRouteSubobjectParser;
21 import org.opendaylight.protocol.pcep.impl.subobject.EROIpv4PrefixSubobjectParser;
22 import org.opendaylight.protocol.pcep.impl.subobject.EROIpv6PrefixSubobjectParser;
23 import org.opendaylight.protocol.pcep.impl.subobject.EROLabelSubobjectParser;
24 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKey128SubobjectParser;
25 import org.opendaylight.protocol.pcep.impl.subobject.EROPathKey32SubobjectParser;
26 import org.opendaylight.protocol.pcep.impl.subobject.EROUnnumberedInterfaceSubobjectParser;
27 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
28 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
29 import org.opendaylight.protocol.util.ByteArray;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.PathKeyCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.subobject.subobject.type.path.key._case.PathKeyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.AsNumberCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.as.number._case.AsNumberBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.ExrsCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.ExrsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
50
51 import com.google.common.collect.Lists;
52
53 public class PCEPEROSubobjectParserTest {
54         private static final byte[] ip4PrefixBytes = { (byte) 0x81, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
55                 (byte) 0x16, (byte) 0x00 };
56         private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
57                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
58                 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x00 };
59         private static final byte[] asNumberBytes = { (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
60         private static final byte[] unnumberedBytes = { (byte) 0x84, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x12, (byte) 0x34,
61                 (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
62         private static final byte[] pathKey32Bytes = { (byte) 0xc0, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
63                 (byte) 0x50, (byte) 0x00 };
64         private static final byte[] pathKey128Bytes = { (byte) 0xc1, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
65                 (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00,
66                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
67         private static final byte[] labelBytes = { (byte) 0x83, (byte) 0x08, (byte) 0x80, (byte) 0x02, (byte) 0x12, (byte) 0x00, (byte) 0x25,
68                 (byte) 0xFF };
69         private static final byte[] exrsBytes = { (byte) 0xa1, (byte) 0x06, (byte) 0xa0, (byte) 0x04, (byte) 0x00, (byte) 0x64 };
70
71         private SimplePCEPExtensionProviderContext ctx;
72         private Activator act;
73
74         @Before
75         public void setUp() {
76                 this.ctx = new SimplePCEPExtensionProviderContext();
77                 this.act = new Activator();
78                 this.act.start(this.ctx);
79         }
80
81         @Test
82         public void testEROIp4PrefixSubobject() throws PCEPDeserializerException {
83                 final EROIpv4PrefixSubobjectParser parser = new EROIpv4PrefixSubobjectParser();
84                 final SubobjectBuilder subs = new SubobjectBuilder();
85                 subs.setLoose(true);
86                 subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
87                                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
88                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2)), true));
89                 assertArrayEquals(ip4PrefixBytes, parser.serializeSubobject(subs.build()));
90         }
91
92         @Test
93         public void testEROIp6PrefixSubobject() throws PCEPDeserializerException {
94                 final EROIpv6PrefixSubobjectParser parser = new EROIpv6PrefixSubobjectParser();
95                 final SubobjectBuilder subs = new SubobjectBuilder();
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                 subs.setLoose(false);
102                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2)), false));
103                 assertArrayEquals(ip6PrefixBytes, parser.serializeSubobject(subs.build()));
104         }
105
106         @Test
107         public void testEROAsNumberSubobject() throws PCEPDeserializerException {
108                 final EROAsNumberSubobjectParser parser = new EROAsNumberSubobjectParser();
109                 final SubobjectBuilder subs = new SubobjectBuilder();
110                 subs.setLoose(true);
111                 subs.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
112                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(asNumberBytes, 2)), true));
113                 assertArrayEquals(asNumberBytes, parser.serializeSubobject(subs.build()));
114         }
115
116         @Test
117         public void testEROUnnumberedSubobject() throws PCEPDeserializerException {
118                 final EROUnnumberedInterfaceSubobjectParser parser = new EROUnnumberedInterfaceSubobjectParser();
119                 final SubobjectBuilder subs = new SubobjectBuilder();
120                 subs.setLoose(true);
121                 subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
122                                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
123                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2)), true));
124                 assertArrayEquals(unnumberedBytes, parser.serializeSubobject(subs.build()));
125         }
126
127         @Test
128         public void testEROPathKey32Subobject() throws PCEPDeserializerException {
129                 final EROPathKey32SubobjectParser parser = new EROPathKey32SubobjectParser();
130                 final SubobjectBuilder subs = new SubobjectBuilder();
131                 subs.setLoose(true);
132                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
133                 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
134                 pBuilder.setPathKey(new PathKey(4660));
135                 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
136                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2)), true));
137                 assertArrayEquals(pathKey32Bytes, parser.serializeSubobject(subs.build()));
138         }
139
140         @Test
141         public void testEROPathKey128Subobject() throws PCEPDeserializerException {
142                 final EROPathKey128SubobjectParser parser = new EROPathKey128SubobjectParser();
143                 final SubobjectBuilder subs = new SubobjectBuilder();
144                 subs.setLoose(true);
145                 final PathKeyBuilder pBuilder = new PathKeyBuilder();
146                 pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
147                                 (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
148                 pBuilder.setPathKey(new PathKey(4660));
149                 subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
150                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2)), true));
151                 assertArrayEquals(pathKey128Bytes, parser.serializeSubobject(subs.build()));
152         }
153
154         @Test
155         public void testEROLabelSubobject() throws Exception {
156                 final EROLabelSubobjectParser parser = new EROLabelSubobjectParser(this.ctx.getLabelHandlerRegistry());
157
158                 final SubobjectBuilder subs = new SubobjectBuilder();
159                 subs.setLoose(true);
160                 subs.setSubobjectType(new LabelCaseBuilder().setLabel(
161                                 new LabelBuilder().setUniDirectional(true).setLabelType(
162                                                 new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
163                                                                 new GeneralizedLabelBuilder().setGeneralizedLabel(
164                                                                                 new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
165                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(labelBytes, 2)), true));
166                 assertArrayEquals(labelBytes, parser.serializeSubobject(subs.build()));
167         }
168
169         @Test
170         public void testEROEXRSSubobject() throws Exception {
171                 final EROExplicitExclusionRouteSubobjectParser parser = new EROExplicitExclusionRouteSubobjectParser(this.ctx.getXROSubobjectHandlerRegistry());
172                 final SubobjectBuilder subs = new SubobjectBuilder();
173                 subs.setLoose(true);
174                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.Exrs> list = Lists.newArrayList();
175                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.explicit.route.subobjects.subobject.type.exrs._case.exrs.ExrsBuilder();
176                 builder.setMandatory(true);
177                 builder.setSubobjectType(new AsNumberCaseBuilder().setAsNumber(new AsNumberBuilder().setAsNumber(new AsNumber(0x64L)).build()).build());
178                 list.add(builder.build());
179                 subs.setSubobjectType(new ExrsCaseBuilder().setExrs(new ExrsBuilder().setExrs(list).build()).build());
180                 assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(exrsBytes, 2)), true));
181                 assertArrayEquals(exrsBytes, parser.serializeSubobject(subs.build()));
182         }
183 }