Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / OF10MatchSerializer.java
index 33f31971e89d7ae40c17adc47f9bd6cb8b3168c4..d934e4c75e129f47b35cfb24d12cfe626cbeefea 100644 (file)
@@ -8,11 +8,12 @@
 
 package org.opendaylight.openflowjava.protocol.impl.util;
 
+import io.netty.buffer.ByteBuf;
+
 import java.util.HashMap;
 import java.util.Map;
 
-import io.netty.buffer.ByteBuf;
-
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
 
@@ -20,42 +21,43 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.matc
  * Serializes ofp_match (OpenFlow v1.0) structure
  * @author michal.polkorab
  */
-public abstract class OF10MatchSerializer {
+public class OF10MatchSerializer implements OFSerializer<MatchV10> {
 
     private static final byte PADDING_IN_MATCH = 1;
     private static final byte PADDING_IN_MATCH_2 = 2;
     private static final byte NW_SRC_SHIFT = 8;
     private static final byte NW_DST_SHIFT = 14;
-    
+
     /**
-     * Encodes ofp_match (OpenFlow v1.0)
-     * @param out output ByteBuf that match will be written into
-     * @param match match to be encoded
+     * Serializes ofp_match (OpenFlow v1.0)
+     * @param outBuffer output ByteBuf
+     * @param match match to be serialized
      */
-    public static void encodeMatchV10(ByteBuf out, MatchV10 match) {
-        out.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
-        out.writeShort(match.getInPort());
-        out.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
-        out.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
-        out.writeShort(match.getDlVlan());
-        out.writeByte(match.getDlVlanPcp());
-        ByteBufUtils.padBuffer(PADDING_IN_MATCH, out);
-        out.writeShort(match.getDlType());
-        out.writeByte(match.getNwTos());
-        out.writeByte(match.getNwProto());
-        ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, out);
+    @Override
+    public void serialize(MatchV10 match, ByteBuf outBuffer) {
+        outBuffer.writeInt(encodeWildcards(match.getWildcards(), match.getNwSrcMask(), match.getNwDstMask()));
+        outBuffer.writeShort(match.getInPort());
+        outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlSrc().getValue()));
+        outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(match.getDlDst().getValue()));
+        outBuffer.writeShort(match.getDlVlan());
+        outBuffer.writeByte(match.getDlVlanPcp());
+        ByteBufUtils.padBuffer(PADDING_IN_MATCH, outBuffer);
+        outBuffer.writeShort(match.getDlType());
+        outBuffer.writeByte(match.getNwTos());
+        outBuffer.writeByte(match.getNwProto());
+        ByteBufUtils.padBuffer(PADDING_IN_MATCH_2, outBuffer);
         String[] srcGroups = match.getNwSrc().getValue().split("\\.");
         for (int i = 0; i < srcGroups.length; i++) {
-            out.writeByte(Integer.parseInt(srcGroups[i]));
+            outBuffer.writeByte(Integer.parseInt(srcGroups[i]));
         }
         String[] dstGroups = match.getNwDst().getValue().split("\\.");
         for (int i = 0; i < dstGroups.length; i++) {
-            out.writeByte(Integer.parseInt(dstGroups[i]));
+            outBuffer.writeByte(Integer.parseInt(dstGroups[i]));
         }
-        out.writeShort(match.getTpSrc());
-        out.writeShort(match.getTpDst());
+        outBuffer.writeShort(match.getTpSrc());
+        outBuffer.writeShort(match.getTpDst());
     }
-    
+
     private static int encodeWildcards(FlowWildcardsV10 wildcards, short srcMask, short dstMask) {
         int bitmask = 0;
         Map<Integer, Boolean> wildcardsMap = new HashMap<>();
@@ -74,5 +76,5 @@ public abstract class OF10MatchSerializer {
         bitmask |= ((32 - dstMask) << NW_DST_SHIFT);
         return bitmask;
     }
-    
+
 }