Extensibility refactoring
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GroupModInputMessageFactory.java
index ef30ec046c2645d07216572900a9c7efa1ac12d1..75a6bdfcbc231eb8e727b898557a831fb958bd91 100644 (file)
@@ -1,73 +1,70 @@
-/* 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.Iterator;\r
-import java.util.List;\r
-\r
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.group.mod.Buckets;\r
-\r
-/**\r
- * @author timotej.kubas\r
- * @author michal.polkorab\r
- */\r
-public class GroupModInputMessageFactory implements OFSerializer<GroupModInput> {\r
-    private static final byte MESSAGE_TYPE = 15;\r
-    private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;\r
-    private static final int MESSAGE_LENGTH = 16; \r
-    private static GroupModInputMessageFactory instance;\r
-    \r
-    private GroupModInputMessageFactory() {\r
-        // singleton\r
-    }\r
-    \r
-    /**\r
-     * @return singleton factory\r
-     */\r
-    public static synchronized GroupModInputMessageFactory getInstance() {\r
-        if (instance == null) {\r
-            instance = new GroupModInputMessageFactory();\r
-        }\r
-        return instance;\r
-    }\r
-    \r
-    @Override\r
-    public void messageToBuffer(short version, ByteBuf out,\r
-            GroupModInput message) {\r
-        ByteBufUtils.writeOFHeader(instance, message, out);\r
-        out.writeShort(message.getCommand().getIntValue());\r
-        out.writeByte(message.getType().getIntValue());\r
-        ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out);\r
-        out.writeInt(message.getGroupId().intValue());\r
-        encodeBuckets(message.getBuckets(), out);\r
-    }\r
-\r
-    @Override\r
-    public int computeLength() {\r
-        return MESSAGE_LENGTH;\r
-    }\r
-\r
-    @Override\r
-    public byte getMessageType() {\r
-        return MESSAGE_TYPE;\r
-    }\r
-    \r
-    private static void encodeBuckets(List<Buckets> buckets, ByteBuf outBuffer) {\r
-        final byte PADDING_IN_BUCKET = 4;\r
-        \r
-        for (Iterator<Buckets> iterator = buckets.iterator(); iterator.hasNext();) {\r
-            Buckets currentBucket = iterator.next();\r
-            // TODO get method for field length missing\r
-            outBuffer.writeShort(currentBucket.getWeight().intValue());\r
-            outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());\r
-            outBuffer.writeInt(currentBucket.getWatchGroup().intValue());\r
-            ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);\r
-            // TODO actions structure missing\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 java.util.List;
+
+import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
+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.GroupModInput;
+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>, SerializerRegistryInjector {
+    private static final byte MESSAGE_TYPE = 15;
+    private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
+    private static final byte PADDING_IN_BUCKET = 4;
+    private SerializerRegistry registry;
+
+    @Override
+    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);
+    }
+
+    private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
+        if (buckets != null) {
+            for (BucketsList currentBucket : buckets) {
+                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);
+                OFSerializer<Action> actionSerializer = registry.getSerializer(
+                        new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Action.class));
+                CodingUtils.serializeList(currentBucket.getAction(), actionSerializer, outBuffer);
+                outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex);
+            }
+        }
+    }
+
+    @Override
+    public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
+        this.registry = serializerRegistry;
+    }
+
+}