use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.
the class BGPOpenMessageParser method serializeMessage.
/**
* Serializes given BGP Open message to byte array, without the header.
*
* @param msg BGP Open message to be serialized.
* @param bytes ByteBuf where the message will be serialized
*/
@Override
public void serializeMessage(final Notification msg, final ByteBuf bytes) {
Preconditions.checkArgument(msg instanceof Open, "Message needs to be of type Open");
final Open open = (Open) msg;
final ByteBuf msgBody = Unpooled.buffer();
msgBody.writeByte(BGP_VERSION);
// When our AS number does not fit into two bytes, we report it as AS_TRANS
int openAS = open.getMyAsNumber();
if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) {
openAS = AS_TRANS;
}
msgBody.writeShort(openAS);
msgBody.writeShort(open.getHoldTimer());
msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
final ByteBuf paramsBuffer = Unpooled.buffer();
if (open.getBgpParameters() != null) {
for (final BgpParameters param : open.getBgpParameters()) {
this.reg.serializeParameter(param, paramsBuffer);
}
}
msgBody.writeByte(paramsBuffer.writerIndex());
msgBody.writeBytes(paramsBuffer);
MessageUtil.formatMessage(TYPE, msgBody, bytes);
}
use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.
the class BGPRouteRefreshMessageParser method parseMessageBody.
/**
* Parses BGP Route Refresh message to bytes.
*
* @param body ByteBuf to be parsed
* @param messageLength the length of the message
* @return {@link RouteRefresh} which represents BGP notification message
* @throws BGPDocumentedException if parsing goes wrong
*/
@Override
public RouteRefresh parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
Preconditions.checkArgument(body != null, "Body buffer cannot be null.");
if (body.readableBytes() < TRIPLET_BYTE_SIZE) {
throw BGPDocumentedException.badMessageLength("RouteRefresh message is too small.", messageLength);
}
final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, this.afiReg, this.safiReg);
if (!parsedAfiSafi.isPresent()) {
throw new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
}
return new RouteRefreshBuilder(parsedAfiSafi.get()).build();
}
use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.
the class BGPUpdateMessageParser method parseMessageBody.
/**
* Parse Update message from buffer.
* Calls {@link #checkMandatoryAttributesPresence(Update)} to check for presence of mandatory attributes.
*
* @param buffer Encoded BGP message in ByteBuf
* @param messageLength Length of the BGP message
* @param constraint Peer specific constraints
* @return Parsed Update message body
*/
@Override
public Update parseMessageBody(final ByteBuf buffer, final int messageLength, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");
final UpdateBuilder builder = new UpdateBuilder();
final boolean isMultiPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
final int withdrawnRoutesLength = buffer.readUnsignedShort();
if (withdrawnRoutesLength > 0) {
final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
final ByteBuf withdrawnRoutesBuffer = buffer.readBytes(withdrawnRoutesLength);
while (withdrawnRoutesBuffer.isReadable()) {
final WithdrawnRoutesBuilder withdrawnRoutesBuilder = new WithdrawnRoutesBuilder();
if (isMultiPathSupported) {
withdrawnRoutesBuilder.setPathId(PathIdUtil.readPathId(withdrawnRoutesBuffer));
}
withdrawnRoutesBuilder.setPrefix(Ipv4Util.prefixForByteBuf(withdrawnRoutesBuffer));
withdrawnRoutes.add(withdrawnRoutesBuilder.build());
}
builder.setWithdrawnRoutes(withdrawnRoutes);
}
final int totalPathAttrLength = buffer.readUnsignedShort();
if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
return builder.build();
}
if (totalPathAttrLength > 0) {
try {
final Attributes attributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength), constraint);
builder.setAttributes(attributes);
} catch (final RuntimeException | BGPParsingException e) {
// Catch everything else and turn it into a BGPDocumentedException
throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
}
}
final List<Nlri> nlri = new ArrayList<>();
while (buffer.isReadable()) {
final NlriBuilder nlriBuilder = new NlriBuilder();
if (isMultiPathSupported) {
nlriBuilder.setPathId(PathIdUtil.readPathId(buffer));
}
nlriBuilder.setPrefix(Ipv4Util.prefixForByteBuf(buffer));
nlri.add(nlriBuilder.build());
}
if (!nlri.isEmpty()) {
builder.setNlri(nlri);
}
final Update msg = builder.build();
checkMandatoryAttributesPresence(msg);
LOG.debug("BGP Update message was parsed {}.", msg);
return msg;
}
use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.
the class CapabilityParameterParser method parseParameter.
@Override
public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
if (LOG.isTraceEnabled()) {
LOG.trace("Started parsing of BGP Capabilities: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
}
final List<OptionalCapabilities> optionalCapas = Lists.newArrayList();
while (buffer.isReadable()) {
final OptionalCapabilities optionalCapa = parseOptionalCapability(buffer);
if (optionalCapa != null) {
optionalCapas.add(optionalCapa);
}
}
return new BgpParametersBuilder().setOptionalCapabilities(optionalCapas).build();
}
use of org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp in project bgpcep by opendaylight.
the class CapabilityParameterParser method serializeOptionalCapability.
private void serializeOptionalCapability(final OptionalCapabilities optionalCapa, final ByteBuf byteAggregator) {
if (optionalCapa.getCParameters() != null) {
final CParameters cap = optionalCapa.getCParameters();
final ByteBuf bytes = Unpooled.buffer();
this.reg.serializeCapability(cap, bytes);
Preconditions.checkArgument(bytes != null, "Unhandled capability class %s", cap.getImplementedInterface());
if (LOG.isTraceEnabled()) {
LOG.trace("BGP capability serialized to: {}", ByteBufUtil.hexDump(bytes));
}
byteAggregator.writeBytes(bytes);
}
}
Aggregations