Search in sources :

Example 16 with BGPDocumentedException

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

the class AbstractBGPSessionNegotiator method startNegotiation.

private synchronized void startNegotiation() {
    if (!(this.state == State.IDLE || this.state == State.OPEN_CONFIRM)) {
        return;
    }
    // Open can be sent first either from ODL (IDLE) or from peer (OPEN_CONFIRM)
    final IpAddress remoteIp = getRemoteIp();
    try {
        // Check if peer is configured in registry before retrieving preferences
        if (!this.registry.isPeerConfigured(remoteIp)) {
            final BGPDocumentedException cause = new BGPDocumentedException(String.format("BGP peer with ip: %s not configured, check configured peers in : %s", remoteIp, this.registry), BGPError.CONNECTION_REJECTED);
            negotiationFailed(cause);
            return;
        }
        final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
        int as = preferences.getMyAs().getValue().intValue();
        // Set as AS_TRANS if the value is bigger than 2B
        if (as > Values.UNSIGNED_SHORT_MAX_VALUE) {
            as = AS_TRANS;
        }
        sendMessage(new OpenBuilder().setMyAsNumber(as).setHoldTimer(preferences.getHoldTime()).setBgpIdentifier(preferences.getBgpId()).setBgpParameters(preferences.getParams()).build());
        if (this.state != State.FINISHED) {
            this.state = State.OPEN_SENT;
            this.pending = this.channel.eventLoop().schedule(() -> {
                synchronized (AbstractBGPSessionNegotiator.this) {
                    AbstractBGPSessionNegotiator.this.pending = null;
                    if (AbstractBGPSessionNegotiator.this.state != State.FINISHED) {
                        AbstractBGPSessionNegotiator.this.sendMessage(buildErrorNotify(BGPError.HOLD_TIMER_EXPIRED));
                        negotiationFailed(new BGPDocumentedException("HoldTimer expired", BGPError.FSM_ERROR));
                        AbstractBGPSessionNegotiator.this.state = State.FINISHED;
                    }
                }
            }, INITIAL_HOLDTIMER, TimeUnit.MINUTES);
        }
    } catch (final Exception e) {
        LOG.warn("Unexpected negotiation failure", e);
        negotiationFailedCloseChannel(e);
    }
}
Also used : OpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder) BGPSessionPreferences(org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) IpAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException)

Example 17 with BGPDocumentedException

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

the class SimpleAttributeRegistry method processUnrecognized.

private void processUnrecognized(final BitArray flags, final int type, final ByteBuf buffer, final int len) throws BGPDocumentedException {
    if (!flags.get(OPTIONAL_BIT)) {
        throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
    }
    final UnrecognizedAttributes unrecognizedAttribute = new UnrecognizedAttributesBuilder().setKey(new UnrecognizedAttributesKey((short) type)).setPartial(flags.get(PARTIAL_BIT)).setTransitive(flags.get(TRANSITIVE_BIT)).setType((short) type).setValue(ByteArray.readBytes(buffer, len)).build();
    this.unrecognizedAttributes.add(unrecognizedAttribute);
    LOG.debug("Unrecognized attribute were parsed: {}", unrecognizedAttribute);
}
Also used : BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) UnrecognizedAttributesKey(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.UnrecognizedAttributesKey) UnrecognizedAttributesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.UnrecognizedAttributesBuilder) UnrecognizedAttributes(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.UnrecognizedAttributes)

Example 18 with BGPDocumentedException

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

the class PeerDownHandler method parseBgpNotificationMessage.

private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.Notification parseBgpNotificationMessage(final ByteBuf bytes) throws BmpDeserializationException {
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.NotificationBuilder notificationCBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.NotificationBuilder();
    final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.notification.NotificationBuilder notificationBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.down.data.notification.NotificationBuilder();
    try {
        final Notification not = this.msgRegistry.parseMessage(bytes, null);
        requireNonNull(not, "Notify message may not be null.");
        Preconditions.checkArgument(not instanceof NotifyMessage, "An instance of NotifyMessage is required");
        notificationBuilder.fieldsFrom((NotifyMessage) not);
        notificationCBuilder.setNotification(notificationBuilder.build());
    } catch (final BGPDocumentedException | BGPParsingException e) {
        throw new BmpDeserializationException("Error while parsing BGP Notification message.", e);
    }
    return notificationCBuilder.build();
}
Also used : Preconditions(com.google.common.base.Preconditions) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) PeerDownNotification(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerDownNotification) Notification(org.opendaylight.yangtools.yang.binding.Notification) PeerDownNotificationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerDownNotificationBuilder) NotifyMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyMessage) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) BmpDeserializationException(org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException)

Example 19 with BGPDocumentedException

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

the class BGPOpenMessageParser method parseMessageBody.

/**
 * Parses given byte array to BGP Open message
 *
 * @param body byte array representing BGP Open message, without header
 * @param messageLength the length of the message
 * @return {@link Open} BGP Open Message
 * @throws BGPDocumentedException if the parsing was unsuccessful
 */
@Override
public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
    Preconditions.checkArgument(body != null, "Buffer cannot be null.");
    if (body.readableBytes() < MIN_MSG_LENGTH) {
        throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
    }
    final int version = body.readUnsignedByte();
    if (version != BGP_VERSION) {
        throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
    }
    final AsNumber as = new AsNumber((long) body.readUnsignedShort());
    final int holdTime = body.readUnsignedShort();
    if (holdTime == 1 || holdTime == 2) {
        throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
    }
    Ipv4Address bgpId = null;
    try {
        bgpId = Ipv4Util.addressForByteBuf(body);
    } catch (final IllegalArgumentException e) {
        throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
    }
    final int optLength = body.readUnsignedByte();
    final List<BgpParameters> optParams = new ArrayList<>();
    if (optLength > 0) {
        fillParams(body.slice(), optParams);
    }
    LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
    return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime).setBgpIdentifier(bgpId).setBgpParameters(optParams).build();
}
Also used : OpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) ArrayList(java.util.ArrayList) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters) AsNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber) Ipv4Address(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)

Example 20 with BGPDocumentedException

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

Aggregations

BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)27 BGPParsingException (org.opendaylight.protocol.bgp.parser.BGPParsingException)9 Test (org.junit.Test)5 AsNumber (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber)5 Notification (org.opendaylight.yangtools.yang.binding.Notification)5 IpAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress)4 ByteBuf (io.netty.buffer.ByteBuf)3 ArrayList (java.util.ArrayList)3 BGPSessionPreferences (org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences)3 BmpDeserializationException (org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException)3 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)3 Keepalive (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Keepalive)3 Open (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open)3 Update (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update)3 BgpParameters (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters)3 BgpTableTypeImpl (org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl)2 BGPSessionListener (org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener)2 KeepaliveBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.KeepaliveBuilder)2 Notify (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify)2 OpenBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder)2