Eliminate RouteDistinguisherBuilder
[bgpcep.git] / bgp / extensions / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / unicast / ipv4 / VpnIpv4NlriParserTest.java
1 /*
2  * Copyright (c) 2016 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.l3vpn.unicast.ipv4;
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.Ipv4Prefix;
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.Ipv4AddressFamily;
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.ipv4.rev180329.l3vpn.ipv4.destination.VpnIpv4DestinationBuilder;
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 VpnIpv4NlriParserTest {
44
45     private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
46
47     /* Reach NLRI prefix value.
48     *
49     * prefix contents:
50     * 70          <- length 112
51     * 00 16 3     <- labelValue 355
52     * 1           <- 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     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
57     */
58     private static final byte[] REACH_NLRI = new byte[] {
59         (byte) 0x70,
60         (byte) 0x00, (byte) 0x16, (byte) 0x31,
61         0, 1, 1, 2, 3, 4, 1, 2,
62         (byte) 0x22, (byte) 0x01, (byte) 0x16,
63     };
64
65     /* Unreach NLRI prefix value.
66     *
67     * prefix contents:
68     * 70          <- length 112
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     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
74     */
75     private static final byte[] UNREACH_NLRI = new byte[] {
76         (byte) 0x70,
77         (byte) 0x80, (byte) 0x00, (byte) 0x00,
78         0, 1, 1, 2, 3, 4, 1, 2,
79         (byte) 0x22, (byte) 0x01, (byte) 0x16,
80     };
81
82     static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
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 IPV4_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
87             .setPrefix(IPV4_PREFIX).setPathId(NON_PATH_ID).setLabelStack(LABEL_STACK).build();
88     private static final VpnDestination IPV4_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
89             .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV4_PREFIX).build();
90
91     @Test
92     public void testMpReachNlri() throws BGPParsingException {
93         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder()
94             .setAfi(Ipv4AddressFamily.VALUE)
95             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
96         mpBuilder.setAdvertizedRoutes(
97             new AdvertizedRoutesBuilder().setDestinationType(
98                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
99                         .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder()
100                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
101                                 List.of(new VpnDestinationBuilder(IPV4_VPN).setPathId(null)
102                                         .build())).build()).build()).build()).build();
103
104         final MpReachNlri mpReachExpected = mpBuilder.build();
105
106         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder()
107             .setAfi(Ipv4AddressFamily.VALUE)
108             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
109         PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder, null);
110         assertEquals(mpReachExpected, testBuilder.build());
111
112         final ByteBuf output = Unpooled.buffer();
113         PARSER.serializeAttribute(new AttributesBuilder()
114             .addAugmentation(new AttributesReachBuilder().setMpReachNlri(mpReachExpected).build())
115             .build(), output);
116         assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
117     }
118
119     @Test
120     public void testMpUnreachNlri() throws BGPParsingException {
121         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder()
122             .setAfi(Ipv4AddressFamily.VALUE)
123             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
124
125         mpBuilder.setWithdrawnRoutes(
126             new WithdrawnRoutesBuilder().setDestinationType(
127                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
128                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
129                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
130                                 List.of(IPV4_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
131         final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
132
133         mpBuilder.setWithdrawnRoutes(
134             new WithdrawnRoutesBuilder().setDestinationType(
135                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
136                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
137                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
138                                 List.of(IPV4_VPN)).build()).build()).build()).build();
139         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
140
141         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder()
142             .setAfi(Ipv4AddressFamily.VALUE)
143             .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
144         PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder, null);
145         assertEquals(mpUnreachExpected1, testBuilder.build());
146
147         final ByteBuf output = Unpooled.buffer();
148         PARSER.serializeAttribute(new AttributesBuilder()
149             .addAugmentation(new AttributesUnreachBuilder().setMpUnreachNlri(mpUnreachExpected2).build())
150             .build(), output);
151         assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));
152     }
153 }