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