Updated extension registration keys
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactory.java
index ba4f97088e375efe7fa14980af28e1687474df95..421bfc4a4f29e57c1eec0a0b03b8cf3bf21eb861 100644 (file)
@@ -10,14 +10,18 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
 
 import io.netty.buffer.ByteBuf;
 
-import java.util.HashMap;
-import java.util.Map;
-
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
-import org.opendaylight.openflowjava.protocol.impl.util.InstructionsSerializer;
-import org.opendaylight.openflowjava.protocol.impl.util.MatchSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
+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.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
+import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
+import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
 
 /**
@@ -25,68 +29,46 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
  * @author timotej.kubas
  * @author michal.polkorab
  */
-public class FlowModInputMessageFactory implements OFSerializer<FlowModInput> {
+public class FlowModInputMessageFactory implements OFSerializer<FlowModInput>, SerializerRegistryInjector {
     private static final byte MESSAGE_TYPE = 14;
     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
-    private static final int MESSAGE_LENGTH = 48;
-    private static FlowModInputMessageFactory instance;
-   
-    private FlowModInputMessageFactory() {
-        // singleton
-    }
-    
-    /**
-     * @return singleton factory
-     */
-    public static synchronized FlowModInputMessageFactory getInstance() {
-        if(instance == null) {
-            instance = new FlowModInputMessageFactory();
-        }
-        return instance;
-    }
-    
-    @Override
-    public void messageToBuffer(short version, ByteBuf out, FlowModInput message) {
-        ByteBufUtils.writeOFHeader(instance, message, out);
-        out.writeLong(message.getCookie().longValue());
-        out.writeLong(message.getCookieMask().longValue());
-        out.writeByte(message.getTableId().getValue().byteValue());
-        out.writeByte(message.getCommand().getIntValue());
-        out.writeShort(message.getIdleTimeout().intValue());
-        out.writeShort(message.getHardTimeout().intValue());
-        out.writeShort(message.getPriority());
-        out.writeInt(message.getBufferId().intValue());
-        out.writeInt(message.getOutPort().getValue().intValue());
-        out.writeInt(message.getOutGroup().intValue());
-        out.writeShort(createFlowModFlagsBitmask(message.getFlags()));
-        ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, out);
-        MatchSerializer.encodeMatch(message.getMatch(), out);
-        InstructionsSerializer.encodeInstructions(message.getInstruction(), out);
-    }
+    private static final TypeKeyMaker<Instruction> INSTRUCTION_KEY_MAKER =
+            TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
+    private SerializerRegistry registry;
 
     @Override
-    public int computeLength(FlowModInput message) {
-        return MESSAGE_LENGTH + MatchSerializer.computeMatchLength(message.getMatch())
-                + InstructionsSerializer.computeInstructionsLength(message.getInstruction());
+    public void serialize(final FlowModInput message, final ByteBuf outBuffer) {
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        outBuffer.writeLong(message.getCookie().longValue());
+        outBuffer.writeLong(message.getCookieMask().longValue());
+        outBuffer.writeByte(message.getTableId().getValue().byteValue());
+        outBuffer.writeByte(message.getCommand().getIntValue());
+        outBuffer.writeShort(message.getIdleTimeout().intValue());
+        outBuffer.writeShort(message.getHardTimeout().intValue());
+        outBuffer.writeShort(message.getPriority());
+        outBuffer.writeInt(message.getBufferId().intValue());
+        outBuffer.writeInt(message.getOutPort().getValue().intValue());
+        outBuffer.writeInt(message.getOutGroup().intValue());
+        outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlags()));
+        ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, outBuffer);
+        registry.<Match, OFSerializer<Match>>getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class))
+            .serialize(message.getMatch(), outBuffer);
+        ListSerializer.serializeList(message.getInstruction(), INSTRUCTION_KEY_MAKER, registry, outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer);
     }
 
     @Override
-    public byte getMessageType() {
-        return MESSAGE_TYPE;
+    public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
+        this.registry = serializerRegistry;
     }
 
-    private static int createFlowModFlagsBitmask(FlowModFlags flags) {
-        int flowModFlagBitmask = 0;
-        Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();
-        flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());
-        flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());
-        flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());
-        flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());
-        flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());
-        
-        flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);
-        return flowModFlagBitmask;
+    private static int createFlowModFlagsBitmask(final FlowModFlags flags) {
+        return ByteBufUtils.fillBitMask(0,
+                flags.isOFPFFSENDFLOWREM(),
+                flags.isOFPFFCHECKOVERLAP(),
+                flags.isOFPFFRESETCOUNTS(),
+                flags.isOFPFFNOPKTCOUNTS(),
+                flags.isOFPFFNOBYTCOUNTS());
     }
-    
-    
+
 }