Search in sources :

Example 76 with Peer

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.peers.Peer in project bgpcep by opendaylight.

the class AbstractBmpPerPeerMessageParserTest method testPerPeerHeader.

@Test
public void testPerPeerHeader() {
    final byte[] msgBytes = { (byte) 0x00, (byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, // IPV4 192.168.1.1
    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xC0, (byte) 0xA8, (byte) 0x01, (byte) 0x01, // AS 168
    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xA8, // Peer BGP ID 1.1.1.1
    (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, // Timestamp
    (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
    final PeerHeader perHeader = AbstractBmpPerPeerMessageParser.parsePerPeerHeader(Unpooled.wrappedBuffer(msgBytes));
    assertEquals(perHeader, new PeerHeaderBuilder().setType(PeerType.forValue(0)).setAdjRibInType(AdjRibInType.forValue(1)).setIpv4(true).setAddress(new IpAddressNoZone(new Ipv4AddressNoZone("192.168.1.1"))).setAs(new AsNumber(Uint32.valueOf(168))).setBgpId(new Ipv4AddressNoZone("1.1.1.1")).setTimestampSec(new Timestamp(Uint32.valueOf(16909060))).setTimestampMicro(new Timestamp(Uint32.valueOf(16909060))).build());
    final ByteBuf aggregator = Unpooled.buffer();
    this.parser.serializePerPeerHeader(perHeader, aggregator);
    assertArrayEquals(msgBytes, ByteArray.getAllBytes(aggregator));
}
Also used : PeerHeader(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.peer.header.PeerHeader) Ipv4AddressNoZone(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone) IpAddressNoZone(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone) PeerHeaderBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.peer.header.PeerHeaderBuilder) ByteBuf(io.netty.buffer.ByteBuf) AsNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber) Timestamp(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timestamp) Test(org.junit.Test)

Example 77 with Peer

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.peers.Peer in project bgpcep by opendaylight.

the class AbstractBmpPerPeerMessageParser method serializePerPeerHeader.

protected void serializePerPeerHeader(final PeerHeader peerHeader, final ByteBuf output) {
    checkArgument(peerHeader != null, "Per-peer header cannot be null.");
    final PeerType peerType = peerHeader.getType();
    output.writeByte(peerType.getIntValue());
    final BitArray flags = new BitArray(FLAGS_SIZE);
    flags.set(L_FLAG_POS, peerHeader.getAdjRibInType().getIntValue() != 0);
    flags.set(V_FLAG_POS, !peerHeader.getIpv4());
    flags.toByteBuf(output);
    final PeerDistinguisher peerDistinguisher = peerHeader.getPeerDistinguisher();
    switch(peerType) {
        case L3vpn:
            RouteDistinguisherUtil.serializeRouteDistinquisher(peerDistinguisher.getRouteDistinguisher(), output);
            break;
        case Local:
            output.writeBytes(peerDistinguisher.getBinary());
            break;
        case Global:
        default:
            output.writeZero(PEER_DISTINGUISHER_SIZE);
            break;
    }
    if (peerHeader.getIpv4()) {
        output.writeZero(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
        Ipv4Util.writeIpv4Address(peerHeader.getAddress().getIpv4AddressNoZone(), output);
    } else {
        Ipv6Util.writeIpv6Address(peerHeader.getAddress().getIpv6AddressNoZone(), output);
    }
    ByteBufUtils.write(output, peerHeader.getAs().getValue());
    Ipv4Util.writeIpv4Address(peerHeader.getBgpId(), output);
    final Timestamp stampSec = peerHeader.getTimestampSec();
    if (stampSec != null) {
        ByteBufUtils.write(output, stampSec.getValue());
    } else {
        output.writeInt(0);
    }
    final Timestamp stampMicro = peerHeader.getTimestampMicro();
    if (stampMicro != null) {
        ByteBufUtils.write(output, stampMicro.getValue());
    } else {
        output.writeInt(0);
    }
}
Also used : PeerType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.PeerType) BitArray(org.opendaylight.protocol.util.BitArray) PeerDistinguisher(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.Peer.PeerDistinguisher) Timestamp(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Timestamp)

Example 78 with Peer

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.peers.Peer in project bgpcep by opendaylight.

the class BGPOpenMessageParser method parseParameters.

private List<BgpParameters> parseParameters(final ByteBuf buffer, final int length) throws BGPDocumentedException {
    if (length == 0) {
        return ImmutableList.of();
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Started parsing of BGP parameter: {} length {}", ByteBufUtil.hexDump(buffer), length);
    }
    final int realLength;
    final OptionalInt extendedLength = extractExtendedLength(buffer, length);
    if (extendedLength.isPresent()) {
        realLength = extendedLength.getAsInt();
        if (realLength < Values.UNSIGNED_BYTE_MAX_VALUE) {
            LOG.debug("Peer used Extended Optional Parameters Length to encode length {}", realLength);
        }
    } else {
        realLength = length;
    }
    // We have determined the real length, we can trim the buffer now
    if (buffer.readableBytes() > realLength) {
        buffer.writerIndex(buffer.readerIndex() + realLength);
        LOG.trace("Truncated BGP parameter buffer to length {}: {}", realLength, ByteBufUtil.hexDump(buffer));
    }
    final int lengthSize = extendedLength.isPresent() ? 1 : 2;
    final List<BgpParameters> params = new ArrayList<>();
    while (buffer.isReadable()) {
        final int paramType = buffer.readUnsignedByte();
        final Optional<ParameterParser> parser = reg.findParser(paramType);
        if (!parser.isPresent()) {
            throw new BGPDocumentedException("Parameter " + paramType + " not supported", BGPError.OPT_PARAM_NOT_SUPPORTED);
        }
        if (buffer.readableBytes() <= lengthSize) {
            throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.UNSPECIFIC_OPEN_ERROR);
        }
        final int paramLength = extendedLength.isPresent() ? buffer.readUnsignedShort() : buffer.readUnsignedByte();
        final ByteBuf paramBody = buffer.readSlice(paramLength);
        final BgpParameters param;
        try {
            param = parser.get().parseParameter(paramBody);
        } catch (final BGPParsingException e) {
            throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
        }
        params.add(verifyNotNull(param));
    }
    LOG.trace("Parsed BGP parameters: {}", params);
    return params;
}
Also used : ParameterParser(org.opendaylight.protocol.bgp.parser.spi.ParameterParser) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) ArrayList(java.util.ArrayList) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) OptionalInt(java.util.OptionalInt) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters) ByteBuf(io.netty.buffer.ByteBuf) PeerSpecificParserConstraint(org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)

Example 79 with Peer

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.peers.Peer 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 80 with Peer

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.peers.Peer 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

Test (org.junit.Test)29 ByteBuf (io.netty.buffer.ByteBuf)13 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)11 IpAddressNoZone (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone)9 BGPSessionPreferences (org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences)8 Peer (org.opendaylight.protocol.bgp.rib.spi.Peer)8 MacAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress)7 ArrayList (java.util.ArrayList)6 AsNumber (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber)6 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)6 BGPRouteEntryExportParametersImpl (org.opendaylight.protocol.bgp.mode.impl.BGPRouteEntryExportParametersImpl)5 RIBSupport (org.opendaylight.protocol.bgp.rib.spi.RIBSupport)5 BGPRouteEntryExportParameters (org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters)5 BgpParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters)5 PeerId (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId)5 CommitInfo (org.opendaylight.mdsal.common.api.CommitInfo)4 PeerSpecificParserConstraint (org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint)4 Ipv4AddressNoZone (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone)4 Ipv4Prefix (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix)4 PortStatusMessage (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage)4