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.netide.netiplib.NetIPUtils.java

License:Open Source License

/**
 * To openflow message.//from   w w w.j  a v  a  2 s .c  om
 *
 * @param message
 *            the message
 * @return the openflow message
 */
private static OpenFlowMessage toOpenFlowMessage(Message message) {
    if (message.getHeader().getMessageType() != MessageType.OPENFLOW)
        throw new IllegalArgumentException("Can only convert OPENFLOW messages");
    ByteBuf buffer = Unpooled.wrappedBuffer(message.getPayload());
    short ofVersion = buffer.readUnsignedByte();
    OpenFlowMessage ofm = new OpenFlowMessage(ofVersion);
    ofm.setPayload(message.getPayload());
    ofm.setHeader(message.header);

    // DESERIALIZATION
    DeserializerRegistry registry = new DeserializerRegistryImpl();
    registry.init();
    DeserializationFactory factory = new DeserializationFactory();
    factory.setRegistry(registry);

    DataObject dObj = factory.deserialize(buffer, ofVersion);
    ofm.setOfMessage(dObj);
    return ofm;
}

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

License:Open Source License

@Override
public FlowModInput deserialize(ByteBuf rawMessage) {
    FlowModInputBuilder builder = new FlowModInputBuilder();
    builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    builder.setXid(rawMessage.readUnsignedInt());
    byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(cookie);/* w  w w .  j  av a 2s  .  c  om*/
    builder.setCookie(new BigInteger(1, cookie));
    byte[] cookie_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    rawMessage.readBytes(cookie_mask);
    builder.setCookieMask(new BigInteger(1, cookie_mask));
    builder.setTableId(new TableId((long) rawMessage.readUnsignedByte()));
    builder.setCommand(FlowModCommand.forValue(rawMessage.readUnsignedByte()));
    builder.setIdleTimeout(rawMessage.readUnsignedShort());
    builder.setHardTimeout(rawMessage.readUnsignedShort());
    builder.setPriority(rawMessage.readUnsignedShort());
    builder.setBufferId(rawMessage.readUnsignedInt());
    builder.setOutPort(new PortNumber(rawMessage.readUnsignedInt()));
    builder.setOutGroup(rawMessage.readUnsignedInt());
    builder.setFlags(createFlowModFlagsFromBitmap(rawMessage.readUnsignedShort()));
    rawMessage.skipBytes(PADDING);
    OFDeserializer<Match> matchDeserializer = registry.getDeserializer(
            new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, EncodeConstants.EMPTY_VALUE, Match.class));
    builder.setMatch(matchDeserializer.deserialize(rawMessage));
    CodeKeyMaker keyMaker = CodeKeyMakerFactory.createInstructionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
    List<Instruction> instructions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
            rawMessage.readableBytes(), rawMessage, keyMaker, registry);
    builder.setInstruction(instructions);
    return builder.build();
}

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

License:Open Source License

@Override
public GroupModInput deserialize(ByteBuf rawMessage) {
    GroupModInputBuilder builder = new GroupModInputBuilder();
    builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    builder.setXid(rawMessage.readUnsignedInt());
    builder.setCommand(GroupModCommand.forValue(rawMessage.readUnsignedShort()));
    builder.setType(GroupType.forValue(rawMessage.readUnsignedByte()));
    rawMessage.skipBytes(PADDING);/* www .  j  a  va  2s  .com*/
    builder.setGroupId(new GroupId(rawMessage.readUnsignedInt()));
    List<BucketsList> bucketsList = new ArrayList<>();
    while (rawMessage.readableBytes() > 0) {
        BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
        int bucketsLength = rawMessage.readUnsignedShort();
        bucketsBuilder.setWeight(rawMessage.readUnsignedShort());
        bucketsBuilder.setWatchPort(new PortNumber(rawMessage.readUnsignedInt()));
        bucketsBuilder.setWatchGroup(rawMessage.readUnsignedInt());
        rawMessage.skipBytes(PADDING_IN_BUCKETS_HEADER);
        CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
        List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
                bucketsLength - BUCKETS_HEADER_LENGTH, rawMessage, keyMaker, registry);
        bucketsBuilder.setAction(actions);
        bucketsList.add(bucketsBuilder.build());
    }
    builder.setBucketsList(bucketsList);
    return builder.build();
}

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

