Eliminate RouteDistinguisherBuilder
[bgpcep.git] / bgp / extensions / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / unicast / ipv6 / VpnIpv6NlriParserTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.l3vpn.unicast.ipv6;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReachBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreachBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.mp.unreach.nlri.WithdrawnRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.MplsLabeledVpnSubsequentAddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.l3vpn.ipv6.destination.VpnIpv6DestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
41 import org.opendaylight.yangtools.yang.common.Uint32;
42
43 public class VpnIpv6NlriParserTest {
44
45     private static final VpnIpv6NlriParser PARSER = new VpnIpv6NlriParser();
46
47     /* Reach NLRI prefix value.
48     *
49     * prefix contents:
50     * 88          <- length 136
51     * 00 16 3     <- labelValue 355
52     * 01          <- bottomBit 1
53     * 00 01       <- routeDistinguisher Type=1
54     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
55     * 01 02       <- routeDistinguisher AS=258
56     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
57     */
58     private static final byte[] REACH_NLRI = new byte[] {
59         (byte) 0x88,
60         (byte) 0x00, (byte) 0x16, (byte) 0x31,
61         0, 1, 1, 2, 3, 4, 1, 2,
62         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
63     };
64
65     /* Unreach NLRI prefix value.
66     *
67     * prefix contents:
68     * 88          <- length 136
69     * 80 00 00    <- labelValue for withdraw
70     * 00 01       <- routeDistinguisher Type=1
71     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
72     * 01 02       <- routeDistinguisher AS=258
73     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
74     */
75     private static final byte[] UNREACH_NLRI = new byte[] {
76         (byte) 0x88,
77         (byte) 0x80, (byte) 0x00, (byte) 0x00,
78         0, 1, 1, 2, 3, 4, 1, 2,
79         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
80     };
81
82     static final IpPrefix IPV6PREFIX = new IpPrefix(new Ipv6Prefix("2001:2345:5689::/48"));
83     static final List<LabelStack> LABEL_STACK = List.of(
84         new LabelStackBuilder().setLabelValue(new MplsLabel(Uint32.valueOf(355))).build());
85     static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherUtil.parseRouteDistinguisher("1.2.3.4:258");
86     static final VpnDestination IPV6_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
87             .setPrefix(IPV6PREFIX).setLabelStack(LABEL_STACK).setPathId(NON_PATH_ID)
88             .build();
89     private static final VpnDestination IPV6_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
90             .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV6PREFIX).build();
91
92     @Test
93     public void testMpReachNlri() throws BGPParsingException {
94         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder()
95             .setAfi(Ipv6AddressFamily.VALUE)
96             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE)
97             .setAdvertizedRoutes(new AdvertizedRoutesBuilder()
98                 .setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6
99                     .rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type
100                     .DestinationVpnIpv6CaseBuilder()
101                         .setVpnIpv6Destination(new VpnIpv6DestinationBuilder()
102                             .setVpnDestination(List.of(new VpnDestinationBuilder(IPV6_VPN).setPathId(null).build()))
103                             .build())
104                         .build())
105                 .build());
106
107         final MpReachNlri mpReachExpected = mpBuilder.build();
108
109         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder()
110             .setAfi(Ipv6AddressFamily.VALUE)
111             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
112         PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder, null);
113         assertEquals(mpReachExpected, testBuilder.build());
114
115         final ByteBuf output = Unpooled.buffer();
116         PARSER.serializeAttribute(new AttributesBuilder()
117             .addAugmentation(new AttributesReachBuilder().setMpReachNlri(mpReachExpected).build())
118             .build(), output);
119         assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
120     }
121
122     @Test
123     public void testMpUnreachNlri() throws BGPParsingException {
124         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder()
125             .setAfi(Ipv6AddressFamily.VALUE)
126             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE)
127             .setWithdrawnRoutes(new WithdrawnRoutesBuilder()
128                 .setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6
129                     .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
130                     .DestinationVpnIpv6CaseBuilder()
131                         .setVpnIpv6Destination(new VpnIpv6DestinationBuilder()
132                             .setVpnDestination(List.of(IPV6_VPN_WITHOUT_LABELS))
133                             .build())
134                         .build())
135                 .build());
136         final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
137
138         mpBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder()
139             .setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6
140                 .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
141                 .DestinationVpnIpv6CaseBuilder()
142                     .setVpnIpv6Destination(new VpnIpv6DestinationBuilder()
143                         .setVpnDestination(List.of(IPV6_VPN))
144                         .build())
145                     .build())
146             .build());
147         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
148
149         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder()
150             .setAfi(Ipv6AddressFamily.VALUE)
151             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
152         PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder, null);
153         assertEquals(mpUnreachExpected1, testBuilder.build());
154
155         final ByteBuf output = Unpooled.buffer();
156         PARSER.serializeAttribute(new AttributesBuilder()
157             .addAugmentation(new AttributesUnreachBuilder().setMpUnreachNlri(mpUnreachExpected2).build())
158             .build(), output);
159         assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));
160     }
161 }