X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fapi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Futils%2FNetUtils.java;h=f81e7e3d03b0c4af14f5e539b45567725d80c43b;hb=c5630f2945eb5370f9829514ef72de41d41eb2be;hp=6a3a42fbb9ba52b5414937027d53788ed5058c9e;hpb=336327522d9b1bd81958feeac03bbd8332e938e4;p=controller.git diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java index 6a3a42fbb9..f81e7e3d03 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java @@ -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 * @@ -296,6 +322,19 @@ public abstract class NetUtils { return false; } + /** + * Returns true if the MAC address is a unicast MAC address and false + * otherwise. + * + * @param MACAddress + * @return + */ + public static boolean isUnicastMACAddr(byte[] MACAddress) { + if (MACAddress.length == MACAddrLengthInBytes) { + return (MACAddress[0] & 1) == 0; + } + return false; + } /** * Returns true if the MAC address is a multicast MAC address and false