From: Timotej Kubas Date: Tue, 1 Oct 2013 12:24:06 +0000 (+0200) Subject: new serialization factories and tests X-Git-Tag: jenkins-openflowjava-bulk-release-prepare-only-1~102 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=33762df0363fb27459f69480653717f78b310497;hp=00a7ea60960149c9c5f7fbe8157ed58ea05b8f2f;p=openflowjava.git new serialization factories and tests Change-Id: I33dcb69226129bafa8811c3ed1ffd4c2db590f33 Signed-off-by: Timotej Kubas --- diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DecoderTable.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DecoderTable.java index 364f7f31..ba59351e 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DecoderTable.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/deserialization/DecoderTable.java @@ -8,9 +8,17 @@ import org.opendaylight.openflowjava.protocol.impl.core.OFVersionDetector; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.BarrierReplyMessageFactory; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.EchoReplyMessageFactory; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.EchoRequestMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.ErrorMessageFactory; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.ExperimenterMessageFactory; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.FeaturesReplyMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.FlowRemovedMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.GetConfigReplyMessageFactory; import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.PacketInMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.PortStatusMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.QueueGetConfigReplyMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.RoleReplyMessageFactory; /** * @author michal.polkorab @@ -79,11 +87,19 @@ public class DecoderTable { public void init() { table = new HashMap<>(); table.put(new MessageTypeCodeKey(OF13, (short) 0), HelloMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 1), ErrorMessageFactory.getInstance()); table.put(new MessageTypeCodeKey(OF13, (short) 2), EchoRequestMessageFactory.getInstance()); table.put(new MessageTypeCodeKey(OF13, (short) 3), EchoReplyMessageFactory.getInstance()); table.put(new MessageTypeCodeKey(OF13, (short) 4), ExperimenterMessageFactory.getInstance()); table.put(new MessageTypeCodeKey(OF13, (short) 6), FeaturesReplyMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 8), GetConfigReplyMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 10), PacketInMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 11), FlowRemovedMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 12), PortStatusMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 19), MultipartReplyMessageFactory.getInstance()); table.put(new MessageTypeCodeKey(OF13, (short) 21), BarrierReplyMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 23), QueueGetConfigReplyMessageFactory.getInstance()); + table.put(new MessageTypeCodeKey(OF13, (short) 25), RoleReplyMessageFactory.getInstance()); } /** diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/EncoderTable.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/EncoderTable.java index e196d878..088fa8fe 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/EncoderTable.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/EncoderTable.java @@ -5,8 +5,42 @@ import java.util.HashMap; import java.util.Map; import org.opendaylight.openflowjava.protocol.impl.core.OFVersionDetector; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.BarrierInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.EchoInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.EchoReplyInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.ExperimenterInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.FlowModInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.GetAsyncRequestMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.GetConfigInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.GetFeaturesInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.GetQueueConfigInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.GroupModInputMessageFactory; import org.opendaylight.openflowjava.protocol.impl.serialization.factories.HelloInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.MeterModInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.PacketOutInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.PortModInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.RoleRequestInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.SetAsyncInputMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.SetConfigMessageFactory; +import org.opendaylight.openflowjava.protocol.impl.serialization.factories.TableModInputMessageFactory; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput; import org.opendaylight.yangtools.yang.binding.DataObject; /** @@ -42,7 +76,24 @@ public class EncoderTable { */ public void init() { table = new HashMap<>(); + table.put(new MessageTypeKey<>(OF13, BarrierInput.class), BarrierInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, EchoInput.class), EchoInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, EchoReplyInput.class), EchoReplyInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, ExperimenterInput.class), ExperimenterInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, FlowModInput.class), FlowModInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, GetAsyncInput.class), GetAsyncRequestMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, GetConfigInput.class), GetConfigInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, GetFeaturesInput.class), GetFeaturesInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, GetQueueConfigInput.class), GetQueueConfigInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, GroupModInput.class), GroupModInputMessageFactory.getInstance()); table.put(new MessageTypeKey<>(OF13, HelloInput.class), HelloInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, MeterModInput.class), MeterModInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, PacketOutInput.class), PacketOutInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, PortModInput.class), PortModInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, RoleRequestInput.class), RoleRequestInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, SetAsyncInput.class), SetAsyncInputMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, SetConfigInput.class), SetConfigMessageFactory.getInstance()); + table.put(new MessageTypeKey<>(OF13, TableModInput.class), TableModInputMessageFactory.getInstance()); } /** diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactory.java new file mode 100644 index 00000000..d42cf347 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactory.java @@ -0,0 +1,78 @@ +/* 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.FlowModFlags; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class FlowModInputMessageFactory implements OFSerializer { + private static final byte MESSAGE_TYPE = 14; + private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2; + private static final int MESSAGE_LENGTH = 48; //flags + private static FlowModInputMessageFactory instance; + + private FlowModInputMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static FlowModInputMessageFactory getInstance() { + if(instance == null) { + instance = new FlowModInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, FlowModInput message) { + ByteBufUtils.writeOFHeader(instance, message, out); + out.writeLong(message.getCookie().longValue()); + out.writeLong(message.getCookieMask().longValue()); + out.writeByte(message.getTableId().getValue().byteValue()); + out.writeByte(message.getCommand().getIntValue()); + out.writeShort(message.getIdleTimeout().intValue()); + out.writeShort(message.getHardTimeout().intValue()); + out.writeShort(message.getPriority()); + out.writeInt(message.getOutPort().getValue().intValue()); + out.writeInt(message.getOutGroup().intValue()); + out.writeShort(createFlowModFlagsBitmask(message.getFlags())); + ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, out); + // TODO implementation of match structure + // TODO implementation of instructions + } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } + + private static int createFlowModFlagsBitmask(FlowModFlags flags) { + int flowModFlagBitmask = 0; + Map flowModFlagsMap = new HashMap<>(); + flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM()); + flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP()); + flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS()); + flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS()); + flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS()); + + flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap); + return flowModFlagBitmask; + } +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java new file mode 100644 index 00000000..19b9041d --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.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.Iterator; +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.protocol.rev130731.GroupModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.group.mod.Buckets; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class GroupModInputMessageFactory implements OFSerializer { + private static final byte MESSAGE_TYPE = 15; + private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1; + private static final int MESSAGE_LENGTH = 16; + private static GroupModInputMessageFactory instance; + + private GroupModInputMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static GroupModInputMessageFactory getInstance() { + if (instance == null) { + instance = new GroupModInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, + GroupModInput message) { + ByteBufUtils.writeOFHeader(instance, message, out); + out.writeShort(message.getCommand().getIntValue()); + out.writeByte(message.getType().getIntValue()); + ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out); + out.writeInt(message.getGroupId().intValue()); + encodeBuckets(message.getBuckets(), out); + } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } + + private static void encodeBuckets(List buckets, ByteBuf outBuffer) { + final byte PADDING_IN_BUCKET = 4; + + for (Iterator iterator = buckets.iterator(); iterator.hasNext();) { + Buckets currentBucket = iterator.next(); + // TODO get method for field length missing + outBuffer.writeShort(currentBucket.getWeight().intValue()); + outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue()); + outBuffer.writeInt(currentBucket.getWatchGroup().intValue()); + ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer); + // TODO actions structure missing + } + } + +} 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 new file mode 100644 index 00000000..aed60143 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactory.java @@ -0,0 +1,83 @@ +/* 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.Iterator; +import java.util.List; +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.MeterFlags; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class MeterModInputMessageFactory implements OFSerializer { + private static final byte MESSAGE_TYPE = 29; + private static final int MESSAGE_LENGTH = 16; + private static MeterModInputMessageFactory instance; + + private MeterModInputMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static MeterModInputMessageFactory getInstance() { + if (instance == null) { + instance = new MeterModInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, + MeterModInput message) { + ByteBufUtils.writeOFHeader(instance, message, out); + out.writeShort(message.getCommand().getIntValue()); + out.writeShort(createMeterFlagsBitmask(message.getFlags())); + out.writeInt(message.getMeterId().getValue().intValue()); + encodeBands(message.getBands(), out); + } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } + + private static int createMeterFlagsBitmask(MeterFlags flags) { + int meterFlagBitmask = 0; + Map meterModFlagsMap = new HashMap<>(); + meterModFlagsMap.put(0, flags.isOFPMFKBPS()); + meterModFlagsMap.put(1, flags.isOFPMFPKTPS()); + meterModFlagsMap.put(2, flags.isOFPMFBURST()); + meterModFlagsMap.put(3, flags.isOFPMFSTATS()); + + meterFlagBitmask = ByteBufUtils.fillBitMaskFromMap(meterModFlagsMap); + return meterFlagBitmask; + } + + private static void encodeBands(List bands, ByteBuf outBuffer) { + for (Iterator iterator = bands.iterator(); iterator.hasNext();) { + Bands currentBands = iterator.next(); + outBuffer.writeShort(currentBands.getType().getIntValue()); + // TODO outBuffer.writeShort(currentBands.get); length is missing + outBuffer.writeInt(currentBands.getRate().intValue()); + outBuffer.writeInt(currentBands.getBurstSize().intValue()); + // TODO what to do with ofp_meter_band_drop? + } + } + +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactory.java new file mode 100644 index 00000000..d72e25fc --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactory.java @@ -0,0 +1,152 @@ +/* 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.Iterator; +import java.util.List; +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.FlowRemovedReason; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class SetAsyncInputMessageFactory implements OFSerializer { + private static final byte MESSAGE_TYPE = 28; + private static final int MESSAGE_LENGTH = 32; + private static SetAsyncInputMessageFactory instance; + + private SetAsyncInputMessageFactory() { + // singleton + } + + /** + * @return singleton factory + */ + public static SetAsyncInputMessageFactory getInstance() { + if (instance == null) { + instance = new SetAsyncInputMessageFactory(); + } + return instance; + } + + @Override + public void messageToBuffer(short version, ByteBuf out, + SetAsyncInput message) { + ByteBufUtils.writeOFHeader(instance, message, out); + encodePacketInMask(message.getPacketInMask(), out); + encodePortStatusMask(message.getPortStatusMask(), out); + encodeFlowRemovedMask(message.getFlowRemovedMask(), out); + } + + @Override + public int computeLength() { + return MESSAGE_LENGTH; + } + + @Override + public byte getMessageType() { + return MESSAGE_TYPE; + } + + private static void encodePacketInMask(List packetInMask, ByteBuf outBuffer) { + for (Iterator iterator = packetInMask.iterator(); iterator.hasNext();) { + PacketInReason currentPacketInReason = iterator.next(); + outBuffer.writeInt(packetInReasonToBitmask(currentPacketInReason.getIntValue())); + } + } + + private static void encodePortStatusMask(List portStatusMask, ByteBuf outBuffer) { + for (Iterator iterator = portStatusMask.iterator(); iterator.hasNext();) { + PortReason currentPortReason = iterator.next(); + outBuffer.writeInt(portReasonToBitmask(currentPortReason.getIntValue())); + } + } + + private static void encodeFlowRemovedMask(List flowRemovedMask, ByteBuf outBuffer) { + for (Iterator iterator = flowRemovedMask.iterator(); iterator.hasNext();) { + FlowRemovedReason currentFlowRemovedReason = iterator.next(); + outBuffer.writeInt(flowRemovedReasonToBitmask(currentFlowRemovedReason.getIntValue())); + } + } + + private static int packetInReasonToBitmask(int option) { + Boolean OFPRNOMATCH = false; + Boolean OFPRACTION = false; + Boolean OFPRINVALIDTTL = false; + int packetInReasonBitmask = 0; + + switch(option) { + case 0: OFPRNOMATCH = true; break; + case 1: OFPRACTION = true; break; + case 2: OFPRINVALIDTTL = true; break; + default: break; + } + + Map packetInReasonMap = new HashMap<>(); + packetInReasonMap.put(0, OFPRNOMATCH); + packetInReasonMap.put(1, OFPRACTION); + packetInReasonMap.put(2, OFPRINVALIDTTL); + + packetInReasonBitmask = ByteBufUtils.fillBitMaskFromMap(packetInReasonMap); + + return packetInReasonBitmask; + } + + private static int portReasonToBitmask(int option) { + Boolean OFPPRADD = false; + Boolean OFPPRDELETE = false; + Boolean OFPPRMODIFY = false; + int portReasonBitmask = 0; + + switch(option) { + case 0: OFPPRADD = true; break; + case 1: OFPPRDELETE = true; break; + case 2: OFPPRMODIFY = true; break; + default: break; + } + + Map portReasonMap = new HashMap<>(); + portReasonMap.put(0, OFPPRADD); + portReasonMap.put(1, OFPPRDELETE); + portReasonMap.put(2, OFPPRMODIFY); + + portReasonBitmask = ByteBufUtils.fillBitMaskFromMap(portReasonMap); + + return portReasonBitmask; + } + + private static int flowRemovedReasonToBitmask(int option) { + Boolean OFPRRIDLETIMEOUT = false; + Boolean OFPRRHARDTIMEOUT = false; + Boolean OFPRRDELETE = false; + Boolean OFPRRGROUPDELETE = false; + int flowRemovedReasonBitmask = 0; + + switch(option) { + case 0: OFPRRIDLETIMEOUT = true; break; + case 1: OFPRRHARDTIMEOUT = true; break; + case 2: OFPRRDELETE = true; break; + case 3: OFPRRGROUPDELETE = true; break; + default: break; + } + + Map portReasonMap = new HashMap<>(); + portReasonMap.put(0, OFPRRIDLETIMEOUT); + portReasonMap.put(1, OFPRRHARDTIMEOUT); + portReasonMap.put(2, OFPRRDELETE); + portReasonMap.put(3, OFPRRGROUPDELETE); + + flowRemovedReasonBitmask = ByteBufUtils.fillBitMaskFromMap(portReasonMap); + + return flowRemovedReasonBitmask; + } +} 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 new file mode 100644 index 00000000..646c2569 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java @@ -0,0 +1,81 @@ +/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ +package org.opendaylight.openflowjava.protocol.impl.serialization.factories; + +import static org.junit.Assert.*; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; + +import java.math.BigInteger; + +import junit.framework.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.common.types.rev130731.ControllerRole; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig; +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.TableId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class FlowModInputMessageFactoryTest { + private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2; + /** + * @throws Exception + * Testing of {@link FlowModInputMessageFactory} for correct translation from POJO + */ + @Test + public void testFlowModInputMessageFactory() throws Exception { + FlowModInputBuilder builder = new FlowModInputBuilder(); + BufferHelper.setupHeader(builder); + byte[] cookie = new byte[]{0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; + builder.setCookie(new BigInteger(cookie)); + byte[] cookieMask = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + builder.setCookieMask(new BigInteger(cookieMask)); + builder.setTableId(new TableId(65L)); + builder.setCommand(FlowModCommand.forValue(2)); + builder.setIdleTimeout(12); + builder.setHardTimeout(0); + builder.setPriority(126); + builder.setOutPort(new PortNumber(4422L)); + builder.setOutGroup(98L); + builder.setFlags(new FlowModFlags(true, false, true, false, true)); + FlowModInput message = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + FlowModInputMessageFactory factory = FlowModInputMessageFactory.getInstance(); + factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); + + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + 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()); + Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readByte()); + Assert.assertEquals("Wrong idleTimeOut", message.getIdleTimeout().intValue(), out.readShort()); + Assert.assertEquals("Wrong hardTimeOut", message.getHardTimeout().intValue(), out.readShort()); + Assert.assertEquals("Wrong priority", message.getPriority().intValue(), out.readShort()); + Assert.assertEquals("Wrong outPort", message.getOutPort().getValue().intValue(), out.readUnsignedInt()); + Assert.assertEquals("Wrong outGroup", message.getOutGroup().intValue(), out.readUnsignedInt()); + Assert.assertEquals("Wrong flags", message.getFlags(), createFlowModFalgsFromBitmap(out.readShort())); + out.skipBytes(PADDING_IN_FLOW_MOD_MESSAGE); + // TODO implementation of match structure + // TODO implementation of instructions + } + + private static FlowModFlags createFlowModFalgsFromBitmap(short input){ + final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0; + final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0; + final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0; + final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0; + final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0; + return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS, _oFPFFSENDFLOWREM); + } + +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetaAsyncRequestMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetaAsyncRequestMessageFactoryTest.java index 902afdbf..fa63cdab 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetaAsyncRequestMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GetaAsyncRequestMessageFactoryTest.java @@ -19,11 +19,11 @@ public class GetaAsyncRequestMessageFactoryTest { private static final int MESSAGE_LENGTH = 8; /** - * Testing of {@link GetaAsyncRequestMessageFactory} for correct translation from POJO + * Testing of {@link GetAsyncRequestMessageFactory} for correct translation from POJO * @throws Exception */ @Test - public void testGetaAsyncReques() throws Exception { + public void testGetAsyncReques() throws Exception { GetAsyncInputBuilder builder = new GetAsyncInputBuilder(); BufferHelper.setupHeader(builder); GetAsyncInput message = builder.build(); 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 new file mode 100644 index 00000000..53794882 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactoryTest.java @@ -0,0 +1,110 @@ +/* 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 java.util.ArrayList; +import java.util.List; + +import junit.framework.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.common.types.rev130731.GroupModCommand; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.group.mod.Buckets; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.group.mod.BucketsBuilder; + +/** + * @author timotej.kubas + * + */ +public class GroupModInputMessageFactoryTest { + private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1; + + /** + * @throws Exception + * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO + */ + @Test + public void testGroupModInputMessage() throws Exception { + GroupModInputBuilder builder = new GroupModInputBuilder(); + BufferHelper.setupHeader(builder); + builder.setCommand(GroupModCommand.forValue(2)); + builder.setType(GroupType.forValue(3)); + builder.setGroupId(256L); + builder.setBuckets(createBucketsList()); + GroupModInput message = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + GroupModInputMessageFactory factory = GroupModInputMessageFactory.getInstance(); + factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); + + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + 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); + Assert.assertEquals("Wrong groupId", message.getGroupId().intValue(), out.readUnsignedInt()); + Assert.assertEquals("Wrong bucketList", true, compareBucketsLists(createBucketsList(), createBucketsListFromBufer(out))); + } + + private static List createBucketsList(){ + List bucketsList = new ArrayList(); + BucketsBuilder bucketsBuilder = new BucketsBuilder(); + Buckets bucket; + bucketsBuilder.setWeight(10); + bucketsBuilder.setWatchPort(new PortNumber(65L)); + bucketsBuilder.setWatchGroup(22L); + bucket = bucketsBuilder.build(); + bucketsList.add(bucket); + return bucketsList; + } + + private static List createBucketsListFromBufer(ByteBuf out){ + List bucketsList = new ArrayList(); + BucketsBuilder bucketsBuilder = new BucketsBuilder(); + Buckets bucket; + bucketsBuilder.setWeight((int) out.readShort()); + bucketsBuilder.setWatchPort(new PortNumber(out.readUnsignedInt())); + bucketsBuilder.setWatchGroup(out.readUnsignedInt()); + out.skipBytes(4); + bucket = bucketsBuilder.build(); + bucketsList.add(bucket); + return bucketsList; + } + + private static boolean compareBucketsLists(List bucketsFromMessage, List bucketsFromBuffer) { + boolean result = false; + int bucketsFromMessageLength = bucketsFromMessage.size(); + for (int i = 0; i < bucketsFromMessageLength; i++) { + if (bucketsFromMessage.get(i).getWeight().equals(bucketsFromBuffer.get(i).getWeight())) { + result = true; + } else { + result = false; + break; + } + if (bucketsFromMessage.get(i).getWatchPort() + .equals(bucketsFromBuffer.get(i).getWatchPort())) { + result = true; + } else { + result = false; + break; + } + if (bucketsFromMessage.get(i).getWatchGroup() + .equals(bucketsFromBuffer.get(i).getWatchGroup())) { + result = true; + } else { + result = false; + break; + } + } + // TODO get method for field length missing + // TODO actions structure missing + return result; + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactoryTest.java new file mode 100644 index 00000000..c8a5b762 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactoryTest.java @@ -0,0 +1,116 @@ +/* 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 java.util.ArrayList; +import java.util.List; + +import junit.framework.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.common.types.rev130731.MeterBandType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterModCommand; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.BandsBuilder; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class MeterModInputMessageFactoryTest { + + /** + * @throws Exception + * Testing of {@link MeterModInputMessageFactory} for correct translation from POJO + */ + @Test + public void testMeterModInputMessage() throws Exception { + MeterModInputBuilder builder = new MeterModInputBuilder(); + BufferHelper.setupHeader(builder); + builder.setCommand(MeterModCommand.forValue(1)); + builder.setFlags(new MeterFlags(false, true, true, false)); + builder.setMeterId(new MeterId(2248L)); + builder.setBands(createBandsList()); + MeterModInput message = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + MeterModInputMessageFactory factory = MeterModInputMessageFactory.getInstance(); + factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); + + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + Assert.assertEquals("Wrong meterModCommand", message.getCommand().getIntValue(), out.readShort()); + Assert.assertEquals("Wrong meterFlags", message.getFlags(), decodeMeterModFlags(out.readShort())); + Assert.assertEquals("Wrong meterId", message.getMeterId().getValue().intValue(), out.readUnsignedInt()); + Assert.assertEquals("Wrong bands", true, compareBandsLists(createBandsList(), decodeBandsList(out))); + } + + private static MeterFlags decodeMeterModFlags(short input){ + final Boolean _oFPMFKBPS = (input & (1 << 0)) > 0; + final Boolean _oFPMFPKTPS = (input & (1 << 1)) > 0; + final Boolean _oFPMFBURST = (input & (1 << 2)) > 0; + final Boolean _oFPMFSTATS = (input & (1 << 3)) > 0; + return new MeterFlags(_oFPMFBURST, _oFPMFKBPS, _oFPMFPKTPS, _oFPMFSTATS); + } + + private static List createBandsList(){ + List bandsList = new ArrayList(); + BandsBuilder bandsBuilder = new BandsBuilder(); + Bands band; + bandsBuilder.setType(MeterBandType.forValue(1)); + bandsBuilder.setRate(2254L); + bandsBuilder.setBurstSize(12L); + band = bandsBuilder.build(); + bandsList.add(band); + return bandsList; + } + + private static List decodeBandsList(ByteBuf input){ + List bandsList = new ArrayList(); + BandsBuilder bandsBuilder = new BandsBuilder(); + Bands band; + bandsBuilder.setType(MeterBandType.forValue(input.readShort())); + bandsBuilder.setRate(input.readUnsignedInt()); + bandsBuilder.setBurstSize(input.readUnsignedInt()); + band = bandsBuilder.build(); + bandsList.add(band); + return bandsList; + } + + private static boolean compareBandsLists(List bandsFromMessage, List bandsFromBuffer) { + boolean result = false; + int bandsFromMessageLength = bandsFromMessage.size(); + for (int i = 0; i < bandsFromMessageLength; i++) { + if (bandsFromMessage.get(i).getType().equals(bandsFromBuffer.get(i).getType())) { + result = true; + } else { + result = false; + break; + } + if (bandsFromMessage.get(i).getRate() + .equals(bandsFromBuffer.get(i).getRate())) { + result = true; + } else { + result = false; + break; + } + if (bandsFromMessage.get(i).getBurstSize() + .equals(bandsFromBuffer.get(i).getBurstSize())) { + result = true; + } else { + result = false; + break; + } + } + // TODO get method for field length missing + // TODO actions structure missing + return result; + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactoryTest.java index 6a657032..e3d83798 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactoryTest.java @@ -60,32 +60,32 @@ public class PortModInputMessageFactoryTest { } private static PortConfig createPortConfig(long input){ - final Boolean _portDown = ((input) & 0x01) > 0; - final Boolean _noRecv = ((input) & 0x04) > 0; - final Boolean _noFwd = ((input) & 0x20) > 0; - final Boolean _noPacketIn = ((input) & 0x40) > 0; - return new PortConfig(_portDown, _noRecv, _noFwd, _noPacketIn); + final Boolean _portDown = ((input) & (1<<0)) > 0; + final Boolean _noRecv = ((input) & (1<<2)) > 0; + final Boolean _noFwd = ((input) & (1<<5)) > 0; + final Boolean _noPacketIn = ((input) & (1<<6)) > 0; + return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown); } private static PortFeatures createPortFeatures(long input){ - final Boolean _10mbHd = ((input) & 0x01) > 0; - final Boolean _10mbFd = ((input) & 0x02) > 0; - final Boolean _100mbHd = ((input) & 0x04) > 0; - final Boolean _100mbFd = ((input) & 0x08) > 0; - final Boolean _1gbHd = ((input) & 0x10) > 0; - final Boolean _1gbFd = ((input) & 0x20) > 0; - final Boolean _10gbFd = ((input) & 0x40) > 0; - final Boolean _40gbFd = ((input) & 0x80) > 0; - final Boolean _100gbFd = ((input) & 0x100) > 0; - final Boolean _1tbFd = ((input) & 0x200) > 0; - final Boolean _other = ((input) & 0x400) > 0; - final Boolean _copper = ((input) & 0x800) > 0; - final Boolean _fiber = ((input) & 0x1000) > 0; - final Boolean _autoneg = ((input) & 0x2000) > 0; - final Boolean _pause = ((input) & 0x4000) > 0; - final Boolean _pauseAsym = ((input) & 0x8000) > 0; - return new PortFeatures(_10mbHd, _10mbFd, _100mbHd, _100mbFd, _1gbHd, _1gbFd, _10gbFd, - _40gbFd, _100gbFd, _1tbFd, _other, _copper, _fiber, _autoneg, _pause, _pauseAsym); + final Boolean _10mbHd = ((input) & (1<<0)) > 0; + final Boolean _10mbFd = ((input) & (1<<1)) > 0; + final Boolean _100mbHd = ((input) & (1<<2)) > 0; + final Boolean _100mbFd = ((input) & (1<<3)) > 0; + final Boolean _1gbHd = ((input) & (1<<4)) > 0; + final Boolean _1gbFd = ((input) & (1<<5)) > 0; + final Boolean _10gbFd = ((input) & (1<<6)) > 0; + final Boolean _40gbFd = ((input) & (1<<7)) > 0; + final Boolean _100gbFd = ((input) & (1<<8)) > 0; + final Boolean _1tbFd = ((input) & (1<<9)) > 0; + final Boolean _other = ((input) & (1<<10)) > 0; + final Boolean _copper = ((input) & (1<<11)) > 0; + final Boolean _fiber = ((input) & (1<<12)) > 0; + final Boolean _autoneg = ((input) & (1<<13)) > 0; + final Boolean _pause = ((input) & (1<<14)) > 0; + final Boolean _pauseAsym = ((input) & (1<<15)) > 0; + return new PortFeatures(_100gbFd, _100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, + _1gbFd, _1gbHd, _1tbFd, _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym); } private static String makeMacAddress(ByteBuf input) { diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactoryTest.java new file mode 100644 index 00000000..c9952e87 --- /dev/null +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactoryTest.java @@ -0,0 +1,196 @@ +/* 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 java.util.ArrayList; +import java.util.List; + +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.common.types.rev130731.FlowRemovedReason; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInputBuilder; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class SetAsyncInputMessageFactoryTest { + + /** + * @throws Exception + * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO + */ + @Test + public void testSetAsyncInputMessage() throws Exception { + SetAsyncInputBuilder builder = new SetAsyncInputBuilder(); + BufferHelper.setupHeader(builder); + builder.setPacketInMask(createPacketInMask()); + builder.setPortStatusMask(createPortStatusMask()); + builder.setFlowRemovedMask(createFlowRemowedMask()); + SetAsyncInput message = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + SetAsyncInputMessageFactory factory = SetAsyncInputMessageFactory.getInstance(); + factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); + + BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength()); + Assert.assertTrue("Wrong packetInMask", comparePIRLists(message.getPacketInMask(), readPacketInMask(out))); + Assert.assertTrue("Wrong packetInMask", comparePortReasonLists(message.getPortStatusMask(), readPortStatusMask(out))); + Assert.assertTrue("Wrong packetInMask", compareFlowRemovedReasonLists(message.getFlowRemovedMask(), readFlowRemovedReasonMask(out))); + + } + + private static List createPacketInMask() { + List packetInReasonList = new ArrayList(); + packetInReasonList.add(PacketInReason.forValue(1)); + packetInReasonList.add(PacketInReason.forValue(2)); + return packetInReasonList; + } + + private static List createPortStatusMask() { + List portReasonList = new ArrayList(); + portReasonList.add(PortReason.forValue(1)); + portReasonList.add(PortReason.forValue(2)); + return portReasonList; + } + + private static List createFlowRemowedMask() { + List flowRemovedReasonList = new ArrayList(); + flowRemovedReasonList.add(FlowRemovedReason.forValue(2)); + flowRemovedReasonList.add(FlowRemovedReason.forValue(3)); + return flowRemovedReasonList; + } + + private static List readPacketInMask(ByteBuf outputBuf) { + List readPIRList = new ArrayList(); + readPIRList.add(readPacketInReason((int) outputBuf.readUnsignedInt())); + readPIRList.add(readPacketInReason((int) outputBuf.readUnsignedInt())); + return readPIRList; + } + + private static List readPortStatusMask(ByteBuf outputBuf) { + List readPortReasonList = new ArrayList(); + readPortReasonList.add(readPortReason((int) outputBuf.readUnsignedInt())); + readPortReasonList.add(readPortReason((int) outputBuf.readUnsignedInt())); + return readPortReasonList; + } + + private static List readFlowRemovedReasonMask(ByteBuf outputBuf) { + List readFlowRemovedReasonList = new ArrayList(); + readFlowRemovedReasonList.add(readFlowRemovedReason((int) outputBuf.readUnsignedInt())); + readFlowRemovedReasonList.add(readFlowRemovedReason((int) outputBuf.readUnsignedInt())); + return readFlowRemovedReasonList; + } + + private static PacketInReason readPacketInReason(int input) { + PacketInReason reason = null; + Boolean OFPRNOMATCH = (input & (1 << 0)) > 0; + Boolean OFPRACTION = (input & (1 << 1)) > 0; + Boolean OFPRINVALIDTTL = (input & (1 << 2)) > 0; + + if (OFPRNOMATCH) { + return PacketInReason.forValue(0); + } + if (OFPRACTION) { + return PacketInReason.forValue(1); + } + if (OFPRINVALIDTTL) { + return PacketInReason.forValue(2); + } + + return reason; + } + + private static PortReason readPortReason(int input) { + PortReason reason = null; + Boolean OFPPRADD = (input & (1 << 0)) > 0; + Boolean OFPPRDELETE = (input & (1 << 1)) > 0; + Boolean OFPPRMODIFY = (input & (1 << 2)) > 0; + + if (OFPPRADD) { + return PortReason.forValue(0); + } + if (OFPPRDELETE) { + return PortReason.forValue(1); + } + if (OFPPRMODIFY) { + return PortReason.forValue(2); + } + + return reason; + } + + private static FlowRemovedReason readFlowRemovedReason(int input) { + FlowRemovedReason reason = null; + Boolean OFPRRIDLETIMEOUT = (input & (1 << 0)) > 0; + Boolean OFPRRHARDTIMEOUT = (input & (1 << 1)) > 0; + Boolean OFPRRDELETE = (input & (1 << 2)) > 0; + Boolean OFPRRGROUPDELETE = (input & (1 << 3)) > 0; + + if (OFPRRIDLETIMEOUT) { + return FlowRemovedReason.forValue(0); + } + if (OFPRRHARDTIMEOUT) { + return FlowRemovedReason.forValue(1); + } + if (OFPRRDELETE) { + return FlowRemovedReason.forValue(2); + } + if (OFPRRGROUPDELETE) { + return FlowRemovedReason.forValue(3); + } + + return reason; + } + + private static boolean comparePIRLists(List fromMessage, List fromBuffer) { + boolean result = false; + int romMessageLength = fromMessage.size(); + for (int i = 0; i < romMessageLength; i++) { + if ((fromMessage.get(i).getIntValue()) == (fromBuffer.get(i).getIntValue())) { + result = true; + } else { + result = false; + break; + } + } + return result; + } + + private static boolean comparePortReasonLists(List fromMessage, + List fromBuffer) { + boolean result = false; + int fromMessageLength = fromMessage.size(); + for (int i = 0; i < fromMessageLength; i++) { + if ((fromMessage.get(i).getIntValue()) == (fromBuffer.get(i).getIntValue())) { + result = true; + } else { + result = false; + break; + } + } + return result; + } + + private static boolean compareFlowRemovedReasonLists(List fromMessage, + List fromBuffer) { + boolean result = false; + int fromMessageLength = fromMessage.size(); + for (int i = 0; i < fromMessageLength; i++) { + if ((fromMessage.get(i).getIntValue()) == (fromBuffer.get(i).getIntValue())) { + result = true; + } else { + result = false; + break; + } + } + return result; + } +} diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactoryTest.java index ad17ce48..501453bd 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/TableModInputMessageFactoryTest.java @@ -38,8 +38,16 @@ public class TableModInputMessageFactoryTest { factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message); BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 16); - Assert.assertEquals("Wrong TableID", 0x09, out.readByte()); + Assert.assertEquals("Wrong TableID", message.getTableId().getValue().intValue(), out.readByte()); out.skipBytes(PADDING_IN_TABLE_MOD_MESSAGE); - Assert.assertEquals("Wrong PortConfig", 33, out.readInt()); + Assert.assertEquals("Wrong PortConfig", message.getConfig(), createPortConfig(out.readInt())); + } + + private static PortConfig createPortConfig(long input){ + final Boolean _portDown = ((input) & (1<<0)) > 0; + final Boolean _noRecv = ((input) & (1<<2)) > 0; + final Boolean _noFwd = ((input) & (1<<5)) > 0; + final Boolean _noPacketIn = ((input) & (1<<6)) > 0; + return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown); } }