Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / 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.ipv4;
9
10 import com.google.common.collect.Lists;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.Collections;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
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
42 public class VpnIpv4NlriParserTest {
43
44     private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
45
46     /* Reach NLRI prefix value.
47     *
48     * prefix contents:
49     * 70          <- length 112
50     * 00 16 3     <- labelValue 355
51     * 1           <- bottomBit 1
52     * 00 01       <- routeDistinguisher Type=1
53     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
54     * 01 02       <- routeDistinguisher AS=258
55     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
56     */
57     private static final byte[] REACH_NLRI = new byte[] {
58         (byte) 0x70,
59         (byte) 0x00, (byte) 0x16, (byte) 0x31,
60         0, 1, 1, 2, 3, 4, 1, 2,
61         (byte) 0x22, (byte) 0x01, (byte) 0x16,
62     };
63
64     /* Unreach NLRI prefix value.
65     *
66     * prefix contents:
67     * 70          <- length 112
68     * 80 00 00    <- labelValue for withdraw
69     * 00 01       <- routeDistinguisher Type=1
70     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
71     * 01 02       <- routeDistinguisher AS=258
72     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
73     */
74     private static final byte[] UNREACH_NLRI = new byte[] {
75         (byte) 0x70,
76         (byte) 0x80, (byte) 0x00, (byte) 0x00,
77         0, 1, 1, 2, 3, 4, 1, 2,
78         (byte) 0x22, (byte) 0x01, (byte) 0x16,
79     };
80
81     static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
82     static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
83         new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
84     static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder
85             .getDefaultInstance("1.2.3.4:258");
86     static final VpnDestination IPV4_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
87             .setPrefix(IPV4_PREFIX).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         mpBuilder.setAfi(Ipv4AddressFamily.class);
95         mpBuilder.setAdvertizedRoutes(
96             new AdvertizedRoutesBuilder().setDestinationType(
97                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
98                         .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder()
99                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
100                                 Collections.singletonList(IPV4_VPN)).build()).build()).build()).build();
101
102         final MpReachNlri mpReachExpected = mpBuilder.build();
103
104         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
105         testBuilder.setAfi(Ipv4AddressFamily.class);
106         PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder);
107         Assert.assertEquals(mpReachExpected, testBuilder.build());
108
109         final ByteBuf output = Unpooled.buffer();
110         PARSER.serializeAttribute(
111             new AttributesBuilder().addAugmentation(Attributes1.class,
112                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
113             ).build(), output
114         );
115         Assert.assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
116     }
117
118     @Test
119     public void testMpUnreachNlri() throws BGPParsingException {
120         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
121         mpBuilder.setAfi(Ipv4AddressFamily.class);
122
123         mpBuilder.setWithdrawnRoutes(
124             new WithdrawnRoutesBuilder().setDestinationType(
125                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
126                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
127                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
128                                 Collections.singletonList(IPV4_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
129         final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
130
131         mpBuilder.setWithdrawnRoutes(
132             new WithdrawnRoutesBuilder().setDestinationType(
133                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
134                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
135                         .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
136                                 Collections.singletonList(IPV4_VPN)).build()).build()).build()).build();
137         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
138
139         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
140         testBuilder.setAfi(Ipv4AddressFamily.class);
141         PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder);
142         Assert.assertEquals(mpUnreachExpected1, testBuilder.build());
143
144         final ByteBuf output = Unpooled.buffer();
145         PARSER.serializeAttribute(
146             new AttributesBuilder().addAugmentation(Attributes2.class,
147                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build()
148             ).build(), output
149         );
150         Assert.assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));
151     }
152 }