78dc99dc9c450b7830e56ca534aa948f88dc4660
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPRROSubobjectParserTest.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 io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.junit.Test;
16 import org.opendaylight.protocol.pcep.impl.subobject.RROIpv4PrefixSubobjectParser;
17 import org.opendaylight.protocol.pcep.impl.subobject.RROIpv6PrefixSubobjectParser;
18 import org.opendaylight.protocol.pcep.impl.subobject.RROLabelSubobjectParser;
19 import org.opendaylight.protocol.pcep.impl.subobject.RROPathKey128SubobjectParser;
20 import org.opendaylight.protocol.pcep.impl.subobject.RROPathKey32SubobjectParser;
21 import org.opendaylight.protocol.pcep.impl.subobject.RROUnnumberedInterfaceSubobjectParser;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PceId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.PathKeyCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.subobject.subobject.type.path.key._case.PathKeyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.LabelCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.unnumbered._case.UnnumberedBuilder;
41
42 public class PCEPRROSubobjectParserTest {
43
44     private static final byte[] ip4PrefixBytes = { (byte) 0x01, (byte) 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
45         (byte) 0x16, (byte) 0x01 };
46     private static final byte[] ip6PrefixBytes = { (byte) 0x02, (byte) 0x14, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
47         (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
48         (byte) 0xFF, (byte) 0xFF, (byte) 0x16, (byte) 0x02 };
49     private static final byte[] unnumberedBytes = { (byte) 0x04, (byte) 0x0c, (byte) 0x02, (byte) 0x00, (byte) 0x12, (byte) 0x34,
50         (byte) 0x50, (byte) 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
51     private static final byte[] pathKey32Bytes = { (byte) 0x40, (byte) 0x08, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
52         (byte) 0x50, (byte) 0x00 };
53     private static final byte[] pathKey128Bytes = { (byte) 0x41, (byte) 0x14, (byte) 0x12, (byte) 0x34, (byte) 0x12, (byte) 0x34,
54         (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00,
55         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
56     private static final byte[] labelBytes = { 0x03, 0x08, (byte) 0x80, 0x02, 0x12, 0x00, 0x25, (byte) 0xFF };
57
58     @Test
59     public void testRROIp4PrefixSubobject() throws PCEPDeserializerException {
60         final RROIpv4PrefixSubobjectParser parser = new RROIpv4PrefixSubobjectParser();
61         final SubobjectBuilder subs = new SubobjectBuilder();
62         subs.setProtectionAvailable(true);
63         subs.setProtectionInUse(false);
64         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
65                 new IpPrefixBuilder().setIpPrefix(new IpPrefix(new Ipv4Prefix("255.255.255.255/22"))).build()).build());
66         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip4PrefixBytes, 2))));
67         final ByteBuf buff = Unpooled.buffer();
68         parser.serializeSubobject(subs.build(), buff);
69         assertArrayEquals(ip4PrefixBytes, ByteArray.getAllBytes(buff));
70     }
71
72     @Test
73     public void testRROIp6PrefixSubobject() throws PCEPDeserializerException {
74         final RROIpv6PrefixSubobjectParser parser = new RROIpv6PrefixSubobjectParser();
75         final SubobjectBuilder subs = new SubobjectBuilder();
76         subs.setProtectionAvailable(false);
77         subs.setProtectionInUse(true);
78         subs.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(
79                 new IpPrefixBuilder().setIpPrefix(
80                         new IpPrefix(Ipv6Util.prefixForBytes(new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
81                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
82                             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 22))).build()).build());
83         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(ip6PrefixBytes, 2))));
84         final ByteBuf buff = Unpooled.buffer();
85         parser.serializeSubobject(subs.build(), buff);
86         assertArrayEquals(ip6PrefixBytes, ByteArray.getAllBytes(buff));
87     }
88
89     @Test
90     public void testRROUnnumberedSubobject() throws PCEPDeserializerException {
91         final RROUnnumberedInterfaceSubobjectParser parser = new RROUnnumberedInterfaceSubobjectParser();
92         final SubobjectBuilder subs = new SubobjectBuilder();
93         subs.setProtectionAvailable(false);
94         subs.setProtectionInUse(true);
95         subs.setSubobjectType(new UnnumberedCaseBuilder().setUnnumbered(
96                 new UnnumberedBuilder().setRouterId(0x12345000L).setInterfaceId(0xffffffffL).build()).build());
97         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(unnumberedBytes, 2))));
98         final ByteBuf buff = Unpooled.buffer();
99         parser.serializeSubobject(subs.build(), buff);
100         assertArrayEquals(unnumberedBytes, ByteArray.getAllBytes(buff));
101     }
102
103     @Test
104     public void testRROPathKey32Subobject() throws PCEPDeserializerException {
105         final RROPathKey32SubobjectParser parser = new RROPathKey32SubobjectParser();
106         final SubobjectBuilder subs = new SubobjectBuilder();
107         final PathKeyBuilder pBuilder = new PathKeyBuilder();
108         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x50, (byte) 0x00 }));
109         pBuilder.setPathKey(new PathKey(4660));
110         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
111         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey32Bytes, 2))));
112         final ByteBuf buff = Unpooled.buffer();
113         parser.serializeSubobject(subs.build(), buff);
114         assertArrayEquals(pathKey32Bytes, ByteArray.getAllBytes(buff));
115     }
116
117     @Test
118     public void testRROPathKey128Subobject() throws PCEPDeserializerException {
119         final RROPathKey128SubobjectParser parser = new RROPathKey128SubobjectParser();
120         final SubobjectBuilder subs = new SubobjectBuilder();
121         final PathKeyBuilder pBuilder = new PathKeyBuilder();
122         pBuilder.setPceId(new PceId(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE,
123             (byte) 0x12, (byte) 0x34, (byte) 0x54, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
124         pBuilder.setPathKey(new PathKey(4660));
125         subs.setSubobjectType(new PathKeyCaseBuilder().setPathKey(pBuilder.build()).build());
126         assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(pathKey128Bytes, 2))));
127         final ByteBuf buff = Unpooled.buffer();
128         parser.serializeSubobject(subs.build(), buff);
129         assertArrayEquals(pathKey128Bytes, ByteArray.getAllBytes(buff));
130     }
131
132     @Test
133     public void testRROLabelSubobject() throws Exception {
134         final SimplePCEPExtensionProviderContext ctx = new SimplePCEPExtensionProviderContext();
135         try (Activator a = new Activator()) {
136             a.start(ctx);
137             final RROLabelSubobjectParser parser = new RROLabelSubobjectParser(ctx.getLabelHandlerRegistry());
138             final SubobjectBuilder subs = new SubobjectBuilder();
139             subs.setSubobjectType(new LabelCaseBuilder().setLabel(
140                     new LabelBuilder().setUniDirectional(true).setGlobal(false).setLabelType(
141                             new GeneralizedLabelCaseBuilder().setGeneralizedLabel(
142                                     new GeneralizedLabelBuilder().setGeneralizedLabel(
143                                             new byte[] { (byte) 0x12, (byte) 0x00, (byte) 0x25, (byte) 0xFF }).build()).build()).build()).build());
144             assertEquals(subs.build(), parser.parseSubobject(Unpooled.wrappedBuffer(ByteArray.cutBytes(labelBytes, 2))));
145             final ByteBuf buff = Unpooled.buffer();
146             parser.serializeSubobject(subs.build(), buff);
147             assertArrayEquals(labelBytes, ByteArray.getAllBytes(buff));
148         }
149     }
150 }