InstructionDeserializer split into separate deserializers
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GroupModInputMessageFactory.java
index a9a2c510d2b3d801a12dfc0f5b9053e9aad2e56d..925e78dab8d43373d7fbe86c6663f52a2e5c7c54 100644 (file)
@@ -12,90 +12,57 @@ import io.netty.buffer.ByteBuf;
 
 import java.util.List;
 
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
-import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.util.EnhancedTypeKeyMakerFactory;
+import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.BucketsList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
 
 /**
  * Translates GroupMod messages
  * @author timotej.kubas
  * @author michal.polkorab
  */
-public class GroupModInputMessageFactory implements OFSerializer<GroupModInput> {
+public class GroupModInputMessageFactory implements OFSerializer<GroupModInput>, SerializerRegistryInjector {
     private static final byte MESSAGE_TYPE = 15;
-    private static final int MESSAGE_LENGTH = 16;
     private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
-    private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16;
     private static final byte PADDING_IN_BUCKET = 4;
-    private static GroupModInputMessageFactory instance;
-
-    private GroupModInputMessageFactory() {
-        // singleton
-    }
-    
-    /**
-     * @return singleton factory
-     */
-    public static synchronized GroupModInputMessageFactory getInstance() {
-        if (instance == null) {
-            instance = new GroupModInputMessageFactory();
-        }
-        return instance;
-    }
-    
-    @Override
-    public void messageToBuffer(short version, ByteBuf out,
-            GroupModInput message) {
-        ByteBufUtils.writeOFHeader(instance, message, out);
-        out.writeShort(message.getCommand().getIntValue());
-        out.writeByte(message.getType().getIntValue());
-        ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out);
-        out.writeInt(message.getGroupId().getValue().intValue());
-        encodeBuckets(message.getBucketsList(), out);
-    }
+    private SerializerRegistry registry;
 
     @Override
-    public int computeLength(GroupModInput message) {
-        return MESSAGE_LENGTH + computeLengthOfBuckets(message.getBucketsList());
+    public void serialize(GroupModInput message, ByteBuf outBuffer) {
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        outBuffer.writeShort(message.getCommand().getIntValue());
+        outBuffer.writeByte(message.getType().getIntValue());
+        ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, outBuffer);
+        outBuffer.writeInt(message.getGroupId().getValue().intValue());
+        serializerBuckets(message.getBucketsList(), outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer);
     }
 
-    @Override
-    public byte getMessageType() {
-        return MESSAGE_TYPE;
-    }
-    
-    private static void encodeBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
+    private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
         if (buckets != null) {
             for (BucketsList currentBucket : buckets) {
-                outBuffer.writeShort(computeLengthOfBucket(currentBucket));
+                int bucketLengthIndex = outBuffer.writerIndex();
+                outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
                 outBuffer.writeShort(currentBucket.getWeight().shortValue());
                 outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
                 outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
                 ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
-                ActionsSerializer.encodeActions(currentBucket.getActionsList(), outBuffer);
+                ListSerializer.serializeList(currentBucket.getAction(), EnhancedTypeKeyMakerFactory
+                        .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, outBuffer);
+                outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex);
             }
         }
     }
-    
-    private static int computeLengthOfBucket(BucketsList bucket) {
-        int lengthOfBuckets = 0;
-        if (bucket != null) {
-            lengthOfBuckets = LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(bucket.getActionsList());
-        }
-        return lengthOfBuckets;
-    }
-    
-    private static int computeLengthOfBuckets(List<BucketsList> buckets) {
-        int lengthOfBuckets = 0;
-        if (buckets != null) {
-            for (BucketsList currentBucket : buckets) {
-                lengthOfBuckets += LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(currentBucket.getActionsList());
-            }
-        }
-        return lengthOfBuckets;
+
+    @Override
+    public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
+        this.registry = serializerRegistry;
     }
 
-    
 }