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.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;
47 public class VpnIpv4NlriParserTest {
49 private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
51 /* Reach NLRI prefix value.
55 * 00 16 3 <- labelValue 355
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
62 private static final byte[] REACH_NLRI = new byte[] {
64 (byte) 0x00, (byte) 0x16, (byte) 0x31,
65 0, 1, 1, 2, 3, 4, 1, 2,
66 (byte) 0x22, (byte) 0x01, (byte) 0x16,
69 /* Unreach NLRI prefix value.
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
79 private static final byte[] UNREACH_NLRI = new byte[] {
81 (byte) 0x80, (byte) 0x00, (byte) 0x00,
82 0, 1, 1, 2, 3, 4, 1, 2,
83 (byte) 0x22, (byte) 0x01, (byte) 0x16,
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();
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();
109 final MpReachNlri mpReachExpected = mpBuilder.build();
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());
117 final ByteBuf output = Unpooled.buffer();
118 PARSER.serializeAttribute(
119 new AttributesBuilder().addAugmentation(Attributes1.class,
120 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
123 assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
127 public void testMpUnreachNlri() throws BGPParsingException {
128 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
129 mpBuilder.setAfi(Ipv4AddressFamily.class);
130 mpBuilder.setSafi(MplsLabeledVpnSubsequentAddressFamily.class);
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();
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();
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());
154 final ByteBuf output = Unpooled.buffer();
155 PARSER.serializeAttribute(
156 new AttributesBuilder().addAugmentation(Attributes2.class,
157 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build()
160 assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));