From: Timotej Kubas Date: Tue, 24 Sep 2013 08:58:54 +0000 (+0200) Subject: GetAsyncReplyMessageFactory, QueueGetConfigReplyMessageFactoryTest X-Git-Tag: jenkins-openflowjava-bulk-release-prepare-only-1~107 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=1e439a51ce35e34f6203a64dba532aba9b5999af;p=openflowjava.git GetAsyncReplyMessageFactory, QueueGetConfigReplyMessageFactoryTest adding new serialization/deserialization factories Change-Id: Ib39c1c28f4146791d16f1b56d25231ecb61ae3df Signed-off-by: Timotej Kubas --- 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 b8d16c53..bbd70fe5 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 @@ -19,4 +19,16 @@ public interface OFSerializer { * @param message message that will be transformed into ByteBuf */ public abstract void messageToBuffer(short version, ByteBuf out, E message); + + /** + * Compute length of received message + * @return computed length + */ + public abstract int computeLength(); + + /** + * + * @return message code type + */ + public byte getMessageType(); } 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 3d74d7ca..4d4fe92b 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 @@ -3,8 +3,8 @@ 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.BarrierInput; /** @@ -16,6 +16,7 @@ public class BarrierInputMessageFactory implements OFSerializer { /** Code type of BarrierRequest message */ public static final byte MESSAGE_TYPE = 20; private static BarrierInputMessageFactory instance; + private static final int MESSAGE_LENGTH = 8; private BarrierInputMessageFactory() { // do nothing, just singleton @@ -33,10 +34,16 @@ public class BarrierInputMessageFactory implements OFSerializer { @Override public void messageToBuffer(short version, ByteBuf out, BarrierInput message) { - out.writeByte(message.getVersion()); - out.writeByte(MESSAGE_TYPE); - out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); - out.writeInt(message.getXid().intValue()); + ByteBufUtils.writeOFHeader(instance, message, out); } + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } } 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 6d799fe4..a45ce0ca 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 @@ -3,8 +3,8 @@ 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.EchoInput; /** @@ -16,6 +16,7 @@ public class EchoInputMessageFactory implements OFSerializer { /** Code type of EchoRequest message */ public static final byte MESSAGE_TYPE = 2; private static EchoInputMessageFactory instance; + private static final int MESSAGE_LENGTH = 8; private EchoInputMessageFactory() { // do nothing, just singleton @@ -33,10 +34,17 @@ public class EchoInputMessageFactory implements OFSerializer { @Override public void messageToBuffer(short version, ByteBuf out, EchoInput message) { - out.writeByte(message.getVersion()); - out.writeByte(MESSAGE_TYPE); - out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); - out.writeInt(message.getXid().intValue()); + ByteBufUtils.writeOFHeader(instance, message, out); + } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; } } 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 3b3e188e..be44e0dd 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 @@ -3,8 +3,8 @@ 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.EchoReplyInput; /** @@ -15,6 +15,7 @@ public class EchoReplyInputMessageFactory implements OFSerializer { + + private static final byte MESSAGE_TYPE = 22; + private static final byte PADDING_IN_GET_QUEUE_CONFIG_MESSAGE = 4; + private static final int MESSAGE_LENGTH = 16; + + private static GetQueueConfigInputMessageFactory instance; + + + private GetQueueConfigInputMessageFactory() { + // singleton + } + + + /** + * @return singleton factory + */ + public static GetQueueConfigInputMessageFactory getInstance(){ + + if(instance == null){ + instance = new GetQueueConfigInputMessageFactory(); + } + + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, GetQueueConfigInput message){ + ByteBufUtils.writeOFHeader(instance, message, out); + out.writeInt(message.getPort().getValue().intValue()); + ByteBufUtils.padBuffer(PADDING_IN_GET_QUEUE_CONFIG_MESSAGE, out); + } + + @Override + public int computeLength(){ + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } +} 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 ea03326d..c9b24561 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 @@ -3,8 +3,8 @@ 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.HelloInput; /** @@ -15,6 +15,7 @@ public class HelloInputMessageFactory implements OFSerializer{ /** Code type of Hello message */ public static final byte MESSAGE_TYPE = 0; + private static final int MESSAGE_LENGTH = 8; private static HelloInputMessageFactory instance; private HelloInputMessageFactory() { @@ -33,11 +34,18 @@ public class HelloInputMessageFactory implements OFSerializer{ @Override public void messageToBuffer(short version, ByteBuf out, HelloInput message) { - out.writeByte(message.getVersion()); - out.writeByte(MESSAGE_TYPE); - out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); - out.writeInt(message.getXid().intValue()); + ByteBufUtils.writeOFHeader(instance, message, out); // TODO - fill list of elements into ByteBuf, check length too } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } } 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 index 9a16738b..36842d66 100644 --- 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 @@ -3,7 +3,6 @@ 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; @@ -16,6 +15,7 @@ public class PacketOutInputMessageFactory implements OFSerializer { /** Code type of SetConfig message */ public static final byte MESSAGE_TYPE = 9; + private static final int MESSAGE_LENGTH = 8; private static SetConfigMessageFactory instance; private SetConfigMessageFactory() { @@ -34,13 +35,20 @@ public class SetConfigMessageFactory implements OFSerializer { @Override public void messageToBuffer(short version, ByteBuf out, SetConfigInput message) { - out.writeByte(message.getVersion()); - out.writeByte(MESSAGE_TYPE); - out.writeShort(OFFrameDecoder.LENGTH_OF_HEADER); - out.writeInt(message.getXid().intValue()); + ByteBufUtils.writeOFHeader(instance, message, out); // TODO - finish implementation after enums provide needed funcionality out.writeShort(message.getFlags().getIntValue()); out.writeShort(message.getMissSendLen()); } + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } + } 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 new file mode 100644 index 00000000..718d93d7 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactory.java @@ -0,0 +1,73 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.serialization.factories; + +import io.netty.buffer.ByteBuf; + +import java.util.HashMap; +import java.util.Map; + +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.PortConfig; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class TableModInputMessageFactory implements OFSerializer { + + private static final byte MESSAGE_TYPE = 17; + private static final byte PADDING_IN_TABLE_MOD_MESSAGE = 3; + private static final int MESSAGE_LENGTH = 16; + private static TableModInputMessageFactory instance; + + private TableModInputMessageFactory() { + // just singleton + } + + /** + * @return singleton factory + */ + public static TableModInputMessageFactory getInstance() { + if(instance == null){ + instance = new TableModInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, TableModInput message) { + + ByteBufUtils.writeOFHeader(instance, message, out); + out.writeByte(message.getTableId().getValue().byteValue()); + ByteBufUtils.padBuffer(PADDING_IN_TABLE_MOD_MESSAGE, out); + out.writeInt(createConfigBitmask(message.getConfig())); + } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } + + /** + * @param config + * @return port config bitmask + */ + private static int createConfigBitmask(PortConfig config) { + int configBitmask = 0; + Map portConfigMap = new HashMap<>(); + portConfigMap.put(0, config.isPortDown()); + portConfigMap.put(2, config.isNoRecv()); + portConfigMap.put(5, config.isNoFwd()); + portConfigMap.put(6, config.isNoPacketIn()); + + configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap); + return configBitmask; + } +} 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 3f5e3600..a4fe7458 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 @@ -5,6 +5,12 @@ package org.opendaylight.openflowjava.protocol.impl.util; import io.netty.buffer.ByteBuf; import io.netty.buffer.UnpooledByteBufAllocator; +import java.util.Map; +import java.util.Map.Entry; + +import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; + /** Class for common operations on ByteBuf * * @author michal.polkorab @@ -69,4 +75,34 @@ public abstract class ByteBufUtils { out.writeByte(0); } } + + /** + * Create standard OF header + * @param factory serialization factory + * @param message POJO + * @param out writing buffer + */ + public static void writeOFHeader(OFSerializer factory, OfHeader message, ByteBuf out) { + out.writeByte(message.getVersion()); + out.writeByte(factory.getMessageType()); + out.writeShort(factory.computeLength()); + out.writeInt(message.getXid().intValue()); + + } + + /** + * Fills the bitmask from boolean map where key is bit position + * @param booleanMap bit to boolean mapping + * @return bit mask + */ + public static int fillBitMaskFromMap(Map booleanMap) { + int bitmask = 0; + + for (Entry iterator : booleanMap.entrySet()) { + if (iterator.getValue() != null && iterator.getValue().booleanValue()) { + bitmask |= 1 << iterator.getKey(); + } + } + return bitmask; + } } diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/QueueGetConfigReplyMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/QueueGetConfigReplyMessageFactoryTest.java new file mode 100644 index 00000000..74c9a378 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/deserialization/factories/QueueGetConfigReplyMessageFactoryTest.java @@ -0,0 +1,89 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.deserialization.factories; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +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.PortNumber; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.QueueProperty; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.packet.queue.Properties; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.packet.queue.PropertiesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.Queues; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.queue.get.config.reply.QueuesBuilder; + +import com.google.common.collect.ComparisonChain; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class QueueGetConfigReplyMessageFactoryTest { + + @Test + public void test(){ + ByteBuf bb = BufferHelper.buildBuffer("00 01 02 03 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 02 00 00 00 00 00 00"); + GetQueueConfigOutput builtByFactory = BufferHelper.decodeV13(QueueGetConfigReplyMessageFactory.getInstance(), bb); + BufferHelper.checkHeaderV13(builtByFactory); + Assert.assertTrue("Wrong port",66051L == builtByFactory.getPort().getValue()); + Assert.assertTrue("Wrong queues", true == compareLists(builtByFactory.getQueues(), createQueuesList())); + } + + public List createQueuesList(){ + final byte PADDING_IN_PACKET_QUEUE_HEADER = 6; + List queuesList = new ArrayList(); + QueuesBuilder qb = new QueuesBuilder(); + qb.setQueueId(new QueueId((long) 1)); + qb.setPort(new PortNumber((long) 1)); + qb.setProperties(createPropertiesList()); + queuesList.add(qb.build()); + + return queuesList; + } + + public List createPropertiesList(){ + final byte PADDING_IN_QUEUE_PROPERTY_HEADER = 4; + List propertiesList = new ArrayList(); + PropertiesBuilder pb = new PropertiesBuilder(); + pb.setProperty(QueueProperty.values()[2]); + propertiesList.add(pb.build()); + + return propertiesList; + } + + public boolean compareLists(List originalList, List testList){ + boolean decision = false; + int originalListLength = originalList.size(); + int testListLength = testList.size(); + + for(int i=0; i