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