Remove trailing whitespace
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / HelloInputMessageFactory.java
index c839cd4a3b2d9d92166d9521299aecd5d3af26d9..bd87e16f67e26a50717f0bbfd8eceaf690a3680d 100644 (file)
@@ -1,84 +1,66 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
-\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import io.netty.buffer.ByteBuf;\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.HelloInput;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;\r
-\r
-/**\r
- * @author michal.polkorab\r
- *\r
- */\r
-public class HelloInputMessageFactory implements OFSerializer<HelloInput>{\r
-\r
-    /** Code type of Hello message */\r
-    private static final byte MESSAGE_TYPE = 0;\r
-    private static int MESSAGE_LENGTH = 0;\r
-    private static HelloInputMessageFactory instance;\r
-    \r
-    private HelloInputMessageFactory() {\r
-        // do nothing, just singleton\r
-    }\r
-    \r
-    /**\r
-     * @return singleton factory\r
-     */\r
-    public static HelloInputMessageFactory getInstance() {\r
-        if (instance == null) {\r
-            instance = new HelloInputMessageFactory();\r
-        }\r
-        return instance;\r
-    }\r
-\r
-    @Override\r
-    public void messageToBuffer(short version, ByteBuf out, HelloInput message) {\r
-        computeElementsLength(message.getElements());\r
-        ByteBufUtils.writeOFHeader(instance, message, out);\r
-        encodeElementsList(message.getElements(), 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 int computeElementsLength(List<Elements> elements) {\r
-        int versionBitmapSize = 0;\r
-        final int OFHeaderSize = 8;\r
-        int typeSize = 0;\r
-        \r
-        if (elements != null) {\r
-            typeSize = 2;\r
-            versionBitmapSize = elements.get(0).getVersionBitmap().size()/Byte.SIZE;\r
-            } \r
-        MESSAGE_LENGTH = OFHeaderSize + versionBitmapSize + typeSize;\r
-        return MESSAGE_LENGTH;\r
-    }\r
\r
-    private static void encodeElementsList(List<Elements> elements, ByteBuf output) {\r
-        int[] versionBitmap;\r
-        int arraySize = 0;\r
-        if (elements != null) {\r
-            for (Iterator<Elements> iterator = elements.iterator(); iterator.hasNext();) {\r
-                Elements currElement = iterator.next();\r
-                output.writeShort(currElement.getType().getIntValue());\r
-                versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap());\r
-                arraySize = (versionBitmap.length/Integer.SIZE);\r
-                for (int i = 0; i < arraySize; i++) {\r
-                    output.writeInt(versionBitmap[i]);\r
-                }\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.util.EncodeConstants;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
+
+/**
+ * Translates Hello messages
+ * @author michal.polkorab
+ * @author timotej.kubas
+ */
+public class HelloInputMessageFactory implements OFSerializer<HelloInput>{
+
+    /** Code type of Hello message */
+    private static final byte MESSAGE_TYPE = 0;
+    private static final byte HELLO_ELEMENT_HEADER_SIZE = 4;
+
+    private static void serializeElementsList(HelloInput message, ByteBuf output) {
+        int[] versionBitmap;
+        if (message.getElements() != null) {
+            for (Elements currElement : message.getElements()) {
+                int elementStartIndex = output.writerIndex();
+                output.writeShort(currElement.getType().getIntValue());
+                if (currElement.getType().equals(HelloElementType.VERSIONBITMAP)) {
+                    int elementLengthIndex = output.writerIndex();
+                    output.writeShort(EncodeConstants.EMPTY_LENGTH);
+                    versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap());
+                    for (int i = 0; i < versionBitmap.length; i++) {
+                        output.writeInt(versionBitmap[i]);
+                    }
+                    int length = output.writerIndex() - elementStartIndex;
+                    int padding = length - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;
+                    output.writeZero(padding);
+                    output.setShort(elementLengthIndex, output.writerIndex() - elementStartIndex);
+                }
+            }
+        }
+    }
+
+    @Override
+    public void serialize(HelloInput message, ByteBuf outBuffer) {
+        int startWriterIndex = outBuffer.writerIndex();
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        serializeElementsList(message, outBuffer);
+        int endWriterIndex = outBuffer.writerIndex();
+        int paddingRemainder = (endWriterIndex - startWriterIndex) % EncodeConstants.PADDING;
+        if (paddingRemainder != 0) {
+            outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
+        }
+        ByteBufUtils.updateOFHeaderLength(outBuffer);
+    }
+
+}