use of org.opendaylight.protocol.bgp.parser.BGPParsingException 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.BGPParsingException in project bgpcep by opendaylight.
the class AddPathCapabilityHandler method parseCapability.
@Override
public CParameters parseCapability(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
final List<AddressFamilies> families = new ArrayList<>();
while (buffer.isReadable()) {
final int afiVal = buffer.readUnsignedShort();
final Class<? extends AddressFamily> afi = this.afiReg.classForFamily(afiVal);
if (afi == null) {
throw new BGPParsingException("Address Family Identifier: '" + afiVal + "' not supported.");
}
final int safiVal = buffer.readUnsignedByte();
final Class<? extends SubsequentAddressFamily> safi = this.safiReg.classForFamily(safiVal);
if (safi == null) {
throw new BGPParsingException("Subsequent Address Family Identifier: '" + safiVal + "' not supported.");
}
final SendReceive sendReceive = SendReceive.forValue(buffer.readUnsignedByte());
if (sendReceive != null) {
families.add(new AddressFamiliesBuilder().setAfi(afi).setSafi(safi).setSendReceive(sendReceive).build());
}
}
return new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder().setAddPathCapability(new AddPathCapabilityBuilder().setAddressFamilies(families).build()).build()).build();
}
use of org.opendaylight.protocol.bgp.parser.BGPParsingException 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.BGPParsingException 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();
}
use of org.opendaylight.protocol.bgp.parser.BGPParsingException in project bgpcep by opendaylight.
the class RouteMonitoringMessageHandler method parseMessageBody.
@Override
public Notification parseMessageBody(final ByteBuf bytes) throws BmpDeserializationException {
final RouteMonitoringMessageBuilder routeMonitor = new RouteMonitoringMessageBuilder().setPeerHeader(parsePerPeerHeader(bytes));
try {
final Notification message = this.msgRegistry.parseMessage(bytes, null);
requireNonNull(message, "UpdateMessage may not be null");
Preconditions.checkArgument(message instanceof UpdateMessage, "An instance of UpdateMessage is required");
final UpdateMessage updateMessage = (UpdateMessage) message;
final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.route.monitoring.message.Update update = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev171207.route.monitoring.message.UpdateBuilder(updateMessage).build();
routeMonitor.setUpdate(update);
} catch (final BGPDocumentedException | BGPParsingException e) {
throw new BmpDeserializationException("Error while parsing Update Message.", e);
}
return routeMonitor.build();
}
Aggregations