Handle trailing bits under L3vpn mcast 05/73305/2
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Thu, 21 Jun 2018 10:00:56 +0000 (12:00 +0200)
committerClaudio David Gasparini <claudio.gasparini@pantheon.tech>
Thu, 21 Jun 2018 11:57:33 +0000 (11:57 +0000)
deserialization.

Change-Id: Ib46b269ec0d6138a0234e4478e7d62420814a3bd
JIRA: BGPCEP-800
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bgp/extensions/l3vpn/src/main/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializer.java
bgp/extensions/l3vpn/src/test/java/org/opendaylight/protocol/bgp/l3vpn/mcast/L3vpnMcastIpv4RIBSupportTest.java
bgp/extensions/l3vpn/src/test/java/org/opendaylight/protocol/bgp/l3vpn/mcast/L3vpnMcastIpv6RIBSupportTest.java
bgp/extensions/l3vpn/src/test/java/org/opendaylight/protocol/bgp/l3vpn/mcast/nlri/L3vpnMcastNlriSerializerTest.java

index 6c7bdb30cfe79bd3c59a76ca5abcaf879c91c178..691f47189e375297c291a12a52743723c82491dd 100644 (file)
@@ -8,7 +8,7 @@
 
 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;
@@ -16,6 +16,7 @@ import java.util.ArrayList;
 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;
@@ -35,13 +36,20 @@ public final class L3vpnMcastNlriSerializer {
                 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;
     }
@@ -49,20 +57,20 @@ public final class L3vpnMcastNlriSerializer {
     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);
         }
     }
 }
index 8966a4c7825e04f25a02cb077934b42620372cab..68b479d7bc5be917745846583ffcfc82ebdced74 100644 (file)
@@ -57,7 +57,7 @@ public class L3vpnMcastIpv4RIBSupportTest extends AbstractRIBSupportTest<L3vpnMc
     private static final IpPrefix IPV4_PREFIX = new IpPrefix(new Ipv4Prefix("34.1.22.0/24"));
     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdIpv4("1.2.3.4:258"));
     private static final L3vpnMcastRouteKey ROUTE_KEY
-            = new L3vpnMcastRouteKey(PATH_ID, "IAABAQIDBAECIgEWABgAAAA=");
+            = new L3vpnMcastRouteKey(PATH_ID, "IAABAQIDBAECGCIBFgAAAAA=");
     private static final L3vpnMcastRoute ROUTE = new L3vpnMcastRouteBuilder()
             .setRouteKey(ROUTE_KEY.getRouteKey())
             .setPathId(ROUTE_KEY.getPathId())
index 81d0a5c723b1a90dcf5654809a086111e8fbc56c..1421c7d716d3765ba68b03c6c047e68b26a2d971 100644 (file)
@@ -57,7 +57,7 @@ public class L3vpnMcastIpv6RIBSupportTest extends AbstractRIBSupportTest<L3vpnMc
     private static final IpPrefix IPV6_PREFIX = new IpPrefix(new Ipv6Prefix("2001:db8:1:1::/64"));
     private static final RouteDistinguisher RD = new RouteDistinguisher(new RdIpv4("1.2.3.4:258"));
     private static final L3vpnMcastRouteKey ROUTE_KEY
-            = new L3vpnMcastRouteKey(PATH_ID, "gAABAQIDBAECIAENuAABAAEAAAAAAAAAAEAAAAAAAAAA");
+            = new L3vpnMcastRouteKey(PATH_ID, "gAABAQIDBAECQCABDbgAAQABAAAAAAAAAA==");
     private static final L3vpnMcastRoute ROUTE = new L3vpnMcastRouteBuilder()
             .setRouteKey(ROUTE_KEY.getRouteKey())
             .setPathId(ROUTE_KEY.getPathId())
index 2e441c1c6486d272bda723c673c6ae566863989b..bc07e9e4c7f42b48ac89c4fa70e65a230f195ece 100644 (file)
@@ -9,37 +9,73 @@
 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