License:Open Source License

@Override
public MeterModInput deserialize(ByteBuf rawMessage) {
    MeterModInputBuilder builder = new MeterModInputBuilder();
    builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
    builder.setXid(rawMessage.readUnsignedInt());
    builder.setCommand(MeterModCommand.forValue(rawMessage.readUnsignedShort()));
    builder.setFlags(createMeterFlags(rawMessage.readUnsignedShort()));
    builder.setMeterId(new MeterId(rawMessage.readUnsignedInt()));
    List<Bands> bandsList = new ArrayList<>();
    while (rawMessage.readableBytes() > 0) {
        BandsBuilder bandsBuilder = new BandsBuilder();
        int bandStartIndex = rawMessage.readerIndex();
        int bandType = rawMessage.readUnsignedShort();
        switch (bandType) {
        case 1:/*from  ww w  .  j a  va  2s  . c  om*/
            MeterBandDropCaseBuilder bandDropCaseBuilder = new MeterBandDropCaseBuilder();
            MeterBandDropBuilder bandDropBuilder = new MeterBandDropBuilder();
            bandDropBuilder.setType(MeterBandType.forValue(bandType));
            rawMessage.readUnsignedShort();
            bandDropBuilder.setRate(rawMessage.readUnsignedInt());
            bandDropBuilder.setBurstSize(rawMessage.readUnsignedInt());
            rawMessage.skipBytes(PADDING_IN_METER_BAND_DROP_HEADER);
            bandDropCaseBuilder.setMeterBandDrop(bandDropBuilder.build());
            bandsBuilder.setMeterBand(bandDropCaseBuilder.build());
            break;
        case 2:
            MeterBandDscpRemarkCaseBuilder bandDscpRemarkCaseBuilder = new MeterBandDscpRemarkCaseBuilder();
            MeterBandDscpRemarkBuilder bandDscpRemarkBuilder = new MeterBandDscpRemarkBuilder();
            bandDscpRemarkBuilder.setType(MeterBandType.forValue(bandType));
            rawMessage.readUnsignedShort();
            bandDscpRemarkBuilder.setRate(rawMessage.readUnsignedInt());
            bandDscpRemarkBuilder.setBurstSize(rawMessage.readUnsignedInt());
            bandDscpRemarkBuilder.setPrecLevel(rawMessage.readUnsignedByte());
            rawMessage.skipBytes(PADDING_IN_METER_BAND_DSCP_HEADER);
            bandDscpRemarkCaseBuilder.setMeterBandDscpRemark(bandDscpRemarkBuilder.build());
            bandsBuilder.setMeterBand(bandDscpRemarkCaseBuilder.build());
            break;
        case 0xFFFF:
            long expId = rawMessage
                    .getUnsignedInt(rawMessage.readerIndex() + 2 * EncodeConstants.SIZE_OF_INT_IN_BYTES);
            rawMessage.readerIndex(bandStartIndex);
            OFDeserializer<MeterBandExperimenterCase> deserializer = registry
                    .getDeserializer(ExperimenterDeserializerKeyFactory
                            .createMeterBandDeserializerKey(EncodeConstants.OF13_VERSION_ID, expId));
            bandsBuilder.setMeterBand(deserializer.deserialize(rawMessage));
            break;
        }
        bandsList.add(bandsBuilder.build());
    }
    builder.setBands(bandsList);
    return builder.build();
}

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

License:Open Source License

