Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / ByteBufUtils.java
index 80b818e01e1b89aedbd09592628d584f10b807ac..0e7654d12c47a82986fbcf891ace44e05c8d2540 100644 (file)
@@ -13,12 +13,10 @@ import io.netty.buffer.ByteBuf;
 import io.netty.buffer.UnpooledByteBufAllocator;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 
@@ -37,7 +35,7 @@ public abstract class ByteBufUtils {
      */
     public static String byteBufToHexString(ByteBuf bb) {
         StringBuffer sb = new StringBuffer();
-        for (int i = 0; i < bb.readableBytes(); i++) {
+        for (int i = bb.readerIndex(); i < (bb.readerIndex() + bb.readableBytes()); i++) {
             sb.append(String.format(" %02x", bb.getUnsignedByte(i)));
         }
         return sb.toString().trim();
@@ -103,18 +101,27 @@ public abstract class ByteBufUtils {
         }
     }
     
+    
     /**
      * Create standard OF header
-     * @param factory serialization factory 
+     * @param msgType message code
      * @param message POJO
      * @param out writing buffer
+     * @param length ofheader length
      */
-    public static <E extends OfHeader> void writeOFHeader(OFSerializer<E> factory, E message, ByteBuf out) { 
+    public static <E extends OfHeader> void writeOFHeader(byte msgType, E message, ByteBuf out, int length) { 
         out.writeByte(message.getVersion());
-        out.writeByte(factory.getMessageType());
-        out.writeShort(factory.computeLength(message));
+        out.writeByte(msgType);
+        out.writeShort(length);
         out.writeInt(message.getXid().intValue());
-
+    }
+    
+    /**
+     * Write length standard OF header
+     * @param out writing buffer
+     */
+    public static void updateOFHeaderLength(ByteBuf out) { 
+        out.setShort(EncodeConstants.OFHEADER_LENGTH_INDEX, out.readableBytes());
     }
 
     /**
@@ -176,6 +183,7 @@ public abstract class ByteBufUtils {
      * @return byte representation of mac address
      * @see {@link MacAddress}
      */
+    @SuppressWarnings("javadoc")
     public static byte[] macAddressToBytes(String macAddress) {
         String[] sequences = macAddress.split(":");
         byte[] result = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
@@ -191,6 +199,7 @@ public abstract class ByteBufUtils {
      * @return String representation of mac address
      * @see {@link MacAddress}
      */
+    @SuppressWarnings("javadoc")
     public static String macAddressToString(byte[] address) {
         List<String> groups = new ArrayList<>();
         for(int i=0; i < EncodeConstants.MAC_ADDRESS_LENGTH; i++){
@@ -201,23 +210,15 @@ public abstract class ByteBufUtils {
     }
     
     /**
-     * Reads and parses port name from ByteBuf
+     * Reads and parses null-terminated string from ByteBuf
      * @param rawMessage
      * @param length maximal length of String
      * @return String with name of port
      */
     public static String decodeNullTerminatedString(ByteBuf rawMessage, int length) {
-        byte[] name = new byte[EncodeConstants.MAX_PORT_NAME_LENGTH];
+        byte[] name = new byte[length];
         rawMessage.readBytes(name);
-        int index = 0;
-        for (int i = 0; i < name.length; i++) {
-            if (name[i] != 0) {
-                index++;
-            } else {
-                break;
-            }
-        }
-        byte[] correctName = Arrays.copyOf(name, index);
-        return new String(correctName);
+        return new String(name).trim();
     }
+    
 }