X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fserialization%2Ffactories%2FSetAsyncInputMessageFactoryTest.java;h=d31a117ad8079d9b8bd024cba0d443696ba72b05;hb=07de1ed897da9d7dc70c6d550f38c59339ed751e;hp=c43c0c368ac2df2034c779b344f4ff07971eda9f;hpb=ff2f581fe0e7a7138dc765cfe713bc2d0791297e;p=openflowjava.git 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 index c43c0c36..d31a117a 100644 --- 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 @@ -1,152 +1,161 @@ -/* 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.assertEquals("Wrong packetInMask", message.getPacketInMask(), readPacketInMask(out)); - Assert.assertEquals("Wrong packetInMask", message.getPortStatusMask(), readPortStatusMask(out)); - Assert.assertEquals("Wrong packetInMask", 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; - } -} +/* + * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.openflowjava.protocol.impl.serialization.factories; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.UnpooledByteBufAllocator; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey; +import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer; +import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl; +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; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMaskBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMaskBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMaskBuilder; + +/** + * @author timotej.kubas + * @author michal.polkorab + */ +public class SetAsyncInputMessageFactoryTest { + + private SerializerRegistry registry; + private OFSerializer setAsyncFactory; + + /** + * Initializes serializer registry and stores correct factory in field + */ + @Before + public void startUp() { + registry = new SerializerRegistryImpl(); + registry.init(); + setAsyncFactory = registry.getSerializer( + new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, SetAsyncInput.class)); + } + + /** + * @throws Exception + * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO + */ + @Test + public void testSetAsyncInputMessage() throws Exception { + SetAsyncInputBuilder builder = new SetAsyncInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); + builder.setPacketInMask(createPacketInMask()); + builder.setPortStatusMask(createPortStatusMask()); + builder.setFlowRemovedMask(createFlowRemowedMask()); + SetAsyncInput message = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + setAsyncFactory.serialize(message, out); + + BufferHelper.checkHeaderV13(out,(byte) 28, 32); + Assert.assertEquals("Wrong packetInMask", 7, out.readUnsignedInt()); + Assert.assertEquals("Wrong packetInMask", 0, out.readUnsignedInt()); + Assert.assertEquals("Wrong portStatusMask", 7, out.readUnsignedInt()); + Assert.assertEquals("Wrong portStatusMask", 0, out.readUnsignedInt()); + Assert.assertEquals("Wrong flowRemovedMask", 15, out.readUnsignedInt()); + Assert.assertEquals("Wrong flowRemovedMask", 0, out.readUnsignedInt()); + + } + + private static List createPacketInMask() { + List masks = new ArrayList<>(); + PacketInMaskBuilder builder; + // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER + builder = new PacketInMaskBuilder(); + List packetInReasonList = new ArrayList<>(); + packetInReasonList.add(PacketInReason.OFPRNOMATCH); + packetInReasonList.add(PacketInReason.OFPRACTION); + packetInReasonList.add(PacketInReason.OFPRINVALIDTTL); + builder.setMask(packetInReasonList); + masks.add(builder.build()); + // OFPCR_ROLE_SLAVE + builder = new PacketInMaskBuilder(); + packetInReasonList = new ArrayList<>(); + builder.setMask(packetInReasonList); + masks.add(builder.build()); + return masks; + } + + private static List createPortStatusMask() { + List masks = new ArrayList<>(); + PortStatusMaskBuilder builder; + builder = new PortStatusMaskBuilder(); + // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER + List portReasonList = new ArrayList<>(); + portReasonList.add(PortReason.OFPPRADD); + portReasonList.add(PortReason.OFPPRDELETE); + portReasonList.add(PortReason.OFPPRMODIFY); + builder.setMask(portReasonList); + masks.add(builder.build()); + // OFPCR_ROLE_SLAVE + builder = new PortStatusMaskBuilder(); + portReasonList = new ArrayList<>(); + builder.setMask(portReasonList); + masks.add(builder.build()); + return masks; + } + + private static List createFlowRemowedMask() { + List masks = new ArrayList<>(); + FlowRemovedMaskBuilder builder; + // OFPCR_ROLE_EQUAL or OFPCR_ROLE_MASTER + builder = new FlowRemovedMaskBuilder(); + List flowRemovedReasonList = new ArrayList<>(); + flowRemovedReasonList.add(FlowRemovedReason.OFPRRIDLETIMEOUT); + flowRemovedReasonList.add(FlowRemovedReason.OFPRRHARDTIMEOUT); + flowRemovedReasonList.add(FlowRemovedReason.OFPRRDELETE); + flowRemovedReasonList.add(FlowRemovedReason.OFPRRGROUPDELETE); + builder.setMask(flowRemovedReasonList); + masks.add(builder.build()); + // OFPCR_ROLE_SLAVE + builder = new FlowRemovedMaskBuilder(); + flowRemovedReasonList = new ArrayList<>(); + builder.setMask(flowRemovedReasonList); + masks.add(builder.build()); + return masks; + } + + /** + * @throws Exception + * Testing of {@link SetAsyncInputMessageFactory} for correct translation from POJO + */ + @Test + public void testSetAsyncInputWithNullMasks() throws Exception { + SetAsyncInputBuilder builder = new SetAsyncInputBuilder(); + BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID); + builder.setPacketInMask(null); + builder.setPortStatusMask(null); + builder.setFlowRemovedMask(null); + SetAsyncInput message = builder.build(); + + ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer(); + setAsyncFactory.serialize(message, out); + + BufferHelper.checkHeaderV13(out,(byte) 28, 8); + Assert.assertTrue("Unexpected data", out.readableBytes() == 0); + } +} \ No newline at end of file