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.rev100924.IpPrefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.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.rev160210.l3vpn.ipv4.destination.VpnIpv4DestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestination;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.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 private static final byte[] NLRI_TYPE1 = new byte[]{
47 (byte) 0x00, (byte) 0x16, (byte) 0x31,
48 0, 1, 1, 2, 3, 4, 1, 2,
49 (byte) 0x22, (byte) 0x01, (byte) 0x16,
51 private static final IpPrefix IPv4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
52 private static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
53 new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
54 private static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder.getDefaultInstance("1.2.3.4:258");
55 private static final VpnDestination VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER).setPrefix(IPv4_PREFIX).setLabelStack(LABEL_STACK).build();
58 public void testMpReachNlri() throws BGPParsingException {
59 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
60 mpBuilder.setAfi(Ipv4AddressFamily.class);
61 mpBuilder.setAdvertizedRoutes(
62 new AdvertizedRoutesBuilder().setDestinationType(
63 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(
64 new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(VPN)).build()
69 final MpReachNlri mpReachExpected = mpBuilder.build();
71 final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
72 testBuilder.setAfi(Ipv4AddressFamily.class);
73 PARSER.parseNlri(Unpooled.copiedBuffer(NLRI_TYPE1), testBuilder);
74 Assert.assertEquals(mpReachExpected, testBuilder.build());
76 final ByteBuf output = Unpooled.buffer();
77 PARSER.serializeAttribute(
78 new AttributesBuilder().addAugmentation(Attributes1.class,
79 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
82 Assert.assertArrayEquals(NLRI_TYPE1, ByteArray.readAllBytes(output));
86 public void testMpUnreachNlri() throws BGPParsingException {
87 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
88 mpBuilder.setAfi(Ipv4AddressFamily.class);
89 mpBuilder.setWithdrawnRoutes(
90 new WithdrawnRoutesBuilder().setDestinationType(
91 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv4.rev160210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv4CaseBuilder().setVpnIpv4Destination(
92 new VpnIpv4DestinationBuilder().setVpnDestination(Lists.newArrayList(VPN)).build()
97 final MpUnreachNlri mpUnreachExpected = mpBuilder.build();
99 final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
100 testBuilder.setAfi(Ipv4AddressFamily.class);
101 PARSER.parseNlri(Unpooled.copiedBuffer(NLRI_TYPE1), testBuilder);
102 Assert.assertEquals(mpUnreachExpected, testBuilder.build());
104 final ByteBuf output = Unpooled.buffer();
105 PARSER.serializeAttribute(
106 new AttributesBuilder().addAugmentation(Attributes2.class,
107 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected).build()
110 Assert.assertArrayEquals(NLRI_TYPE1, ByteArray.readAllBytes(output));