X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fserialization%2Ffactories%2FHelloInputMessageFactory.java;h=bd87e16f67e26a50717f0bbfd8eceaf690a3680d;hb=07de1ed897da9d7dc70c6d550f38c59339ed751e;hp=303eb6c8055e1db3fa8547bcfdef1d9e7d707773;hpb=5b05fc8e7dd2b61a076dfd1864a205b0995c7722;p=openflowjava.git diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactory.java index 303eb6c8..bd87e16f 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactory.java @@ -1,112 +1,66 @@ -/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ -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.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{ - - /** 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 = 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) * (Integer.SIZE / Byte.SIZE); - } - return elementlength; - } -} +/* + * 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{ + + /** 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); + } + +}