Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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

License:Open Source License

/**
 * Testing OF10StatsRequestInputFactory (Port) for correct serialization
 * @throws Exception/*from  w ww  .j a  v a  2s. co m*/
 */
@Test
public void testPort() throws Exception {
    MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setType(MultipartType.OFPMPPORTSTATS);
    builder.setFlags(new MultipartRequestFlags(false));
    MultipartRequestPortStatsCaseBuilder caseBuilder = new MultipartRequestPortStatsCaseBuilder();
    MultipartRequestPortStatsBuilder portBuilder = new MultipartRequestPortStatsBuilder();
    portBuilder.setPortNo(15L);
    caseBuilder.setMultipartRequestPortStats(portBuilder.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(), 20);
    Assert.assertEquals("Wrong type", 4, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 0, out.readUnsignedShort());
    Assert.assertEquals("Wrong port-no", 15, out.readUnsignedShort());
    out.skipBytes(6);
    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 (Queue) for correct serialization
 * @throws Exception/*w ww  . j a va  2  s.  com*/
 */
@Test
public void testQueue() throws Exception {
    MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    builder.setType(MultipartType.OFPMPQUEUE);
    builder.setFlags(new MultipartRequestFlags(false));
    MultipartRequestQueueCaseBuilder caseBuilder = new MultipartRequestQueueCaseBuilder();
    MultipartRequestQueueBuilder queueBuilder = new MultipartRequestQueueBuilder();
    queueBuilder.setPortNo(15L);
    queueBuilder.setQueueId(16L);
    caseBuilder.setMultipartRequestQueue(queueBuilder.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(), 20);
    Assert.assertEquals("Wrong type", 5, out.readUnsignedShort());
    Assert.assertEquals("Wrong flags", 0, out.readUnsignedShort());
    Assert.assertEquals("Wrong port-no", 15, out.readUnsignedShort());
    out.skipBytes(2);
    Assert.assertEquals("Wrong queue-id", 16, out.readUnsignedInt());
    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);/* www  .  ja v a  2 s  . com*/
    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.PacketOutInputMessageFactoryTest.java

License:Open Source License

/**
 * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
 * @throws Exception //from w w w. j  a  v  a 2s  .co m
 */
@Test
public void testPacketOutInputMessage() throws Exception {
    PacketOutInputBuilder builder = new PacketOutInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
    builder.setBufferId(256L);
    builder.setInPort(new PortNumber(256L));
    List<Action> actions = new ArrayList<>();
    ActionBuilder actionBuilder = new ActionBuilder();
    actionBuilder.setType(PushVlan.class);
    EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();
    ethertypeBuilder.setEthertype(new EtherType(25));
    actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());
    actions.add(actionBuilder.build());
    actionBuilder = new ActionBuilder();
    actionBuilder.setType(PopVlan.class);
    actions.add(actionBuilder.build());
    builder.setAction(actions);
    builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
    PacketOutInput message = builder.build();

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    PacketOutInputMessageFactory factory = PacketOutInputMessageFactory.getInstance();
    factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);

    BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 56);
    Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), out.readUnsignedInt());
    Assert.assertEquals("Wrong PortNumber", message.getInPort().getValue().longValue(), out.readUnsignedInt());
    Assert.assertEquals("Wrong ActionsLength", 16, out.readUnsignedShort());
    out.skipBytes(PADDING_IN_PACKET_OUT_MESSAGE);
    Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
    Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
    Assert.assertEquals("Wrong ethertype", 25, out.readUnsignedShort());
    out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
    Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
    Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
    out.skipBytes(PADDING_IN_ACTION_HEADER);
    Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
}

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

License:Open Source License

/**
 * Testing of {@link SetConfigMessageFactory} for correct translation from POJO
 * @throws Exception /*  www  .  j  a  va  2 s .  co  m*/
 */
