2 * Copyright (c) 2016 Brocade Communications 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.ipv6;
10 import com.google.common.collect.Lists;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.Collections;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.l3vpn.ipv6.destination.VpnIpv6DestinationBuilder;
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;
42 public class VpnIpv6NlriParserTest {
44 private static final VpnIpv6NlriParser PARSER = new VpnIpv6NlriParser();
46 /* Reach NLRI prefix value.
50 * 00 16 3 <- labelValue 355
52 * 00 01 <- routeDistinguisher Type=1
53 * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
54 * 01 02 <- routeDistinguisher AS=258
55 * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
57 private static final byte[] REACH_NLRI = new byte[] {
59 (byte) 0x00, (byte) 0x16, (byte) 0x31,
60 0, 1, 1, 2, 3, 4, 1, 2,
61 (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
64 /* Unreach NLRI prefix value.
68 * 80 00 00 <- labelValue for withdraw
69 * 00 01 <- routeDistinguisher Type=1
70 * 01 02 03 04 <- routeDistinguisher IPV4=1.2.3.4
71 * 01 02 <- routeDistinguisher AS=258
72 * 20 01 23 45 56 89 <- prefixType IPV6=2001:2345:5689::/48
74 private static final byte[] UNREACH_NLRI = new byte[] {
76 (byte) 0x80, (byte) 0x00, (byte) 0x00,
77 0, 1, 1, 2, 3, 4, 1, 2,
78 (byte) 0x20, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x56, (byte) 0x89
81 static final IpPrefix IPV6PREFIX = new IpPrefix(new Ipv6Prefix("2001:2345:5689::/48"));
82 static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
83 new LabelStackBuilder().setLabelValue(new MplsLabel(355L)).build());
84 static final RouteDistinguisher DISTINGUISHER = RouteDistinguisherBuilder
85 .getDefaultInstance("1.2.3.4:258");
86 static final VpnDestination IPV6_VPN = new VpnDestinationBuilder().setRouteDistinguisher(DISTINGUISHER)
87 .setPrefix(IPV6PREFIX).setLabelStack(LABEL_STACK).build();
88 private static final VpnDestination IPV6_VPN_WITHOUT_LABELS = new VpnDestinationBuilder()
89 .setRouteDistinguisher(DISTINGUISHER).setPrefix(IPV6PREFIX).build();
92 public void testMpReachNlri() throws BGPParsingException {
93 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
94 mpBuilder.setAfi(Ipv6AddressFamily.class);
95 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
96 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
97 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationVpnIpv6CaseBuilder()
98 .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
99 Collections.singletonList(IPV6_VPN)).build()).build()).build()).build();
101 final MpReachNlri mpReachExpected = mpBuilder.build();
103 final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
104 testBuilder.setAfi(Ipv6AddressFamily.class);
105 PARSER.parseNlri(Unpooled.copiedBuffer(REACH_NLRI), testBuilder);
106 Assert.assertEquals(mpReachExpected, testBuilder.build());
108 final ByteBuf output = Unpooled.buffer();
109 PARSER.serializeAttribute(
110 new AttributesBuilder().addAugmentation(Attributes1.class,
111 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()
114 Assert.assertArrayEquals(REACH_NLRI, ByteArray.readAllBytes(output));
118 public void testMpUnreachNlri() throws BGPParsingException {
119 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
120 mpBuilder.setAfi(Ipv6AddressFamily.class);
122 mpBuilder.setWithdrawnRoutes(
123 new WithdrawnRoutesBuilder().setDestinationType(
124 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
125 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder()
126 .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
127 Collections.singletonList(IPV6_VPN_WITHOUT_LABELS)).build()).build()).build()).build();
128 final MpUnreachNlri mpUnreachExpected1 = mpBuilder.build();
130 mpBuilder.setWithdrawnRoutes(
131 new WithdrawnRoutesBuilder().setDestinationType(
132 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.ipv6.rev180329.update
133 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationVpnIpv6CaseBuilder()
134 .setVpnIpv6Destination(new VpnIpv6DestinationBuilder().setVpnDestination(
135 Collections.singletonList(IPV6_VPN)).build()).build()).build()).build();
136 final MpUnreachNlri mpUnreachExpected2 = mpBuilder.build();
138 final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
139 testBuilder.setAfi(Ipv6AddressFamily.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));