use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class BGPOpenMessageParser method fillParams.
private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
Preconditions.checkArgument(buffer != null && buffer.isReadable(), "BUffer cannot be null or empty.");
if (LOG.isTraceEnabled()) {
LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
}
while (buffer.isReadable()) {
if (buffer.readableBytes() <= 2) {
throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
}
final int paramType = buffer.readUnsignedByte();
final int paramLength = buffer.readUnsignedByte();
final ByteBuf paramBody = buffer.readSlice(paramLength);
final BgpParameters param;
try {
param = this.reg.parseParameter(paramType, paramBody);
} catch (final BGPParsingException e) {
throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
}
if (param != null) {
params.add(param);
} else {
LOG.debug("Ignoring BGP Parameter type: {}", paramType);
}
}
LOG.trace("Parsed BGP parameters: {}", params);
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class AbstractMessageRegistry method parseMessage.
@Override
public Notification parseMessage(final ByteBuf buffer, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPParsingException {
Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes cannot be null or empty.");
Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH, "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(), MessageUtil.COMMON_HEADER_LENGTH);
final byte[] marker = ByteArray.readBytes(buffer, MessageUtil.MARKER_LENGTH);
if (!Arrays.equals(marker, MARKER)) {
throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
}
final int messageLength = buffer.readUnsignedShort();
// to be sent with Error message
final byte typeBytes = buffer.readByte();
final int messageType = UnsignedBytes.toInt(typeBytes);
if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
throw BGPDocumentedException.badMessageLength("Message length field not within valid range.", messageLength);
}
if (messageLength - MessageUtil.COMMON_HEADER_LENGTH != buffer.readableBytes()) {
throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + buffer.readableBytes() + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". ");
}
final ByteBuf msgBody = buffer.readSlice(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
final Notification msg = parseBody(messageType, msgBody, messageLength, constraint);
if (msg == null) {
throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes });
}
return msg;
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class AbstractBGPSessionNegotiator method handleMessage.
protected 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:
// to avoid race condition when Open message was sent by the peer before startNegotiation could be executed
if (msg instanceof Open) {
startNegotiation();
handleOpen((Open) msg);
return;
}
sendMessage(buildErrorNotify(BGPError.FSM_ERROR));
break;
case OPEN_CONFIRM:
if (msg instanceof Keepalive) {
negotiationSuccessful(this.session);
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.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class AbstractBGPSessionNegotiator method handleOpen.
private synchronized void handleOpen(final Open openObj) {
final IpAddress remoteIp = getRemoteIp();
final BGPSessionPreferences preferences = this.registry.getPeerPreferences(remoteIp);
try {
final BGPSessionListener peer = this.registry.getPeer(remoteIp, getSourceId(openObj, preferences), getDestinationId(openObj, preferences), openObj);
sendMessage(new KeepaliveBuilder().build());
this.state = State.OPEN_CONFIRM;
this.session = new BGPSessionImpl(peer, this.channel, openObj, preferences, this.registry);
this.session.setChannelExtMsgCoder(openObj);
LOG.debug("Channel {} moved to OPEN_CONFIRM state with remote proposal {}", this.channel, openObj);
} catch (final BGPDocumentedException e) {
LOG.warn("Channel {} negotiation failed", this.channel, e);
negotiationFailed(e);
}
}
use of org.opendaylight.protocol.bgp.parser.BGPDocumentedException in project bgpcep by opendaylight.
the class PeerUpHandler method parseMessageBody.
@Override
public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
final PeerUpNotificationBuilder peerUpNot = new PeerUpNotificationBuilder().setPeerHeader(parsePerPeerHeader(bytes));
if (peerUpNot.getPeerHeader().isIpv4()) {
bytes.skipBytes(Ipv6Util.IPV6_LENGTH - Ipv4Util.IP4_LENGTH);
peerUpNot.setLocalAddress(new IpAddress(Ipv4Util.addressForByteBuf(bytes)));
} else {
peerUpNot.setLocalAddress(new IpAddress(Ipv6Util.addressForByteBuf(bytes)));
}
peerUpNot.setLocalPort(new PortNumber(bytes.readUnsignedShort()));
peerUpNot.setRemotePort(new PortNumber(bytes.readUnsignedShort()));
try {
final Notification opSent = this.msgRegistry.parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
requireNonNull(opSent, "Error on parse Sent OPEN Message, Sent OPEN Message is null");
Preconditions.checkArgument(opSent instanceof OpenMessage, "An instance of OpenMessage notification is required");
final OpenMessage sent = (OpenMessage) opSent;
final Notification opRec = this.msgRegistry.parseMessage(bytes.readSlice(getBgpMessageLength(bytes)), null);
requireNonNull(opRec, "Error on parse Received OPEN Message, Received OPEN Message is null");
Preconditions.checkArgument(opRec instanceof OpenMessage, "An instance of OpenMessage notification is required");
final OpenMessage received = (OpenMessage) opRec;
peerUpNot.setSentOpen(new SentOpenBuilder(sent).build());
peerUpNot.setReceivedOpen(new ReceivedOpenBuilder(received).build());
final InformationBuilder infos = new InformationBuilder();
if (bytes.isReadable()) {
parseTlvs(infos, bytes);
peerUpNot.setInformation(infos.build());
}
} catch (final BGPDocumentedException | BGPParsingException e) {
throw new BmpDeserializationException("Error while parsing BGP Open Message.", e);
}
return peerUpNot.build();
}
Aggregations