Quickfix - Vlan-vid match entry
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / ByteBufUtils.java
index 8e1ba7f817046d1af0f963fb9f02c1644f583094..e912e6228d4661306f724976ff2261140d4c9d18 100644 (file)
@@ -5,20 +5,23 @@ package org.opendaylight.openflowjava.protocol.impl.util;
 import io.netty.buffer.ByteBuf;\r
 import io.netty.buffer.UnpooledByteBufAllocator;\r
 \r
+import java.util.ArrayList;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Map.Entry;\r
 \r
 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
 \r
+import com.google.common.base.Joiner;\r
+\r
 /** Class for common operations on ByteBuf\r
  * @author michal.polkorab\r
  * @author timotej.kubas\r
  */\r
 public abstract class ByteBufUtils {\r
 \r
-\r
     /**\r
      * Converts ByteBuf into String\r
      * @param bb input ByteBuf\r
@@ -160,4 +163,34 @@ public abstract class ByteBufUtils {
         }\r
         return sb.toString();\r
     }\r
+    \r
+    /**\r
+     * Converts macAddress to byte array\r
+     * @param macAddress\r
+     * @return byte representation of mac address\r
+     * @see {@link MacAddress}\r
+     */\r
+    public static byte[] macAddressToBytes(String macAddress) {\r
+        String[] sequences = macAddress.split(":");\r
+        byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];\r
+        for (int i = 0; i < sequences.length; i++) {\r
+             result[i] = (byte) Short.parseShort(sequences[i], 16);\r
+        }\r
+        return result;\r
+    }\r
+    \r
+    /**\r
+     * Converts mac address represented in bytes to String\r
+     * @param address\r
+     * @return String representation of mac address\r
+     * @see {@link MacAddress}\r
+     */\r
+    public static String macAddressToString(byte[] address) {\r
+        List<String> groups = new ArrayList<>();\r
+        for(int i=0; i < EncodeConstants.MAC_ADDRESS_LENGTH; i++){\r
+            groups.add(String.format("%02X", address[i]));\r
+        }\r
+        Joiner joiner = Joiner.on(":");\r
+        return joiner.join(groups); \r
+    }\r
 }\r