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;
}
Aggregations