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