Search in sources :

Example 1 with BGPDocumentedException

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

the class BGPOpenMessageParser method fillParams.

private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(), "BUffer cannot be null or empty.");
    if (LOG.isTraceEnabled()) {
        LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
    }
    while (buffer.isReadable()) {
        if (buffer.readableBytes() <= 2) {
            throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
        }
        final int paramType = buffer.readUnsignedByte();
        final int paramLength = buffer.readUnsignedByte();
        final ByteBuf paramBody = buffer.readSlice(paramLength);
        final BgpParameters param;
        try {
            param = this.reg.parseParameter(paramType, paramBody);
        } catch (final BGPParsingException e) {
            throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
        }
        if (param != null) {
            params.add(param);
        } else {
            LOG.debug("Ignoring BGP Parameter type: {}", paramType);
        }
    }
    LOG.trace("Parsed BGP parameters: {}", params);
}
Also used : BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) ByteBuf(io.netty.buffer.ByteBuf) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters)

Example 2 with BGPDocumentedException

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

the class AbstractMessageRegistry method parseMessage.

@Override
public Notification parseMessage(final ByteBuf buffer, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPParsingException {
    Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes cannot be null or empty.");
    Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH, "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), MessageUtil.COMMON_HEADER_LENGTH);
    final byte[] marker = ByteArray.readBytes(buffer, MessageUtil.MARKER_LENGTH);
    if (!Arrays.equals(marker, MARKER)) {
        throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
    }
    final int messageLength = buffer.readUnsignedShort();
    // to be sent with Error message
    final byte typeBytes = buffer.readByte();
    final int messageType = UnsignedBytes.toInt(typeBytes);
    if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
        throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
    }
    if (messageLength - MessageUtil.COMMON_HEADER_LENGTH != buffer.readableBytes()) {
        throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + buffer.readableBytes() + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
    }
    final ByteBuf msgBody = buffer.readSlice(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
    final Notification msg = parseBody(messageType, msgBody, messageLength, constraint);
    if (msg == null) {
        throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes });
    }
    return msg;
}
Also used : BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) ByteBuf(io.netty.buffer.ByteBuf) Notification(org.opendaylight.yangtools.yang.binding.Notification)

Example 3 with BGPDocumentedException

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

the class AbstractBGPSessionNegotiator method handleMessage.

protected synchronized void handleMessage(final Notification msg) {
    LOG.debug("Channel {} handling message in state {}, msg: {}", this.channel, this.state, msg);
    switch(this.state) {
        case FINISHED:
            sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
            return;
        case IDLE:
            // to avoid race condition when Open message was sent by the peer before startNegotiation could be executed
            if (msg instanceof Open) {
                startNegotiation();
                handleOpen((Open) msg);
                return;
            }
            sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
            break;
        case OPEN_CONFIRM:
            if (msg instanceof Keepalive) {
                negotiationSuccessful(this.session);
                LOG.info("BGP Session with peer {} established successfully.", this.channel);
            } else if (msg instanceof Notify) {
                final Notify ntf = (Notify) msg;
                negotiationFailed(new BGPDocumentedException("Peer refusal", BGPError.forValue(ntf.getErrorCode(), ntf.getErrorSubcode())));
            }
            this.state = State.FINISHED;
            return;
        case OPEN_SENT:
            if (msg instanceof Open) {
                handleOpen((Open) msg);
                return;
            }
            break;
        default:
            break;
    }
    // Catch-all for unexpected message
    LOG.warn("Channel {} state {} unexpected message {}", this.channel, this.state, msg);
    sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
    negotiationFailed(new BGPDocumentedException("Unexpected message channel: " + this.channel + ", state: " + this.state + ", message: " + msg, BGPError.FSM_ERROR));
    this.state = State.FINISHED;
}
Also used : Notify(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) Keepalive(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Keepalive) Open(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open)

Example 4 with BGPDocumentedException

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

the class AbstractBGPSessionNegotiator method handleOpen.

private synchronized void handleOpen(final Open openObj) {
    final IpAddress remoteIp = getRemoteIp();
    final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
    try {
        final BGPSessionListener peer = this.registry.getPeer(remoteIp, getSourceId(openObj, preferences), getDestinationId(openObj, preferences), openObj);
        sendMessage(new KeepaliveBuilder().build());
        this.state = State.OPEN_CONFIRM;
        this.session = new BGPSessionImpl(peer, this.channel, openObj, preferences, this.registry);
        this.session.setChannelExtMsgCoder(openObj);
        LOG.debug("Channel {} moved to OPEN_CONFIRM state with remote proposal {}", this.channel, openObj);
    } catch (final BGPDocumentedException e) {
        LOG.warn("Channel {} negotiation failed", this.channel, e);
        negotiationFailed(e);
    }
}
Also used : BGPSessionListener(org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener) 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) KeepaliveBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.KeepaliveBuilder)

Example 5 with BGPDocumentedException

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

the class PeerUpHandler method parseMessageBody.

@Override
public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
    final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder().setPeerHeader(parsePerPeerHeader(bytes));
    if (peerUpNot.getPeerHeader().isIpv4()) {
        bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
        peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes)));
    } else {
        peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes)));
    }
    peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort()));
    peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort()));
    try {
        final Notification opSent = this.msgRegistry.parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
        requireNonNull(opSent, "Error on parse Sent OPEN Message, Sent OPEN Message is null");
        Preconditions.checkArgument(opSent instanceof OpenMessage, "An instance of OpenMessage notification is required");
        final OpenMessage sent = (OpenMessage) opSent;
        final Notification opRec = this.msgRegistry.parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
        requireNonNull(opRec, "Error on parse Received  OPEN Message, Received  OPEN Message is null");
        Preconditions.checkArgument(opRec instanceof OpenMessage, "An instance of OpenMessage notification is required");
        final OpenMessage received = (OpenMessage) opRec;
        peerUpNot.setSentOpen(new SentOpenBuilder(sent).build());
        peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build());
        final InformationBuilder infos = new InformationBuilder();
        if (bytes.isReadable()) {
            parseTlvs(infos, bytes);
            peerUpNot.setInformation(infos.build());
        }
    } catch (final BGPDocumentedException | BGPParsingException e) {
        throw new BmpDeserializationException("Error while parsing BGP Open Message.", e);
    }
    return peerUpNot.build();
}
Also used : ReceivedOpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.ReceivedOpenBuilder) StringInformationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.string.informations.StringInformationBuilder) InformationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.InformationBuilder) OpenMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenMessage) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) SentOpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.peer.up.SentOpenBuilder) IpAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) BmpDeserializationException(org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException) PortNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber) PeerUpNotification(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerUpNotification) Notification(org.opendaylight.yangtools.yang.binding.Notification) PeerUpNotificationBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.PeerUpNotificationBuilder)

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