@Test
public void testSetConfigMessageV13() throws Exception {
    SetConfigInputBuilder builder = new SetConfigInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
    SwitchConfigFlag flag = SwitchConfigFlag.FRAGNORMAL;
    builder.setFlags(flag);
    builder.setMissSendLen(10);
    SetConfigInput message = builder.build();

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

    BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
    Assert.assertEquals("Wrong flags", SwitchConfigFlag.FRAGNORMAL.getIntValue(), out.readUnsignedShort());
    Assert.assertEquals("Wrong missSendLen", 10, out.readUnsignedShort());
}

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

License:Open Source License

/**
 * Testing of {@link SetConfigMessageFactory} for correct translation from POJO
 * @throws Exception /*  w ww.jav a2s. c om*/
 */
@Test
public void testSetConfigMessageV10() throws Exception {
    SetConfigInputBuilder builder = new SetConfigInputBuilder();
    BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
    SwitchConfigFlag flag = SwitchConfigFlag.OFPCFRAGDROP;
    builder.setFlags(flag);
    builder.setMissSendLen(85);
    SetConfigInput message = builder.build();

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

    BufferHelper.checkHeaderV10(out, MESSAGE_TYPE, MESSAGE_LENGTH);
    Assert.assertEquals("Wrong flags", SwitchConfigFlag.OFPCFRAGDROP.getIntValue(), out.readUnsignedShort());
    Assert.assertEquals("Wrong missSendLen", 85, out.readUnsignedShort());
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.match.OxmArpOpSerializerTest.java

License:Open Source License

/**
 * Test correct serialization/*w w w .j  a va  2s .c  o  m*/
 */
@Test
public void testSerialize() {
    MatchEntryBuilder builder = prepareArpOpMatchEntry(1402);

    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    serializer.serialize(builder.build(), buffer);

    checkHeader(buffer, false);
    assertEquals("Wrong value", 1402, buffer.readUnsignedShort());
    assertTrue("Unexpected data", buffer.readableBytes() == 0);
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.match.OxmArpOpSerializerTest.java

License:Open Source License

private static void checkHeader(ByteBuf buffer, boolean hasMask) {
    assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
    short fieldAndMask = buffer.readUnsignedByte();
    assertEquals("Wrong oxm-field", OxmMatchConstants.ARP_OP, fieldAndMask >>> 1);
    assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
    assertEquals("Wrong length", EncodeConstants.SIZE_OF_SHORT_IN_BYTES, buffer.readUnsignedByte());
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.match.OxmArpShaSerializerTest.java

License:Open Source License

private static void checkHeader(ByteBuf buffer, boolean hasMask) {
    assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
    short fieldAndMask = buffer.readUnsignedByte();
    assertEquals("Wrong oxm-field", OxmMatchConstants.ARP_SHA, fieldAndMask >>> 1);
    assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
    if (hasMask) {
        assertEquals("Wrong length", 2 * EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
    } else {/*from  w w w .j a  v  a2s  .  c  o  m*/
        assertEquals("Wrong length", EncodeConstants.MAC_ADDRESS_LENGTH, buffer.readUnsignedByte());
    }
}

From source file:org.opendaylight.openflowjava.protocol.impl.serialization.match.OxmArpSpaSerializerTest.java

License:Open Source License

private static void checkHeader(ByteBuf buffer, boolean hasMask) {
    assertEquals("Wrong oxm-class", OxmMatchConstants.OPENFLOW_BASIC_CLASS, buffer.readUnsignedShort());
    short fieldAndMask = buffer.readUnsignedByte();
    assertEquals("Wrong oxm-field", OxmMatchConstants.ARP_SPA, fieldAndMask >>> 1);
    assertEquals("Wrong hasMask", hasMask, (fieldAndMask & 1) != 0);
    if (hasMask) {
        assertEquals("Wrong length", EncodeConstants.SIZE_OF_LONG_IN_BYTES, buffer.readUnsignedByte());
    } else {/*from w ww  . j ava2  s . c o m*/
        assertEquals("Wrong length", EncodeConstants.SIZE_OF_INT_IN_BYTES, buffer.readUnsignedByte());
    }
}