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();
}
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;
}
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());
}
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));
}
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);
}
Aggregations