private MultipartRequestTableFeaturesCase setTableFeatures(ByteBuf input) {
    MultipartRequestTableFeaturesCaseBuilder caseBuilder = new MultipartRequestTableFeaturesCaseBuilder();
    MultipartRequestTableFeaturesBuilder tableFeaturesBuilder = new MultipartRequestTableFeaturesBuilder();
    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_REQUEST_TABLE_FEATURES);
        featuresBuilder.setName(ByteBufUtils.decodeNullTerminatedString(input, MAX_TABLE_NAME_LENGTH));
        byte[] metadataMatch = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        input.readBytes(metadataMatch);/*w  w  w . j  ava  2 s.  c om*/
        featuresBuilder.setMetadataMatch(new BigInteger(1, metadataMatch));
        byte[] metadataWrite = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
        input.readBytes(metadataWrite);
        featuresBuilder.setMetadataWrite(new BigInteger(1, metadataWrite));
        featuresBuilder.setConfig(createTableConfig(input.readUnsignedInt()));
        featuresBuilder.setMaxEntries(input.readUnsignedInt());
        featuresBuilder.setTableFeatureProperties(createTableFeaturesProperties(input,
                length - MULTIPART_REQUEST_TABLE_FEATURES_STRUCTURE_LENGTH));
        features.add(featuresBuilder.build());
    }
    tableFeaturesBuilder.setTableFeatures(features);
    caseBuilder.setMultipartRequestTableFeatures(tableFeaturesBuilder.build());
    return caseBuilder.build();
}

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

License:Open Source License

private List<TableFeatureProperties> createTableFeaturesProperties(ByteBuf input, int length) {
    List<TableFeatureProperties> properties = new ArrayList<>();
    int tableFeaturesLength = length;
    while (tableFeaturesLength > 0) {
        int propStartIndex = input.readerIndex();
        TableFeaturePropertiesBuilder builder = new TableFeaturePropertiesBuilder();
        TableFeaturesPropType type = TableFeaturesPropType.forValue(input.readUnsignedShort());
        builder.setType(type);/*w w  w.  j  av  a2 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();
            CodeKeyMaker keyMaker = CodeKeyMakerFactory
                    .createInstructionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
            List<Instruction> instructions = ListDeserializer.deserializeHeaders(
                    EncodeConstants.OF13_VERSION_ID, propertyLength - COMMON_PROPERTY_LENGTH, input, keyMaker,
                    registry);
            insBuilder.setInstruction(instructions);
            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();
            CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
            List<Action> actions = ListDeserializer.deserializeHeaders(EncodeConstants.OF13_VERSION_ID,
                    propertyLength - COMMON_PROPERTY_LENGTH, input, keyMaker, registry);
            actionBuilder.setAction(actions);
            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();
            CodeKeyMaker keyMaker = CodeKeyMakerFactory
                    .createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
            List<MatchEntry> entries = ListDeserializer.deserializeHeaders(EncodeConstants.OF13_VERSION_ID,
                    propertyLength - COMMON_PROPERTY_LENGTH, input, keyMaker, registry);
            oxmBuilder.setMatchEntry(entries);
            builder.addAugmentation(OxmRelatedTableFeatureProperty.class, oxmBuilder.build());
        } else if (type.equals(TableFeaturesPropType.OFPTFPTEXPERIMENTER)
                || type.equals(TableFeaturesPropType.OFPTFPTEXPERIMENTERMISS)) {
            long expId = input.readUnsignedInt();
            input.readerIndex(propStartIndex);
            OFDeserializer<TableFeatureProperties> propDeserializer = registry
                    .getDeserializer(ExperimenterDeserializerKeyFactory
                            .createMultipartReplyTFDeserializerKey(EncodeConstants.OF13_VERSION_ID, expId));
            TableFeatureProperties expProp = propDeserializer.deserialize(input);
            properties.add(expProp);
            continue;
        }
        if (paddingRemainder != 0) {
            input.skipBytes(EncodeConstants.PADDING - paddingRemainder);
            tableFeaturesLength -= EncodeConstants.PADDING - paddingRemainder;
        }
        properties.add(builder.build());
    }
    return properties;
}

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

License:Open Source License

private MultipartRequestFlowCase setFlow(ByteBuf input) {
    MultipartRequestFlowCaseBuilder caseBuilder = new MultipartRequestFlowCaseBuilder();
    MultipartRequestFlowBuilder flowBuilder = new MultipartRequestFlowBuilder();
    flowBuilder.setTableId(input.readUnsignedByte());
    input.skipBytes(FLOW_PADDING_1);/*w w w  .j  av  a  2 s.  c  o  m*/
    flowBuilder.setOutPort(input.readUnsignedInt());
    flowBuilder.setOutGroup(input.readUnsignedInt());
    input.skipBytes(FLOW_PADDING_2);
    byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    input.readBytes(cookie);
    flowBuilder.setCookie(new BigInteger(1, cookie));
    byte[] cookie_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    input.readBytes(cookie_mask);
    flowBuilder.setCookieMask(new BigInteger(1, cookie_mask));
    OFDeserializer<Match> matchDeserializer = registry.getDeserializer(
            new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, EncodeConstants.EMPTY_VALUE, Match.class));
    flowBuilder.setMatch(matchDeserializer.deserialize(input));
    caseBuilder.setMultipartRequestFlow(flowBuilder.build());
    return caseBuilder.build();
}

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

