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