-/* 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) * (Integer.SIZE / Byte.SIZE);\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 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);
+ }
+
+}