use of org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl 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.protocol.bgp.parser.BgpTableTypeImpl in project bgpcep by opendaylight.
the class ParserTest method testOpenMessage.
/*
* ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff <- marker
* 00 3d <- length (61) - including header
* 01 <- message type
* 04 <- BGP version
* 00 64 <- My AS Number (AS TRANS in this case)
* 00 b4 <- Hold Time
* 00 00 00 00 <- BGP Identifier
* 20 <- Optional Parameters Length
* 02 <- opt. param. type (capabilities)
* 06 <- length
* 01 <- capability code (MP Extensions for BGP4)
* 04 <- length
* 00 01 00 01 <- AFI 1, SAFI 1
* 02 <- opt. param. type (capabilities)
* 06 <- length
* 01 <- capability code (MP Extensions for BGP4)
* 04 <- length
* 00 02 00 01 <- AFI 2, SAFI 1
* 02 <- opt. param. type (capabilities)
* 06 <- length
* 01 <- capability code (MP Extensions for BGP4)
* 04 <- length
* 40 04 00 47 <- AFI 16388, SAFI 71
* 02 <- opt. param. type (capabilities)
* 06 <- length
* 41 <- capability code (AS4 octet support)
* 04 <- length
* 00 00 00 64 <- AS number
*/
@Test
public void testOpenMessage() throws Exception {
final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
final Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(inputBytes.get(3)), null);
final Open open = (Open) o;
final Set<BgpTableType> types = Sets.newHashSet();
for (final BgpParameters param : open.getBgpParameters()) {
for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
final CParameters cParam = optCapa.getCParameters();
if (cParam != null && cParam.getAugmentation(CParameters1.class) != null && cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability() != null) {
final MultiprotocolCapability mp = cParam.getAugmentation(CParameters1.class).getMultiprotocolCapability();
final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
types.add(type);
}
}
}
final Set<BgpTableType> expected = Sets.newHashSet();
expected.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
expected.add(new BgpTableTypeImpl(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class));
expected.add(new BgpTableTypeImpl(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class));
assertEquals(expected, types);
final ByteBuf buffer = Unpooled.buffer();
msgReg.serializeMessage(o, buffer);
assertArrayEquals(inputBytes.get(3), ByteArray.readAllBytes(buffer));
}
use of org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl in project bgpcep by opendaylight.
the class LUNlriParser method parseNlri.
@Override
public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder, final PeerSpecificParserConstraint constraint) throws BGPParsingException {
if (!nlri.isReadable()) {
return;
}
final Class<? extends AddressFamily> afi = builder.getAfi();
final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
DestinationType destination = null;
if (afi == Ipv4AddressFamily.class) {
destination = new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
} else if (afi == Ipv6AddressFamily.class) {
destination = new DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(new DestinationIpv6LabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
}
builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
}
Aggregations