Example usage for io.netty.buffer ByteBuf getUnsignedInt

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

Introduction

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

Prototype

public abstract long getUnsignedInt(int index);

Source Link

Document

Gets an unsigned 32-bit integer at the specified absolute index in this buffer.

Usage

From source file:com.streamsets.pipeline.lib.parser.collectd.CollectdParser.java

License:Apache License

/**
 * Parses the value part of the packet where metrics are located
 *
 * @param startOffset beginning offset for this part
 * @param buf         buffered packet/*from   www . jav a  2s  . com*/
 * @return offset after consuming part
 */
private int parseValues(int startOffset, ByteBuf buf) throws OnRecordErrorException {
    int offset = startOffset;
    // N Values
    // For each Value:
    // 1 byte data type code
    int numValues = buf.getUnsignedShort(offset); // 4-5
    offset += 2;

    List<Byte> types = new ArrayList<>(numValues);

    while (numValues-- > 0) {
        types.add(buf.getByte(offset));
        offset += 1;
    }

    for (int i = 0; i < types.size(); i++) {
        Byte type = types.get(i);
        String label = getValueLabel(i, type);
        switch (type) {
        case COUNTER:
            fields.put(label, Field.create(buf.getUnsignedInt(offset)));
            offset += 8;
            break;
        case GAUGE:
            fields.put(label, Field.create(buf.order(ByteOrder.LITTLE_ENDIAN).getDouble(offset)));
            offset += 8;
            break;
        case DERIVE:
            fields.put(label, Field.create(buf.getLong(offset)));
            offset += 8;
            break;
        case ABSOLUTE:
            fields.put(label, Field.create(buf.getUnsignedInt(offset)));
            offset += 8;
            break;
        default:
            // error
            throw new OnRecordErrorException(Errors.COLLECTD_01, type);
        }
    }
    return offset;
}

From source file:com.streamsets.pipeline.lib.parser.netflow.NetflowParser.java

License:Apache License

