Search in sources :

Example 6 with BGPTreatAsWithdrawException

use of org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException 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 7 with BGPTreatAsWithdrawException

use of org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException 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)

Example 8 with BGPTreatAsWithdrawException

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

the class ClusterIdAttributeParser method parseAttribute.

@Override
public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder, final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPTreatAsWithdrawException {
    if (errorHandling == RevisedErrorHandling.EXTERNAL) {
        // RFC7606 section 7.10
        LOG.debug("Discarded CLUSTER_LIST attribute from external peer");
        return;
    }
    final int readable = buffer.readableBytes();
    if (readable == 0 && errorHandling != RevisedErrorHandling.NONE) {
        throw new BGPTreatAsWithdrawException(BGPError.ATTR_LENGTH_ERROR, "Empty CLUSTER_LIST attribute");
    }
    if (readable % Ipv4Util.IP4_LENGTH != 0) {
        throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR, "Length of CLUSTER_LIST should be a multiple of 4, but is %s", readable);
    }
    final int count = readable / Ipv4Util.IP4_LENGTH;
    final List<ClusterIdentifier> list = new ArrayList<>(count);
    for (int i = 0; i < count; ++i) {
        list.add(new ClusterIdentifier(Ipv4Util.addressForByteBuf(buffer)));
    }
    builder.setClusterId(new ClusterIdBuilder().setCluster(list).build());
}
Also used : BGPTreatAsWithdrawException(org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException) ArrayList(java.util.ArrayList) ClusterIdentifier(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.ClusterIdentifier) ClusterIdBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.ClusterIdBuilder) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)

Aggregations

BGPTreatAsWithdrawException (org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException)8 ArrayList (java.util.ArrayList)7 PeerSpecificParserConstraint (org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)7 ParsedAttributes (org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes)3 RevisedErrorHandling (org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling)3 ByteBuf (io.netty.buffer.ByteBuf)2 BGPParsingException (org.opendaylight.protocol.bgp.parser.BGPParsingException)2 BgpTableTypeImpl (org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl)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