6de11fd1b2783f13c45cece0a225b17370689331
[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.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.rev130715.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.LabelStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.LabelStackBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlriBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev171207.l3vpn.ipv6.destination.VpnIpv6DestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestination;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
40
41 /**
42  * @author Kevin Wang
43  */
44 public class VpnIpv6NlriParserTest {
45
46     private static final VpnIpv6NlriParser PARSER = new VpnIpv6NlriParser();
47
48     /* Reach NLRI prefix value.
49     *
50     * prefix contents:
51     * 88          <- length 136
52     * 00 16 3     <- labelValue 355
53     * 01          <- bottomBit 1
54     * 00 01       <- routeDistinguisher Type=1
55     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
56     * 01 02       <- routeDistinguisher AS=258
57     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
58     */
59     private static final byte[] REACH_NLRI = new byte[] {
60         (byte) 0x88,
61         (byte) 0x00, (byte) 0x16, (byte) 0x31,
62         0, 1, 1, 2, 3, 4, 1, 2,
63         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
64     };
65
66     /* Unreach NLRI prefix value.
67     *
68     * prefix contents:
69     * 88          <- length 136
70     * 80 00 00    <- labelValue for withdraw
71     * 00 01       <- routeDistinguisher Type=1
72     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
73     * 01 02       <- routeDistinguisher AS=258
74     * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
75     */
76     private static final byte[] UNREACH_NLRI = new byte[] {
77         (byte) 0x88,
78         (byte) 0x80, (byte) 0x00, (byte) 0x00,
79         0, 1, 1, 2, 3, 4, 1, 2,
80         (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
81     };
82
83     static final IpPrefix IPv6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:2345:5689::/48"));
84     static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
85         new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
86     static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder.getDefaultInstance("1.2.3.4:258");
87     static final VpnDestination IPV6_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER).setPrefix(IPv6_PREFIX).setLabelStack(LABEL_STACK).build();
88     private static final VpnDestination IPV6_VPN_WITHOUT_LABELS = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER).setPrefix(IPv6_PREFIX).build();
89
90     @Test
91     public void testMpReachNlri() throws BGPParsingException {
92         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
93         mpBuilder.setAfi(Ipv6AddressFamily.class);
94         mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
95             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv6CaseBuilder().setVpnIpv6Destination(
96                 new VpnIpv6DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV6_VPN)).build()
97             ).build()
98             ).build()
99         ).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.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder().setVpnIpv6Destination(
125                     new VpnIpv6DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV6_VPN_WITHOUT_LABELS)).build()
126                 ).build()
127             ).build()
128         ).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.ipv6.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder().setVpnIpv6Destination(
134                     new VpnIpv6DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV6_VPN)).build()
135                 ).build()
136             ).build()
137         ).build();
138         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
139
140         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
141         testBuilder.setAfi(Ipv6AddressFamily.class);
142         PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder);
143         Assert.assertEquals(mpUnreachExpected1, testBuilder.build());
144
145         final ByteBuf output = Unpooled.buffer();
146         PARSER.serializeAttribute(
147             new AttributesBuilder().addAugmentation(Attributes2.class,
148                 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build()
149             ).build(), output
150         );
151         Assert.assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));
152     }
153 }