2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.l3vpn.unicast.ipv4;
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;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReachBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreachBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.mp.unreach.nlri.WithdrawnRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.MplsLabeledVpnSubsequentAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.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 import org.opendaylight.yangtools.yang.common.Uint32;
43 public class VpnIpv4NlriParserTest {
45 private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
47 /* Reach NLRI prefix value.
51 * 00 16 3 <- labelValue 355
53 * 00 01 <- routeDistinguisher Type=1
54 * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
55 * 01 02 <- routeDistinguisher AS=258
56 * 22 01 16 <- prefixType IPV4=34.1.22.0/24
58 private static final byte[] REACH_NLRI = new byte[] {
60 (byte) 0x00, (byte) 0x16, (byte) 0x31,
61 0, 1, 1, 2, 3, 4, 1, 2,
62 (byte) 0x22, (byte) 0x01, (byte) 0x16,
65 /* Unreach NLRI prefix value.
69 * 80 00 00 <- labelValue for withdraw
70 * 00 01 <- routeDistinguisher Type=1
71 * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
72 * 01 02 <- routeDistinguisher AS=258
73 * 22 01 16 <- prefixType IPV4=34.1.22.0/24
75 private static final byte[] UNREACH_NLRI = new byte[] {
77 (byte) 0x80, (byte) 0x00, (byte) 0x00,
78 0, 1, 1, 2, 3, 4, 1, 2,
79 (byte) 0x22, (byte) 0x01, (byte) 0x16,
82 static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
83 static final List<LabelStack> LABEL_STACK = List.of(
84 new LabelStackBuilder().setLabelValue(new MplsLabel(Uint32.valueOf(355))).build());
85 static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder
86 .getDefaultInstance("1.2.3.4:258");
87 static final VpnDestination IPV4_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
88 .setPrefix(IPV4_PREFIX).setPathId(NON_PATH_ID).setLabelStack(LABEL_STACK).build();
89 private static final VpnDestination IPV4_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
90 .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV4_PREFIX).build();
93 public void testMpReachNlri() throws BGPParsingException {
94 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder()
95 .setAfi(Ipv4AddressFamily.VALUE)
96 .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
97 mpBuilder.setAdvertizedRoutes(
98 new AdvertizedRoutesBuilder().setDestinationType(
99 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
100 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder()
101 .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
102 List.of(new VpnDestinationBuilder(IPV4_VPN).setPathId(null)
103 .build())).build()).build()).build()).build();
105 final MpReachNlri mpReachExpected = mpBuilder.build();
107 final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder()
108 .setAfi(Ipv4AddressFamily.VALUE)
109 .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
110 PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder, null);
111 assertEquals(mpReachExpected, testBuilder.build());
113 final ByteBuf output = Unpooled.buffer();
114 PARSER.serializeAttribute(new AttributesBuilder()
115 .addAugmentation(new AttributesReachBuilder().setMpReachNlri(mpReachExpected).build())
117 assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
121 public void testMpUnreachNlri() throws BGPParsingException {
122 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder()
123 .setAfi(Ipv4AddressFamily.VALUE)
124 .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
126 mpBuilder.setWithdrawnRoutes(
127 new WithdrawnRoutesBuilder().setDestinationType(
128 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
129 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
130 .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
131 List.of(IPV4_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
132 final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
134 mpBuilder.setWithdrawnRoutes(
135 new WithdrawnRoutesBuilder().setDestinationType(
136 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
137 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
138 .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
139 List.of(IPV4_VPN)).build()).build()).build()).build();
140 final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
142 final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder()
143 .setAfi(Ipv4AddressFamily.VALUE)
144 .setSafi(MplsLabeledVpnSubsequentAddressFamily.VALUE);
145 PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder, null);
146 assertEquals(mpUnreachExpected1, testBuilder.build());
148 final ByteBuf output = Unpooled.buffer();
149 PARSER.serializeAttribute(new AttributesBuilder()
150 .addAugmentation(new AttributesUnreachBuilder().setMpUnreachNlri(mpUnreachExpected2).build())
152 assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));