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.ipv4;
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;
41 public class VpnIpv4NlriParserTest {
43 private static final VpnIpv4NlriParser PARSER = new VpnIpv4NlriParser();
45 /* Reach NLRI prefix value.
49 * 00 16 3 <- labelValue 355
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
56 private static final byte[] REACH_NLRI = new byte[] {
58 (byte) 0x00, (byte) 0x16, (byte) 0x31,
59 0, 1, 1, 2, 3, 4, 1, 2,
60 (byte) 0x22, (byte) 0x01, (byte) 0x16,
63 /* Unreach NLRI prefix value.
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
73 private static final byte[] UNREACH_NLRI = new byte[] {
75 (byte) 0x80, (byte) 0x00, (byte) 0x00,
76 0, 1, 1, 2, 3, 4, 1, 2,
77 (byte) 0x22, (byte) 0x01, (byte) 0x16,
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();
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()
99 final MpReachNlri mpReachExpected = mpBuilder.build();
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());
106 final ByteBuf output = Unpooled.buffer();
107 PARSER.serializeAttribute(
108 new AttributesBuilder().addAugmentation(Attributes1.class,
109 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
112 Assert.assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
116 public void testMpUnreachNlri() throws BGPParsingException {
117 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
118 mpBuilder.setAfi(Ipv4AddressFamily.class);
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()
127 final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
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()
136 final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
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());
143 final ByteBuf output = Unpooled.buffer();
144 PARSER.serializeAttribute(
145 new AttributesBuilder().addAugmentation(Attributes2.class,
146 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected2).build()
149 Assert.assertArrayEquals(UNREACH_NLRI, ByteArray.readAllBytes(output));