From 7494ef8cb011ce850af5d1cf33e90e212fdc2a97 Mon Sep 17 00:00:00 2001 From: Michal Polkorab Date: Mon, 4 Nov 2013 16:28:38 +0100 Subject: [PATCH] hello-VersionBitmap fixed added generic type to OFSerializer.computeLength() Signed-off-by: Michal Polkorab Change-Id: Iae422c353e0f0da6dfa864781dc47688bcdad71a --- .../factories/HelloMessageFactory.java | 27 ++++-- .../impl/serialization/OFSerializer.java | 2 +- .../factories/BarrierInputMessageFactory.java | 2 +- .../factories/EchoInputMessageFactory.java | 2 +- .../EchoReplyInputMessageFactory.java | 2 +- .../factories/EncodeConstants.java | 13 +++ .../ExperimenterInputMessageFactory.java | 2 +- .../factories/FlowModInputMessageFactory.java | 2 +- .../GetAsyncRequestMessageFactory.java | 2 +- .../GetConfigInputMessageFactory.java | 2 +- .../GetFeaturesInputMessageFactory.java | 2 +- .../GetQueueConfigInputMessageFactory.java | 2 +- .../GroupModInputMessageFactory.java | 2 +- .../factories/HelloInputMessageFactory.java | 83 ++++++++++++------- .../MeterModInputMessageFactory.java | 2 +- .../MultipartRequestMessageFactory.java | 2 +- .../PacketOutInputMessageFactory.java | 2 +- .../factories/PortModInputMessageFactory.java | 2 +- .../RoleRequestInputMessageFactory.java | 2 +- .../SetAsyncInputMessageFactory.java | 2 +- .../factories/SetConfigMessageFactory.java | 2 +- .../TableModInputMessageFactory.java | 2 +- .../protocol/impl/util/ByteBufUtils.java | 4 +- .../factories/HelloMessageFactoryTest.java | 12 ++- .../ExperimenterInputMessageFactoryTest.java | 2 +- .../FlowModInputMessageFactoryTest.java | 2 +- .../GroupModInputMessageFactoryTest.java | 2 +- .../HelloInputMessageFactoryTest.java | 46 +++++++--- .../MeterModInputMessageFactoryTest.java | 2 +- .../MultipartRequestMessageFactoryTest.java | 20 ++--- .../SetAsyncInputMessageFactoryTest.java | 2 +- 31 files changed, 163 insertions(+), 90 deletions(-) create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EncodeConstants.java diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactory.java index 78ee0ace..4f8113ee 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactory.java @@ -7,6 +7,8 @@ import java.util.List; import io.netty.buffer.ByteBuf; import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.HelloInputMessageFactory; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder; @@ -47,16 +49,25 @@ public class HelloMessageFactory implements OFDeserializer { } private static List readElement(ByteBuf input) { - ElementsBuilder elementsBuilder = new ElementsBuilder(); List elementsList = new ArrayList<>(); - elementsBuilder.setType(HelloElementType.forValue(input.readUnsignedShort())); - int arrayLength = input.readableBytes()/4; - int[] versionBitmap = new int[arrayLength]; - for (int i = 0; i < arrayLength; i++) { - versionBitmap[i] = (int) input.readUnsignedInt(); + while (input.readableBytes() > 0) { + ElementsBuilder elementsBuilder = new ElementsBuilder(); + int type = input.readUnsignedShort(); + int elementLength = input.readUnsignedShort(); + if (type == HelloElementType.VERSIONBITMAP.getIntValue()) { + elementsBuilder.setType(HelloElementType.forValue(type)); + int[] versionBitmap = new int[(elementLength - HelloInputMessageFactory.HELLO_ELEMENT_HEADER_SIZE) / 4]; + for (int i = 0; i < versionBitmap.length; i++) { + versionBitmap[i] = (int) input.readUnsignedInt(); + } + elementsBuilder.setVersionBitmap(readVersionBitmap(versionBitmap)); + int paddingRemainder = elementLength % EncodeConstants.PADDING; + if (paddingRemainder != 0) { + input.readBytes(EncodeConstants.PADDING - paddingRemainder); + } + } + elementsList.add(elementsBuilder.build()); } - elementsBuilder.setVersionBitmap(readVersionBitmap(versionBitmap)); - elementsList.add(elementsBuilder.build()); return elementsList; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/OFSerializer.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/OFSerializer.java index bbd70fe5..0148ffb9 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/OFSerializer.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/OFSerializer.java @@ -24,7 +24,7 @@ public interface OFSerializer { * Compute length of received message * @return computed length */ - public abstract int computeLength(); + public abstract int computeLength(E message); /** * diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactory.java index 83f77ba6..f32b7142 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactory.java @@ -38,7 +38,7 @@ public class BarrierInputMessageFactory implements OFSerializer { } @Override - public int computeLength() { + public int computeLength(BarrierInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactory.java index 5c7ac67e..0965cb5d 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactory.java @@ -38,7 +38,7 @@ public class EchoInputMessageFactory implements OFSerializer { } @Override - public int computeLength() { + public int computeLength(EchoInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactory.java index 11dbdef7..4ef83a7a 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactory.java @@ -39,7 +39,7 @@ public class EchoReplyInputMessageFactory implements OFSerializer { } @Override - public int computeLength() { + public int computeLength(FlowModInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetAsyncRequestMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetAsyncRequestMessageFactory.java index 180f0bdc..33fac000 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetAsyncRequestMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetAsyncRequestMessageFactory.java @@ -37,7 +37,7 @@ public class GetAsyncRequestMessageFactory implements OFSerializer } @Override - public int computeLength() { + public int computeLength(GroupModInput message) { return MESSAGE_LENGTH; } 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 27af1639..d2b7f33c 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,15 +1,17 @@ /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; -import java.util.Iterator; -import java.util.List; - 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; /** * @author michal.polkorab @@ -19,9 +21,14 @@ public class HelloInputMessageFactory implements OFSerializer{ /** Code type of Hello message */ private static final byte MESSAGE_TYPE = 0; - private static int MESSAGE_LENGTH = 0; + private static int MESSAGE_LENGTH = 8; + 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 } @@ -38,14 +45,31 @@ public class HelloInputMessageFactory implements OFSerializer{ @Override public void messageToBuffer(short version, ByteBuf out, HelloInput message) { - computeElementsLength(message.getElements()); + int startWriterIndex = out.writerIndex(); ByteBufUtils.writeOFHeader(instance, message, out); - encodeElementsList(message.getElements(), out); + encodeElementsList(message, out); + int endWriterIndex = out.writerIndex(); + int writtenBytesDiff = computeLength(message) - (endWriterIndex - startWriterIndex); + LOGGER.info("writtenbytes: " + writtenBytesDiff); + ByteBufUtils.padBuffer(writtenBytesDiff, out); } @Override - public int computeLength() { - return MESSAGE_LENGTH; + 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())) { + length += computeVersionBitmapLength(element); + } + } + int paddingRemainder = length % EncodeConstants.PADDING; + if (paddingRemainder != 0) { + length += EncodeConstants.PADDING - paddingRemainder; + } + } + return length; } @Override @@ -53,32 +77,33 @@ public class HelloInputMessageFactory implements OFSerializer{ return MESSAGE_TYPE; } - private static int computeElementsLength(List elements) { - int versionBitmapSize = 0; - final int ofHeaderSize = 8; - int typeSize = 0; - - if (elements != null) { - typeSize = 2; - versionBitmapSize = elements.get(0).getVersionBitmap().size()/Byte.SIZE; - } - MESSAGE_LENGTH = ofHeaderSize + versionBitmapSize + typeSize; - return MESSAGE_LENGTH; - } - - private static void encodeElementsList(List elements, ByteBuf output) { + private static void encodeElementsList(HelloInput message, ByteBuf output) { int[] versionBitmap; int arraySize = 0; - if (elements != null) { - for (Iterator iterator = elements.iterator(); iterator.hasNext();) { - Elements currElement = iterator.next(); + if (message.getElements() != null) { + for (Elements currElement : message.getElements()) { output.writeShort(currElement.getType().getIntValue()); - versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap()); - arraySize = (versionBitmap.length/Integer.SIZE); - for (int i = 0; i < arraySize; i++) { - output.writeInt(versionBitmap[i]); + + if (currElement.getType().equals(HelloElementType.VERSIONBITMAP)) { + short bitmapLength = computeVersionBitmapLength(currElement); + output.writeShort(bitmapLength); + versionBitmap = ByteBufUtils.fillBitMaskFromList(currElement.getVersionBitmap()); + arraySize = (versionBitmap.length/Integer.SIZE); + for (int i = 0; i < arraySize; i++) { + output.writeInt(versionBitmap[i]); + } + int padding = bitmapLength - arraySize * 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) * 4; + } + return elementlength; + } } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactory.java index 681638a2..463c82d4 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactory.java @@ -55,7 +55,7 @@ public class MeterModInputMessageFactory implements OFSerializer } @Override - public int computeLength() { + public int computeLength(MeterModInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MultipartRequestMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MultipartRequestMessageFactory.java index c3026336..7b9f7b11 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MultipartRequestMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MultipartRequestMessageFactory.java @@ -76,7 +76,7 @@ public class MultipartRequestMessageFactory implements OFSerializer { } @Override - public int computeLength() { + public int computeLength(PortModInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/RoleRequestInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/RoleRequestInputMessageFactory.java index 6a4d43f8..969df050 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/RoleRequestInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/RoleRequestInputMessageFactory.java @@ -43,7 +43,7 @@ public class RoleRequestInputMessageFactory implements OFSerializer } @Override - public int computeLength() { + public int computeLength(SetAsyncInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetConfigMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetConfigMessageFactory.java index 86cf0067..b43f1862 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetConfigMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetConfigMessageFactory.java @@ -42,7 +42,7 @@ public class SetConfigMessageFactory implements OFSerializer { } @Override - public int computeLength() { + public int computeLength(SetConfigInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactory.java index 3a30b3ee..668da91b 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactory.java @@ -44,7 +44,7 @@ public class TableModInputMessageFactory implements OFSerializer } @Override - public int computeLength() { + public int computeLength(TableModInput message) { return MESSAGE_LENGTH; } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtils.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtils.java index 975f49ad..4c212514 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtils.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtils.java @@ -98,10 +98,10 @@ public abstract class ByteBufUtils { * @param message POJO * @param out writing buffer */ - public static void writeOFHeader(OFSerializer factory, OfHeader message, ByteBuf out) { + public static void writeOFHeader(OFSerializer factory, E message, ByteBuf out) { out.writeByte(message.getVersion()); out.writeByte(factory.getMessageType()); - out.writeShort(factory.computeLength()); + out.writeShort(factory.computeLength(message)); out.writeInt(message.getXid().intValue()); } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactoryTest.java index 756760ba..b2433433 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/HelloMessageFactoryTest.java @@ -28,15 +28,19 @@ public class HelloMessageFactoryTest { */ @Test public void test() { - ByteBuf bb = BufferHelper.buildBuffer("00 01 "+ //type - "00 00 00 11 "+//booelan element 1 - "00 00 00 11"//booelan element 2 + ByteBuf bb = BufferHelper.buildBuffer("00 01 " // type + + "00 0c " // length + + "00 00 00 11 " // bitmap 1 + + "00 00 00 00 " // bitmap 2 + + "00 00 00 00" // padding ); HelloMessage builtByFactory = BufferHelper.decodeV13( HelloMessageFactory.getInstance(), bb); BufferHelper.checkHeaderV13(builtByFactory); - Assert.assertEquals("Wrong type", createElement().get(0).getType().getIntValue(), builtByFactory.getElements().get(0).getType().getIntValue()); + List element = createElement(); + Assert.assertEquals("Wrong type", element.get(0).getType(), builtByFactory.getElements().get(0).getType()); + Assert.assertEquals("Wrong versionBitmap", element.get(0).getVersionBitmap(), builtByFactory.getElements().get(0).getVersionBitmap()); } private static List createElement() { diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java index 8615225f..1bea8f42 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java @@ -36,7 +36,7 @@ public class ExperimenterInputMessageFactoryTest { ExperimenterInputMessageFactory factory = ExperimenterInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); - BufferHelper.checkHeaderV13(out, EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE, factory.computeLength()); + BufferHelper.checkHeaderV13(out, EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE, factory.computeLength(message)); Assert.assertEquals("Wrong experimenter", 0x0001020304L, out.readUnsignedInt()); Assert.assertEquals("Wrong expType", 0x0001020304L, out.readUnsignedInt()); Assert.assertArrayEquals("Wrong data", message.getData(), readData(out)); diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java index b57f8c35..583a7f34 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java @@ -49,7 +49,7 @@ public class FlowModInputMessageFactoryTest { FlowModInputMessageFactory factory = FlowModInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); - BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message)); Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), out.readLong()); Assert.assertEquals("Wrong cookieMask", message.getCookieMask().longValue(), out.readLong()); Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(), out.readByte()); diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactoryTest.java index d2162da7..bc5950fd 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactoryTest.java @@ -45,7 +45,7 @@ public class GroupModInputMessageFactoryTest { GroupModInputMessageFactory factory = GroupModInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); - BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message)); Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readShort()); Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readByte()); out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE); diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactoryTest.java index 1388a055..756a1101 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/HelloInputMessageFactoryTest.java @@ -11,11 +11,14 @@ import org.junit.Assert; import org.junit.Test; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +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.HelloInputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author michal.polkorab @@ -23,6 +26,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 */ public class HelloInputMessageFactoryTest { + private static final Logger LOGGER = LoggerFactory.getLogger(HelloInputMessageFactoryTest.class); + /** * Testing of {@link HelloInputMessageFactory} for correct translation from POJO * @throws Exception @@ -37,7 +42,7 @@ public class HelloInputMessageFactoryTest { HelloInputMessageFactory himf = HelloInputMessageFactory.getInstance(); himf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, hi); - BufferHelper.checkHeaderV13(out, himf.getMessageType(), himf.computeLength()); + BufferHelper.checkHeaderV13(out, himf.getMessageType(), himf.computeLength(hi)); } /** @@ -48,17 +53,21 @@ public class HelloInputMessageFactoryTest { public void testWithElementsSet() throws Exception { HelloInputBuilder builder = new HelloInputBuilder(); BufferHelper.setupHeader(builder); - builder.setElements(createElement()); + List expectedElement = createElement(); + builder.setElements(expectedElement); HelloInput message = builder.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); HelloInputMessageFactory factory = HelloInputMessageFactory.getInstance(); factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); + LOGGER.info("bytebuf: " + ByteBufUtils.byteBufToHexString(out)); - BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength(message)); Elements element = readElement(out).get(0); - Assert.assertEquals("Wrong element type", createElement().get(0).getType(), element.getType()); - Assert.assertArrayEquals("Wrong element bitmap", createElement().get(0).getVersionBitmap().toArray(), element.getVersionBitmap().toArray()); + Assert.assertEquals("Wrong element type", expectedElement.get(0).getType(), element.getType()); + LOGGER.info(expectedElement.get(0).getVersionBitmap().toString()); + LOGGER.info(element.getVersionBitmap().toString()); + Assert.assertArrayEquals("Wrong element bitmap", expectedElement.get(0).getVersionBitmap().toArray(), element.getVersionBitmap().toArray()); } private static List createElement() { @@ -78,20 +87,30 @@ public class HelloInputMessageFactoryTest { } private static List readElement(ByteBuf input) { - ElementsBuilder elementsBuilder = new ElementsBuilder(); List elementsList = new ArrayList<>(); - elementsBuilder.setType(HelloElementType.forValue(input.readUnsignedShort())); - int arrayLength = input.readableBytes()/4; - int[] versionBitmap = new int[arrayLength]; - for (int i = 0; i < arrayLength; i++) { - versionBitmap[i] = (int) input.readUnsignedInt(); + while (input.readableBytes() > 0) { + ElementsBuilder elementsBuilder = new ElementsBuilder(); + int type = input.readUnsignedShort(); + int elementLength = input.readUnsignedShort(); + if (type == HelloElementType.VERSIONBITMAP.getIntValue()) { + elementsBuilder.setType(HelloElementType.forValue(type)); + int[] versionBitmap = new int[(elementLength - HelloInputMessageFactory.HELLO_ELEMENT_HEADER_SIZE) / 4]; + for (int i = 0; i < versionBitmap.length; i++) { + versionBitmap[i] = (int) input.readUnsignedInt(); + } + elementsBuilder.setVersionBitmap(readVersionBitmap(versionBitmap)); + int paddingRemainder = elementLength % EncodeConstants.PADDING; + if (paddingRemainder != 0) { + input.readBytes(EncodeConstants.PADDING - paddingRemainder); + } + } + elementsList.add(elementsBuilder.build()); } - elementsBuilder.setVersionBitmap(readVersionBitmap(versionBitmap)); - elementsList.add(elementsBuilder.build()); return elementsList; } private static List readVersionBitmap(int[] input){ + LOGGER.info("input.length: " + input.length); List versionBitmapList = new ArrayList<>(); for (int i = 0; i < input.length; i++) { int mask = input[i]; @@ -99,6 +118,7 @@ public class HelloInputMessageFactoryTest { versionBitmapList.add((mask & (1<