Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PacketOutInputMessageFactory.java
index 443830bd388327ed92fe850674741aa9c9abc4c3..2b14b19d7332e1d226b80355c62a990b844d6f2e 100644 (file)
@@ -10,9 +10,14 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
 
 import io.netty.buffer.ByteBuf;
 
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
-import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.RegistryInjector;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.impl.util.CodingUtils;
+import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
 
 /**
@@ -20,57 +25,36 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
  * @author michal.polkorab
  * @author timotej.kubas
  */
-public class PacketOutInputMessageFactory implements OFSerializer<PacketOutInput>{
+public class PacketOutInputMessageFactory implements OFSerializer<PacketOutInput>, RegistryInjector {
 
     /** Code type of PacketOut message */
-    public static final byte MESSAGE_TYPE = 13;
-    private static final int MESSAGE_LENGTH = 24;
+    private static final byte MESSAGE_TYPE = 13;
     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
-    private static PacketOutInputMessageFactory instance;
-    
-    private PacketOutInputMessageFactory() {
-        // do nothing, just singleton
-    }
-    
-    /**
-     * @return singleton factory
-     */
-    public static synchronized PacketOutInputMessageFactory getInstance() {
-        if (instance == null) {
-            instance = new PacketOutInputMessageFactory();
-        }
-        return instance;
-    }
-    
-    @Override
-    public void messageToBuffer(short version, ByteBuf out,
-            PacketOutInput message) {
-        ByteBufUtils.writeOFHeader(instance, message, out);
-        out.writeInt(message.getBufferId().intValue());
-        out.writeInt(message.getInPort().getValue().intValue());
-        out.writeShort(ActionsSerializer.computeLengthOfActions(message.getAction()));
-        ByteBufUtils.padBuffer(PADDING_IN_PACKET_OUT_MESSAGE, out);
-        ActionsSerializer.encodeActions(message.getAction(), out);
-        byte[] data = message.getData();
-        if (data != null) {
-            out.writeBytes(data);
-        }
-    }
+    private SerializerRegistry registry;
 
     @Override
-    public int computeLength(PacketOutInput message) {
-        int length = MESSAGE_LENGTH;
-        length += ActionsSerializer.computeLengthOfActions(message.getAction());
+    public void serialize(PacketOutInput message, ByteBuf outBuffer) {
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        outBuffer.writeInt(message.getBufferId().intValue());
+        outBuffer.writeInt(message.getInPort().getValue().intValue());
+        int actionsLengthIndex = outBuffer.writerIndex();
+        outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
+        ByteBufUtils.padBuffer(PADDING_IN_PACKET_OUT_MESSAGE, outBuffer);
+        int actionsStartIndex = outBuffer.writerIndex();
+        OFSerializer<Action> actionSerializer = registry.getSerializer(
+                new MessageTypeKey<>(message.getVersion(), Action.class));
+        CodingUtils.serializeList(message.getAction(), actionSerializer, outBuffer);
+        outBuffer.setShort(actionsLengthIndex, outBuffer.writerIndex() - actionsStartIndex);
         byte[] data = message.getData();
         if (data != null) {
-            length += data.length;
+            outBuffer.writeBytes(data);
         }
-        return length;
+        ByteBufUtils.updateOFHeaderLength(outBuffer);
     }
 
     @Override
-    public byte getMessageType() {
-        return MESSAGE_TYPE;
+    public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
+        this.registry = serializerRegistry;
     }
 
 }