Search in sources :

Example 16 with BgpTableTypeImpl

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

Example 17 with BgpTableTypeImpl

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

the class ParserTest method testOpenMessage.

/*
     * ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
     * 00 3d <- length (61) - including header
     * 01 <- message type
     * 04 <- BGP version
     * 00 64 <- My AS Number (AS TRANS in this case)
     * 00 b4 <- Hold Time
     * 00 00 00 00 <- BGP Identifier
     * 20 <- Optional Parameters Length
     * 02 <- opt. param. type (capabilities)
     * 06 <- length
     * 01 <- capability code (MP Extensions for BGP4)
     * 04 <- length
     * 00 01 00 01 <- AFI 1, SAFI 1
     * 02 <- opt. param. type (capabilities)
     * 06 <- length
     * 01 <- capability code (MP Extensions for BGP4)
     * 04 <- length
     * 00 02 00 01 <- AFI 2, SAFI 1
     * 02 <- opt. param. type (capabilities)
     * 06 <- length
     * 01 <- capability code (MP Extensions for BGP4)
     * 04 <- length
     * 40 04 00 47 <- AFI 16388, SAFI 71
     * 02 <- opt. param. type (capabilities)
     * 06 <- length
     * 41 <- capability code (AS4 octet support)
     * 04 <- length
     * 00 00 00 64 <- AS number
     */
@Test
public void testOpenMessage() throws Exception {
    final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
    final Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(inputBytes.get(3)), null);
    final Open open = (Open) o;
    final Set<BgpTableType> types = Sets.newHashSet();
    for (final BgpParameters param : open.getBgpParameters()) {
        for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
            final CParameters cParam = optCapa.getCParameters();
            if (cParam != null && cParam.getAugmentation(CParameters1.class) != null && cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
                final MultiprotocolCapability mp = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
                final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
                types.add(type);
            }
        }
    }
    final Set<BgpTableType> expected = Sets.newHashSet();
    expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
    expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
    expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
    assertEquals(expected, types);
    final ByteBuf buffer = Unpooled.buffer();
    msgReg.serializeMessage(o, buffer);
    assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
}
Also used : BgpTableType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType) Ipv6AddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily) LinkstateSubsequentAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateSubsequentAddressFamily) Ipv4AddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily) CParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.optional.capabilities.CParameters) LinkstateAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateAddressFamily) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters) CParameters1(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.CParameters1) ByteBuf(io.netty.buffer.ByteBuf) Notification(org.opendaylight.yangtools.yang.binding.Notification) Open(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open) OptionalCapabilities(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilities) MultiprotocolCapability(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.MultiprotocolCapability) MessageRegistry(org.opendaylight.protocol.bgp.parser.spi.MessageRegistry) BgpTableTypeImpl(org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl) UnicastSubsequentAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily) Test(org.junit.Test)

Example 18 with BgpTableTypeImpl

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

the class LUNlriParser method parseNlri.

@Override
public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder, final PeerSpecificParserConstraint constraint) throws BGPParsingException {
    if (!nlri.isReadable()) {
        return;
    }
    final Class<? extends AddressFamily> afi = builder.getAfi();
    final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
    final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
    DestinationType destination = null;
    if (afi == Ipv4AddressFamily.class) {
        destination = new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
    } else if (afi == Ipv6AddressFamily.class) {
        destination = new DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(new DestinationIpv6LabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
    }
    builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
}
Also used : Ipv6AddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily) AdvertizedRoutesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder) CLabeledUnicastDestination(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.destination.CLabeledUnicastDestination) DestinationLabeledUnicastCaseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder) DestinationLabeledUnicastBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder) DestinationIpv6LabeledUnicastBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.ipv6.labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder) DestinationIpv6LabeledUnicastCaseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCaseBuilder) BgpTableTypeImpl(org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl) DestinationType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType)

Aggregations

BgpTableTypeImpl (org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl)18 BgpTableType (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType)11 Test (org.junit.Test)7 Ipv4AddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily)6 ArrayList (java.util.ArrayList)5 Ipv6AddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily)5 ByteBuf (io.netty.buffer.ByteBuf)4 UnicastSubsequentAddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily)4 Open (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open)3 BgpParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters)3 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)2 CLabeledUnicastDestination (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.destination.CLabeledUnicastDestination)2 DestinationIpv6LabeledUnicastCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCaseBuilder)2 DestinationIpv6LabeledUnicastBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.ipv6.labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder)2 LinkstateAddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateAddressFamily)2 LinkstateSubsequentAddressFamily (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev171207.LinkstateSubsequentAddressFamily)2 OptionalCapabilities (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilities)2 CParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.optional.capabilities.CParameters)2 CParameters1 (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.CParameters1)2 RibId (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.RibId)2