Search in sources :

Example 1 with ParameterParser

use of org.opendaylight.protocol.bgp.parser.spi.ParameterParser in project bgpcep by opendaylight.

the class BGPOpenMessageParser method parseParameters.

private List<BgpParameters> parseParameters(final ByteBuf buffer, final int length) throws BGPDocumentedException {
    if (length == 0) {
        return ImmutableList.of();
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Started parsing of BGP parameter: {} length {}", ByteBufUtil.hexDump(buffer), length);
    }
    final int realLength;
    final OptionalInt extendedLength = extractExtendedLength(buffer, length);
    if (extendedLength.isPresent()) {
        realLength = extendedLength.getAsInt();
        if (realLength < Values.UNSIGNED_BYTE_MAX_VALUE) {
            LOG.debug("Peer used Extended Optional Parameters Length to encode length {}", realLength);
        }
    } else {
        realLength = length;
    }
    // We have determined the real length, we can trim the buffer now
    if (buffer.readableBytes() > realLength) {
        buffer.writerIndex(buffer.readerIndex() + realLength);
        LOG.trace("Truncated BGP parameter buffer to length {}: {}", realLength, ByteBufUtil.hexDump(buffer));
    }
    final int lengthSize = extendedLength.isPresent() ? 1 : 2;
    final List<BgpParameters> params = new ArrayList<>();
    while (buffer.isReadable()) {
        final int paramType = buffer.readUnsignedByte();
        final Optional<ParameterParser> parser = reg.findParser(paramType);
        if (!parser.isPresent()) {
            throw new BGPDocumentedException("Parameter " + paramType + " not supported", BGPError.OPT_PARAM_NOT_SUPPORTED);
        }
        if (buffer.readableBytes() <= lengthSize) {
            throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.UNSPECIFIC_OPEN_ERROR);
        }
        final int paramLength = extendedLength.isPresent() ? buffer.readUnsignedShort() : buffer.readUnsignedByte();
        final ByteBuf paramBody = buffer.readSlice(paramLength);
        final BgpParameters param;
        try {
            param = parser.get().parseParameter(paramBody);
        } catch (final BGPParsingException e) {
            throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
        }
        params.add(verifyNotNull(param));
    }
    LOG.trace("Parsed BGP parameters: {}", params);
    return params;
}
Also used : ParameterParser(org.opendaylight.protocol.bgp.parser.spi.ParameterParser) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) ArrayList(java.util.ArrayList) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) OptionalInt(java.util.OptionalInt) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters) ByteBuf(io.netty.buffer.ByteBuf) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 ArrayList (java.util.ArrayList)1 OptionalInt (java.util.OptionalInt)1 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)1 BGPParsingException (org.opendaylight.protocol.bgp.parser.BGPParsingException)1 ParameterParser (org.opendaylight.protocol.bgp.parser.spi.ParameterParser)1 PeerSpecificParserConstraint (org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)1 BgpParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters)1