Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / HelloInputMessageFactory.java
index 1a35932809efe910cd2b250f58d5e25c78de9ce8..a0ffc2dd9354268dabd26e99cc1aeaf010889baa 100644 (file)
-/* 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.List;\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.EncodeConstants;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;\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
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-/**\r
- * Translates Hello messages\r
- * @author michal.polkorab\r
- * @author timotej.kubas\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 = 8;\r
-    /** Size of hello element header (in bytes) */\r
-    public static final byte HELLO_ELEMENT_HEADER_SIZE = 4;\r
-    private static HelloInputMessageFactory instance;\r
-    \r
-    private static final Logger LOGGER = LoggerFactory\r
-            .getLogger(HelloInputMessageFactory.class);\r
-    \r
-    \r
-    private HelloInputMessageFactory() {\r
-        // do nothing, just singleton\r
-    }\r
-    \r
-    /**\r
-     * @return singleton factory\r
-     */\r
-    public static synchronized 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
-        int startWriterIndex = out.writerIndex();\r
-        ByteBufUtils.writeOFHeader(instance, message, out);\r
-        encodeElementsList(message, out);\r
-        int endWriterIndex = out.writerIndex();\r
-        int writtenBytesDiff = computeLength(message) - (endWriterIndex - startWriterIndex);\r
-        LOGGER.debug("writtenbytes: " + writtenBytesDiff);\r
-        ByteBufUtils.padBuffer(writtenBytesDiff, out);\r
-    }\r
-\r
-    @Override\r
-    public int computeLength(HelloInput message) {\r
-        int length = MESSAGE_LENGTH;\r
-        List<Elements> elements = message.getElements();\r
-        if (elements != null) {\r
-            for (Elements element : elements) {\r
-                if (HelloElementType.VERSIONBITMAP.equals(element.getType())) {\r
-                    int bitmapLength = computeVersionBitmapLength(element);\r
-                    int paddingRemainder = bitmapLength % EncodeConstants.PADDING;\r
-                    if (paddingRemainder != 0) {\r
-                        bitmapLength += EncodeConstants.PADDING - paddingRemainder;\r
-                    }\r
-                    length += bitmapLength;\r
-                }\r
-            }\r
-        }\r
-        return length;\r
-    }\r
-\r
-    @Override\r
-    public byte getMessageType() {\r
-        return MESSAGE_TYPE;\r
-    }\r
-    \r
-    private static void encodeElementsList(HelloInput message, ByteBuf output) {\r
-        int[] versionBitmap;\r
-        if (message.getElements() != null) {\r
-            for (Elements currElement : message.getElements()) {\r
-                output.writeShort(currElement.getType().getIntValue());\r
-                if (currElement.getType().equals(HelloElementType.VERSIONBITMAP)) {\r
-                    short bitmapLength = computeVersionBitmapLength(currElement);\r
-                    output.writeShort(bitmapLength);\r
-                    versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap());\r
-                    LOGGER.debug("vbs: " + versionBitmap.length);\r
-                    LOGGER.debug("Version bitmap (below):");\r
-                    for (int i = 0; i < versionBitmap.length; i++) {\r
-                        LOGGER.debug(Integer.toBinaryString(versionBitmap[i]));\r
-                        output.writeInt(versionBitmap[i]);\r
-                    }\r
-                    int padding = bitmapLength - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;\r
-                    ByteBufUtils.padBuffer(padding , output);\r
-                }\r
-            } \r
-        }\r
-    }\r
-    \r
-    private static short computeVersionBitmapLength(Elements element) {\r
-        short elementlength = HELLO_ELEMENT_HEADER_SIZE;\r
-        if (!element.getVersionBitmap().isEmpty()) {\r
-            elementlength += ((element.getVersionBitmap().size() - 1) / Integer.SIZE + 1) * (EncodeConstants.SIZE_OF_INT_IN_BYTES);\r
-        }\r
-        return elementlength;\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.impl.serialization.OFSerializer;
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
+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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * 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 int MESSAGE_LENGTH = 8;
+    /** Size of hello element header (in bytes) */
+    public static final byte HELLO_ELEMENT_HEADER_SIZE = 4;
+    private static HelloInputMessageFactory instance;
+    
+    private static final Logger LOGGER = LoggerFactory
+            .getLogger(HelloInputMessageFactory.class);
+    
+    
+    private HelloInputMessageFactory() {
+        // do nothing, just singleton
+    }
+    
+    /**
+     * @return singleton factory
+     */
+    public static synchronized HelloInputMessageFactory getInstance() {
+        if (instance == null) {
+            instance = new HelloInputMessageFactory();
+        }
+        return instance;
+    }
+
+    @Override
+    public void messageToBuffer(short version, ByteBuf out, HelloInput message) {
+        int startWriterIndex = out.writerIndex();
+        ByteBufUtils.writeOFHeader(instance, message, out);
+        encodeElementsList(message, out);
+        int endWriterIndex = out.writerIndex();
+        int writtenBytesDiff = computeLength(message) - (endWriterIndex - startWriterIndex);
+        LOGGER.debug("writtenbytes: " + writtenBytesDiff);
+        ByteBufUtils.padBuffer(writtenBytesDiff, out);
+    }
+
+    @Override
+    public int computeLength(HelloInput message) {
+        int length = MESSAGE_LENGTH;
+        List<Elements> elements = message.getElements();
+        if (elements != null) {
+            for (Elements element : elements) {
+                if (HelloElementType.VERSIONBITMAP.equals(element.getType())) {
+                    int bitmapLength = computeVersionBitmapLength(element);
+                    int paddingRemainder = bitmapLength % EncodeConstants.PADDING;
+                    if (paddingRemainder != 0) {
+                        bitmapLength += EncodeConstants.PADDING - paddingRemainder;
+                    }
+                    length += bitmapLength;
+                }
+            }
+        }
+        return length;
+    }
+
+    @Override
+    public byte getMessageType() {
+        return MESSAGE_TYPE;
+    }
+    
+    private static void encodeElementsList(HelloInput message, ByteBuf output) {
+        int[] versionBitmap;
+        if (message.getElements() != null) {
+            for (Elements currElement : message.getElements()) {
+                output.writeShort(currElement.getType().getIntValue());
+                if (currElement.getType().equals(HelloElementType.VERSIONBITMAP)) {
+                    short bitmapLength = computeVersionBitmapLength(currElement);
+                    output.writeShort(bitmapLength);
+                    versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap());
+                    LOGGER.debug("vbs: " + versionBitmap.length);
+                    LOGGER.debug("Version bitmap (below):");
+                    for (int i = 0; i < versionBitmap.length; i++) {
+                        LOGGER.debug(Integer.toBinaryString(versionBitmap[i]));
+                        output.writeInt(versionBitmap[i]);
+                    }
+                    int padding = bitmapLength - versionBitmap.length * 4 - HELLO_ELEMENT_HEADER_SIZE;
+                    ByteBufUtils.padBuffer(padding , output);
+                }
+            } 
+        }
+    }
+    
+    private static short computeVersionBitmapLength(Elements element) {
+        short elementlength = HELLO_ELEMENT_HEADER_SIZE;
+        if (!element.getVersionBitmap().isEmpty()) {
+            elementlength += ((element.getVersionBitmap().size() - 1) / Integer.SIZE + 1) * (EncodeConstants.SIZE_OF_INT_IN_BYTES);
+        }
+        return elementlength;
+    }
+}