Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactory.java
index 4793c286bf0556ba9dc4d1c04aadac5c02c1f8b8..79f831bad952e0fe8f9b4bed6c124a94e8c8c6ce 100644 (file)
@@ -1,86 +1,74 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
-\r
-import io.netty.buffer.ByteBuf;\r
-\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
-import org.opendaylight.openflowjava.protocol.impl.util.InstructionsSerializer;\r
-import org.opendaylight.openflowjava.protocol.impl.util.MatchSerializer;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;\r
-\r
-/**\r
- * Translates FlowMod messages\r
- * @author timotej.kubas\r
- * @author michal.polkorab\r
- */\r
-public class FlowModInputMessageFactory implements OFSerializer<FlowModInput> {\r
-    private static final byte MESSAGE_TYPE = 14;\r
-    private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;\r
-    private static final int MESSAGE_LENGTH = 48;\r
-    private static FlowModInputMessageFactory instance;\r
-   \r
-    private FlowModInputMessageFactory() {\r
-        // singleton\r
-    }\r
-    \r
-    /**\r
-     * @return singleton factory\r
-     */\r
-    public static synchronized FlowModInputMessageFactory getInstance() {\r
-        if(instance == null) {\r
-            instance = new FlowModInputMessageFactory();\r
-        }\r
-        return instance;\r
-    }\r
-    \r
-    @Override\r
-    public void messageToBuffer(short version, ByteBuf out, FlowModInput message) {\r
-        ByteBufUtils.writeOFHeader(instance, message, out);\r
-        out.writeLong(message.getCookie().longValue());\r
-        out.writeLong(message.getCookieMask().longValue());\r
-        out.writeByte(message.getTableId().getValue().byteValue());\r
-        out.writeByte(message.getCommand().getIntValue());\r
-        out.writeShort(message.getIdleTimeout().intValue());\r
-        out.writeShort(message.getHardTimeout().intValue());\r
-        out.writeShort(message.getPriority());\r
-        out.writeInt(message.getBufferId().intValue());\r
-        out.writeInt(message.getOutPort().getValue().intValue());\r
-        out.writeInt(message.getOutGroup().intValue());\r
-        out.writeShort(createFlowModFlagsBitmask(message.getFlags()));\r
-        ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, out);\r
-        MatchSerializer.encodeMatch(message.getMatch(), out);\r
-        InstructionsSerializer.encodeInstructions(message.getInstructions(), out);\r
-        \r
-    }\r
-\r
-    @Override\r
-    public int computeLength(FlowModInput message) {\r
-        return MESSAGE_LENGTH + MatchSerializer.computeMatchLength(message.getMatch())\r
-                + InstructionsSerializer.computeInstructionsLength(message.getInstructions());\r
-    }\r
-\r
-    @Override\r
-    public byte getMessageType() {\r
-        return MESSAGE_TYPE;\r
-    }\r
-\r
-    private static int createFlowModFlagsBitmask(FlowModFlags flags) {\r
-        int flowModFlagBitmask = 0;\r
-        Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();\r
-        flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());\r
-        flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());\r
-        flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());\r
-        flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());\r
-        flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());\r
-        \r
-        flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);\r
-        return flowModFlagBitmask;\r
-    }\r
-    \r
-    \r
-}\r
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
+
+import io.netty.buffer.ByteBuf;
+
+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.api.keys.MessageTypeKey;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
+import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
+import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+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.rev150225.match.grouping.Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
+
+/**
+ * Translates FlowMod messages
+ * @author timotej.kubas
+ * @author michal.polkorab
+ */
+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 TypeKeyMaker<Instruction> INSTRUCTION_KEY_MAKER =
+            TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
+    private SerializerRegistry registry;
+
+    @Override
+    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()));
+        outBuffer.writeZero(PADDING_IN_FLOW_MOD_MESSAGE);
+        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 void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
+        this.registry = serializerRegistry;
+    }
+
+    private static int createFlowModFlagsBitmask(final FlowModFlags flags) {
+        return ByteBufUtils.fillBitMask(0,
+                flags.isOFPFFSENDFLOWREM(),
+                flags.isOFPFFCHECKOVERLAP(),
+                flags.isOFPFFRESETCOUNTS(),
+                flags.isOFPFFNOPKTCOUNTS(),
+                flags.isOFPFFNOBYTCOUNTS());
+    }
+
+}