Search in sources :

Example 1 with NotifyBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.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.rev171207.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(notificationBMsg, ByteArray.subByte(bytes.array(), 0, bytes.writerIndex()));
    Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
    assertTrue(m instanceof Notify);
    assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
    assertArrayEquals(new byte[] { 4, 9 }, ((Notify) m).getData());
    notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()).setErrorSubcode(BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
    bytes.clear();
    ParserTest.reg.serializeMessage(notMsg, bytes);
    m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
    assertTrue(m instanceof Notify);
    assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) m).getErrorCode(), ((Notify) m).getErrorSubcode()));
    assertNull(((Notify) m).getData());
}
Also used : NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder) Notify(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify) ByteBuf(io.netty.buffer.ByteBuf) Notification(org.opendaylight.yangtools.yang.binding.Notification) Test(org.junit.Test)

Example 3 with NotifyBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.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.rev171207.NotifyBuilder) Notification(org.opendaylight.yangtools.yang.binding.Notification)

Example 4 with NotifyBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.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 e BGPDocumentedException
 */
@VisibleForTesting
synchronized void terminate(final BGPDocumentedException e) {
    final BGPError error = e.getError();
    final byte[] data = e.getData();
    final NotifyBuilder builder = new NotifyBuilder().setErrorCode(error.getCode()).setErrorSubcode(error.getSubcode());
    if (data != null && data.length != 0) {
        builder.setData(data);
    }
    this.writeAndFlush(builder.build());
    notifyTerminationReasonAndCloseWithoutMessage(error.getCode(), error.getSubcode());
}
Also used : BGPError(org.opendaylight.protocol.bgp.parser.BGPError) NotifyBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.NotifyBuilder) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

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