Updated extension registration keys
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FlowModInputMessageFactory.java
index 7821cd4979ad354993b1c37f63fd822c8a1da9a4..7ba6e7ebef1e6480c047f647a288cc2eb2a57d55 100644 (file)
@@ -1,75 +1,67 @@
-/* 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.OF10ActionsSerializer;\r
-import org.opendaylight.openflowjava.protocol.impl.util.OF10MatchSerializer;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;\r
-\r
-/**\r
- * Translates FlowMod messages\r
- * @author michal.polkorab\r
- */\r
-public class OF10FlowModInputMessageFactory implements OFSerializer<FlowModInput> {\r
-\r
-    private static final byte MESSAGE_TYPE = 14;\r
-    private static final int MESSAGE_LENGTH = 72;\r
-    \r
-    private static OF10FlowModInputMessageFactory instance;\r
-   \r
-    private OF10FlowModInputMessageFactory() {\r
-        // singleton\r
-    }\r
-    \r
-    /**\r
-     * @return singleton factory\r
-     */\r
-    public static synchronized OF10FlowModInputMessageFactory getInstance() {\r
-        if(instance == null) {\r
-            instance = new OF10FlowModInputMessageFactory();\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
-        OF10MatchSerializer.encodeMatchV10(out, message.getMatchV10());\r
-        out.writeLong(message.getCookie().longValue());\r
-        out.writeShort(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.writeShort(message.getOutPort().getValue().intValue());\r
-        out.writeShort(createFlowModFlagsBitmask(message.getFlagsV10()));\r
-        OF10ActionsSerializer.encodeActionsV10(out, message.getActionsList());\r
-    }\r
-\r
-    @Override\r
-    public int computeLength(FlowModInput message) {\r
-        return MESSAGE_LENGTH + OF10ActionsSerializer.computeActionsLength(message.getActionsList());\r
-    }\r
-\r
-    @Override\r
-    public byte getMessageType() {\r
-        return MESSAGE_TYPE;\r
-    }\r
-\r
-    private static int createFlowModFlagsBitmask(FlowModFlagsV10 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.isOFPFFEMERG());\r
-        flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);\r
-        return flowModFlagBitmask;\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.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.action.rev130731.actions.grouping.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
+
+/**
+ * Translates FlowMod messages
+ * @author michal.polkorab
+ */
+public class OF10FlowModInputMessageFactory implements OFSerializer<FlowModInput>, SerializerRegistryInjector {
+
+    private static final byte MESSAGE_TYPE = 14;
+    private static final TypeKeyMaker<Action> ACTION_KEY_MAKER =
+            TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF10_VERSION_ID);
+    private SerializerRegistry registry;
+
+    @Override
+    public void serialize(final FlowModInput message, final ByteBuf outBuffer) {
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        OFSerializer<MatchV10> matchSerializer = registry.getSerializer(new MessageTypeKey<>(
+                message.getVersion(), MatchV10.class));
+        matchSerializer.serialize(message.getMatchV10(), outBuffer);
+        outBuffer.writeLong(message.getCookie().longValue());
+        outBuffer.writeShort(message.getCommand().getIntValue());
+        outBuffer.writeShort(message.getIdleTimeout().intValue());
+        outBuffer.writeShort(message.getHardTimeout().intValue());
+        outBuffer.writeShort(message.getPriority());
+        outBuffer.writeInt(message.getBufferId().intValue());
+        outBuffer.writeShort(message.getOutPort().getValue().intValue());
+        outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlagsV10()));
+        ListSerializer.serializeList(message.getAction(), ACTION_KEY_MAKER, registry, outBuffer);
+        ByteBufUtils.updateOFHeaderLength(outBuffer);
+    }
+
+    private static int createFlowModFlagsBitmask(final FlowModFlagsV10 flags) {
+        return ByteBufUtils.fillBitMask(0,
+                flags.isOFPFFSENDFLOWREM(),
+                flags.isOFPFFCHECKOVERLAP(),
+                flags.isOFPFFEMERG());
+    }
+
+    @Override
+    public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
+        this.registry = serializerRegistry;
+    }
+}