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 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.Attributes1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.MplsLabeledVpnSubsequentAddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisher;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.RouteDistinguisherBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.l3vpn.ipv4.destination.VpnIpv4DestinationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
43 import org.opendaylight.yangtools.yang.common.Uint32;
45 public class VpnIpv4NlriParserTest {
47 private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
49 /* Reach NLRI prefix value.
53 * 00 16 3 <- labelValue 355
55 * 00 01 <- routeDistinguisher Type=1
56 * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
57 * 01 02 <- routeDistinguisher AS=258
58 * 22 01 16 <- prefixType IPV4=34.1.22.0/24
60 private static final byte[] REACH_NLRI = new byte[] {
62 (byte) 0x00, (byte) 0x16, (byte) 0x31,
63 0, 1, 1, 2, 3, 4, 1, 2,
64 (byte) 0x22, (byte) 0x01, (byte) 0x16,
67 /* Unreach NLRI prefix value.
71 * 80 00 00 <- labelValue for withdraw
72 * 00 01 <- routeDistinguisher Type=1
73 * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
74 * 01 02 <- routeDistinguisher AS=258
75 * 22 01 16 <- prefixType IPV4=34.1.22.0/24
77 private static final byte[] UNREACH_NLRI = new byte[] {
79 (byte) 0x80, (byte) 0x00, (byte) 0x00,
80 0, 1, 1, 2, 3, 4, 1, 2,
81 (byte) 0x22, (byte) 0x01, (byte) 0x16,
84 static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
85 static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
86 new LabelStackBuilder().setLabelValue(new MplsLabel(Uint32.valueOf(355))).build());
87 static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder
88 .getDefaultInstance("1.2.3.4:258");
89 static final VpnDestination IPV4_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
90 .setPrefix(IPV4_PREFIX).setPathId(NON_PATH_ID).setLabelStack(LABEL_STACK).build();
91 private static final VpnDestination IPV4_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
92 .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV4_PREFIX).build();
95 public void testMpReachNlri() throws BGPParsingException {
96 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
97 mpBuilder.setAfi(Ipv4AddressFamily.class);
98 mpBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
99 mpBuilder.setAdvertizedRoutes(
100 new AdvertizedRoutesBuilder().setDestinationType(
101 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
102 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder()
103 .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
104 Collections.singletonList(new VpnDestinationBuilder(IPV4_VPN).setPathId(null)
105 .build())).build()).build()).build()).build();
107 final MpReachNlri mpReachExpected = mpBuilder.build();
109 final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
110 testBuilder.setAfi(Ipv4AddressFamily.class);
111 testBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
112 PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder, null);
113 assertEquals(mpReachExpected, testBuilder.build());
115 final ByteBuf output = Unpooled.buffer();
116 PARSER.serializeAttribute(new AttributesBuilder()
117 .addAugmentation(new Attributes1Builder().setMpReachNlri(mpReachExpected).build())
119 assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
123 public void testMpUnreachNlri() throws BGPParsingException {
124 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
125 mpBuilder.setAfi(Ipv4AddressFamily.class);
126 mpBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
128 mpBuilder.setWithdrawnRoutes(
129 new WithdrawnRoutesBuilder().setDestinationType(
130 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
131 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
132 .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
133 Collections.singletonList(IPV4_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
134 final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
136 mpBuilder.setWithdrawnRoutes(
137 new WithdrawnRoutesBuilder().setDestinationType(
138 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev180329.update
139 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder()
140 .setVpnIpv4Destination(new VpnIpv4DestinationBuilder().setVpnDestination(
141 Collections.singletonList(IPV4_VPN)).build()).build()).build()).build();
142 final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
144 final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
145 testBuilder.setAfi(Ipv4AddressFamily.class);
146 testBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
147 PARSER.parseNlri(Unpooled.copiedBuffer(UNREACH_NLRI), testBuilder, null);
148 assertEquals(mpUnreachExpected1, testBuilder.build());
150 final ByteBuf output = Unpooled.buffer();
151 PARSER.serializeAttribute(new AttributesBuilder()
152 .addAugmentation(new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build())
154 assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));