576559ceab79ec37f5130b200c1e02033d6ccc1f
[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.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.Ipv4Prefix;
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.Ipv4AddressFamily;
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.ipv4.rev171207.l3vpn.ipv4.destination.VpnIpv4DestinationBuilder;
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 public class VpnIpv4NlriParserTest {
42
43     private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
44
45     /* Reach NLRI prefix value.
46     *
47     * prefix contents:
48     * 70          <- length 112
49     * 00 16 3     <- labelValue 355
50     * 1           <- bottomBit 1
51     * 00 01       <- routeDistinguisher Type=1
52     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
53     * 01 02       <- routeDistinguisher AS=258
54     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
55     */
56     private static final byte[] REACH_NLRI = new byte[] {
57         (byte) 0x70,
58         (byte) 0x00, (byte) 0x16, (byte) 0x31,
59         0, 1, 1, 2, 3, 4, 1, 2,
60         (byte) 0x22, (byte) 0x01, (byte) 0x16,
61     };
62
63     /* Unreach NLRI prefix value.
64     *
65     * prefix contents:
66     * 70          <- length 112
67     * 80 00 00    <- labelValue for withdraw
68     * 00 01       <- routeDistinguisher Type=1
69     * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
70     * 01 02       <- routeDistinguisher AS=258
71     * 22 01 16    <- prefixType IPV4=34.1.22.0/24
72     */
73     private static final byte[] UNREACH_NLRI = new byte[] {
74         (byte) 0x70,
75         (byte) 0x80, (byte) 0x00, (byte) 0x00,
76         0, 1, 1, 2, 3, 4, 1, 2,
77         (byte) 0x22, (byte) 0x01, (byte) 0x16,
78     };
79
80     static final IpPrefix IPv4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
81     static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
82         new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
83     static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder.getDefaultInstance("1.2.3.4:258");
84     static final VpnDestination IPV4_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER).setPrefix(IPv4_PREFIX).setLabelStack(LABEL_STACK).build();
85     private static final VpnDestination IPV4_VPN_WITHOUT_LABELS = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER).setPrefix(IPv4_PREFIX).build();
86
87     @Test
88     public void testMpReachNlri() throws BGPParsingException {
89         final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
90         mpBuilder.setAfi(Ipv4AddressFamily.class);
91         mpBuilder.setAdvertizedRoutes(
92             new AdvertizedRoutesBuilder().setDestinationType(
93                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(
94                     new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV4_VPN)).build()
95                 ).build()
96             ).build()
97         ).build();
98
99         final MpReachNlri mpReachExpected = mpBuilder.build();
100
101         final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
102         testBuilder.setAfi(Ipv4AddressFamily.class);
103         PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder);
104         Assert.assertEquals(mpReachExpected, testBuilder.build());
105
106         final ByteBuf output = Unpooled.buffer();
107         PARSER.serializeAttribute(
108             new AttributesBuilder().addAugmentation(Attributes1.class,
109                 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
110             ).build(), output
111         );
112         Assert.assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
113     }
114
115     @Test
116     public void testMpUnreachNlri() throws BGPParsingException {
117         final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
118         mpBuilder.setAfi(Ipv4AddressFamily.class);
119
120         mpBuilder.setWithdrawnRoutes(
121             new WithdrawnRoutesBuilder().setDestinationType(
122                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(
123                     new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV4_VPN_WITHOUT_LABELS)).build()
124                 ).build()
125             ).build()
126         ).build();
127         final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
128
129         mpBuilder.setWithdrawnRoutes(
130             new WithdrawnRoutesBuilder().setDestinationType(
131                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev171207.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(
132                     new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(IPV4_VPN)).build()
133                 ).build()
134             ).build()
135         ).build();
136         final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
137
138         final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
139         testBuilder.setAfi(Ipv4AddressFamily.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 }