From f7b135c5a0fab2841b445a567b27271081722984 Mon Sep 17 00:00:00 2001 From: "michal.polkorab" Date: Mon, 16 Sep 2013 13:36:22 +0200 Subject: [PATCH] Added more (de)serialization factories + ByteBufUtils methods Mantaining code - extracting methods in tests Signed-off-by: michal.polkorab Change-Id: I07fdf59fa2602bc78630b8b0946381c3f3e146fe --- .../factories/ErrorMessageFactory.java | 4 +- .../FeaturesReplyMessageFactory.java | 1 - .../factories/FlowRemovedMessageFactory.java | 68 +++++++++++ .../GetConfigReplyMessageFactory.java | 44 +++++++ .../MultipartReplyMessageFactory.java | 52 ++++++++ .../factories/PacketInMessageFactory.java | 56 +++++++++ .../factories/PortStatusMessageFactory.java | 50 ++++++++ .../ExperimenterInputMessageFactory.java | 45 +++++++ .../PacketOutInputMessageFactory.java | 51 ++++++++ .../RoleRequestInputMessageFactory.java | 49 ++++++++ .../factories/SetConfigMessageFactory.java | 5 +- .../protocol/impl/util/ByteBufUtils.java | 46 +++++++ .../BarrierReplyMessageFactoryTest.java | 10 +- .../EchoReplyMessageFactoryTest.java | 16 +-- .../EchoRequestMessageFactoryTest.java | 15 +-- .../ExperimenterMessageFactoryTest.java | 14 +-- .../FeaturesReplyMessageFactoryTest.java | 26 ++-- .../FlowRemovedMessageFactoryTest.java | 43 +++++++ .../GetConfigReplyMessageFactoryTest.java | 34 ++++++ .../factories/HelloMessageFactoryTest.java | 18 ++- .../MultipartReplyMessageFactoryTest.java | 31 +++++ .../factories/PacketInMessageFactoryTest.java | 36 ++++++ .../PortStatusMessageFactoryTest.java | 29 +++++ .../BarrierInputMessageFactoryTest.java | 14 +-- .../EchoInputMessageFactoryTest.java | 14 +-- .../EchoReplyInputMessageFactoryTest.java | 14 +-- .../ExperimenterInputMessageFactoryTest.java | 44 +++++++ .../GetConfigInputMessageFactoryTest.java | 14 +-- .../GetFeaturesInputMessageFactoryTest.java | 14 +-- .../HelloInputMessageFactoryTest.java | 45 +++++-- .../protocol/impl/util/BufferHelper.java | 113 +++++++++++++++++- .../protocol/impl/util/ByteBufUtilsTest.java | 55 +++++++++ 32 files changed, 952 insertions(+), 118 deletions(-) create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FlowRemovedMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PacketOutInputMessageFactory.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/RoleRequestInputMessageFactory.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FlowRemovedMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java create mode 100644 openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtilsTest.java diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ErrorMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ErrorMessageFactory.java index 691041ea..e4122ac4 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ErrorMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ErrorMessageFactory.java @@ -39,7 +39,9 @@ private static ErrorMessageFactory instance; // TODO - finish implementation after enums are generated with proper funcionality //emb.setType(); emb.setCode(rawMessage.readInt()); - //emb.setData(value); + byte[] data = new byte[rawMessage.readableBytes()]; + rawMessage.readBytes(data); + emb.setData(data); return emb.build(); } diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactory.java index a33c8092..cb111cff 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactory.java @@ -46,7 +46,6 @@ public class FeaturesReplyMessageFactory implements OFDeserializer { + + private static FlowRemovedMessageFactory instance; + + private FlowRemovedMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static FlowRemovedMessageFactory getInstance(){ + if(instance == null){ + instance = new FlowRemovedMessageFactory(); + } + + return instance; + } + + @Override + public FlowRemovedMessage bufferToMessage(ByteBuf rawMessage, short version) { + FlowRemovedMessageBuilder frmb = new FlowRemovedMessageBuilder(); + + frmb.setVersion(version); + frmb.setXid(rawMessage.readUnsignedInt()); + + byte[] cookie = new byte[8]; + rawMessage.readBytes(cookie); + frmb.setCookie(new BigInteger(cookie)); + frmb.setPriority(rawMessage.readUnsignedShort()); + +// TODO enum! +// frmb.setReason(FlowRemovedReason.values()[rawMessage.readInt()]); + rawMessage.skipBytes(1); //instead of setReason + + frmb.setTableId(new TableId((long)rawMessage.readUnsignedByte())); + frmb.setDurationSec(rawMessage.readUnsignedInt()); + frmb.setDurationNsec(rawMessage.readUnsignedInt()); + frmb.setIdleTimeout(rawMessage.readUnsignedShort()); + frmb.setHardTimeout(rawMessage.readUnsignedShort()); + + byte[] packet_count = new byte[8]; + rawMessage.readBytes(packet_count); + frmb.setPacketCount(new BigInteger(packet_count)); + + byte[] byte_count = new byte[8]; + rawMessage.readBytes(byte_count); + frmb.setByteCount(new BigInteger(byte_count)); + + return frmb.build(); + } + +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactory.java new file mode 100644 index 00000000..8d548992 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactory.java @@ -0,0 +1,44 @@ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutputBuilder; + +/** + * @author michal.polkorab + * @author timotej.kubas + */ +public class GetConfigReplyMessageFactory implements OFDeserializer { + + private static GetConfigReplyMessageFactory instance; + + private GetConfigReplyMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static GetConfigReplyMessageFactory getInstance(){ + if(instance == null){ + instance = new GetConfigReplyMessageFactory(); + } + + return instance; + } + + @Override + public GetConfigOutput bufferToMessage(ByteBuf rawMessage, short version) { + GetConfigOutputBuilder gcob = new GetConfigOutputBuilder(); + gcob.setVersion(version); + gcob.setXid(rawMessage.readUnsignedInt()); + // TODO - waiting for enum funcionality +// gcob.setFlags(SwitchConfigFlag.values()[rawMessage.readInt()]); + gcob.setMissSendLen(rawMessage.readUnsignedShort()); + return gcob.build(); + } + + +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactory.java new file mode 100644 index 00000000..d43b83cd --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactory.java @@ -0,0 +1,52 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class MultipartReplyMessageFactory implements OFDeserializer { + + private static MultipartReplyMessageFactory instance; + private static final byte PADDING_IN_MULTIPART_REPLY_HEADER = 4; + + private MultipartReplyMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static MultipartReplyMessageFactory getInstance(){ + if (instance == null){ + + instance = new MultipartReplyMessageFactory(); + } + + return instance; + } + + @Override + public MultipartReplyMessage bufferToMessage(ByteBuf rawMessage, short version) { + MultipartReplyMessageBuilder mrmb = new MultipartReplyMessageBuilder(); + mrmb.setVersion(version); + mrmb.setXid(rawMessage.readUnsignedInt()); + +// TODO enum MultipartType +// mrmb.setType(MultipartType.values()[rawMessage.readInt()]); + rawMessage.skipBytes(2); //instead of enum + + mrmb.setFlags(new MultipartRequestFlags((rawMessage.readUnsignedShort() & 0x01) > 0)); + rawMessage.skipBytes(PADDING_IN_MULTIPART_REPLY_HEADER); + mrmb.setBody(rawMessage.readBytes(rawMessage.readableBytes()).array()); + + return mrmb.build(); + } +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactory.java new file mode 100644 index 00000000..a8733aff --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactory.java @@ -0,0 +1,56 @@ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import java.math.BigInteger; + +import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder; + +/** + * @author michal.polkorab + * @author timotej.kubas + */ +public class PacketInMessageFactory implements OFDeserializer { + + private static PacketInMessageFactory instance; + private static final byte PADDING_IN_PACKET_IN_HEADER = 2; + + private PacketInMessageFactory() { + // Singleton + } + + /** + * @return singleton factory + */ + public static PacketInMessageFactory getInstance(){ + if(instance == null){ + instance = new PacketInMessageFactory(); + } + + return instance; + } + + @Override + public PacketInMessage bufferToMessage(ByteBuf rawMessage, short version) { + PacketInMessageBuilder pimb = new PacketInMessageBuilder(); + pimb.setVersion(version); + pimb.setXid(rawMessage.readUnsignedInt()); + pimb.setBufferId(rawMessage.readUnsignedInt()); + pimb.setTotalLen(rawMessage.readUnsignedShort()); + pimb.setReason(rawMessage.readUnsignedByte()); + pimb.setTableId(new TableId((long)rawMessage.readUnsignedByte())); + + byte[] cookie = new byte[8]; + rawMessage.readBytes(cookie); + pimb.setCookie(new BigInteger(cookie)); + // TODO - implement match factories to finish this factory + rawMessage.skipBytes(PADDING_IN_PACKET_IN_HEADER); + + pimb.setData(rawMessage.readBytes(rawMessage.readableBytes()).array()); + + return pimb.build(); + } +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactory.java new file mode 100644 index 00000000..79183330 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactory.java @@ -0,0 +1,50 @@ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessageBuilder; + +/** + * @author michal.polkorab + * @author timotej.kubas + */ +public class PortStatusMessageFactory implements OFDeserializer { + + private static PortStatusMessageFactory instance; + private static final byte PADDING_IN_FEATURES_REPLY_HEADER = 7; + + private PortStatusMessageFactory() { + // Singleton + } + + /** + * @return singleton factory + */ + public static PortStatusMessageFactory getInstance(){ + if(instance == null){ + + instance = new PortStatusMessageFactory(); + } + + return instance; + } + + @Override + public PortStatusMessage bufferToMessage(ByteBuf rawMessage, short version) { + PortStatusMessageBuilder psmb = new PortStatusMessageBuilder(); + psmb.setVersion(version); + psmb.setXid(rawMessage.readUnsignedInt()); + +// TODO enum portReason +// psmb.setReason(PortReason.values()[rawMessage.readInt()]); + rawMessage.skipBytes(1); //instead of portReason enum + + rawMessage.skipBytes(PADDING_IN_FEATURES_REPLY_HEADER); + return psmb.build(); + } + + + +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactory.java new file mode 100644 index 00000000..fde87133 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactory.java @@ -0,0 +1,45 @@ +/* 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 org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; +import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; + +/** + * @author michal.polkorab + * + */ +public class ExperimenterInputMessageFactory implements OFSerializer{ + + /** Code type of Experimenter message */ + public static final byte MESSAGE_TYPE = 4; + private static ExperimenterInputMessageFactory instance; + + private ExperimenterInputMessageFactory() { + // do nothing, just singleton + } + + /** + * @return singleton factory + */ + public static ExperimenterInputMessageFactory getInstance() { + if (instance == null) { + instance = new ExperimenterInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, + ExperimenterInput message) { + out.writeByte(message.getVersion()); + out.writeByte(MESSAGE_TYPE); + out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER + (Integer.SIZE/Byte.SIZE)*2); + out.writeInt(message.getXid().intValue()); + out.writeInt(message.getExperimenter().intValue()); + out.writeInt(message.getExpType().intValue()); + } + +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PacketOutInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PacketOutInputMessageFactory.java new file mode 100644 index 00000000..9a16738b --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PacketOutInputMessageFactory.java @@ -0,0 +1,51 @@ +/* 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 org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; +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.protocol.rev130731.PacketOutInput; + +/** + * @author michal.polkorab + * + */ +public class PacketOutInputMessageFactory implements OFSerializer{ + + /** Code type of PacketOut message */ + public static final byte MESSAGE_TYPE = 13; + private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6; + private static PacketOutInputMessageFactory instance; + + private PacketOutInputMessageFactory() { + // do nothing, just singleton + } + + /** + * @return singleton factory + */ + public static PacketOutInputMessageFactory getInstance() { + if (instance == null) { + instance = new PacketOutInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, + PacketOutInput message) { + out.writeByte(message.getVersion()); + out.writeByte(MESSAGE_TYPE); + out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); + out.writeInt(message.getXid().intValue()); + out.writeInt(message.getBufferId().intValue()); + out.writeInt(message.getInPort().getValue().intValue()); + // TODO - finish implementation after Action serialization is done + //out.writeShort(message.getActions().size()); + ByteBufUtils.padBuffer(PADDING_IN_PACKET_OUT_MESSAGE, out); + + } + +} 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 new file mode 100644 index 00000000..8789d7a5 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/RoleRequestInputMessageFactory.java @@ -0,0 +1,49 @@ +/* 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 org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; +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.protocol.rev130731.RoleRequestInput; + +/** + * @author michal.polkorab + * + */ +public class RoleRequestInputMessageFactory implements OFSerializer { + + /** Code type of RoleRequest message */ + public static final byte MESSAGE_TYPE = 24; + private static final byte PADDING_IN_ROLE_REQUEST_MESSAGE = 4; + private static RoleRequestInputMessageFactory instance; + + private RoleRequestInputMessageFactory() { + // do nothing, just singleton + } + + /** + * @return singleton factory + */ + public static RoleRequestInputMessageFactory getInstance() { + if (instance == null) { + instance = new RoleRequestInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, + RoleRequestInput message) { + out.writeByte(message.getVersion()); + out.writeByte(MESSAGE_TYPE); + out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); + out.writeInt(message.getXid().intValue()); + // TODO - finish implementation after enum support needed funcionality + //out.writeInt(message.getRole()); + ByteBufUtils.padBuffer(PADDING_IN_ROLE_REQUEST_MESSAGE, out); + out.writeLong(message.getGenerationId().longValue()); + } + +} 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 130cd85e..0c6d2265 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 @@ -5,9 +5,6 @@ import io.netty.buffer.ByteBuf; import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsync; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput; /** @@ -42,7 +39,7 @@ public class SetConfigMessageFactory implements OFSerializer { out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); out.writeInt(message.getXid().intValue()); // TODO - finish implementation after enums provide needed funcionality - + out.writeInt(message.getMissSendLen()); } } 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 b5e01e79..3f5e3600 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 @@ -3,6 +3,7 @@ package org.opendaylight.openflowjava.protocol.impl.util; import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; /** Class for common operations on ByteBuf * @@ -23,4 +24,49 @@ public abstract class ByteBufUtils { } return sb.toString(); } + + /** + * Converts String into byte[] + * @param hexSrc input String + * @return byte[] filled with input data + */ + public static byte[] hexStringToBytes(String hexSrc) { + String[] byteChips = hexSrc.split("\\s+"); + byte[] result = new byte[byteChips.length]; + for (int i = 0; i < byteChips.length; i++) { + result[i] = (byte) Short.parseShort(byteChips[i], 16); + } + return result; + } + + /** + * Creates ByteBuf filled with specified data + * @param hexSrc input String of bytes in hex format + * @return ByteBuf with specified hexString converted + */ + public static ByteBuf hexStringToByteBuf(String hexSrc) { + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + hexStringToByteBuf(hexSrc, out); + return out; + } + + /** + * Creates ByteBuf filled with specified data + * @param hexSrc input String of bytes in hex format + * @param out ByteBuf with specified hexString converted + */ + public static void hexStringToByteBuf(String hexSrc, ByteBuf out) { + out.writeBytes(hexStringToBytes(hexSrc)); + } + + /** + * Fills specified ByteBuf with 0 (zeros) of desired length, used for padding + * @param length + * @param out ByteBuf to be padded + */ + public static void padBuffer(int length, ByteBuf out) { + for (int i = 0; i < length; i++) { + out.writeByte(0); + } + } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/BarrierReplyMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/BarrierReplyMessageFactoryTest.java index bbd8af74..d828c852 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/BarrierReplyMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/BarrierReplyMessageFactoryTest.java @@ -3,9 +3,7 @@ package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; import io.netty.buffer.ByteBuf; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.BarrierReplyMessageFactory; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput; @@ -20,10 +18,10 @@ public class BarrierReplyMessageFactoryTest { */ @Test public void test() { - ByteBuf bb = BufferHelper.buildBuffer(new byte[0]); - BarrierOutput builtByFactory = BarrierReplyMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + ByteBuf bb = BufferHelper.buildBuffer(); + BarrierOutput builtByFactory = BufferHelper.decodeV13( + BarrierReplyMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); + BufferHelper.checkHeaderV13(builtByFactory); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoReplyMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoReplyMessageFactoryTest.java index c88db4a9..5338ed03 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoReplyMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoReplyMessageFactoryTest.java @@ -20,11 +20,11 @@ public class EchoReplyMessageFactoryTest { */ @Test public void testWithEmptyDataField() { - ByteBuf bb = BufferHelper.buildBuffer(new byte[0]); - EchoOutput builtByFactory = EchoReplyMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + ByteBuf bb = BufferHelper.buildBuffer(); + EchoOutput builtByFactory = BufferHelper.decodeV13( + EchoReplyMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); + BufferHelper.checkHeaderV13(builtByFactory); } /** @@ -34,10 +34,10 @@ public class EchoReplyMessageFactoryTest { public void testWithDataFieldSet() { byte[] data = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; ByteBuf bb = BufferHelper.buildBuffer(data); - EchoOutput builtByFactory = EchoReplyMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); + EchoOutput builtByFactory = BufferHelper.decodeV13( + EchoReplyMessageFactory.getInstance(), bb); + + BufferHelper.checkHeaderV13(builtByFactory); Assert.assertArrayEquals(builtByFactory.getData(), data); } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoRequestMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoRequestMessageFactoryTest.java index de2c807e..e136f5ad 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoRequestMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/EchoRequestMessageFactoryTest.java @@ -20,11 +20,11 @@ public class EchoRequestMessageFactoryTest { */ @Test public void testWithEmptyDataField() { - ByteBuf bb = BufferHelper.buildBuffer(new byte[0]); - EchoRequestMessage builtByFactory = EchoRequestMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + ByteBuf bb = BufferHelper.buildBuffer(); + EchoRequestMessage builtByFactory = BufferHelper.decodeV13( + EchoRequestMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); + BufferHelper.checkHeaderV13(builtByFactory); } /** @@ -34,10 +34,11 @@ public class EchoRequestMessageFactoryTest { public void testWithDataFieldSet() { byte[] data = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; ByteBuf bb = BufferHelper.buildBuffer(data); - EchoRequestMessage builtByFactory = EchoRequestMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + EchoRequestMessage builtByFactory = BufferHelper.decodeV13( + EchoRequestMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); + BufferHelper.checkHeaderV13(builtByFactory); + Assert.assertArrayEquals(builtByFactory.getData(), data); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java index 4991a48b..091e6a52 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/ExperimenterMessageFactoryTest.java @@ -5,7 +5,6 @@ import io.netty.buffer.ByteBuf; import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.ExperimenterMessageFactory; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage; @@ -20,14 +19,13 @@ public class ExperimenterMessageFactoryTest { */ @Test public void test() { - byte[] data = new byte[]{0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04}; - ByteBuf bb = BufferHelper.buildBuffer(data); - ExperimenterMessage builtByFactory = ExperimenterMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + ByteBuf bb = BufferHelper.buildBuffer("01 02 03 04 01 02 03 04"); + ExperimenterMessage builtByFactory = BufferHelper.decodeV13( + ExperimenterMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); - Assert.assertEquals(builtByFactory.getExperimenter().longValue(), 16909060L); - Assert.assertEquals(builtByFactory.getExpType().longValue(), 16909060L); + BufferHelper.checkHeaderV13(builtByFactory); + Assert.assertEquals(builtByFactory.getExperimenter().longValue(), 0x01020304L); + Assert.assertEquals(builtByFactory.getExpType().longValue(), 0x01020304L); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactoryTest.java index 7f158c99..44cd9664 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FeaturesReplyMessageFactoryTest.java @@ -5,7 +5,6 @@ import io.netty.buffer.ByteBuf; import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.FeaturesReplyMessageFactory; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput; @@ -20,20 +19,19 @@ public class FeaturesReplyMessageFactoryTest { */ @Test public void test() { - byte[] data = new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x00, 0x01, 0x02, 0x03, 0x01, 0x01, 0x00, 0x00, - 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03 }; - ByteBuf bb = BufferHelper.buildBuffer(data); - GetFeaturesOutput builtByFactory = FeaturesReplyMessageFactory.getInstance().bufferToMessage(bb, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + ByteBuf bb = BufferHelper.buildBuffer("00 01 02 03 04 05 06 07 00 01 02 03 01 01 00 00 00" + + " 01 02 03 00 01 02 03"); + GetFeaturesOutput builtByFactory = BufferHelper.decodeV13( + FeaturesReplyMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); - Assert.assertTrue(builtByFactory.getTables() == 1); - Assert.assertTrue(builtByFactory.getAuxiliaryId() == 1); - Assert.assertEquals(66051L, builtByFactory.getBuffers().longValue()); - Assert.assertEquals(66051L, builtByFactory.getCapabilities().longValue()); - Assert.assertEquals(66051L, builtByFactory.getReserved().longValue()); - Assert.assertTrue(builtByFactory.getDatapathId().longValue() == 283686952306183L); + BufferHelper.checkHeaderV13(builtByFactory); + Assert.assertEquals("Wrong datapathId", 0x0001020304050607L, builtByFactory.getDatapathId().longValue()); + Assert.assertEquals("Wrong buffers", 0x00010203L, builtByFactory.getBuffers().longValue()); + Assert.assertEquals("Wrong number of tables", 0x01, builtByFactory.getTables().shortValue()); + Assert.assertEquals("Wrong auxiliaryId", 0x01, builtByFactory.getAuxiliaryId().shortValue()); + Assert.assertEquals("Wrong capabilities", 0x00010203L, builtByFactory.getCapabilities().longValue()); + Assert.assertEquals("Wrong reserved", 0x00010203L, builtByFactory.getReserved().longValue()); + } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FlowRemovedMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FlowRemovedMessageFactoryTest.java new file mode 100644 index 00000000..7f1aa2e0 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/FlowRemovedMessageFactoryTest.java @@ -0,0 +1,43 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class FlowRemovedMessageFactoryTest { + + /** + * Testing {@link FlowRemovedMessageFactory} for correct translation into POJO + */ + @Test + public void test(){ + ByteBuf bb = BufferHelper.buildBuffer("00 01 02 03 04 05 06 07 00 03 00 04 00 00 00 02" + + " 00 00 00 05 00 01 00 03 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07"); + FlowRemovedMessage builtByFactory = BufferHelper.decodeV13(FlowRemovedMessageFactory.getInstance(), bb); + + BufferHelper.checkHeaderV13(builtByFactory); + + Assert.assertTrue(builtByFactory.getCookie().longValue() == 0x0001020304050607L); + Assert.assertTrue(builtByFactory.getPriority() == 0x03); + + // TODO enum type! + // builtByFactory.getReason() + Assert.assertEquals("Wrong tableId", new TableId((long) 4), builtByFactory.getTableId()); + Assert.assertEquals("Wrong durationSec", 0x02L, builtByFactory.getDurationSec().longValue()); + Assert.assertEquals("Wrong durationNsec", 0x05L, builtByFactory.getDurationNsec().longValue()); + Assert.assertEquals("Wrong idleTimeout", 0x01, builtByFactory.getIdleTimeout().intValue()); + Assert.assertEquals("Wrong hardTimeout", 0x03, builtByFactory.getHardTimeout().intValue()); + Assert.assertEquals("Wrong packetCount", 0x0001020304050607L, builtByFactory.getPacketCount().longValue()); + Assert.assertEquals("Wrong byteCount", 0x0001020304050607L, builtByFactory.getByteCount().longValue()); + } + +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactoryTest.java new file mode 100644 index 00000000..06ef8b5b --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/GetConfigReplyMessageFactoryTest.java @@ -0,0 +1,34 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput; + +/** + * @author michal.polkorab + * @author timotej.kubas + */ +public class GetConfigReplyMessageFactoryTest { + + + /** + * Testing {@link GetConfigReplyMessageFactory} for correct translation into POJO + */ + @Test + public void test() { + ByteBuf bb = BufferHelper.buildBuffer("00 01 00 03"); + GetConfigOutput builtByFactory = BufferHelper.decodeV13( + GetConfigReplyMessageFactory.getInstance(), bb); + + BufferHelper.checkHeaderV13(builtByFactory); + // TODO - enum problem ! +// Assert.assertEquals(builtByFactory.getFlags().toString(),"OFPCFRAGDROP"); + Assert.assertEquals("Wrong missSendLen", 0x01, builtByFactory.getMissSendLen().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 de3ce14c..04af39d4 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 @@ -3,32 +3,28 @@ package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; import io.netty.buffer.ByteBuf; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactory; import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage; /** * @author michal.polkorab - * + * */ public class HelloMessageFactoryTest { - + /** Number of currently supported version / codec */ - public static final short VERSION_YET_SUPPORTED = 0x04; - /** Index of Xid in OpenFlow 1.3 header */ - public static final byte INDEX_OF_XID_IN_HEADER = 4; + public static final Short VERSION_YET_SUPPORTED = 0x04; /** * Testing {@link HelloMessageFactory} for correct translation into POJO */ @Test public void test() { - ByteBuf bb = BufferHelper.buildBuffer(new byte[0]); - HelloMessage builtByFactory = HelloMessageFactory.getInstance().bufferToMessage(bb, VERSION_YET_SUPPORTED); + ByteBuf bb = BufferHelper.buildBuffer(); + HelloMessage builtByFactory = BufferHelper.decodeV13( + HelloMessageFactory.getInstance(), bb); - Assert.assertTrue(builtByFactory.getVersion() == VERSION_YET_SUPPORTED); - Assert.assertEquals(builtByFactory.getXid().longValue(), 16909060L); + BufferHelper.checkHeaderV13(builtByFactory); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactoryTest.java new file mode 100644 index 00000000..ed93d7ac --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/MultipartReplyMessageFactoryTest.java @@ -0,0 +1,31 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class MultipartReplyMessageFactoryTest { + + /** + * Testing {@link MultipartReplyMessageFactory} for correct translation into POJO + */ + @Test + public void test(){ + ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 00 00 00 01 02 03 04"); + MultipartReplyMessage builtByFactory = BufferHelper.decodeV13(MultipartReplyMessageFactory.getInstance(), bb); + + BufferHelper.checkHeaderV13(builtByFactory); + + Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE()); + Assert.assertArrayEquals("Wrong body", new byte[]{0x01, 0x02, 0x03, 0x04}, builtByFactory.getBody()); + } + +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactoryTest.java new file mode 100644 index 00000000..fa94c278 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PacketInMessageFactoryTest.java @@ -0,0 +1,36 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.junit.Assert; +import org.junit.Test; +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.TableId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage; + +/** + * @author timotej.kubas + * + */ +public class PacketInMessageFactoryTest { + + /** + * Testing {@link PacketInMessageFactory} for correct translation into POJO + */ + @Test + public void test(){ + ByteBuf bb = BufferHelper.buildBuffer("00 01 02 03 01 02 01 04 00 01 02 03 04 05 06 07 00 00 01 02 03 04"); + PacketInMessage builtByFactory = BufferHelper.decodeV13(PacketInMessageFactory.getInstance(), bb); + + BufferHelper.checkHeaderV13(builtByFactory); + + Assert.assertEquals("Wrong bufferID", 0x00010203L, builtByFactory.getBufferId().longValue()); + Assert.assertEquals("Wrong totalLen", 0x0102, builtByFactory.getTotalLen().intValue()); + Assert.assertEquals("Wrong reason", 0x01, builtByFactory.getReason().shortValue()); + Assert.assertEquals("Wrong tableID", new TableId((long) 4), builtByFactory.getTableId()); + Assert.assertEquals("Wrong cookie", 0x0001020304050607L, builtByFactory.getCookie().longValue()); + Assert.assertArrayEquals("Wrong data", ByteBufUtils.hexStringToBytes("01 02 03 04"), builtByFactory.getData()); + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactoryTest.java new file mode 100644 index 00000000..2100a5a4 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/PortStatusMessageFactoryTest.java @@ -0,0 +1,29 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ + +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import io.netty.buffer.ByteBuf; + +import org.junit.Test; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class PortStatusMessageFactoryTest { + + /** + * Testing {@link PortStatusMessageFactory} for correct translation into POJO + */ + @Test + public void test(){ + ByteBuf bb = BufferHelper.buildBuffer("01 00 00 00 00 00 00 00"); + + PortStatusMessage builtByFactory = BufferHelper.decodeV13(PortStatusMessageFactory.getInstance(), bb); + + BufferHelper.checkHeaderV13(builtByFactory); + //Assert.assertEquals("Wrong reason", 0x01, builtByFactory.getReason()); + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactoryTest.java index b65beb21..37b6d676 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/BarrierInputMessageFactoryTest.java @@ -4,10 +4,9 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInputBuilder; @@ -21,22 +20,19 @@ public class BarrierInputMessageFactoryTest { /** * Testing of {@link BarrierInputMessageFactory} for correct translation from POJO + * @throws Exception */ @Test - public void test() { + public void test() throws Exception { BarrierInputBuilder bib = new BarrierInputBuilder(); - bib.setVersion(HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - bib.setXid(16909060L); + BufferHelper.setupHeader(bib); BarrierInput bi = bib.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); BarrierInputMessageFactory bimf = BarrierInputMessageFactory.getInstance(); bimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, bi); - Assert.assertTrue(out.readByte() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertTrue(out.readByte() == BARRIER_REQUEST_MESSAGE_CODE_TYPE); - Assert.assertTrue(out.readUnsignedShort() == OFFrameDecoder.LENGTH_OF_HEADER); - Assert.assertTrue(out.readUnsignedInt() == 16909060L); + BufferHelper.checkHeaderV13(out, BARRIER_REQUEST_MESSAGE_CODE_TYPE, 8); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java index 07ecc194..2dc4aa16 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoInputMessageFactoryTest.java @@ -4,10 +4,9 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder; @@ -21,22 +20,19 @@ public class EchoInputMessageFactoryTest { /** * Testing of {@link EchoInputMessageFactory} for correct translation from POJO + * @throws Exception */ @Test - public void test() { + public void test() throws Exception { EchoInputBuilder eib = new EchoInputBuilder(); - eib.setVersion(HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - eib.setXid(16909060L); + BufferHelper.setupHeader(eib); EchoInput ei = eib.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); EchoInputMessageFactory eimf = EchoInputMessageFactory.getInstance(); eimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, ei); - Assert.assertTrue(out.readByte() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertTrue(out.readByte() == ECHO_REQUEST_MESSAGE_CODE_TYPE); - Assert.assertTrue(out.readUnsignedShort() == OFFrameDecoder.LENGTH_OF_HEADER); - Assert.assertTrue(out.readUnsignedInt() == 16909060L); + BufferHelper.checkHeaderV13(out, ECHO_REQUEST_MESSAGE_CODE_TYPE, 8); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java index 8ebe18f8..01b8047c 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/EchoReplyInputMessageFactoryTest.java @@ -4,10 +4,9 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder; @@ -21,22 +20,19 @@ public class EchoReplyInputMessageFactoryTest { /** * Testing of {@link EchoReplyInputMessageFactory} for correct translation from POJO + * @throws Exception */ @Test - public void test() { + public void test() throws Exception { EchoReplyInputBuilder erib = new EchoReplyInputBuilder(); - erib.setVersion(HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - erib.setXid(16909060L); + BufferHelper.setupHeader(erib); EchoReplyInput eri = erib.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); EchoReplyInputMessageFactory eimf = EchoReplyInputMessageFactory.getInstance(); eimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, eri); - Assert.assertTrue(out.readByte() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertTrue(out.readByte() == ECHO_REPLY_MESSAGE_CODE_TYPE); - Assert.assertTrue(out.readUnsignedShort() == OFFrameDecoder.LENGTH_OF_HEADER); - Assert.assertTrue(out.readUnsignedInt() == 16909060L); + BufferHelper.checkHeaderV13(out, ECHO_REPLY_MESSAGE_CODE_TYPE, 8); } } 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 new file mode 100644 index 00000000..a7de110d --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/ExperimenterInputMessageFactoryTest.java @@ -0,0 +1,44 @@ +/* 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 io.netty.buffer.UnpooledByteBufAllocator; + +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.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder; + +/** + * @author michal.polkorab + * + */ +public class ExperimenterInputMessageFactoryTest { + + private static final byte EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE = ExperimenterInputMessageFactory.MESSAGE_TYPE; + + /** + * Testing of {@link ExperimenterInputMessageFactory} for correct translation from POJO + * @throws Exception + */ + @Test + public void test() throws Exception { + ExperimenterInputBuilder eib = new ExperimenterInputBuilder(); + BufferHelper.setupHeader(eib); + eib.setExperimenter(0x0001020304L); + eib.setExpType(0x0001020304L); + ExperimenterInput ei = eib.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + ExperimenterInputMessageFactory eimf = ExperimenterInputMessageFactory.getInstance(); + eimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, ei); + + BufferHelper.checkHeaderV13(out, EXPERIMENTER_REQUEST_MESSAGE_CODE_TYPE, 16); + Assert.assertEquals("Wrong experimenter", 0x0001020304L, out.readUnsignedInt()); + Assert.assertEquals("Wrong expType", 0x0001020304L, out.readUnsignedInt()); + } + + +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetConfigInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetConfigInputMessageFactoryTest.java index eb18faad..f2236e65 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetConfigInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetConfigInputMessageFactoryTest.java @@ -4,10 +4,9 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInputBuilder; @@ -21,22 +20,19 @@ public class GetConfigInputMessageFactoryTest { /** * Testing of {@link GetConfigInputMessageFactory} for correct translation from POJO + * @throws Exception */ @Test - public void test() { + public void test() throws Exception { GetConfigInputBuilder gcib = new GetConfigInputBuilder(); - gcib.setVersion(HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - gcib.setXid(16909060L); + BufferHelper.setupHeader(gcib); GetConfigInput gci = gcib.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); GetConfigInputMessageFactory gcimf = GetConfigInputMessageFactory.getInstance(); gcimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, gci); - Assert.assertTrue(out.readByte() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertTrue(out.readByte() == GET_CONFIG_REQUEST_MESSAGE_CODE_TYPE); - Assert.assertTrue(out.readUnsignedShort() == OFFrameDecoder.LENGTH_OF_HEADER); - Assert.assertTrue(out.readUnsignedInt() == 16909060L); + BufferHelper.checkHeaderV13(out, GET_CONFIG_REQUEST_MESSAGE_CODE_TYPE, 8); } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetFeaturesInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetFeaturesInputMessageFactoryTest.java index b43854d9..3153b0dd 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetFeaturesInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetFeaturesInputMessageFactoryTest.java @@ -4,10 +4,9 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.junit.Assert; import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder; @@ -21,22 +20,19 @@ public class GetFeaturesInputMessageFactoryTest { /** * Testing of {@link GetFeaturesInputMessageFactory} for correct translation from POJO + * @throws Exception */ @Test - public void test() { + public void test() throws Exception { GetFeaturesInputBuilder gfib = new GetFeaturesInputBuilder(); - gfib.setVersion(HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - gfib.setXid(16909060L); + BufferHelper.setupHeader(gfib); GetFeaturesInput gfi = gfib.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); GetFeaturesInputMessageFactory gfimf = GetFeaturesInputMessageFactory.getInstance(); gfimf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, gfi); - Assert.assertTrue(out.readByte() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertTrue(out.readByte() == FEATURES_REQUEST_MESSAGE_CODE_TYPE); - Assert.assertTrue(out.readUnsignedShort() == OFFrameDecoder.LENGTH_OF_HEADER); - Assert.assertTrue(out.readUnsignedInt() == 16909060L); + BufferHelper.checkHeaderV13(out, FEATURES_REQUEST_MESSAGE_CODE_TYPE, 8); } } 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 523419c9..49cfcbf4 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 @@ -4,13 +4,18 @@ package org.opendaylight.openflowjava.protocol.impl.serialization.factories; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; -import org.junit.Assert; +import java.util.List; + import org.junit.Test; -import org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; -import org.opendaylight.openflowjava.protocol.impl.serialization.factories.HelloInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper; +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 com.google.common.collect.Lists; /** * @author michal.polkorab @@ -22,22 +27,42 @@ public class HelloInputMessageFactoryTest { /** * Testing of {@link HelloInputMessageFactory} for correct translation from POJO + * @throws Exception + */ + @Test + public void testWithoutElementsSet() throws Exception { + HelloInputBuilder hib = new HelloInputBuilder(); + BufferHelper.setupHeader(hib); + HelloInput hi = hib.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + HelloInputMessageFactory himf = HelloInputMessageFactory.getInstance(); + himf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, hi); + + BufferHelper.checkHeaderV13(out, HELLO_MESSAGE_CODE_TYPE, 8); + } + + /** + * Testing of {@link HelloInputMessageFactory} for correct translation from POJO + * @throws Exception */ @Test - public void testWithoutElementsSet() { + public void testWithElementsSet() throws Exception { HelloInputBuilder hib = new HelloInputBuilder(); - hib.setVersion(HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - hib.setXid(16909060L); + BufferHelper.setupHeader(hib); + ElementsBuilder eb = new ElementsBuilder(); + eb.setType(HelloElementType.VERSIONBITMAP); + eb.setData(new byte[]{0x01, 0x02, 0x42, 0x03}); + List elementList = Lists.newArrayList(eb.build()); + hib.setElements(elementList); HelloInput hi = hib.build(); ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); HelloInputMessageFactory himf = HelloInputMessageFactory.getInstance(); himf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, hi); - Assert.assertTrue(out.readByte() == HelloMessageFactoryTest.VERSION_YET_SUPPORTED); - Assert.assertTrue(out.readByte() == HELLO_MESSAGE_CODE_TYPE); - Assert.assertTrue(out.readUnsignedShort() == OFFrameDecoder.LENGTH_OF_HEADER); - Assert.assertTrue(out.readUnsignedInt() == 16909060L); + BufferHelper.checkHeaderV13(out, HELLO_MESSAGE_CODE_TYPE, 8); + // TODO - element list } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/BufferHelper.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/BufferHelper.java index d6f0fd08..c8c62dfd 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/BufferHelper.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/BufferHelper.java @@ -4,20 +4,32 @@ package org.opendaylight.openflowjava.protocol.impl.util; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.Assert; +import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest; +import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; +import org.opendaylight.yangtools.yang.binding.DataObject; + /** * @author michal.polkorab - * + * */ public abstract class BufferHelper { /** * */ - private static final byte[] XID = new byte[]{0x01, 0x02, 0x03, 0x04}; + public static final Long DEFAULT_XID = 0x01020304L; + private static final byte[] XID = new byte[] { 0x01, 0x02, 0x03, 0x04 }; /** * @param payload - * @return ByteBuf filled with OpenFlow protocol message without first 2 bytes + * @return ByteBuf filled with OpenFlow protocol message without first 4 + * bytes */ public static ByteBuf buildBuffer(byte[] payload) { ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer(); @@ -25,5 +37,100 @@ public abstract class BufferHelper { bb.writeBytes(payload); return bb; } + + /** + * @param payload String in hex format + * @return ByteBuf filled with OpenFlow protocol message without first 4 + * bytes + */ + public static ByteBuf buildBuffer(String payload) { + return buildBuffer(ByteBufUtils.hexStringToBytes(payload)); + } + + /** + * @return ByteBuf filled with OpenFlow protocol header message without first 4 + * bytes + */ + public static ByteBuf buildBuffer() { + ByteBuf bb = UnpooledByteBufAllocator.DEFAULT.buffer(); + bb.writeBytes(XID); + bb.writeBytes(new byte[0]); + return bb; + } + + /** + * Use version 1.3 for encoded message + * @param input + * ByteBuf to be checked for correct OpenFlow Protocol header + * @param msgType + * type of received message + * @param length TODO + */ + public static void checkHeaderV13(ByteBuf input, byte msgType, int length) { + checkHeader(input, msgType, length, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + } + + private static void checkHeader(ByteBuf input, byte msgType, int length, Short version) { + Assert.assertEquals("Wrong version", version, Short.valueOf(input.readByte())); + Assert.assertEquals("Wrong type", msgType, input.readByte()); + Assert.assertEquals("Wrong length", length, input.readUnsignedShort()); + Assert.assertEquals("Wrong Xid", DEFAULT_XID, Long.valueOf(input.readUnsignedInt())); + } + + /** + * @param ofHeader OpenFlow protocol header + */ + public static void checkHeaderV13(OfHeader ofHeader) { + checkHeader(ofHeader, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + } + + private static void checkHeader(OfHeader ofHeader, Short version) { + Assert.assertEquals("Wrong version", version, ofHeader.getVersion()); + Assert.assertEquals("Wrong Xid", DEFAULT_XID, ofHeader.getXid()); + } + + /** + * @param builder + * @throws NoSuchMethodException + * @throws SecurityException + * @throws IllegalAccessException + * @throws IllegalArgumentException + * @throws InvocationTargetException + */ + public static void setupHeader(Object builder) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Method m = builder.getClass().getMethod("setVersion", Short.class); + m.invoke(builder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + Method m2 = builder.getClass().getMethod("setXid", Long.class); + m2.invoke(builder, BufferHelper.DEFAULT_XID); + } + + /** + * Use version 1.3 for decoding message + * @param decoder decoder instance + * @param bb data input buffer + * @return message decoded pojo + */ + public static E decodeV13(OFDeserializer decoder, ByteBuf bb) { + return bufferToMessage(decoder, HelloMessageFactoryTest.VERSION_YET_SUPPORTED, bb); + } + + private static E bufferToMessage(OFDeserializer decoder, short version, ByteBuf bb) { + return decoder.bufferToMessage(bb, version); + } + + /** + * Use OF-protocol version 1.3 + * @param encoder serialize factory + * @param out buffer the result will be written into + * @param pojo input message + */ + public static void encodeV13(OFSerializer encoder, ByteBuf out, E pojo) { + messageToBuffer(encoder, out, pojo, HelloMessageFactoryTest.VERSION_YET_SUPPORTED); + } + + private static void messageToBuffer( + OFSerializer encoder, ByteBuf out, E pojo, Short version) { + encoder.messageToBuffer(version, out, pojo); + } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtilsTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtilsTest.java new file mode 100644 index 00000000..b8919a87 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/ByteBufUtilsTest.java @@ -0,0 +1,55 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.util; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author michal.polkorab + * + */ +public class ByteBufUtilsTest { + + private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff}; + + /** + * Test of {@link ByteBufUtils#hexStringToBytes(String)} + */ + @Test + public void hexStringToBytesTest() { + byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff"); + + Assert.assertArrayEquals(expected, data); + } + + /** + * Test of {@link ByteBufUtils#hexStringToByteBuf(String)} + */ + @Test + public void hexStringToByteBufTest() { + ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff"); + + Assert.assertArrayEquals(expected, byteBufToByteArray(bb)); + } + + /** + * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)} + */ + @Test + public void hexStringToGivenByteBufTest() { + ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer(); + ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer); + + Assert.assertArrayEquals(expected, byteBufToByteArray(buffer)); + } + + private static byte[] byteBufToByteArray(ByteBuf bb) { + byte[] result = new byte[bb.readableBytes()]; + bb.readBytes(result); + return result; + } + +} -- 2.36.6