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