Search in sources :

Example 1 with PeerSpecificParserConstraint

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

the class SimpleNlriRegistry method parseMpReach.

@Override
public MpReachNlri parseMpReach(final ByteBuf buffer, final PeerSpecificParserConstraint constraint) throws BGPParsingException {
    final MpReachNlriBuilder builder = new MpReachNlriBuilder();
    final Class<? extends AddressFamily> afi = getAfi(buffer);
    final Class<? extends SubsequentAddressFamily> safi = getSafi(buffer);
    builder.setAfi(afi);
    builder.setSafi(safi);
    final BgpTableType key = createKey(builder.getAfi(), builder.getSafi());
    final int nextHopLength = buffer.readUnsignedByte();
    if (nextHopLength != 0) {
        final NextHopParserSerializer nextHopParser = this.nextHopParsers.get(key);
        if (nextHopParser != null) {
            builder.setCNextHop(nextHopParser.parseNextHop(buffer.readSlice(nextHopLength)));
        } else {
            builder.setCNextHop(NextHopUtil.parseNextHop(buffer.readSlice(nextHopLength)));
            LOG.warn("NexHop Parser/Serializer for AFI/SAFI ({},{}) not bound", afi, safi);
        }
    }
    buffer.skipBytes(RESERVED);
    final ByteBuf nlri = buffer.slice();
    final NlriParser parser = this.handlers.get(key);
    if (parser == null) {
        LOG.warn(PARSER_NOT_FOUND, key);
    } else {
        parser.parseNlri(nlri, builder, constraint);
    }
    return builder.build();
}
Also used : BgpTableType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType) MpReachNlriBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder) NlriParser(org.opendaylight.protocol.bgp.parser.spi.NlriParser) ByteBuf(io.netty.buffer.ByteBuf) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint) NextHopParserSerializer(org.opendaylight.protocol.bgp.parser.spi.NextHopParserSerializer)

Example 2 with PeerSpecificParserConstraint

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

the class BGPUpdateMessageParser method parseMessageBody.

/**
 * Parse Update message from buffer.
 * Calls {@link #checkMandatoryAttributesPresence(Update)} 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 {
    Preconditions.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 int withdrawnRoutesLength = buffer.readUnsignedShort();
    if (withdrawnRoutesLength > 0) {
        final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
        final ByteBuf withdrawnRoutesBuffer = buffer.readBytes(withdrawnRoutesLength);
        while (withdrawnRoutesBuffer.isReadable()) {
            final WithdrawnRoutesBuilder withdrawnRoutesBuilder = new WithdrawnRoutesBuilder();
            if (isMultiPathSupported) {
                withdrawnRoutesBuilder.setPathId(PathIdUtil.readPathId(withdrawnRoutesBuffer));
            }
            withdrawnRoutesBuilder.setPrefix(Ipv4Util.prefixForByteBuf(withdrawnRoutesBuffer));
            withdrawnRoutes.add(withdrawnRoutesBuilder.build());
        }
        builder.setWithdrawnRoutes(withdrawnRoutes);
    }
    final int totalPathAttrLength = buffer.readUnsignedShort();
    if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
        return builder.build();
    }
    if (totalPathAttrLength > 0) {
        try {
            final Attributes attributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength), constraint);
            builder.setAttributes(attributes);
        } catch (final RuntimeException | BGPParsingException e) {
            // Catch everything else and turn it into a BGPDocumentedException
            throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
        }
    }
    final List<Nlri> nlri = new ArrayList<>();
    while (buffer.isReadable()) {
        final NlriBuilder nlriBuilder = new NlriBuilder();
        if (isMultiPathSupported) {
            nlriBuilder.setPathId(PathIdUtil.readPathId(buffer));
        }
        nlriBuilder.setPrefix(Ipv4Util.prefixForByteBuf(buffer));
        nlri.add(nlriBuilder.build());
    }
    if (!nlri.isEmpty()) {
        builder.setNlri(nlri);
    }
    final Update msg = builder.build();
    checkMandatoryAttributesPresence(msg);
    LOG.debug("BGP Update message was parsed {}.", msg);
    return msg;
}
Also used : Ipv4AddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily) ArrayList(java.util.ArrayList) Attributes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes) UpdateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.UpdateBuilder) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) WithdrawnRoutes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.WithdrawnRoutes) ByteBuf(io.netty.buffer.ByteBuf) Update(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint) Nlri(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.Nlri) WithdrawnRoutesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.WithdrawnRoutesBuilder) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) BgpTableTypeImpl(org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl) NlriBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.NlriBuilder) UnicastSubsequentAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 PeerSpecificParserConstraint (org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)2 ArrayList (java.util.ArrayList)1 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)1 BGPParsingException (org.opendaylight.protocol.bgp.parser.BGPParsingException)1 BgpTableTypeImpl (org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl)1 NextHopParserSerializer (org.opendaylight.protocol.bgp.parser.spi.NextHopParserSerializer)1 NlriParser (org.opendaylight.protocol.bgp.parser.spi.NlriParser)1 Update (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update)1 UpdateBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.UpdateBuilder)1 Attributes (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes)1 Nlri (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.Nlri)1 NlriBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.NlriBuilder)1 WithdrawnRoutes (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.WithdrawnRoutes)1 WithdrawnRoutesBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.update.message.WithdrawnRoutesBuilder)1 BgpTableType (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType)1 MpReachNlriBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder)1 Ipv4AddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily)1 UnicastSubsequentAddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily)1