package org.opendaylight.protocol.bgp.l3vpn.mcast.nlri;
-import static org.opendaylight.protocol.util.Ipv6Util.IPV6_LENGTH;
+import static org.opendaylight.protocol.util.Ipv6Util.IPV6_BITS_LENGTH;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.List;
import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
+import org.opendaylight.protocol.util.ByteBufWriteUtil;
import org.opendaylight.protocol.util.Ipv4Util;
import org.opendaylight.protocol.util.Ipv6Util;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
builder.setPathId(PathIdUtil.readPathId(nlri));
}
final int length = nlri.readUnsignedByte();
+
+ final int initialLength = nlri.readableBytes();
builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
- if (length == IPV6_LENGTH) {
+ if (length == IPV6_BITS_LENGTH) {
builder.setPrefix(new IpPrefix(Ipv6Util.prefixForByteBuf(nlri)));
} else {
builder.setPrefix(new IpPrefix(Ipv4Util.prefixForByteBuf(nlri)));
}
dests.add(builder.build());
+ int readed = initialLength - nlri.readableBytes();
+ while (readed % 8 != 0) {
+ nlri.readByte();
+ readed = initialLength - nlri.readableBytes();
+ }
}
return dests;
}
public static void serializeNlri(final List<L3vpnMcastDestination> destinationList, final ByteBuf output) {
for (final L3vpnMcastDestination dest : destinationList) {
PathIdUtil.writePathId(dest.getPathId(), output);
- ByteBuf nlriOutput = Unpooled.buffer();
- RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriOutput);
+ ByteBuf prefixBuf = Unpooled.buffer();
+ RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), prefixBuf);
final IpPrefix prefix = dest.getPrefix();
if (prefix.getIpv4Prefix() != null) {
output.writeByte(Ipv4Util.IP4_BITS_LENGTH);
- nlriOutput.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
+ ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv4Prefix(), prefixBuf);
} else {
- output.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
- nlriOutput.writeBytes(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()));
+ output.writeByte(IPV6_BITS_LENGTH);
+ ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), prefixBuf);
}
- while (nlriOutput.readableBytes() % 8 != 0) {
- nlriOutput.writeZero(1);
+ while (prefixBuf.readableBytes() % 8 != 0) {
+ prefixBuf.writeZero(1);
}
- output.writeBytes(nlriOutput);
+ output.writeBytes(prefixBuf);
}
}
}
package org.opendaylight.protocol.bgp.l3vpn.mcast.nlri;
import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
+import java.util.List;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestinationBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RdIpv4;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
+@RunWith(Parameterized.class)
public class L3vpnMcastNlriSerializerTest {
- private static final byte[] EXPECTED = new byte[]{
- (byte) 0x80,
+ private static final byte[] IPV4_EXPECTED = new byte[]{
+ 32, //length
0, 1, 1, 2, 3, 4, 1, 2, //RD
- 32, 1, 13, (byte) 0xb8, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 64, // Prefix
+ 24, (byte) 0xac, 17, 1, 0,// Prefix
+ 0, 0, 0//trailing bits
+ };
+ private static final byte[] IPV6_EXPECTED = new byte[]{
+ (byte) 0x80, //length
+ 0, 1, 1, 2, 3, 4, 1, 2, //RD
+ 64, 32, 1, 13, (byte) 0xb8, 0, 1, 0, 2,// Prefix
0, 0, 0, 0, 0, 0, 0 //trailing bits
};
private static final RouteDistinguisher RD = new RouteDistinguisher(new RdIpv4("1.2.3.4:258"));
private static final IpPrefix IPV6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/64"));
- private static final L3vpnMcastDestination MCAST_L3VPN_DESTINATION = new L3vpnMcastDestinationBuilder()
+ private static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("172.17.1.0/24"));
+ private static final L3vpnMcastDestination MCAST_IPV4_L3VPN_DESTINATION = new L3vpnMcastDestinationBuilder()
+ .setRouteDistinguisher(RD)
+ .setPrefix(IPV4_PREFIX)
+ .build();
+ private static final L3vpnMcastDestination MCAST_IPV6_L3VPN_DESTINATION = new L3vpnMcastDestinationBuilder()
.setRouteDistinguisher(RD)
.setPrefix(IPV6_PREFIX)
.build();
+ private final byte[] expectedArray;
+ private final List<L3vpnMcastDestination> destination;
+
+ public L3vpnMcastNlriSerializerTest(final byte[] expectedArray, final List<L3vpnMcastDestination> destination) {
+ this.expectedArray = expectedArray;
+ this.destination = destination;
+ }
+
+ @Parameterized.Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(new Object[][]{
+ {IPV4_EXPECTED, Collections.singletonList(MCAST_IPV4_L3VPN_DESTINATION)},
+ {IPV6_EXPECTED, Collections.singletonList(MCAST_IPV6_L3VPN_DESTINATION)},
+ });
+ }
@Test
public void testL3vpnMcastNlriSerializer() {
ByteBuf actual = Unpooled.buffer();
- L3vpnMcastNlriSerializer.serializeNlri(Collections.singletonList(MCAST_L3VPN_DESTINATION), actual);
- assertArrayEquals(EXPECTED, ByteArray.getAllBytes(actual));
+ L3vpnMcastNlriSerializer.serializeNlri(this.destination, actual);
+ assertArrayEquals(this.expectedArray, ByteArray.getAllBytes(actual));
+ assertEquals(this.destination,
+ L3vpnMcastNlriSerializer.extractDest(Unpooled.copiedBuffer(this.expectedArray), false));
}
}
\ No newline at end of file