Search in sources :

Example 1 with RevisedErrorHandling

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

the class SimpleAttributeRegistry method parseAttributes.

@Override
public ParsedAttributes parseAttributes(final ByteBuf buffer, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPParsingException {
    final RevisedErrorHandling errorHandling = RevisedErrorHandling.from(constraint);
    final Map<Integer, RawAttribute> attributes = new TreeMap<>();
    BGPTreatAsWithdrawException withdrawCause = null;
    while (buffer.isReadable()) {
        try {
            addAttribute(buffer, errorHandling, attributes);
        } catch (BGPTreatAsWithdrawException e) {
            LOG.info("Failed to completely parse attributes list.");
            withdrawCause = e;
            break;
        }
    }
    /*
         * TreeMap guarantees that we will be invoking the parser in the order
         * of increasing attribute type.
         */
    // We may have multiple attribute errors, each specifying a withdraw. We need to finish parsing the message
    // all attributes before we can decide whether we can discard attributes, or whether we need to terminate
    // the session.
    final AttributesBuilder builder = new AttributesBuilder();
    for (final Entry<Integer, RawAttribute> entry : attributes.entrySet()) {
        LOG.debug("Parsing attribute type {}", entry.getKey());
        final RawAttribute a = entry.getValue();
        try {
            a.parser.parseAttribute(a.buffer, builder, errorHandling, constraint);
        } catch (BGPTreatAsWithdrawException e) {
            LOG.info("Attribute {} indicated treat-as-withdraw", entry.getKey(), e);
            if (withdrawCause == null) {
                withdrawCause = e;
            } else {
                withdrawCause.addSuppressed(e);
            }
        }
    }
    builder.setUnrecognizedAttributes(BindingMap.ordered(this.unrecognizedAttributes));
    return new ParsedAttributes(builder.build(), withdrawCause);
}
Also used : RevisedErrorHandling(org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling) BGPTreatAsWithdrawException(org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException) ParsedAttributes(org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes) TreeMap(java.util.TreeMap) UnrecognizedAttributesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.UnrecognizedAttributesBuilder) AttributesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder)

Example 2 with RevisedErrorHandling

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

the class BGPUpdateMessageParser method parseMessageBody.

/**
 * Parse Update message from buffer. Calls {@link #checkMandatoryAttributesPresence(Update, RevisedErrorHandling)}
 * to check for presence of mandatory attributes.
 *
 * @param buffer Encoded BGP message in ByteBuf
 * @param messageLength Length of the BGP message
 * @param constraint Peer specific constraints
 * @return Parsed Update message body
 */
@Override
public Update parseMessageBody(final ByteBuf buffer, final int messageLength, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
    checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");
    final UpdateBuilder builder = new UpdateBuilder();
    final boolean isMultiPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
    final RevisedErrorHandling errorHandling = RevisedErrorHandling.from(constraint);
    final int withdrawnRoutesLength = buffer.readUnsignedShort();
    if (withdrawnRoutesLength > 0) {
        final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
        final ByteBuf withdrawnRoutesBuffer = buffer.readSlice(withdrawnRoutesLength);
        while (withdrawnRoutesBuffer.isReadable()) {
            final WithdrawnRoutesBuilder withdrawnRoutesBuilder = new WithdrawnRoutesBuilder();
            if (isMultiPathSupported) {
                withdrawnRoutesBuilder.setPathId(PathIdUtil.readPathId(withdrawnRoutesBuffer));
            }
            withdrawnRoutesBuilder.setPrefix(readPrefix(withdrawnRoutesBuffer, errorHandling, "Withdrawn Routes"));
            withdrawnRoutes.add(withdrawnRoutesBuilder.build());
        }
        builder.setWithdrawnRoutes(withdrawnRoutes);
    }
    final int totalPathAttrLength = buffer.readUnsignedShort();
    if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
        return builder.build();
    }
    Optional<BGPTreatAsWithdrawException> withdrawCauseOpt;
    if (totalPathAttrLength > 0) {
        final ParsedAttributes attributes = parseAttributes(buffer, totalPathAttrLength, constraint);
        builder.setAttributes(attributes.getAttributes());
        withdrawCauseOpt = attributes.getWithdrawCause();
    } else {
        withdrawCauseOpt = Optional.empty();
    }
    final List<Nlri> nlri = new ArrayList<>();
    while (buffer.isReadable()) {
        final NlriBuilder nlriBuilder = new NlriBuilder();
        if (isMultiPathSupported) {
            nlriBuilder.setPathId(PathIdUtil.readPathId(buffer));
        }
        nlriBuilder.setPrefix(readPrefix(buffer, errorHandling, "NLRI"));
        nlri.add(nlriBuilder.build());
    }
    if (!nlri.isEmpty()) {
        builder.setNlri(nlri);
    }
    try {
        checkMandatoryAttributesPresence(builder.build(), errorHandling);
    } catch (BGPTreatAsWithdrawException e) {
        LOG.debug("Well-known mandatory attributes missing", e);
        if (withdrawCauseOpt.isPresent()) {
            final BGPTreatAsWithdrawException exception = withdrawCauseOpt.get();
            exception.addSuppressed(e);
            withdrawCauseOpt = Optional.of(exception);
        } else {
            withdrawCauseOpt = Optional.of(e);
        }
    }
    Update msg = builder.build();
    if (withdrawCauseOpt.isPresent()) {
        // Attempt to apply treat-as-withdraw
        msg = withdrawUpdate(msg, errorHandling, withdrawCauseOpt.get());
    }
    LOG.debug("BGP Update message was parsed {}.", msg);
    return msg;
}
Also used : RevisedErrorHandling(org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling) BGPTreatAsWithdrawException(org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException) Ipv4AddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily) ArrayList(java.util.ArrayList) UpdateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder) WithdrawnRoutes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutes) ParsedAttributes(org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes) ByteBuf(io.netty.buffer.ByteBuf) Update(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint) MpReachNlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri) Nlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.Nlri) MpUnreachNlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri) WithdrawnRoutesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutesBuilder) BgpTableTypeImpl(org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl) NlriBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.NlriBuilder) UnicastSubsequentAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily)

