Bug-3904: BGP-LU NLRI encoder/decoder
[bgpcep.git] / bgp / labeled-unicast / src / test / java / org / opendaylight / protocol / bgp / labeled / unicast / LUNlriParserTest.java
1 /*
2  * Copyright (c) 2015 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.bgp.labeled.unicast;
9
10 import com.google.common.collect.Lists;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabelValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabeledUnicastSubsequentAddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestination;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
42
43 public class LUNlriParserTest {
44
45     /*label stacks with multiple labels.
46      *
47      * label stack:
48      * 60       <- length 96
49      * 00 16 3  <- labelValue 355
50      * 0        <- etc&bottomBit 0
51      * 00 16 4  <- labelValue 356
52      * 0        <- etc&bottomBit 0
53      * 00 16 6  <- labelValue 357
54      * 1        <- bottomBit 1
55      * 22 01 16 <- prefixType IPV4=34.1.22.0/24
56      */
57     private static final byte[] LU_NLRI_IPv4 = new byte[] {
58         (byte) 0x60,
59         (byte) 0x00, (byte) 0x16, (byte) 0x30,
60         (byte) 0x00, (byte) 0x16, (byte) 0x40,
61         (byte) 0x00, (byte) 0x16, (byte) 0x51,
62         (byte) 0x22, (byte) 0x01, (byte) 0x16,
63     };
64
65     /*label stacks with multiple labels.
66      *
67      * label stack:
68      * C8       <- length 200
69      * 00 16 3  <- labelValue 355
70      * 0        <- etc&bottomBit 0
71      * 00 16 4  <- labelValue 356
72      * 0        <- etc&bottomBit 0
73      * 00 16 6  <- labelValue 357
74      * 1        <- bottomBit 1
75      * 20 01 D B8 0 1 0 2 0 0 0 0 0 0 0 0 80  <- prefixType IPV6=2001:db8:1:2::/128
76      */
77     private static final byte[] LU_NLRI_IPv6 = new byte[] {
78         (byte) 0xC8,
79         (byte) 0x00, (byte) 0x16, (byte) 0x30,
80         (byte) 0x00, (byte) 0x16, (byte) 0x40,
81         (byte) 0x00, (byte) 0x16, (byte) 0x51,
82         (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
83         (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02,
84         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
85         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
86     };
87
88     private static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
89             new LabelStackBuilder().setLabelValue(new LabelValue(355)).build(),
90             new LabelStackBuilder().setLabelValue(new LabelValue(356)).build(),
91             new LabelStackBuilder().setLabelValue(new LabelValue(357)).build());
92
93     private static final IpPrefix IPv4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
94     private static final IpPrefix IPv6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/128"));
95
96     @Test
97     public void testMpReachNlriIpv4() throws BGPParsingException {
98         final LUNlriParser parser = new LUNlriParser();
99         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
100         mpBuilder.setAfi(Ipv4AddressFamily.class);
101         mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
102         final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv4_PREFIX).setLabelStack(LABEL_STACK).build();
103         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
104                 new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
105                         new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
106                         .build())
107                         .build());
108         final MpReachNlri mpReachExpected = mpBuilder.build();
109
110         //test parser
111         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
112         testBuilder.setAfi(Ipv4AddressFamily.class);
113         parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv4), testBuilder);
114         Assert.assertEquals(mpBuilder.build(), mpReachExpected);
115
116         //test serializer
117         final ByteBuf output = Unpooled.buffer();
118         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class,
119                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()).build(), output);
120         Assert.assertArrayEquals(LU_NLRI_IPv4, ByteArray.readAllBytes(output));
121     }
122
123     @Test
124     public void testMpUnreachNlriIpv4() throws BGPParsingException {
125         final LUNlriParser parser = new LUNlriParser();
126         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
127         mpBuilder.setAfi(Ipv4AddressFamily.class);
128         mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
129         final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv4_PREFIX).setLabelStack(LABEL_STACK).build();
130         mpBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
131                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
132                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
133                         .build())
134                         .build());
135         final MpUnreachNlri mpUnreachExpected = mpBuilder.build();
136
137         //test parser
138         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
139         testBuilder.setAfi(Ipv4AddressFamily.class);
140         parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv4), testBuilder);
141         Assert.assertEquals(mpBuilder.build(), mpUnreachExpected);
142
143         //test serializer
144         final ByteBuf output = Unpooled.buffer();
145         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class,
146                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected).build()).build(), output);
147         Assert.assertArrayEquals(LU_NLRI_IPv4, ByteArray.readAllBytes(output));
148     }
149
150     @Test
151     public void testMpReachNlriIpv6() throws BGPParsingException {
152         final LUNlriParser parser = new LUNlriParser();
153         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
154         mpBuilder.setAfi(Ipv6AddressFamily.class);
155         mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
156         final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv6_PREFIX).setLabelStack(LABEL_STACK).build();
157         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
158                 new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
159                         new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
160                         .build())
161                         .build());
162         final MpReachNlri mpReachExpected = mpBuilder.build();
163
164         //test parser
165         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
166         testBuilder.setAfi(Ipv6AddressFamily.class);
167         parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv6), testBuilder);
168         Assert.assertEquals(mpBuilder.build(), mpReachExpected);
169
170         //test serializer
171         final ByteBuf output = Unpooled.buffer();
172         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class,
173                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()).build(), output);
174         Assert.assertArrayEquals(LU_NLRI_IPv6, ByteArray.readAllBytes(output));
175     }
176
177     @Test
178     public void testMpUnreachNlriIpv6() throws BGPParsingException {
179         final LUNlriParser parser = new LUNlriParser();
180         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
181         mpBuilder.setAfi(Ipv6AddressFamily.class);
182         mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
183         final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv6_PREFIX).setLabelStack(LABEL_STACK).build();
184         mpBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
185                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
186                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
187                         .build())
188                         .build());
189         final MpUnreachNlri mpUnreachExpected = mpBuilder.build();
190
191         //test parser
192         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
193         testBuilder.setAfi(Ipv6AddressFamily.class);
194         parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv6), testBuilder);
195         Assert.assertEquals(mpBuilder.build(), mpUnreachExpected);
196
197         //test serializer
198         final ByteBuf output = Unpooled.buffer();
199         parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class,
200                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected).build()).build(), output);
201         Assert.assertArrayEquals(LU_NLRI_IPv6, ByteArray.readAllBytes(output));
202     }
203 }