Search in sources :

Example 1 with NotifyBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder 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 2 with NotifyBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder 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 3 with NotifyBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder 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 4 with NotifyBuilder

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

the class AbstractAddPathTest method sendNotification.

void sendNotification(final BGPSessionImpl session) {
    final Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode()).setErrorSubcode(BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }).build();
    waitFutureSuccess(session.writeAndFlush(notMsg));
}
Also used : NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder) Notification(org.opendaylight.yangtools.yang.binding.Notification)

Example 5 with NotifyBuilder

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

the class BGPSessionImpl method terminate.

/**
 * Closes BGP session from the parent with given reason. A message needs to be sent, but parent doesn't have to be
 * modified, because he initiated the closing. (To prevent concurrent modification exception).
 *
 * @param cause BGPDocumentedException
 */
@VisibleForTesting
@Holding({ "this.listener", "this" })
void terminate(final BGPDocumentedException cause) {
    final BGPError error = cause.getError();
    final byte[] data = cause.getData();
    final NotifyBuilder builder = new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode());
    if (data != null && data.length != 0) {
        builder.setData(data);
    }
    writeAndFlush(builder.build());
    notifyTerminationReasonAndCloseWithoutMessage(error);
}
Also used : BGPError(org.opendaylight.protocol.bgp.parser.BGPError) NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Holding(org.checkerframework.checker.lock.qual.Holding)

Aggregations

NotifyBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.NotifyBuilder)4 BGPError (org.opendaylight.protocol.bgp.parser.BGPError)2 Notify (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Notify)2 Notification (org.opendaylight.yangtools.yang.binding.Notification)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ByteBuf (io.netty.buffer.ByteBuf)1 Holding (org.checkerframework.checker.lock.qual.Holding)1 Test (org.junit.Test)1 NotifyBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder)1 Uint8 (org.opendaylight.yangtools.yang.common.Uint8)1