Fixed a bug and added a function for operating on MAC addresses. 54/754/1
authorHideyuki Tai <h-tai@cd.jp.nec.com>
Wed, 31 Jul 2013 02:50:01 +0000 (22:50 -0400)
committerHideyuki Tai <h-tai@cd.jp.nec.com>
Wed, 31 Jul 2013 02:50:01 +0000 (22:50 -0400)
- Fixed a bug in longToByteArray6(byte[]) that converted a long number to a byte array in little endian form.
- Added a method to convert a byte array into a long number.

Signed-off-by: Hideyuki Tai <h-tai@cd.jp.nec.com>
opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java
opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java

index 6a3a42fbb9ba52b5414937027d53788ed5058c9e..a3f21cff3eb50ad3d062b1c381a79abf35b81f2e 100644 (file)
@@ -59,20 +59,46 @@ public abstract class NetUtils {
     }
 
     /**
-     * Converts a long to 6 bytes array for mac addresses
-     * @param addr
-     * @return
+     * Converts a 6 bytes array into a long number MAC addresses.
+     *
+     * @param ba
+     *            The 6 bytes long byte array.
+     * @return The long number.
+     *         Zero is returned if {@code ba} is {@code null} or
+     *         the length of it is not six.
      */
+    public static long byteArray6ToLong(byte[] ba) {
+        if (ba == null || ba.length != MACAddrLengthInBytes) {
+            return 0L;
+        }
+        long num = 0L;
+        int i = 0;
+        do {
+            num <<= NumBitsInAByte;
+            num |= 0xff & ba[i];
+            i++;
+        } while (i < MACAddrLengthInBytes);
+        return num;
+    }
 
+    /**
+     * Converts a long number to a 6 bytes array for MAC addresses.
+     *
+     * @param addr
+     *            The long number.
+     * @return The byte array.
+     */
     public static byte[] longToByteArray6(long addr){
-        byte[] mac = new byte[6];
-        for(int i = 0; i < 6; i++){
-            mac[i] = (byte) (addr >> (i*8));
-        }
+        byte[] mac = new byte[MACAddrLengthInBytes];
+        int i = MACAddrLengthInBytes - 1;
+        do {
+            mac[i] = (byte) addr;
+            addr >>>= NumBitsInAByte;
+            i--;
+        } while (i >= 0);
         return mac;
     }
 
-
     /**
      * Converts an integer number into a 4 bytes array
      *
index 0c7e5bb2e5b4fce7a6ec89e96d08d1903dbe4e34..b8bc6fb4470901c0761df18e51482489c7e4325b 100644 (file)
@@ -65,6 +65,94 @@ public class NetUtilsTest {
                 .byteArray4ToInt(ba4))));
     }
 
+    @Test
+    public void testByteArrayMethodsForLong() {
+        // Test of longToByteArray6 method.
+        byte ba[] = {
+            (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
+            (byte) 0x55, (byte) 0x66
+        };
+        long mac = 0x112233445566L;
+        Assert.assertTrue(Arrays.equals(ba, NetUtils.longToByteArray6(mac)));
+
+        byte ba1[] = {
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff
+        };
+        long mac1 = 0xffffffffffffL;
+        Assert.assertTrue(Arrays.equals(ba1, NetUtils.longToByteArray6(mac1)));
+
+        byte ba2[] = {
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+            (byte) 0x00, (byte) 0x00
+        };
+        long mac2 = 0x000000000000L;
+        Assert.assertTrue(Arrays.equals(ba2, NetUtils.longToByteArray6(mac2)));
+
+        byte ba3[] = {
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00,
+            (byte) 0x00, (byte) 0x00
+        };
+        long mac3 = 0xffffff000000L;
+        Assert.assertTrue(Arrays.equals(ba3, NetUtils.longToByteArray6(mac3)));
+
+        byte ba4[] = {
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff
+        };
+        long mac4 = 0x000000ffffffL;
+        Assert.assertTrue(Arrays.equals(ba4, NetUtils.longToByteArray6(mac4)));
+
+        // Convert a long number to a byte array,
+        // and revert it to the long number again.
+        Assert.assertTrue(NetUtils
+                .byteArray6ToLong(NetUtils.longToByteArray6(mac)) == mac);
+
+        Assert.assertTrue(NetUtils
+                .byteArray6ToLong(NetUtils.longToByteArray6(mac1)) == mac1);
+
+        Assert.assertTrue(NetUtils
+                .byteArray6ToLong(NetUtils.longToByteArray6(mac2)) == mac2);
+
+        Assert.assertTrue(NetUtils
+                .byteArray6ToLong(NetUtils.longToByteArray6(mac3)) == mac3);
+
+        Assert.assertTrue(NetUtils
+                .byteArray6ToLong(NetUtils.longToByteArray6(mac4)) == mac4);
+
+        // Convert a byte array to a long nubmer,
+        // and revert it to the byte array again.
+        Assert.assertTrue(Arrays.equals(ba,
+                    NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba))));
+
+        Assert.assertTrue(Arrays.equals(ba1,
+                    NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba1))));
+
+        Assert.assertTrue(Arrays.equals(ba2,
+                    NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba2))));
+
+        Assert.assertTrue(Arrays.equals(ba3,
+                    NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba3))));
+
+        Assert.assertTrue(Arrays.equals(ba4,
+                    NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba4))));
+
+        // Test of paramter validation of byteArray6ToLong method.
+        byte array5[] = {
+            (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
+        };
+        Assert.assertEquals(0, NetUtils.byteArray6ToLong(array5));
+
+        byte array7[] = {
+            (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
+            (byte) 0x55, (byte) 0x66, (byte) 0x77
+        };
+        Assert.assertEquals(0, NetUtils.byteArray6ToLong(array7));
+
+        byte arrayNull[] = null;
+        Assert.assertEquals(0, NetUtils.byteArray6ToLong(arrayNull));
+    }
+
     @Test
     public void testInetMethods() throws UnknownHostException {
         int ip = 0xfffffff0;