Bug 2280 - MatchConvertorImpl bugs found during test creation
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / util / ByteUtil.java
index 774d9d0dc22b1e8f01d0faceaa9505f33f257811..671c081393b8d91f7262559dea3f166992927674 100644 (file)
@@ -7,31 +7,23 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.util;
 
+import com.google.common.base.Preconditions;
 import java.math.BigInteger;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
-
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
-
-import com.google.common.base.Joiner;
 
 /**
  * @author mirehak
- *
  */
 public abstract class ByteUtil {
 
-    private static final int MAC_ADDRESS_LENGTH = 6;
     /**
      * @param bytes
      * @param delimiter
      * @return hexString containing bytes, separated with delimiter
      */
-    public static String bytesToHexstring(byte[] bytes, String delimiter) {
+    public static String bytesToHexstring(final byte[] bytes, final String delimiter) {
         StringBuffer sb = new StringBuffer();
-        for (int i = 0; i < bytes.length; i++) {
-            byte b = bytes[i];
+        for (byte b : bytes) {
             sb.append(String.format("%02x%s", b, delimiter));
         }
         return sb.toString();
@@ -39,10 +31,11 @@ public abstract class ByteUtil {
 
     /**
      * Utility method to convert BigInteger to n element byte array
+     *
      * @param bigInteger
      * @return byte array containing n * 8 bits.
      */
-    public static byte[] convertBigIntegerToNBytes(BigInteger bigInteger, int numBytes) {
+    public static byte[] convertBigIntegerToNBytes(final BigInteger bigInteger, final int numBytes) {
         if (bigInteger == null) {
             return null;
         }
@@ -54,102 +47,103 @@ public abstract class ByteUtil {
             Arrays.fill(outputArray, (byte) 0);
         }
         System.arraycopy(inputArray,
-                         Math.max(0, inputArray.length - outputArray.length),
-                         outputArray,
-                         Math.max(0, outputArray.length - inputArray.length),
-                         Math.min(outputArray.length, inputArray.length));
+                Math.max(0, inputArray.length - outputArray.length),
+                outputArray,
+                Math.max(0, outputArray.length - inputArray.length),
+                Math.min(outputArray.length, inputArray.length));
         return outputArray;
     }
 
     /**
      * Converts a 4 byte array of unsigned bytes to unsigned int
+     *
      * @param bytes an array of 4 unsigned bytes
      * @return a long representing the unsigned int
      */
-    public static final long bytesToUnsignedInt(byte[] bytes)
-    {
-      long unsignedInt = 0;
-      unsignedInt |= bytes[0] & 0xFF;
-      unsignedInt <<= 8;
-      unsignedInt |= bytes[1] & 0xFF;
-      unsignedInt <<= 8;
-      unsignedInt |= bytes[2] & 0xFF;
-      unsignedInt <<= 8;
-      unsignedInt |= bytes[3] & 0xFF;
-      return unsignedInt;
+    public static final long bytesToUnsignedInt(final byte[] bytes) {
+        Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
+        long unsignedInt = 0;
+        unsignedInt |= bytes[0] & 0xFF;
+        unsignedInt <<= 8;
+        unsignedInt |= bytes[1] & 0xFF;
+        unsignedInt <<= 8;
+        unsignedInt |= bytes[2] & 0xFF;
+        unsignedInt <<= 8;
+        unsignedInt |= bytes[3] & 0xFF;
+        return unsignedInt;
+    }
+
+    /**
+     * Converts a 3 byte array of unsigned bytes to unsigned int
+     *
+     * @param bytes an array of 4 unsigned bytes
+     * @return a long representing the unsigned int
+     */
+    public static final long bytesToUnsignedMedium(final byte[] bytes) {
+        Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
+        long unsignedMedium = 0;
+        unsignedMedium |= bytes[0] & 0xFF;
+        unsignedMedium <<= 8;
+        unsignedMedium |= bytes[1] & 0xFF;
+        unsignedMedium <<= 8;
+        unsignedMedium |= bytes[2] & 0xFF;
+        return unsignedMedium;
     }
 
     /**
      * Converts a 2 byte array of unsigned bytes to unsigned short
+     *
      * @param bytes an array of 2 unsigned bytes
      * @return an int representing the unsigned short
      */
-    public static final int bytesToUnsignedShort(byte[] bytes)
-    {
-      int unsignedShort = 0;
-      unsignedShort |= bytes[0] & 0xFF;
-      unsignedShort <<= 8;
-      unsignedShort |= bytes[1] & 0xFF;
-      return unsignedShort;
+    public static final int bytesToUnsignedShort(final byte[] bytes) {
+        Preconditions.checkArgument(bytes.length == 2, "Input byte array must be exactly two bytes long.");
+        int unsignedShort = 0;
+        unsignedShort |= bytes[0] & 0xFF;
+        unsignedShort <<= 8;
+        unsignedShort |= bytes[1] & 0xFF;
+        return unsignedShort;
     }
 
     /**
      * Converts unsigned integer to a 4 byte array of unsigned bytes
+     *
      * @param unsignedInt representing the unsigned integer
      * @return bytes an array of 4 unsigned bytes
      */
-    public static byte[] unsignedIntToBytes(Long unsignedInt)
-    {
-      byte[] bytes = new byte[4];
-      bytes[3] = (byte) (unsignedInt & 0xFF);
-      bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
-      bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
-      bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
-      return bytes;
-    }
-
-    /**
-     * Converts unsigned short to a 2 byte array of unsigned bytes
-     * @param unsignedShort representing the unsigned short
-     * @return bytes an array of 2 unsigned bytes
-     */
-    public static byte[] unsignedShortToBytes(Integer unsignedShort)
-    {
-      byte[] bytes = new byte[2];
-      bytes[1] = (byte) (unsignedShort & 0xFF);
-      bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
-      return bytes;
+    public static byte[] unsignedIntToBytes(final Long unsignedInt) {
+        byte[] bytes = new byte[4];
+        bytes[3] = (byte) (unsignedInt & 0xFF);
+        bytes[2] = (byte) ((unsignedInt >> 8) & 0xFF);
+        bytes[1] = (byte) ((unsignedInt >> 16) & 0xFF);
+        bytes[0] = (byte) ((unsignedInt >> 24) & 0xFF);
+        return bytes;
     }
 
     /**
-     * Converts mac address represented in bytes to String
-     * @param address
-     * @return String representation of mac address
-     * @see {@link MacAddress}
+     * Converts unsigned integer to a 3 byte array of unsigned bytes
+     *
+     * @param unsignedInt representing the unsigned integer
+     * @return bytes an array of 3 unsigned bytes
      */
-    public static String macAddressToString(byte[] address) {
-        List<String> groups = new ArrayList<>(MAC_ADDRESS_LENGTH);
-        for(int i=0; i < MAC_ADDRESS_LENGTH; i++){
-            groups.add(String.format("%02X", address[i]));
-        }
-        Joiner joiner = Joiner.on(":");
-        return joiner.join(groups);
+    public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
+        byte[] bytes = new byte[3];
+        bytes[2] = (byte) (unsignedInt & 0xFF);
+        bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
+        bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
+        return bytes;
     }
 
     /**
-     * Converts macAddress to byte array
-     * @param macAddress
-     * @return byte representation of mac address
-     * @see {@link MacAddress}
+     * Converts unsigned short to a 2 byte array of unsigned bytes
+     *
+     * @param unsignedShort representing the unsigned short
+     * @return bytes an array of 2 unsigned bytes
      */
-    public static byte[] macAddressToBytes(MacAddress macAddress) {
-        String macAddressString = macAddress.getValue();
-        String[] sequences = macAddressString.split(":");
-        int MAC_ADDRESS_LENGTH = 6;
-        byte[] result = new byte[MAC_ADDRESS_LENGTH];
-        for (int i = 0; i < sequences.length; i++) {
-             result[i] = (byte) Short.parseShort(sequences[i], 16);
-        }
-        return result;
+    public static byte[] unsignedShortToBytes(final Integer unsignedShort) {
+        byte[] bytes = new byte[2];
+        bytes[1] = (byte) (unsignedShort & 0xFF);
+        bytes[0] = (byte) ((unsignedShort >> 8) & 0xFF);
+        return bytes;
     }
 }