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.deserialization.factories.ErrorMessageFactory.java

License:Open Source License

@Override
public ErrorMessage bufferToMessage(ByteBuf rawMessage, short version) {
    ErrorMessageBuilder builder = new ErrorMessageBuilder();
    builder.setVersion(version);// www.ja  v a  2  s. c  o m
    builder.setXid(rawMessage.readUnsignedInt());
    int type = rawMessage.readUnsignedShort();
    ErrorType errorType = ErrorType.forValue(type);
    decodeType(builder, errorType, type);
    decodeCode(rawMessage, builder, errorType);
    if (rawMessage.readableBytes() > 0) {
        builder.setData(rawMessage.readBytes(rawMessage.readableBytes()).array());
    }
    return builder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.ErrorMessageFactory.java

License:Open Source License

private static void decodeCode(ByteBuf rawMessage, ErrorMessageBuilder builder, ErrorType type) {
    int code = rawMessage.readUnsignedShort();
    if (type != null) {
        switch (type) {
        case HELLOFAILED: {
            HelloFailedCode errorCode = HelloFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }/* w w w  .  j a v a2s  .  co m*/
            break;
        }
        case BADREQUEST: {
            BadRequestCode errorCode = BadRequestCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case BADACTION: {
            BadActionCode errorCode = BadActionCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case BADINSTRUCTION: {
            BadInstructionCode errorCode = BadInstructionCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case BADMATCH: {
            BadMatchCode errorCode = BadMatchCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case FLOWMODFAILED: {
            FlowModFailedCode errorCode = FlowModFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case GROUPMODFAILED: {
            GroupModFailedCode errorCode = GroupModFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case PORTMODFAILED: {
            PortModFailedCode errorCode = PortModFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case TABLEMODFAILED: {
            TableModFailedCode errorCode = TableModFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case QUEUEOPFAILED: {
            QueueOpFailedCode errorCode = QueueOpFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case SWITCHCONFIGFAILED: {
            SwitchConfigFailedCode errorCode = SwitchConfigFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case ROLEREQUESTFAILED: {
            RoleRequestFailedCode errorCode = RoleRequestFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case METERMODFAILED: {
            MeterModFailedCode errorCode = MeterModFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case TABLEFEATURESFAILED: {
            TableFeaturesFailedCode errorCode = TableFeaturesFailedCode.forValue(code);
            if (errorCode != null) {
                setCode(builder, errorCode.getIntValue(), errorCode.name());
            } else {
                setUnknownCode(builder, code);
            }
            break;
        }
        case EXPERIMENTER:
            ExperimenterErrorBuilder expBuilder = new ExperimenterErrorBuilder();
            expBuilder.setExpType(code);
            expBuilder.setExperimenter(rawMessage.readUnsignedInt());
            builder.addAugmentation(ExperimenterError.class, expBuilder.build());
            break;
        default:
            setUnknownCode(builder, code);
            break;
        }
    } else {
        setUnknownCode(builder, code);
    }
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.FlowRemovedMessageFactory.java

License:Open Source License

@Override
public FlowRemovedMessage bufferToMessage(ByteBuf rawMessage, short version) {
    FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
    builder.setVersion(version);//from www  .ja v  a  2  s .co  m
    builder.setXid(rawMessage.readUnsignedInt());
    byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(cookie);
    builder.setCookie(new BigInteger(1, cookie));
    builder.setPriority(rawMessage.readUnsignedShort());
    builder.setReason(FlowRemovedReason.forValue(rawMessage.readUnsignedByte()));
    builder.setTableId(new TableId((long) rawMessage.readUnsignedByte()));
    builder.setDurationSec(rawMessage.readUnsignedInt());
    builder.setDurationNsec(rawMessage.readUnsignedInt());
    builder.setIdleTimeout(rawMessage.readUnsignedShort());
    builder.setHardTimeout(rawMessage.readUnsignedShort());
    byte[] packet_count = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(packet_count);
    builder.setPacketCount(new BigInteger(1, packet_count));
    byte[] byte_count = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(byte_count);
    builder.setByteCount(new BigInteger(1, byte_count));
    builder.setMatch(MatchDeserializer.createMatch(rawMessage));
    return builder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.GetConfigReplyMessageFactory.java

License:Open Source License

@Override
public GetConfigOutput bufferToMessage(ByteBuf rawMessage, short version) {
    GetConfigOutputBuilder builder = new GetConfigOutputBuilder();
    builder.setVersion(version);/*www  .  j a  v  a2 s .c  o  m*/
    builder.setXid(rawMessage.readUnsignedInt());
    builder.setFlags(SwitchConfigFlag.forValue(rawMessage.readUnsignedShort()));
    builder.setMissSendLen(rawMessage.readUnsignedShort());
    return builder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactory.java

License:Open Source License

private static List<Elements> readElement(ByteBuf input) {
    List<Elements> elementsList = new ArrayList<>();
    while (input.readableBytes() > 0) {
        ElementsBuilder elementsBuilder = new ElementsBuilder();
        int type = input.readUnsignedShort();
        int elementLength = input.readUnsignedShort();
        if (type == HelloElementType.VERSIONBITMAP.getIntValue()) {
            elementsBuilder.setType(HelloElementType.forValue(type));
            int[] versionBitmap = new int[(elementLength - HelloInputMessageFactory.HELLO_ELEMENT_HEADER_SIZE)
                    / 4];/*  w w w  .  j a v a2s. c o  m*/
            for (int i = 0; i < versionBitmap.length; i++) {
                versionBitmap[i] = (int) input.readUnsignedInt();
            }
            elementsBuilder.setVersionBitmap(readVersionBitmap(versionBitmap));
            int paddingRemainder = elementLength % EncodeConstants.PADDING;
            if (paddingRemainder != 0) {
                input.readBytes(EncodeConstants.PADDING - paddingRemainder);
            }
        }
        elementsList.add(elementsBuilder.build());
    }
    return elementsList;
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

@Override
public MultipartReplyMessage bufferToMessage(ByteBuf rawMessage, short version) {
    MultipartReplyMessageBuilder builder = new MultipartReplyMessageBuilder();
    builder.setVersion(version);//from  w w w . ja  v a 2  s  .c  om
    builder.setXid(rawMessage.readUnsignedInt());
    int type = rawMessage.readUnsignedShort();
    builder.setType(MultipartType.forValue(type));
    builder.setFlags(new MultipartRequestFlags((rawMessage.readUnsignedShort() & 0x01) != 0));
    rawMessage.skipBytes(PADDING_IN_MULTIPART_REPLY_HEADER);

    switch (type) {
    case 0:
        builder.setMultipartReplyBody(setDesc(rawMessage));
        break;
    case 1:
        builder.setMultipartReplyBody(setFlow(rawMessage));
        break;
    case 2:
        builder.setMultipartReplyBody(setAggregate(rawMessage));
        break;
    case 3:
        builder.setMultipartReplyBody(setTable(rawMessage));
        break;
    case 4:
        builder.setMultipartReplyBody(setPortStats(rawMessage));
        break;
    case 5:
        builder.setMultipartReplyBody(setQueue(rawMessage));
        break;
    case 6:
        builder.setMultipartReplyBody(setGroup(rawMessage));
        break;
    case 7:
        builder.setMultipartReplyBody(setGroupDesc(rawMessage));
        break;
    case 8:
        builder.setMultipartReplyBody(setGroupFeatures(rawMessage));
        break;
    case 9:
        builder.setMultipartReplyBody(setMeter(rawMessage));
        break;
    case 10:
        builder.setMultipartReplyBody(setMeterConfig(rawMessage));
        break;
    case 11:
        builder.setMultipartReplyBody(setMeterFeatures(rawMessage));
        break;
    case 12:
        builder.setMultipartReplyBody(setTableFeatures(rawMessage));
        break;
    case 13:
        builder.setMultipartReplyBody(setPortDesc(rawMessage));
        break;
    case 0xFFFF:
        builder.setMultipartReplyBody(setExperimenter(rawMessage));
        break;
    default:
        break;
    }

    return builder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private static MultipartReplyFlowCase setFlow(ByteBuf input) {
    MultipartReplyFlowCaseBuilder caseBuilder = new MultipartReplyFlowCaseBuilder();
    MultipartReplyFlowBuilder flowBuilder = new MultipartReplyFlowBuilder();
    List<FlowStats> flowStatsList = new ArrayList<>();
    while (input.readableBytes() > 0) {
        FlowStatsBuilder flowStatsBuilder = new FlowStatsBuilder();
        int flowRecordLength = input.readUnsignedShort();
        ByteBuf subInput = input.readSlice(flowRecordLength - EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
        flowStatsBuilder.setTableId(subInput.readUnsignedByte());
        subInput.skipBytes(PADDING_IN_FLOW_STATS_HEADER_01);
        flowStatsBuilder.setDurationSec(subInput.readUnsignedInt());
        flowStatsBuilder.setDurationNsec(subInput.readUnsignedInt());
        flowStatsBuilder.setPriority(subInput.readUnsignedShort());
        flowStatsBuilder.setIdleTimeout(subInput.readUnsignedShort());
        flowStatsBuilder.setHardTimeout(subInput.readUnsignedShort());
        flowStatsBuilder.setFlags(createFlowModFlagsFromBitmap(subInput.readUnsignedShort()));
        subInput.skipBytes(PADDING_IN_FLOW_STATS_HEADER_02);
        byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        subInput.readBytes(cookie);//from   w ww  . ja v  a  2 s .  com
        flowStatsBuilder.setCookie(new BigInteger(1, cookie));
        byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        subInput.readBytes(packetCount);
        flowStatsBuilder.setPacketCount(new BigInteger(1, packetCount));
        byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        subInput.readBytes(byteCount);
        flowStatsBuilder.setByteCount(new BigInteger(1, byteCount));
        flowStatsBuilder.setMatch(MatchDeserializer.createMatch(subInput));
        flowStatsBuilder.setInstruction(
                InstructionsDeserializer.createInstructions(subInput, subInput.readableBytes()));
        flowStatsList.add(flowStatsBuilder.build());
    }
    flowBuilder.setFlowStats(flowStatsList);
    caseBuilder.setMultipartReplyFlow(flowBuilder.build());
    return caseBuilder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private static MultipartReplyTableFeaturesCase setTableFeatures(ByteBuf input) {
    MultipartReplyTableFeaturesCaseBuilder caseBuilder = new MultipartReplyTableFeaturesCaseBuilder();
    MultipartReplyTableFeaturesBuilder builder = new MultipartReplyTableFeaturesBuilder();
    List<TableFeatures> features = new ArrayList<>();
    while (input.readableBytes() > 0) {
        TableFeaturesBuilder featuresBuilder = new TableFeaturesBuilder();
        int length = input.readUnsignedShort();
        featuresBuilder.setTableId(input.readUnsignedByte());
        input.skipBytes(PADDING_IN_MULTIPART_REPLY_TABLE_FEATURES);
        featuresBuilder.setName(input.readBytes(MAX_TABLE_NAME_LENGTH).toString());
        byte[] metadataMatch = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        input.readBytes(metadataMatch);// w w  w .  j  ava2  s  .  c o  m
        featuresBuilder.setMetadataMatch(metadataMatch);
        byte[] metadataWrite = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        input.readBytes(metadataWrite);
        featuresBuilder.setMetadataWrite(metadataWrite);
        featuresBuilder.setConfig(createTableConfig(input.readUnsignedInt()));
        featuresBuilder.setMaxEntries(input.readUnsignedInt());
        featuresBuilder.setTableFeatureProperties(
                createTableFeaturesProperties(input, length - MULTIPART_REPLY_TABLE_FEATURES_STRUCTURE_LENGTH));
        features.add(featuresBuilder.build());
    }
    builder.setTableFeatures(features);
    caseBuilder.setMultipartReplyTableFeatures(builder.build());
    return caseBuilder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private static List<TableFeatureProperties> createTableFeaturesProperties(ByteBuf input, int length) {
    List<TableFeatureProperties> properties = new ArrayList<>();
    int tableFeaturesLength = length;
    while (tableFeaturesLength > 0) {
        TableFeaturePropertiesBuilder builder = new TableFeaturePropertiesBuilder();
        TableFeaturesPropType type = TableFeaturesPropType.forValue(input.readUnsignedShort());
        builder.setType(type);//from  w  ww. ja v a  2 s . c o  m
        int propertyLength = input.readUnsignedShort();
        int paddingRemainder = propertyLength % EncodeConstants.PADDING;
        tableFeaturesLength -= propertyLength;
        if (type.equals(TableFeaturesPropType.OFPTFPTINSTRUCTIONS)
                || type.equals(TableFeaturesPropType.OFPTFPTINSTRUCTIONSMISS)) {
            InstructionRelatedTableFeaturePropertyBuilder insBuilder = new InstructionRelatedTableFeaturePropertyBuilder();
            insBuilder.setInstruction(InstructionsDeserializer.createInstructionIds(input,
                    propertyLength - COMMON_PROPERTY_LENGTH));
            builder.addAugmentation(InstructionRelatedTableFeatureProperty.class, insBuilder.build());
        } else if (type.equals(TableFeaturesPropType.OFPTFPTNEXTTABLES)
                || type.equals(TableFeaturesPropType.OFPTFPTNEXTTABLESMISS)) {
            propertyLength -= COMMON_PROPERTY_LENGTH;
            NextTableRelatedTableFeaturePropertyBuilder tableBuilder = new NextTableRelatedTableFeaturePropertyBuilder();
            List<NextTableIds> ids = new ArrayList<>();
            while (propertyLength > 0) {
                NextTableIdsBuilder nextTableIdsBuilder = new NextTableIdsBuilder();
                nextTableIdsBuilder.setTableId(input.readUnsignedByte());
                ids.add(nextTableIdsBuilder.build());
                propertyLength--;
            }
            tableBuilder.setNextTableIds(ids);
            builder.addAugmentation(NextTableRelatedTableFeatureProperty.class, tableBuilder.build());
        } else if (type.equals(TableFeaturesPropType.OFPTFPTWRITEACTIONS)
                || type.equals(TableFeaturesPropType.OFPTFPTWRITEACTIONSMISS)
                || type.equals(TableFeaturesPropType.OFPTFPTAPPLYACTIONS)
                || type.equals(TableFeaturesPropType.OFPTFPTAPPLYACTIONSMISS)) {
            ActionRelatedTableFeaturePropertyBuilder actionBuilder = new ActionRelatedTableFeaturePropertyBuilder();
            actionBuilder.setAction(
                    ActionsDeserializer.createActionIds(input, propertyLength - COMMON_PROPERTY_LENGTH));
            builder.addAugmentation(ActionRelatedTableFeatureProperty.class, actionBuilder.build());
        } else if (type.equals(TableFeaturesPropType.OFPTFPTMATCH)
                || type.equals(TableFeaturesPropType.OFPTFPTWILDCARDS)
                || type.equals(TableFeaturesPropType.OFPTFPTWRITESETFIELD)
                || type.equals(TableFeaturesPropType.OFPTFPTWRITESETFIELDMISS)
                || type.equals(TableFeaturesPropType.OFPTFPTAPPLYSETFIELD)
                || type.equals(TableFeaturesPropType.OFPTFPTAPPLYSETFIELDMISS)) {
            OxmRelatedTableFeaturePropertyBuilder oxmBuilder = new OxmRelatedTableFeaturePropertyBuilder();
            oxmBuilder.setMatchEntries(
                    MatchDeserializer.createMatchIds(input, propertyLength - COMMON_PROPERTY_LENGTH));
            builder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build());
        } else if (type.equals(TableFeaturesPropType.OFPTFPTEXPERIMENTER)
                || type.equals(TableFeaturesPropType.OFPTFPTEXPERIMENTERMISS)) {
            ExperimenterRelatedTableFeaturePropertyBuilder expBuilder = new ExperimenterRelatedTableFeaturePropertyBuilder();
            expBuilder.setExperimenter(input.readUnsignedInt());
            expBuilder.setExpType(input.readUnsignedInt());
            int dataLength = propertyLength - COMMON_PROPERTY_LENGTH - 2 * EncodeConstants.SIZE_OF_INT_IN_BYTES;
            if (dataLength > 0) {
                byte[] data = new byte[dataLength];
                input.readBytes(data);
                expBuilder.setData(data);
            }
            builder.addAugmentation(ExperimenterRelatedTableFeatureProperty.class, expBuilder.build());
        }
        if (paddingRemainder != 0) {
            input.skipBytes(EncodeConstants.PADDING - paddingRemainder);
            tableFeaturesLength -= EncodeConstants.PADDING - paddingRemainder;
        }
        properties.add(builder.build());
    }
    return properties;
}

From source file:org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory.java

License:Open Source License

private static MultipartReplyGroupCase setGroup(ByteBuf input) {
    MultipartReplyGroupCaseBuilder caseBuilder = new MultipartReplyGroupCaseBuilder();
    MultipartReplyGroupBuilder builder = new MultipartReplyGroupBuilder();
    List<GroupStats> groupStatsList = new ArrayList<>();
    while (input.readableBytes() > 0) {
        GroupStatsBuilder groupStatsBuilder = new GroupStatsBuilder();
        int bodyLength = input.readUnsignedShort();
        input.skipBytes(PADDING_IN_GROUP_HEADER_01);
        groupStatsBuilder.setGroupId(new GroupId(input.readUnsignedInt()));
        groupStatsBuilder.setRefCount(input.readUnsignedInt());
        input.skipBytes(PADDING_IN_GROUP_HEADER_02);
        byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        input.readBytes(packetCount);/*from   w w w.  j a  v a 2 s.  co m*/
        groupStatsBuilder.setPacketCount(new BigInteger(1, packetCount));
        byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        input.readBytes(byteCount);
        groupStatsBuilder.setByteCount(new BigInteger(1, byteCount));
        groupStatsBuilder.setDurationSec(input.readUnsignedInt());
        groupStatsBuilder.setDurationNsec(input.readUnsignedInt());
        int actualLength = GROUP_BODY_LENGTH;
        List<BucketStats> bucketStatsList = new ArrayList<>();
        while (actualLength < bodyLength) {
            BucketStatsBuilder bucketStatsBuilder = new BucketStatsBuilder();
            byte[] packetCountBucket = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
            input.readBytes(packetCountBucket);
            bucketStatsBuilder.setPacketCount(new BigInteger(1, packetCountBucket));
            byte[] byteCountBucket = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
            input.readBytes(byteCountBucket);
            bucketStatsBuilder.setByteCount(new BigInteger(1, byteCountBucket));
            bucketStatsList.add(bucketStatsBuilder.build());
            actualLength += BUCKET_COUNTER_LENGTH;
        }
        groupStatsBuilder.setBucketStats(bucketStatsList);
        groupStatsList.add(groupStatsBuilder.build());
    }
    builder.setGroupStats(groupStatsList);
    caseBuilder.setMultipartReplyGroup(builder.build());
    return caseBuilder.build();
}