Fix LinkstateGraphBuilder.ipv6toKey() 74/96974/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 20 Jul 2021 19:04:08 +0000 (21:04 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 21 Jul 2021 12:30:13 +0000 (14:30 +0200)
We are picking 8 most significant bytes of the address, which is
contrary to what the comment says. Add an explicit test and fixup
the implementation.

Change-Id: Ie3817bde8be858c0a5031b643994645aa61932df
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit fb9a9abe502884ceb98a07d337ca2a1c99b2d884)

bgp/topology-provider/src/main/java/org/opendaylight/bgpcep/bgp/topology/provider/LinkstateGraphBuilder.java
bgp/topology-provider/src/test/java/org/opendaylight/bgpcep/bgp/topology/provider/LinkstateGraphBuilderTest.java [new file with mode: 0644]

index c41d4b1b98cbe0df594b97cb77d9091f5e2f4116..566773677dc7a2a5ea0c0980e3a8ab180572ed43 100644 (file)
@@ -644,11 +644,11 @@ public class LinkstateGraphBuilder extends AbstractTopologyBuilder<LinkstateRout
         return Uint32.fromIntBits(IetfInetUtil.INSTANCE.ipv4AddressNoZoneBits(ifId)).toUint64();
     }
 
-    private static Uint64 ipv6ToKey(final Ipv6InterfaceIdentifier ifId) {
+    @VisibleForTesting
+    static Uint64 ipv6ToKey(final Ipv6InterfaceIdentifier ifId) {
         final byte[] ip = IetfInetUtil.INSTANCE.ipv6AddressNoZoneBytes(ifId);
-        /* Keep only the lower 64bits from the IP address */
-        final byte[] key = {ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]};
-        return Uint64.fromLongBits(ByteBuffer.wrap(key).getLong());
+        // Keep only the lower 64bits from the IP address, i.e. we skip first Long.BYTES bytes
+        return Uint64.fromLongBits(ByteBuffer.wrap(ip, Long.BYTES, Long.BYTES).getLong());
     }
 
     @Override
diff --git a/bgp/topology-provider/src/test/java/org/opendaylight/bgpcep/bgp/topology/provider/LinkstateGraphBuilderTest.java b/bgp/topology-provider/src/test/java/org/opendaylight/bgpcep/bgp/topology/provider/LinkstateGraphBuilderTest.java
new file mode 100644 (file)
index 0000000..1d742d3
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.bgpcep.bgp.topology.provider;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Ipv6InterfaceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
+
+public class LinkstateGraphBuilderTest {
+    @Test
+    public void testIpv6ToKey() {
+        assertEquals(Uint64.valueOf(0x08090A0B0C0D0E0FL),
+            LinkstateGraphBuilder.ipv6ToKey(new Ipv6InterfaceIdentifier("0001:0203:0405:0607:0809:0A0B:0C0D:0E0F")));
+    }
+}