Example 3 with RevisedErrorHandling

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

the class BGPUpdateMessageParser method withdrawUpdate.

private Update withdrawUpdate(final Update parsed, final RevisedErrorHandling errorHandling, final BGPTreatAsWithdrawException withdrawCause) throws BGPDocumentedException {
    if (errorHandling == RevisedErrorHandling.NONE) {
        throw new BGPDocumentedException(withdrawCause);
    }
    // TODO: additional checks as per RFC7606 section 5.2
    LOG.debug("Converting BGP Update message {} to withdraw", parsed, withdrawCause);
    final UpdateBuilder builder = new UpdateBuilder();
    final List<Nlri> nlris = parsed.getNlri();
    final List<WithdrawnRoutes> withdrawn;
    if (nlris != null && !nlris.isEmpty()) {
        withdrawn = Streams.concat(parsed.nonnullWithdrawnRoutes().stream(), nlris.stream().map(nlri -> new WithdrawnRoutesBuilder(nlri).build())).collect(ImmutableList.toImmutableList());
    } else {
        withdrawn = parsed.getWithdrawnRoutes();
    }
    builder.setWithdrawnRoutes(withdrawn);
    final Attributes attributes = parsed.getAttributes();
    if (attributes != null) {
        builder.setAttributes(withdrawAttributes(attributes, withdrawCause));
    }
    return builder.build();
}
Also used : MpReachNlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri) Nlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.Nlri) MpUnreachNlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri) LoggerFactory(org.slf4j.LoggerFactory) Attributes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes) Unpooled(io.netty.buffer.Unpooled) Update(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update) UpdateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder) Ipv4Prefix(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix) MpReachNlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri) Ipv4AddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Notification(org.opendaylight.yangtools.yang.binding.Notification) BGPTreatAsWithdrawException(org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException) Nlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.Nlri) BgpTableTypeImpl(org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl) ParsedAttributes(org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes) PathIdUtil(org.opendaylight.protocol.bgp.parser.spi.PathIdUtil) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) Streams(com.google.common.collect.Streams) MultiPathSupportUtil(org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil) AttributesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder) TablesKey(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.graceful.restart.capability.TablesKey) AttributeRegistry(org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry) OriginAttributeParser(org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser) NlriBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.NlriBuilder) MpUnreachNlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri) List(java.util.List) WithdrawnRoutesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutesBuilder) MessageParser(org.opendaylight.protocol.bgp.parser.spi.MessageParser) RevisedErrorHandling(org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling) Optional(java.util.Optional) WithdrawnRoutes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutes) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint) NextHopAttributeParser(org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttributeParser) AsPathAttributeParser(org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser) PathId(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId) BGPError(org.opendaylight.protocol.bgp.parser.BGPError) ArrayList(java.util.ArrayList) AttributesReach(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReach) ImmutableList(com.google.common.collect.ImmutableList) ByteBuf(io.netty.buffer.ByteBuf) MessageUtil(org.opendaylight.protocol.bgp.parser.spi.MessageUtil) Objects.requireNonNull(java.util.Objects.requireNonNull) MessageSerializer(org.opendaylight.protocol.bgp.parser.spi.MessageSerializer) AttributesUnreach(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreach) Logger(org.slf4j.Logger) NlriRegistry(org.opendaylight.protocol.bgp.parser.spi.NlriRegistry) Ipv4Util(org.opendaylight.protocol.util.Ipv4Util) AttributesUnreachBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreachBuilder) UnicastSubsequentAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily) WithdrawnRoutesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutesBuilder) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) Attributes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes) ParsedAttributes(org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes) UpdateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder) WithdrawnRoutes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutes)

Aggregations

BGPTreatAsWithdrawException (org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException)3 ParsedAttributes (org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes)3 RevisedErrorHandling (org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling)3 ByteBuf (io.netty.buffer.ByteBuf)2 ArrayList (java.util.ArrayList)2 BgpTableTypeImpl (org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl)2 PeerSpecificParserConstraint (org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)2 Update (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update)2 UpdateBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder)2 Nlri (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.Nlri)2 NlriBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.NlriBuilder)2 WithdrawnRoutes (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutes)2 WithdrawnRoutesBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutesBuilder)2 MpReachNlri (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri)2 MpUnreachNlri (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri)2 Ipv4AddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily)2 UnicastSubsequentAddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ImmutableList (com.google.common.collect.ImmutableList)1 Streams (com.google.common.collect.Streams)1