Search in sources :

Example 16 with Notify

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify in project bgpcep by opendaylight.

the class BGPNotificationMessageParser method parseMessageBody.

/**
 * Parses BGP Notification message to bytes.
 *
 * @param body ByteBuf to be parsed
 * @param messageLength the length of the message
 * @return {@link Notify} which represents BGP notification message
 * @throws BGPDocumentedException if parsing goes wrong
 */
@Override
public Notify parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
    Preconditions.checkArgument(body != null, "Buffer cannot be null.");
    if (body.readableBytes() < ERROR_SIZE) {
        throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
    }
    final int errorCode = body.readUnsignedByte();
    final int errorSubcode = body.readUnsignedByte();
    final NotifyBuilder builder = new NotifyBuilder().setErrorCode((short) errorCode).setErrorSubcode((short) errorSubcode);
    if (body.isReadable()) {
        builder.setData(ByteArray.readAllBytes(body));
    }
    LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", BGPError.forValue(errorCode, errorSubcode), Arrays.toString(builder.getData()));
    return builder.build();
}
Also used : NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder)

Example 17 with Notify

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify in project bgpcep by opendaylight.

the class BGPNotificationMessageParser method parseMessageBody.

/**
 * Parses BGP Notification message to bytes.
 *
 * @param body ByteBuf to be parsed
 * @param messageLength the length of the message
 * @return {@link Notify} which represents BGP notification message
 * @throws BGPDocumentedException if parsing goes wrong
 */
@Override
public Notify parseMessageBody(final ByteBuf body, final int messageLength, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
    checkArgument(body != null, "Buffer cannot be null.");
    if (body.readableBytes() < ERROR_SIZE) {
        throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
    }
    final Uint8 errorCode = Uint8.valueOf(body.readUnsignedByte());
    final Uint8 errorSubcode = Uint8.valueOf(body.readUnsignedByte());
    final NotifyBuilder builder = new NotifyBuilder().setErrorCode(errorCode).setErrorSubcode(errorSubcode);
    if (body.isReadable()) {
        builder.setData(ByteArray.readAllBytes(body));
    }
    final Notify result = builder.build();
    final BGPError err = BGPError.forValue(errorCode, errorSubcode);
    if (LOG.isDebugEnabled()) {
        LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", err, Arrays.toString(result.getData()));
    }
    return result;
}
Also used : NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder) BGPError(org.opendaylight.protocol.bgp.parser.BGPError) Uint8(org.opendaylight.yangtools.yang.common.Uint8) Notify(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify)

Example 18 with Notify

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify in project bgpcep by opendaylight.

the class ParserTest method testNotificationMsg.

@Test
public void testNotificationMsg() throws BGPParsingException, BGPDocumentedException {
    Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode()).setErrorSubcode(BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }).build();
    final ByteBuf bytes = Unpooled.buffer();
    ParserTest.reg.serializeMessage(notMsg, bytes);
    assertArrayEquals(NOTIFICATION_BMSG, ByteArray.subByte(bytes.array(), 0, bytes.writerIndex()));
    Notification msg = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
    assertTrue(msg instanceof Notify);
    assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) msg).getErrorCode(), ((Notify) msg).getErrorSubcode()));
    assertArrayEquals(new byte[] { 4, 9 }, ((Notify) msg).getData());
    notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()).setErrorSubcode(BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
    bytes.clear();
    ParserTest.reg.serializeMessage(notMsg, bytes);
    msg = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
    assertTrue(msg instanceof Notify);
    assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) msg).getErrorCode(), ((Notify) msg).getErrorSubcode()));
    assertNull(((Notify) msg).getData());
}
Also used : NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder) Notify(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify) ByteBuf(io.netty.buffer.ByteBuf) Notification(org.opendaylight.yangtools.yang.binding.Notification) Test(org.junit.Test)

Example 19 with Notify

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify in project bgpcep by opendaylight.

the class AbstractBGPSessionNegotiator method handleMessage.

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:
            // executed
            if (msg instanceof Open) {
                startNegotiation();
                handleOpen((Open) msg);
                return;
            }
            sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
            break;
        case OPEN_CONFIRM:
            if (msg instanceof Keepalive) {
                negotiationSuccessful();
                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.rev200120.Notify) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) Keepalive(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Keepalive) Open(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open)

Example 20 with Notify

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify in project bgpcep by opendaylight.

the class BGPSessionImpl method handleMessage.

/**
 * Handles incoming message based on their type.
 *
 * @param msg incoming message
 */
void handleMessage(final Notification msg) {
    // synchronize on listener and then on this object to ensure correct order of locking
    synchronized (this.listener) {
        synchronized (this) {
            if (this.state == State.IDLE) {
                return;
            }
            try {
                // Update last reception time
                this.lastMessageReceivedAt = System.nanoTime();
                if (msg instanceof Open) {
                    // Open messages should not be present here
                    terminate(new BGPDocumentedException(null, BGPError.FSM_ERROR));
                } else if (msg instanceof Notify) {
                    final Notify notify = (Notify) msg;
                    // Notifications are handled internally
                    LOG.info("Session closed because Notification message received: {} / {}, data={}", notify.getErrorCode(), notify.getErrorSubcode(), notify.getData() != null ? ByteBufUtil.hexDump(notify.getData()) : null);
                    notifyTerminationReasonAndCloseWithoutMessage(notify.getErrorCode(), notify.getErrorSubcode());
                } else if (msg instanceof Keepalive) {
                    // Keepalives are handled internally
                    LOG.trace("Received KeepAlive message.");
                    this.kaCounter++;
                    if (this.kaCounter >= 2) {
                        this.sync.kaReceived();
                    }
                } else if (msg instanceof RouteRefresh) {
                    this.listener.onMessage(this, msg);
                } else if (msg instanceof Update) {
                    this.listener.onMessage(this, msg);
                    this.sync.updReceived((Update) msg);
                } else {
                    LOG.warn("Ignoring unhandled message: {}.", msg.getClass());
                }
                this.sessionState.messageReceived(msg);
            } catch (final BGPDocumentedException e) {
                terminate(e);
            }
        }
    }
}
Also used : Notify(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) RouteRefresh(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefresh) Keepalive(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Keepalive) Update(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update) Open(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open)

Aggregations

Test (org.junit.Test)16 Notify (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify)12 MapNotify (org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify)8 Notification (org.opendaylight.yangtools.yang.binding.Notification)6 ArrayList (java.util.ArrayList)5 JAXBException (javax.xml.bind.JAXBException)5 Notify (org.oasis_open.docs.wsn.b_2.Notify)5 Open (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Open)5 JMSException (javax.jms.JMSException)4 AclInterface (org.opendaylight.netvirt.aclservice.api.utils.AclInterface)4 MappingRecord (org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord)4 Message (javax.jms.Message)3 TextMessage (javax.jms.TextMessage)3 NotificationMessageHolderType (org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType)3 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)3 Ipv4AddressNoZone (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone)3 OpenBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.OpenBuilder)3 Optional (com.google.common.base.Optional)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 ByteBuf (io.netty.buffer.ByteBuf)2