Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / l3vpn / src / test / java / org / opendaylight / protocol / bgp / l3vpn / 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.ipv6;
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.Ipv6Prefix;
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.Ipv6AddressFamily;
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.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
42 public class VpnIpv6NlriParserTest {
43
44     private static final VpnIpv6NlriParser PARSER = new VpnIpv6NlriParser();
45
46     /* Reach NLRI prefix value.
47     *
48     * prefix contents:
49     * 88          <- length 136
50     * 00 16 3     <- labelValue 355
51     * 01          <- 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     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
56     */
57     private static final byte[] REACH_NLRI = new byte[] {
58         (byte) 0x88,
59         (byte) 0x00, (byte) 0x16, (byte) 0x31,
60         0, 1, 1, 2, 3, 4, 1, 2,
61         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
62     };
63
64     /* Unreach NLRI prefix value.
65     *
66     * prefix contents:
67     * 88          <- length 136
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     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
73     */
74     private static final byte[] UNREACH_NLRI = new byte[] {
75         (byte) 0x88,
76         (byte) 0x80, (byte) 0x00, (byte) 0x00,
77         0, 1, 1, 2, 3, 4, 1, 2,
78         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
79     };
80
81     static final IpPrefix IPV6PREFIX = new IpPrefix(new Ipv6Prefix("2001:2345:5689::/48"));
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 IPV6_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
87             .setPrefix(IPV6PREFIX).setLabelStack(LABEL_STACK).build();
88     private static final VpnDestination IPV6_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
89             .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV6PREFIX).build();
90
91     @Test
92     public void testMpReachNlri() throws BGPParsingException {
93         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
94         mpBuilder.setAfi(Ipv6AddressFamily.class);
95         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
96             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
97                     .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv6CaseBuilder()
98                     .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
99                             Collections.singletonList(IPV6_VPN)).build()).build()).build()).build();
100
101         final MpReachNlri mpReachExpected = mpBuilder.build();
102
103         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
104         testBuilder.setAfi(Ipv6AddressFamily.class);
105         PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder);
106         Assert.assertEquals(mpReachExpected, testBuilder.build());
107
108         final ByteBuf output = Unpooled.buffer();
109         PARSER.serializeAttribute(
110             new AttributesBuilder().addAugmentation(Attributes1.class,
111                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
112             ).build(), output
113         );
114         Assert.assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
115     }
116
117     @Test
118     public void testMpUnreachNlri() throws BGPParsingException {
119         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
120         mpBuilder.setAfi(Ipv6AddressFamily.class);
121
122         mpBuilder.setWithdrawnRoutes(
123             new WithdrawnRoutesBuilder().setDestinationType(
124                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
125                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder()
126                         .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
127                                 Collections.singletonList(IPV6_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
128         final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
129
130         mpBuilder.setWithdrawnRoutes(
131             new WithdrawnRoutesBuilder().setDestinationType(
132                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
133                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder()
134                         .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
135                                 Collections.singletonList(IPV6_VPN)).build()).build()).build()).build();
136         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
137
138         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
139         testBuilder.setAfi(Ipv6AddressFamily.class);
140         PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder);
141         Assert.assertEquals(mpUnreachExpected1, testBuilder.build());
142
143         final ByteBuf output = Unpooled.buffer();
144         PARSER.serializeAttribute(
145             new AttributesBuilder().addAugmentation(Attributes2.class,
146                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build()
147             ).build(), output
148         );
149         Assert.assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));
150     }
151 }