2 * Copyright (c) 2015 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.labeled.unicast;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabelValue;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabeledUnicastSubsequentAddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestination;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
43 public class LUNlriParserTest {
45 /*label stacks with multiple labels.
49 * 00 16 3 <- labelValue 355
50 * 0 <- etc&bottomBit 0
51 * 00 16 4 <- labelValue 356
52 * 0 <- etc&bottomBit 0
53 * 00 16 6 <- labelValue 357
55 * 22 01 16 <- prefixType IPV4=34.1.22.0/24
57 private static final byte[] LU_NLRI_IPv4 = new byte[] {
59 (byte) 0x00, (byte) 0x16, (byte) 0x30,
60 (byte) 0x00, (byte) 0x16, (byte) 0x40,
61 (byte) 0x00, (byte) 0x16, (byte) 0x51,
62 (byte) 0x22, (byte) 0x01, (byte) 0x16,
65 /*label stacks with multiple labels.
69 * 00 16 3 <- labelValue 355
70 * 0 <- etc&bottomBit 0
71 * 00 16 4 <- labelValue 356
72 * 0 <- etc&bottomBit 0
73 * 00 16 6 <- labelValue 357
75 * 20 01 D B8 0 1 0 2 0 0 0 0 0 0 0 0 80 <- prefixType IPV6=2001:db8:1:2::/128
77 private static final byte[] LU_NLRI_IPv6 = new byte[] {
79 (byte) 0x00, (byte) 0x16, (byte) 0x30,
80 (byte) 0x00, (byte) 0x16, (byte) 0x40,
81 (byte) 0x00, (byte) 0x16, (byte) 0x51,
82 (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
83 (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x02,
84 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
85 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
88 private static final List<LabelStack> LABEL_STACK = Lists.newArrayList(
89 new LabelStackBuilder().setLabelValue(new LabelValue(355)).build(),
90 new LabelStackBuilder().setLabelValue(new LabelValue(356)).build(),
91 new LabelStackBuilder().setLabelValue(new LabelValue(357)).build());
93 private static final IpPrefix IPv4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
94 private static final IpPrefix IPv6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/128"));
97 public void testMpReachNlriIpv4() throws BGPParsingException {
98 final LUNlriParser parser = new LUNlriParser();
99 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
100 mpBuilder.setAfi(Ipv4AddressFamily.class);
101 mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
102 final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv4_PREFIX).setLabelStack(LABEL_STACK).build();
103 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
104 new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
105 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
108 final MpReachNlri mpReachExpected = mpBuilder.build();
111 final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
112 testBuilder.setAfi(Ipv4AddressFamily.class);
113 parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv4), testBuilder);
114 Assert.assertEquals(mpBuilder.build(), mpReachExpected);
117 final ByteBuf output = Unpooled.buffer();
118 parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class,
119 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()).build(), output);
120 Assert.assertArrayEquals(LU_NLRI_IPv4, ByteArray.readAllBytes(output));
124 public void testMpUnreachNlriIpv4() throws BGPParsingException {
125 final LUNlriParser parser = new LUNlriParser();
126 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
127 mpBuilder.setAfi(Ipv4AddressFamily.class);
128 mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
129 final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv4_PREFIX).setLabelStack(LABEL_STACK).build();
130 mpBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
131 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
132 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
135 final MpUnreachNlri mpUnreachExpected = mpBuilder.build();
138 final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
139 testBuilder.setAfi(Ipv4AddressFamily.class);
140 parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv4), testBuilder);
141 Assert.assertEquals(mpBuilder.build(), mpUnreachExpected);
144 final ByteBuf output = Unpooled.buffer();
145 parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class,
146 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected).build()).build(), output);
147 Assert.assertArrayEquals(LU_NLRI_IPv4, ByteArray.readAllBytes(output));
151 public void testMpReachNlriIpv6() throws BGPParsingException {
152 final LUNlriParser parser = new LUNlriParser();
153 final MpReachNlriBuilder mpBuilder = new MpReachNlriBuilder();
154 mpBuilder.setAfi(Ipv6AddressFamily.class);
155 mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
156 final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv6_PREFIX).setLabelStack(LABEL_STACK).build();
157 mpBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
158 new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
159 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
162 final MpReachNlri mpReachExpected = mpBuilder.build();
165 final MpReachNlriBuilder testBuilder = new MpReachNlriBuilder();
166 testBuilder.setAfi(Ipv6AddressFamily.class);
167 parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv6), testBuilder);
168 Assert.assertEquals(mpBuilder.build(), mpReachExpected);
171 final ByteBuf output = Unpooled.buffer();
172 parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes1.class,
173 new Attributes1Builder().setMpReachNlri(mpReachExpected).build()).build(), output);
174 Assert.assertArrayEquals(LU_NLRI_IPv6, ByteArray.readAllBytes(output));
178 public void testMpUnreachNlriIpv6() throws BGPParsingException {
179 final LUNlriParser parser = new LUNlriParser();
180 final MpUnreachNlriBuilder mpBuilder = new MpUnreachNlriBuilder();
181 mpBuilder.setAfi(Ipv6AddressFamily.class);
182 mpBuilder.setSafi(LabeledUnicastSubsequentAddressFamily.class);
183 final CLabeledUnicastDestination lu = new CLabeledUnicastDestinationBuilder().setPrefix(IPv6_PREFIX).setLabelStack(LABEL_STACK).build();
184 mpBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
185 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
186 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(Lists.newArrayList(lu)).build())
189 final MpUnreachNlri mpUnreachExpected = mpBuilder.build();
192 final MpUnreachNlriBuilder testBuilder = new MpUnreachNlriBuilder();
193 testBuilder.setAfi(Ipv6AddressFamily.class);
194 parser.parseNlri(Unpooled.copiedBuffer(LU_NLRI_IPv6), testBuilder);
195 Assert.assertEquals(mpBuilder.build(), mpUnreachExpected);
198 final ByteBuf output = Unpooled.buffer();
199 parser.serializeAttribute(new AttributesBuilder().addAugmentation(Attributes2.class,
200 new Attributes2Builder().setMpUnreachNlri(mpUnreachExpected).build()).build(), output);
201 Assert.assertArrayEquals(LU_NLRI_IPv6, ByteArray.readAllBytes(output));