Example usage for io.netty.buffer ByteBuf readUnsignedByte

List of usage examples for io.netty.buffer ByteBuf readUnsignedByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readUnsignedByte.

Prototype

public abstract short readUnsignedByte();

Source Link

Document

Gets an unsigned byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10FlowModInputMessageFactoryTest.java

License:Open Source License

/**
 * @throws Exception //from w  w w. jav a 2 s. c  om
 * Testing of {@link OF10FlowModInputMessageFactory} for correct translation from POJO
 */
@Test
public void testFlowModInputMessageFactory() throws Exception {
    FlowModInputBuilder builder = new FlowModInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    MatchV10Builder matchBuilder = new MatchV10Builder();
    matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
    matchBuilder.setNwSrcMask((short) 0);
    matchBuilder.setNwDstMask((short) 0);
    matchBuilder.setInPort(58);
    matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
    matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));
    matchBuilder.setDlVlan(18);
    matchBuilder.setDlVlanPcp((short) 5);
    matchBuilder.setDlType(42);
    matchBuilder.setNwTos((short) 4);
    matchBuilder.setNwProto((short) 7);
    matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
    matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
    matchBuilder.setTpSrc(6653);
    matchBuilder.setTpDst(6633);
    builder.setMatchV10(matchBuilder.build());
    byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
    builder.setCookie(new BigInteger(1, cookie));
    builder.setCommand(FlowModCommand.forValue(0));
    builder.setIdleTimeout(12);
    builder.setHardTimeout(16);
    builder.setPriority(1);
    builder.setBufferId(2L);
    builder.setOutPort(new PortNumber(4422L));
    builder.setFlagsV10(new FlowModFlagsV10(true, false, true));
    List<Action> actions = new ArrayList<>();
    ActionBuilder actionBuilder = new ActionBuilder();
    actionBuilder.setType(SetNwDst.class);
    IpAddressActionBuilder ipBuilder = new IpAddressActionBuilder();
    ipBuilder.setIpAddress(new Ipv4Address("2.2.2.2"));
    actionBuilder.addAugmentation(IpAddressAction.class, ipBuilder.build());
    actions.add(actionBuilder.build());
    actionBuilder = new ActionBuilder();
    actionBuilder.setType(SetTpSrc.class);
    PortActionBuilder portBuilder = new PortActionBuilder();
    portBuilder.setPort(new PortNumber(42L));
    actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
    actions.add(actionBuilder.build());
    builder.setAction(actions);
    FlowModInput message = builder.build();

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    OF10FlowModInputMessageFactory factory = OF10FlowModInputMessageFactory.getInstance();
    factory.messageToBuffer(EncodeConstants.OF10_VERSION_ID, out, message);

    BufferHelper.checkHeaderV10(out, factory.getMessageType(), factory.computeLength(message));
    Assert.assertEquals("Wrong wildcards", 3678463, out.readUnsignedInt());
    Assert.assertEquals("Wrong inPort", 58, out.readUnsignedShort());
    byte[] dlSrc = new byte[6];
    out.readBytes(dlSrc);
    Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
    byte[] dlDst = new byte[6];
    out.readBytes(dlDst);
    Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));
    Assert.assertEquals("Wrong dlVlan", 18, out.readUnsignedShort());
    Assert.assertEquals("Wrong dlVlanPcp", 5, out.readUnsignedByte());
    out.skipBytes(1);
    Assert.assertEquals("Wrong dlType", 42, out.readUnsignedShort());
    Assert.assertEquals("Wrong nwTos", 4, out.readUnsignedByte());
    Assert.assertEquals("Wrong nwProto", 7, out.readUnsignedByte());
    out.skipBytes(2);
    Assert.assertEquals("Wrong nwSrc", 134744072, out.readUnsignedInt());
    Assert.assertEquals("Wrong nwDst", 269488144, out.readUnsignedInt());
    Assert.assertEquals("Wrong tpSrc", 6653, out.readUnsignedShort());
    Assert.assertEquals("Wrong tpDst", 6633, out.readUnsignedShort());
    byte[] cookieRead = new byte[8];
    out.readBytes(cookieRead);
    Assert.assertArrayEquals("Wrong cookie", cookie, cookieRead);
    Assert.assertEquals("Wrong command", 0, out.readUnsignedShort());
    Assert.assertEquals("Wrong idleTimeOut", 12, out.readUnsignedShort());
    Assert.assertEquals("Wrong hardTimeOut", 16, out.readUnsignedShort());
    Assert.assertEquals("Wrong priority", 1, out.readUnsignedShort());
    Assert.assertEquals("Wrong bufferId", 2, out.readUnsignedInt());
    Assert.assertEquals("Wrong outPort", 4422, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort());
    Assert.assertEquals("Wrong action - type", 7, out.readUnsignedShort());
    Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 33686018, out.readUnsignedInt());
    Assert.assertEquals("Wrong action - type", 9, out.readUnsignedShort());
    Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 42, out.readUnsignedShort());
    out.skipBytes(2);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10FlowRemovedMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() throws Exception {
    FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    MatchV10Builder matchBuilder = new MatchV10Builder();
    matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
    matchBuilder.setNwSrcMask((short) 0);
    matchBuilder.setNwDstMask((short) 0);
    matchBuilder.setInPort(58);/*  ww w.java 2  s .  co m*/
    matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
    matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));
    matchBuilder.setDlVlan(18);
    matchBuilder.setDlVlanPcp((short) 5);
    matchBuilder.setDlType(42);
    matchBuilder.setNwTos((short) 4);
    matchBuilder.setNwProto((short) 7);
    matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
    matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
    matchBuilder.setTpSrc(6653);
    matchBuilder.setTpDst(6633);
    builder.setMatchV10(matchBuilder.build());
    byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x01, 0x01, 0x04, 0x01 };
    builder.setCookie(new BigInteger(1, cookie));
    builder.setPriority(1);
    builder.setReason(FlowRemovedReason.forValue(1));
    builder.setDurationSec(1L);
    builder.setDurationNsec(1L);
    builder.setIdleTimeout(12);
    builder.setPacketCount(BigInteger.valueOf(1L));
    builder.setByteCount(BigInteger.valueOf(2L));
    FlowRemovedMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 88);
    Assert.assertEquals("Wrong wildcards", 3678463, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong inPort", 58, serializedBuffer.readUnsignedShort());
    byte[] dlSrc = new byte[6];
    serializedBuffer.readBytes(dlSrc);
    Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
    byte[] dlDst = new byte[6];
    serializedBuffer.readBytes(dlDst);
    Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));
    Assert.assertEquals("Wrong dlVlan", 18, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong dlVlanPcp", 5, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(1);
    Assert.assertEquals("Wrong dlType", 42, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong nwTos", 4, serializedBuffer.readUnsignedByte());
    Assert.assertEquals("Wrong nwProto", 7, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(2);
    Assert.assertEquals("Wrong nwSrc", 134744072, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong nwDst", 269488144, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong tpSrc", 6653, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong tpDst", 6633, serializedBuffer.readUnsignedShort());
    byte[] cookieRead = new byte[8];
    serializedBuffer.readBytes(cookieRead);
    Assert.assertArrayEquals("Wrong cookie", cookie, cookieRead);
    Assert.assertEquals("Wrong priority", 1, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong reason", 1, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(1);
    Assert.assertEquals("Wrong duration", 1L, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong duration nsec", 1L, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong idle timeout", 12, serializedBuffer.readUnsignedShort());
    serializedBuffer.skipBytes(2);
    Assert.assertEquals("Wrong packet count", 1L, serializedBuffer.readLong());
    Assert.assertEquals("Wrong byte count", 2L, serializedBuffer.readLong());
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10PacketInMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() throws Exception {
    PacketInMessageBuilder builder = new PacketInMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setBufferId(1L);/*ww w.java2  s.  c  o  m*/
    builder.setTotalLen(1);
    builder.setInPort(1);
    builder.setReason(PacketInReason.forValue(0));
    byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
    builder.setData(data);
    PacketInMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 34);
    Assert.assertEquals("Wrong buffer id", message.getBufferId().longValue(),
            serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong total len", message.getTotalLen().intValue(),
            serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong port in", message.getInPort().intValue(), serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(1);
    byte[] readData = new byte[serializedBuffer.readableBytes()];
    serializedBuffer.readBytes(readData);
    Assert.assertArrayEquals("Wrong data", message.getData(), readData);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10PortStatusMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() throws Exception {
    PortStatusMessageBuilder builder = new PortStatusMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setReason(PortReason.forValue(1));
    builder.setPortNo(1L);//from w  ww . j  ava 2s  .c  o m
    builder.setHwAddr(new MacAddress("94:de:80:a6:61:40"));
    builder.setName("Port name");
    builder.setConfigV10(new PortConfigV10(true, false, true, false, true, false, true));
    builder.setStateV10(new PortStateV10(true, false, true, false, true, false, true, false));
    builder.setCurrentFeaturesV10(
            new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
    builder.setAdvertisedFeaturesV10(
            new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
    builder.setSupportedFeaturesV10(
            new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
    builder.setPeerFeaturesV10(
            new PortFeaturesV10(true, false, true, false, true, false, true, false, true, false, true, false));
    PortStatusMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 64);
    Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(7);
    Assert.assertEquals("Wrong port No", message.getPortNo().intValue(), serializedBuffer.readShort());
    byte[] address = new byte[6];
    serializedBuffer.readBytes(address);
    Assert.assertEquals("Wrong MacAddress", message.getHwAddr().getValue().toLowerCase(),
            new MacAddress(ByteBufUtils.macAddressToString(address)).getValue().toLowerCase());
    byte[] name = new byte[16];
    serializedBuffer.readBytes(name);
    Assert.assertEquals("Wrong name", message.getName(), new String(name).trim());
    Assert.assertEquals("Wrong config", message.getConfigV10(), createPortConfig(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong state", message.getStateV10(), createPortState(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong current", message.getCurrentFeaturesV10(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong advertised", message.getAdvertisedFeaturesV10(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong supported", message.getSupportedFeaturesV10(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong peer", message.getPeerFeaturesV10(),
            createPortFeatures(serializedBuffer.readInt()));
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10StatsReplyMessageFactoryTest.java

License:Open Source License

@Test
public void testFlowBodySerialize() throws Exception {
    MultipartReplyMessageBuilder builder;
    builder = new MultipartReplyMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setFlags(new MultipartRequestFlags(true));
    builder.setType(MultipartType.forValue(1));
    MultipartReplyFlowCaseBuilder flowCase = new MultipartReplyFlowCaseBuilder();
    MultipartReplyFlowBuilder flow = new MultipartReplyFlowBuilder();
    flow.setFlowStats(createFlowStats());
    flowCase.setMultipartReplyFlow(flow.build());
    builder.setMultipartReplyBody(flowCase.build());
    MultipartReplyMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 108);
    Assert.assertEquals("Wrong type", MultipartType.OFPMPFLOW.getIntValue(), serializedBuffer.readShort());
    Assert.assertEquals("Wrong flags", message.getFlags(),
            createMultipartRequestFlags(serializedBuffer.readShort()));
    FlowStats flowStats = flow.getFlowStats().get(0);
    Assert.assertEquals("Wrong length", 96, serializedBuffer.readShort());
    Assert.assertEquals("Wrong Table ID", flowStats.getTableId().intValue(),
            serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(1);//from   www  . j a va2s  .  c  o m
    Assert.assertEquals("Wrong wildcards", 3678463, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong inPort", 58, serializedBuffer.readUnsignedShort());
    byte[] dlSrc = new byte[6];
    serializedBuffer.readBytes(dlSrc);
    Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
    byte[] dlDst = new byte[6];
    serializedBuffer.readBytes(dlDst);
    Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));
    Assert.assertEquals("Wrong dlVlan", 18, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong dlVlanPcp", 5, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(1);
    Assert.assertEquals("Wrong dlType", 42, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong nwTos", 4, serializedBuffer.readUnsignedByte());
    Assert.assertEquals("Wrong nwProto", 7, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(2);
    Assert.assertEquals("Wrong nwSrc", 134744072, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong nwDst", 269488144, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong tpSrc", 6653, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong tpDst", 6633, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong duration sec", flowStats.getDurationSec().intValue(),
            serializedBuffer.readInt());
    Assert.assertEquals("Wrong duration nsec", flowStats.getDurationNsec().intValue(),
            serializedBuffer.readInt());
    Assert.assertEquals("Wrong priority", flowStats.getPriority().intValue(), serializedBuffer.readShort());
    Assert.assertEquals("Wrong idle timeout", flowStats.getIdleTimeout().intValue(),
            serializedBuffer.readShort());
    Assert.assertEquals("Wrong hard timeout", flowStats.getHardTimeout().intValue(),
            serializedBuffer.readShort());
    serializedBuffer.skipBytes(6);
    Assert.assertEquals("Wrong cookie", flowStats.getCookie().longValue(), serializedBuffer.readLong());
    Assert.assertEquals("Wrong Packet count", flowStats.getPacketCount().longValue(),
            serializedBuffer.readLong());
    Assert.assertEquals("Wrong Byte count", flowStats.getByteCount().longValue(), serializedBuffer.readLong());
    Assert.assertEquals("Wrong action type", 0, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong action length", 8, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong port", 42, serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong maxlength", 50, serializedBuffer.readUnsignedShort());
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10StatsReplyMessageFactoryTest.java

License:Open Source License

@Test
public void testTableBodySerialize() throws Exception {
    MultipartReplyMessageBuilder builder;
    builder = new MultipartReplyMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setFlags(new MultipartRequestFlags(true));
    builder.setType(MultipartType.forValue(3));
    MultipartReplyTableCaseBuilder tableCase = new MultipartReplyTableCaseBuilder();
    MultipartReplyTableBuilder table = new MultipartReplyTableBuilder();
    table.setTableStats(createTableStats());
    tableCase.setMultipartReplyTable(table.build());
    builder.setMultipartReplyBody(tableCase.build());
    MultipartReplyMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 60);
    Assert.assertEquals("Wrong type", MultipartType.OFPMPTABLE.getIntValue(), serializedBuffer.readShort());
    Assert.assertEquals("Wrong flags", message.getFlags(),
            createMultipartRequestFlags(serializedBuffer.readShort()));
    Assert.assertEquals("Wrong table id", 1, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(3);/*w ww  .j  av a 2s.com*/
    Assert.assertEquals("Wrong name", "Table name",
            ByteBufUtils.decodeNullTerminatedString(serializedBuffer, 16));
    Assert.assertEquals("Wrong wildcards", 3145983, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong max entries", 1L, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong active count", 1L, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong lookup count", 1234L, serializedBuffer.readLong());
    Assert.assertEquals("Wrong matched count", 1234L, serializedBuffer.readLong());
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10StatsRequestInputFactoryTest.java

License:Open Source License

/**
 * Testing OF10StatsRequestInputFactory (Flow) for correct serialization
 * @throws Exception//from w  ww.jav  a2s. c  o m
 */
@Test
public void testFlow() throws Exception {
    MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setType(MultipartType.OFPMPFLOW);
    builder.setFlags(new MultipartRequestFlags(false));
    MultipartRequestFlowCaseBuilder caseBuilder = new MultipartRequestFlowCaseBuilder();
    MultipartRequestFlowBuilder flowBuilder = new MultipartRequestFlowBuilder();
    MatchV10Builder matchBuilder = new MatchV10Builder();
    matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
    matchBuilder.setNwSrcMask((short) 8);
    matchBuilder.setNwDstMask((short) 16);
    matchBuilder.setInPort(51);
    matchBuilder.setDlSrc(new MacAddress("00:01:02:03:04:05"));
    matchBuilder.setDlDst(new MacAddress("05:04:03:02:01:00"));
    matchBuilder.setDlVlan(52);
    matchBuilder.setDlVlanPcp((short) 53);
    matchBuilder.setDlType(54);
    matchBuilder.setNwTos((short) 55);
    matchBuilder.setNwProto((short) 56);
    matchBuilder.setNwSrc(new Ipv4Address("10.0.0.1"));
    matchBuilder.setNwDst(new Ipv4Address("10.0.0.2"));
    matchBuilder.setTpSrc(57);
    matchBuilder.setTpDst(58);
    flowBuilder.setMatchV10(matchBuilder.build());
    flowBuilder.setTableId((short) 1);
    flowBuilder.setOutPort(42L);
    caseBuilder.setMultipartRequestFlow(flowBuilder.build());
    builder.setMultipartRequestBody(caseBuilder.build());
    MultipartRequestInput message = builder.build();

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    OF10StatsRequestInputFactory factory = OF10StatsRequestInputFactory.getInstance();
    factory.messageToBuffer(EncodeConstants.OF10_VERSION_ID, out, message);

    BufferHelper.checkHeaderV10(out, factory.getMessageType(), 56);
    Assert.assertEquals("Wrong type", 1, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 0, out.readUnsignedShort());
    Assert.assertEquals("Wrong wildcards", 3414271, out.readUnsignedInt());
    Assert.assertEquals("Wrong in-port", 51, out.readUnsignedShort());
    byte[] dlSrc = new byte[6];
    out.readBytes(dlSrc);
    Assert.assertEquals("Wrong dl-src", "00:01:02:03:04:05", ByteBufUtils.macAddressToString(dlSrc));
    byte[] dlDst = new byte[6];
    out.readBytes(dlDst);
    Assert.assertEquals("Wrong dl-dst", "05:04:03:02:01:00", ByteBufUtils.macAddressToString(dlDst));
    Assert.assertEquals("Wrong dl-vlan", 52, out.readUnsignedShort());
    Assert.assertEquals("Wrong dl-vlan-pcp", 53, out.readUnsignedByte());
    out.skipBytes(1);
    Assert.assertEquals("Wrong dl-type", 54, out.readUnsignedShort());
    Assert.assertEquals("Wrong nw-tos", 55, out.readUnsignedByte());
    Assert.assertEquals("Wrong nw-proto", 56, out.readUnsignedByte());
    out.skipBytes(2);
    Assert.assertEquals("Wrong nw-src", 167772161, out.readUnsignedInt());
    Assert.assertEquals("Wrong nw-dst", 167772162, out.readUnsignedInt());
    Assert.assertEquals("Wrong tp-src", 57, out.readUnsignedShort());
    Assert.assertEquals("Wrong tp-dst", 58, out.readUnsignedShort());
    Assert.assertEquals("Wrong table-id", 1, out.readUnsignedByte());
    out.skipBytes(1);
    Assert.assertEquals("Wrong out-port", 42, out.readUnsignedShort());
    Assert.assertTrue("Unread data", out.readableBytes() == 0);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.OF10StatsRequestInputFactoryTest.java

License:Open Source License

/**
 * Testing OF10StatsRequestInputFactory (Aggregate) for correct serialization
 * @throws Exception/*from  w w w  .  ja  v  a2  s. c o  m*/
 */
@Test
public void testAggregate() throws Exception {
    MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setType(MultipartType.OFPMPAGGREGATE);
    builder.setFlags(new MultipartRequestFlags(false));
    MultipartRequestFlowCaseBuilder caseBuilder = new MultipartRequestFlowCaseBuilder();
    MultipartRequestFlowBuilder flowBuilder = new MultipartRequestFlowBuilder();
    MatchV10Builder matchBuilder = new MatchV10Builder();
    matchBuilder.setWildcards(
            new FlowWildcardsV10(false, false, false, false, false, false, false, false, false, false));
    matchBuilder.setNwSrcMask((short) 32);
    matchBuilder.setNwDstMask((short) 32);
    matchBuilder.setInPort(51);
    matchBuilder.setDlSrc(new MacAddress("00:01:02:03:04:05"));
    matchBuilder.setDlDst(new MacAddress("05:04:03:02:01:00"));
    matchBuilder.setDlVlan(52);
    matchBuilder.setDlVlanPcp((short) 53);
    matchBuilder.setDlType(54);
    matchBuilder.setNwTos((short) 55);
    matchBuilder.setNwProto((short) 56);
    matchBuilder.setNwSrc(new Ipv4Address("10.0.0.1"));
    matchBuilder.setNwDst(new Ipv4Address("10.0.0.2"));
    matchBuilder.setTpSrc(57);
    matchBuilder.setTpDst(58);
    flowBuilder.setMatchV10(matchBuilder.build());
    flowBuilder.setTableId((short) 42);
    flowBuilder.setOutPort(6653L);
    caseBuilder.setMultipartRequestFlow(flowBuilder.build());
    builder.setMultipartRequestBody(caseBuilder.build());
    MultipartRequestInput message = builder.build();

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    OF10StatsRequestInputFactory factory = OF10StatsRequestInputFactory.getInstance();
    factory.messageToBuffer(EncodeConstants.OF10_VERSION_ID, out, message);

    BufferHelper.checkHeaderV10(out, factory.getMessageType(), 56);
    Assert.assertEquals("Wrong type", 2, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 0, out.readUnsignedShort());
    Assert.assertEquals("Wrong wildcards", 0, out.readUnsignedInt());
    Assert.assertEquals("Wrong in-port", 51, out.readUnsignedShort());
    byte[] dlSrc = new byte[6];
    out.readBytes(dlSrc);
    Assert.assertEquals("Wrong dl-src", "00:01:02:03:04:05", ByteBufUtils.macAddressToString(dlSrc));
    byte[] dlDst = new byte[6];
    out.readBytes(dlDst);
    Assert.assertEquals("Wrong dl-dst", "05:04:03:02:01:00", ByteBufUtils.macAddressToString(dlDst));
    Assert.assertEquals("Wrong dl-vlan", 52, out.readUnsignedShort());
    Assert.assertEquals("Wrong dl-vlan-pcp", 53, out.readUnsignedByte());
    out.skipBytes(1);
    Assert.assertEquals("Wrong dl-type", 54, out.readUnsignedShort());
    Assert.assertEquals("Wrong nw-tos", 55, out.readUnsignedByte());
    Assert.assertEquals("Wrong nw-proto", 56, out.readUnsignedByte());
    out.skipBytes(2);
    Assert.assertEquals("Wrong nw-src", 167772161, out.readUnsignedInt());
    Assert.assertEquals("Wrong nw-dst", 167772162, out.readUnsignedInt());
    Assert.assertEquals("Wrong tp-src", 57, out.readUnsignedShort());
    Assert.assertEquals("Wrong tp-dst", 58, out.readUnsignedShort());
    Assert.assertEquals("Wrong table-id", 42, out.readUnsignedByte());
    out.skipBytes(1);
    Assert.assertEquals("Wrong out-port", 6653, out.readUnsignedShort());
    Assert.assertTrue("Unread data", out.readableBytes() == 0);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.PacketInMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() throws Exception {
    PacketInMessageBuilder builder = new PacketInMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
    builder.setBufferId(256L);//from w w w .j av a  2  s. co  m
    builder.setTotalLen(10);
    builder.setReason(PacketInReason.forValue(0));
    builder.setTableId(new TableId(1L));
    byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
    builder.setCookie(new BigInteger(1, cookie));
    MatchBuilder matchBuilder = new MatchBuilder();
    matchBuilder.setType(OxmMatchType.class);
    List<MatchEntry> entries = new ArrayList<>();
    MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
    entriesBuilder.setOxmClass(OpenflowBasicClass.class);
    entriesBuilder.setOxmMatchField(InPhyPort.class);
    entriesBuilder.setHasMask(false);
    InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
    InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
    inPhyPortBuilder.setPortNumber(new PortNumber(42L));
    inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
    entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
    entries.add(entriesBuilder.build());
    entriesBuilder.setOxmClass(OpenflowBasicClass.class);
    entriesBuilder.setOxmMatchField(IpEcn.class);
    entriesBuilder.setHasMask(false);
    IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
    IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
    ipEcnBuilder.setEcn((short) 4);
    ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
    entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
    entries.add(entriesBuilder.build());
    matchBuilder.setMatchEntry(entries);
    builder.setMatch(matchBuilder.build());
    byte[] data = ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14");
    builder.setData(data);
    PacketInMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 66);
    Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(),
            serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong actions length", message.getTotalLen().intValue(),
            serializedBuffer.readUnsignedShort());
    Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
    Assert.assertEquals("Wrong tableId", message.getTableId().getValue().intValue(),
            serializedBuffer.readUnsignedByte());
    cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    serializedBuffer.readBytes(cookie);
    Assert.assertEquals("Wrong cookie", message.getCookie(), new BigInteger(1, cookie));
    Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort());
    serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
    Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
    short fieldAndMask = serializedBuffer.readUnsignedByte();
    Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
    Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
    serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
    Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt());
    Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
    fieldAndMask = serializedBuffer.readUnsignedByte();
    Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
    Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
    serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
    Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(7);
    serializedBuffer.skipBytes(PADDING);
    byte[] readData = new byte[serializedBuffer.readableBytes()];
    serializedBuffer.readBytes(readData);
    Assert.assertArrayEquals("Wrong data", message.getData(), readData);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.factories.PortStatusMessageFactoryTest.java

License:Open Source License

@Test
public void testSerialize() throws Exception {
    PortStatusMessageBuilder builder = new PortStatusMessageBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
    builder.setReason(PortReason.forValue(1));
    builder.setPortNo(1L);// w  w  w  .  jav  a  2s .  c  o  m
    builder.setHwAddr(new MacAddress("94:de:80:a6:61:40"));
    builder.setName("Port name");
    builder.setConfig(new PortConfig(true, false, true, false));
    builder.setState(new PortState(true, false, true));
    builder.setCurrentFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true, false,
            true, false, true, false, true, false));
    builder.setAdvertisedFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true,
            false, true, false, true, false, true, false));
    builder.setSupportedFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true,
            false, true, false, true, false, true, false));
    builder.setPeerFeatures(new PortFeatures(true, false, true, false, true, false, true, false, true, false,
            true, false, true, false, true, false));
    builder.setCurrSpeed(1234L);
    builder.setMaxSpeed(1234L);
    PortStatusMessage message = builder.build();

    ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
    factory.serialize(message, serializedBuffer);
    BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 80);
    Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readUnsignedByte());
    serializedBuffer.skipBytes(PADDING);
    Assert.assertEquals("Wrong PortNo", message.getPortNo().intValue(), serializedBuffer.readUnsignedInt());
    serializedBuffer.skipBytes(PORT_PADDING_1);
    byte[] address = new byte[6];
    serializedBuffer.readBytes(address);
    Assert.assertEquals("Wrong MacAddress", message.getHwAddr().getValue().toLowerCase(),
            new MacAddress(ByteBufUtils.macAddressToString(address)).getValue().toLowerCase());
    serializedBuffer.skipBytes(PORT_PADDING_2);
    byte[] name = new byte[16];
    serializedBuffer.readBytes(name);
    Assert.assertEquals("Wrong name", message.getName(), new String(name).trim());
    Assert.assertEquals("Wrong config", message.getConfig(), createPortConfig(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong state", message.getState(), createPortState(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong current", message.getCurrentFeatures(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong advertised", message.getAdvertisedFeatures(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong supported", message.getSupportedFeatures(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong peer", message.getPeerFeatures(),
            createPortFeatures(serializedBuffer.readInt()));
    Assert.assertEquals("Wrong Current speed", message.getCurrSpeed().longValue(), serializedBuffer.readInt());
    Assert.assertEquals("Wrong Max speed", message.getMaxSpeed().longValue(), serializedBuffer.readInt());
}