License:Open Source License

private MultipartRequestAggregateCase setAggregate(ByteBuf input) {
    MultipartRequestAggregateCaseBuilder caseBuilder = new MultipartRequestAggregateCaseBuilder();
    MultipartRequestAggregateBuilder aggregateBuilder = new MultipartRequestAggregateBuilder();
    aggregateBuilder.setTableId(input.readUnsignedByte());
    input.skipBytes(AGGREGATE_PADDING_1);
    aggregateBuilder.setOutPort(input.readUnsignedInt());
    aggregateBuilder.setOutGroup(input.readUnsignedInt());
    input.skipBytes(AGGREGATE_PADDING_2);
    byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    input.readBytes(cookie);//from   w w  w.  ja v  a  2  s. c  om
    aggregateBuilder.setCookie(new BigInteger(1, cookie));
    byte[] cookie_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
    input.readBytes(cookie_mask);
    aggregateBuilder.setCookieMask(new BigInteger(1, cookie_mask));
    OFDeserializer<Match> matchDeserializer = registry.getDeserializer(
            new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, EncodeConstants.EMPTY_VALUE, Match.class));
    aggregateBuilder.setMatch(matchDeserializer.deserialize(input));
    caseBuilder.setMultipartRequestAggregate(aggregateBuilder.build());
    return caseBuilder.build();
}

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

License:Open Source License

private MultipartRequestAggregateCase setAggregate(ByteBuf input) {
    MultipartRequestAggregateCaseBuilder caseBuilder = new MultipartRequestAggregateCaseBuilder();
    MultipartRequestAggregateBuilder aggregateBuilder = new MultipartRequestAggregateBuilder();
    OFDeserializer<MatchV10> matchDeserializer = registry.getDeserializer(
            new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE, MatchV10.class));
    aggregateBuilder.setMatchV10(matchDeserializer.deserialize(input));
    aggregateBuilder.setTableId(input.readUnsignedByte());
    input.skipBytes(AGGREGATE_PADDING_1);
    aggregateBuilder.setOutPort((long) input.readUnsignedShort());
    caseBuilder.setMultipartRequestAggregate(aggregateBuilder.build());
    return caseBuilder.build();
}

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

License:Open Source License

private MultipartRequestFlowCase setFlow(ByteBuf input) {
    MultipartRequestFlowCaseBuilder caseBuilder = new MultipartRequestFlowCaseBuilder();
    MultipartRequestFlowBuilder flowBuilder = new MultipartRequestFlowBuilder();
    OFDeserializer<MatchV10> matchDeserializer = registry.getDeserializer(
            new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE, MatchV10.class));
    flowBuilder.setMatchV10(matchDeserializer.deserialize(input));
    flowBuilder.setTableId(input.readUnsignedByte());
    input.skipBytes(FLOW_PADDING_1);/*from w  ww  .j a va 2s  .c  om*/
    flowBuilder.setOutPort((long) input.readUnsignedShort());
    caseBuilder.setMultipartRequestFlow(flowBuilder.build());
    return caseBuilder.build();
}