Search in sources :

Example 61 with Bgp

use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.

the class BGPOpenMessageParser method serializeMessage.

/**
 * Serializes given BGP Open message to byte array, without the header.
 *
 * @param msg BGP Open message to be serialized.
 * @param bytes ByteBuf where the message will be serialized
 */
@Override
public void serializeMessage(final Notification msg, final ByteBuf bytes) {
    Preconditions.checkArgument(msg instanceof Open, "Message needs to be of type Open");
    final Open open = (Open) msg;
    final ByteBuf msgBody = Unpooled.buffer();
    msgBody.writeByte(BGP_VERSION);
    // When our AS number does not fit into two bytes, we report it as AS_TRANS
    int openAS = open.getMyAsNumber();
    if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) {
        openAS = AS_TRANS;
    }
    msgBody.writeShort(openAS);
    msgBody.writeShort(open.getHoldTimer());
    msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
    final ByteBuf paramsBuffer = Unpooled.buffer();
    if (open.getBgpParameters() != null) {
        for (final BgpParameters param : open.getBgpParameters()) {
            this.reg.serializeParameter(param, paramsBuffer);
        }
    }
    msgBody.writeByte(paramsBuffer.writerIndex());
    msgBody.writeBytes(paramsBuffer);
    MessageUtil.formatMessage(TYPE, msgBody, bytes);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters) Open(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open)

Example 62 with Bgp

use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.

the class BGPRouteRefreshMessageParser method parseMessageBody.

/**
 * Parses BGP Route Refresh message to bytes.
 *
 * @param body ByteBuf to be parsed
 * @param messageLength the length of the message
 * @return {@link RouteRefresh} which represents BGP notification message
 * @throws BGPDocumentedException if parsing goes wrong
 */
@Override
public RouteRefresh parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
    Preconditions.checkArgument(body != null, "Body buffer cannot be null.");
    if (body.readableBytes() < TRIPLET_BYTE_SIZE) {
        throw BGPDocumentedException.badMessageLength("RouteRefresh message is too small.", messageLength);
    }
    final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, this.afiReg, this.safiReg);
    if (!parsedAfiSafi.isPresent()) {
        throw new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
    }
    return new RouteRefreshBuilder(parsedAfiSafi.get()).build();
}
Also used : BgpTableType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType) RouteRefreshBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.RouteRefreshBuilder) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException)

Example 63 with Bgp

use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp 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 64 with Bgp

use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.

the class CapabilityParameterParser method parseParameter.

@Override
public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
    Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
    if (LOG.isTraceEnabled()) {
        LOG.trace("Started parsing of BGP Capabilities: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
    }
    final List<OptionalCapabilities> optionalCapas = Lists.newArrayList();
    while (buffer.isReadable()) {
        final OptionalCapabilities optionalCapa = parseOptionalCapability(buffer);
        if (optionalCapa != null) {
            optionalCapas.add(optionalCapa);
        }
    }
    return new BgpParametersBuilder().setOptionalCapabilities(optionalCapas).build();
}
Also used : BgpParametersBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParametersBuilder) OptionalCapabilities(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilities)

Example 65 with Bgp

use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.

the class CapabilityParameterParser method serializeOptionalCapability.

private void serializeOptionalCapability(final OptionalCapabilities optionalCapa, final ByteBuf byteAggregator) {
    if (optionalCapa.getCParameters() != null) {
        final CParameters cap = optionalCapa.getCParameters();
        final ByteBuf bytes = Unpooled.buffer();
        this.reg.serializeCapability(cap, bytes);
        Preconditions.checkArgument(bytes != null, "Unhandled capability class %s", cap.getImplementedInterface());
        if (LOG.isTraceEnabled()) {
            LOG.trace("BGP capability serialized to: {}", ByteBufUtil.hexDump(bytes));
        }
        byteAggregator.writeBytes(bytes);
    }
}
Also used : CParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.optional.capabilities.CParameters) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

BigInteger (java.math.BigInteger)18 Test (org.junit.Test)17 ByteBuf (io.netty.buffer.ByteBuf)16 ArrayList (java.util.ArrayList)16 Uuid (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid)16 IpAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress)11 VrfEntry (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry)10 ExecutionException (java.util.concurrent.ExecutionException)9 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)9 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)9 Bgp (org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp)7 BgpParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters)7 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)7 List (java.util.List)6 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)6 AttributesBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 InstructionGotoTable (org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable)5 Neighbors (org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors)5 Collections (java.util.Collections)4