private List<Record> parseV5(int version, int packetLength, ByteBuf buf, String readerId,
        InetSocketAddress sender) throws OnRecordErrorException {
    int count = buf.getUnsignedShort(2); // 2-3
    if (count <= 0) {
        throw new OnRecordErrorException(Errors.NETFLOW_01, Utils.format("Count is invalid: {}", count));
    } else if (packetLength < V5_HEADER_SIZE + count * V5_FLOW_SIZE) {
        String msg = Utils.format("Readable bytes {} too small for count {} (max {})", packetLength, count,
                (V5_HEADER_SIZE + count * V5_FLOW_SIZE));
        throw new OnRecordErrorException(Errors.NETFLOW_01, msg);
    }/*from  w  ww.j  ava  2 s. co m*/
    List<Record> result = new ArrayList<>(count);
    long uptime = buf.getUnsignedInt(4); // 4-7
    long seconds = buf.getUnsignedInt(8); // 8-11
    long millis = buf.getUnsignedInt(8) / 1000; // 12-15
    long timestamp = (seconds * 1000L) + millis; // java timestamp, which is milliseconds
    UUID packetId = UUIDs.startOfJavaTimestamp(timestamp);
    long flowSequence = buf.getUnsignedInt(16); // 16-19
    short engineType = buf.getUnsignedByte(20); // 20
    short engineId = buf.getUnsignedByte(21); // 21
    // the first 2 bits are the sampling mode, the remaining 14 the interval
    int sampling = buf.getUnsignedShort(22); // 22-23
    int samplingInterval = sampling & 0x3FFF;
    int samplingMode = sampling >> 14;
    Map<String, Field> headers = new HashMap<>();
    headers.put(VERSION, Field.create(version));
    headers.put(PACKETID, Field.create(packetId.toString()));
    headers.put(SENDER, Field.create((sender == null) ? "unknown" : sender.getAddress().toString()));
    headers.put(LENGTH, Field.create(packetLength));
    headers.put(UPTIME, Field.create(uptime));
    headers.put(TIMESTAMP, Field.create(timestamp));
    headers.put(FLOWSEQ, Field.create(flowSequence));
    headers.put(ENGINEID, Field.create(engineId));
    headers.put(ENGINETYPE, Field.create(engineType));
    headers.put(SAMPLINGINT, Field.create(samplingInterval));
    headers.put(SAMPLINGMODE, Field.create(samplingMode));
    headers.put(READERID, Field.create(readerId));
    for (int i = 0; i < count; i++) {
        ByteBuf flowBuf = buf.slice(V5_HEADER_SIZE + (i * V5_FLOW_SIZE), V5_FLOW_SIZE);
        Map<String, Field> fields = new HashMap<>();
        fields.putAll(headers);
        long pkts = flowBuf.getUnsignedInt(16);
        long bytes = flowBuf.getUnsignedInt(20);
        fields.put(ID, Field.create(UUIDs.timeBased().toString()));
        int srcaddr = (int) flowBuf.getUnsignedInt(0);
        int dstaddr = (int) flowBuf.getUnsignedInt(4);
        int nexthop = (int) flowBuf.getUnsignedInt(8);
        fields.put(SRCADDR, Field.create(srcaddr));
        fields.put(DSTADDR, Field.create(dstaddr));
        fields.put(NEXTHOP, Field.create(nexthop));
        fields.put(SRCADDR_S, Field.create(ipToString(srcaddr)));
        fields.put(DSTADDR_S, Field.create(ipToString(dstaddr)));
        fields.put(NEXTHOP_S, Field.create(ipToString(nexthop)));
        fields.put(SRCPORT, Field.create(flowBuf.getUnsignedShort(32)));
        fields.put(DSTPORT, Field.create(flowBuf.getUnsignedShort(34)));
        fields.put(SRCAS, Field.create(flowBuf.getUnsignedShort(40)));
        fields.put(DSTAS, Field.create(flowBuf.getUnsignedShort(42)));
        fields.put(PACKETS, Field.create(pkts));
        fields.put(DOCTECTS, Field.create(bytes));
        fields.put(PROTO, Field.create(flowBuf.getUnsignedByte(38)));
        fields.put(TOS, Field.create(flowBuf.getUnsignedByte(39)));
        fields.put(TCPFLAGS, Field.create(flowBuf.getUnsignedByte(37)));
        long first = flowBuf.getUnsignedInt(24);
        if (first > 0) {
            fields.put(FIRST, Field.create(timestamp - uptime - first));
        } else {
            fields.put(FIRST, Field.create(0L));
        }
        long last = flowBuf.getUnsignedInt(28);
        if (last > 0) {
            fields.put(LAST, Field.create(timestamp - uptime - last));
        } else {
            fields.put(LAST, Field.create(0L));
        }
        fields.put(SNMPINPUT, Field.create(flowBuf.getUnsignedShort(12)));
        fields.put(SNMPOUTPUT, Field.create(flowBuf.getUnsignedShort(14)));
        fields.put(SRCMASK, Field.create(flowBuf.getUnsignedByte(44)));
        fields.put(DSTMASK, Field.create(flowBuf.getUnsignedByte(45)));
        Record record = context.createRecord(readerId + "::" + recordId++);
        record.set(Field.create(fields));
        result.add(record);

    }
    return result;
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPComStmtCloseRequestMessage.java

License:Open Source License

@Override
protected Long unmarshall(ByteBuf source) {
    source.skipBytes(1);
    return source.getUnsignedInt(INDEX_OF_STATEMENTID);
}

From source file:org.graylog.plugins.netflow.utils.ByteBufUtils.java

License:Apache License

public static long getUnsignedInteger(final ByteBuf buf, final int offset, final int length) {
    switch (length) {
    case 1:/*from w  w  w.ja  va 2s.  c o  m*/
        return buf.getUnsignedByte(offset);
    case 2:
        return buf.getUnsignedShort(offset);
    case 3:
        return buf.getUnsignedMedium(offset);
    case 4:
        return buf.getUnsignedInt(offset);
    case 8:
        return buf.getLong(offset) & 0x00000000ffffffffL;
    default:
        return 0L;
    }
}

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 v a2s.c  o m
            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.openflowjava.protocol.impl.deserialization.action.OF13SetFieldActionDeserializer.java

License:Open Source License

@Override
public Action deserialize(ByteBuf input) {
    org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder builder = new ActionBuilder();
    int startIndex = input.readerIndex();
    input.skipBytes(2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
    SetFieldCaseBuilder caseBuilder = new SetFieldCaseBuilder();
    SetFieldActionBuilder actionBuilder = new SetFieldActionBuilder();
    int oxmClass = input.getUnsignedShort(input.readerIndex());
    // get oxm_field & hasMask byte and extract the field value
    int oxmField = input.getUnsignedByte(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
    MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(EncodeConstants.OF13_VERSION_ID, oxmClass,
            oxmField);// www. j  a v a 2s.  c  o m
    if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
        long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
                + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
        key.setExperimenterId(expId);
    }
    OFDeserializer<MatchEntry> matchDeserializer = registry.getDeserializer(key);
    List<MatchEntry> entry = new ArrayList<>();
    entry.add(matchDeserializer.deserialize(input));
    actionBuilder.setMatchEntry(entry);
    caseBuilder.setSetFieldAction(actionBuilder.build());
    builder.setActionChoice(caseBuilder.build());
    int paddingRemainder = (input.readerIndex() - startIndex) % EncodeConstants.PADDING;
    if (paddingRemainder != 0) {
        input.skipBytes(EncodeConstants.PADDING - paddingRemainder);
    }
    return builder.build();
}

From source file:org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory.java

License:Open Source License

/**
 * @param version/*from  w w w . jav  a 2s. c o  m*/
 * @return
 */
public static CodeKeyMaker createMatchEntriesKeyMaker(short version) {
    return new AbstractCodeKeyMaker(version) {
        @Override
        public MessageCodeKey make(ByteBuf input) {
            int oxmClass = input.getUnsignedShort(input.readerIndex());
            int oxmField = input
                    .getUnsignedByte(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES) >>> 1;
            MatchEntryDeserializerKey key = new MatchEntryDeserializerKey(getVersion(), oxmClass, oxmField);
            if (oxmClass == EncodeConstants.EXPERIMENTER_VALUE) {
                long expId = input.getUnsignedInt(input.readerIndex() + EncodeConstants.SIZE_OF_SHORT_IN_BYTES
                        + 2 * EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
                key.setExperimenterId(expId);
                return key;
            }
            key.setExperimenterId(null);
            return key;
        }
    };
}

From source file:org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory.java

License:Open Source License

/**
 * @param version//from  w ww.j av a 2 s  .c o  m
 * @return
 */
public static CodeKeyMaker createActionsKeyMaker(short version) {
    return new AbstractCodeKeyMaker(version) {
        @Override
        public MessageCodeKey make(ByteBuf input) {
            int type = input.getUnsignedShort(input.readerIndex());
            if (type == EncodeConstants.EXPERIMENTER_VALUE) {
                Long expId = input
                        .getUnsignedInt(input.readerIndex() + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
                return new ExperimenterActionDeserializerKey(getVersion(), expId);
            }
            ActionDeserializerKey actionDeserializerKey = new ActionDeserializerKey(getVersion(), type, null);
            return actionDeserializerKey;
        }
    };
}

From source file:org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory.java

License:Open Source License

/**
 * @param version//from   ww  w.j  a  va 2  s . c  o m
 * @return
 */
public static CodeKeyMaker createInstructionsKeyMaker(short version) {
    return new AbstractCodeKeyMaker(version) {
        @Override
        public MessageCodeKey make(ByteBuf input) {
            int type = input.getUnsignedShort(input.readerIndex());
            if (type == EncodeConstants.EXPERIMENTER_VALUE) {
                Long expId = input
                        .getUnsignedInt(input.readerIndex() + 2 * EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
                return new ExperimenterInstructionDeserializerKey(getVersion(), expId);
            }
            return new InstructionDeserializerKey(getVersion(), type, null);
        }
    };
}