use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters in project bgpcep by opendaylight.
the class BGPOpenMessageParser method parseParameters.
private List<BgpParameters> parseParameters(final ByteBuf buffer, final int length) throws BGPDocumentedException {
if (length == 0) {
return ImmutableList.of();
}
if (LOG.isTraceEnabled()) {
LOG.trace("Started parsing of BGP parameter: {} length {}", ByteBufUtil.hexDump(buffer), length);
}
final int realLength;
final OptionalInt extendedLength = extractExtendedLength(buffer, length);
if (extendedLength.isPresent()) {
realLength = extendedLength.getAsInt();
if (realLength < Values.UNSIGNED_BYTE_MAX_VALUE) {
LOG.debug("Peer used Extended Optional Parameters Length to encode length {}", realLength);
}
} else {
realLength = length;
}
// We have determined the real length, we can trim the buffer now
if (buffer.readableBytes() > realLength) {
buffer.writerIndex(buffer.readerIndex() + realLength);
LOG.trace("Truncated BGP parameter buffer to length {}: {}", realLength, ByteBufUtil.hexDump(buffer));
}
final int lengthSize = extendedLength.isPresent() ? 1 : 2;
final List<BgpParameters> params = new ArrayList<>();
while (buffer.isReadable()) {
final int paramType = buffer.readUnsignedByte();
final Optional<ParameterParser> parser = reg.findParser(paramType);
if (!parser.isPresent()) {
throw new BGPDocumentedException("Parameter " + paramType + " not supported", BGPError.OPT_PARAM_NOT_SUPPORTED);
}
if (buffer.readableBytes() <= lengthSize) {
throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.UNSPECIFIC_OPEN_ERROR);
}
final int paramLength = extendedLength.isPresent() ? buffer.readUnsignedShort() : buffer.readUnsignedByte();
final ByteBuf paramBody = buffer.readSlice(paramLength);
final BgpParameters param;
try {
param = parser.get().parseParameter(paramBody);
} catch (final BGPParsingException e) {
throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
}
params.add(verifyNotNull(param));
}
LOG.trace("Parsed BGP parameters: {}", params);
return params;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters in project bgpcep by opendaylight.
the class BGPOpenMessageParser method parseMessageBody.
/**
* Parses given byte array to BGP Open message.
*
* @param body byte array representing BGP Open message, without header
* @param messageLength the length of the message
* @return {@link Open} BGP Open Message
* @throws BGPDocumentedException if the parsing was unsuccessful
*/
@Override
public Open parseMessageBody(final ByteBuf body, final int messageLength, final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
checkArgument(body != null, "Buffer cannot be null.");
if (body.readableBytes() < MIN_MSG_LENGTH) {
throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
}
final int version = body.readUnsignedByte();
if (version != BGP_VERSION) {
throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
}
final AsNumber as = new AsNumber(Uint32.valueOf(body.readUnsignedShort()));
final Uint16 holdTime = ByteBufUtils.readUint16(body);
if (Uint16.ONE.equals(holdTime) || Uint16.TWO.equals(holdTime)) {
throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
}
final Ipv4AddressNoZone bgpId;
try {
bgpId = Ipv4Util.addressForByteBuf(body);
} catch (final IllegalArgumentException e) {
throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
}
final int optLength = body.readUnsignedByte();
final List<BgpParameters> optParams = parseParameters(body.slice(), optLength);
LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
return new OpenBuilder().setMyAsNumber(Uint16.valueOf(as.getValue())).setHoldTimer(holdTime).setBgpIdentifier(bgpId).setBgpParameters(optParams).build();
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters in project bgpcep by opendaylight.
the class CapabilityParameterParser method parseParameter.
@Override
public BgpParameters parseParameter(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
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 = new ArrayList<>();
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.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters 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 Notification o = msgReg.parseMessage(Unpooled.copiedBuffer(INPUT_BYTES.get(3)), null);
final Open open = (Open) o;
final Set<BgpTableType> types = new HashSet<>();
for (final BgpParameters param : open.getBgpParameters()) {
for (final OptionalCapabilities optCapa : param.getOptionalCapabilities()) {
final CParameters cParam = optCapa.getCParameters();
if (cParam != null && cParam.augmentation(CParameters1.class) != null && cParam.augmentation(CParameters1.class).getMultiprotocolCapability() != null) {
final MultiprotocolCapability mp = cParam.augmentation(CParameters1.class).getMultiprotocolCapability();
final BgpTableType type = new BgpTableTypeImpl(mp.getAfi(), mp.getSafi());
types.add(type);
}
}
}
final Set<BgpTableType> expected = new HashSet<>();
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(INPUT_BYTES.get(3), ByteArray.readAllBytes(buffer));
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters in project bgpcep by opendaylight.
the class BGPTestTool method start.
void start(final Arguments arguments) {
final BGPDispatcher dispatcher = initializeActivator();
final ArrayList<OptionalCapabilities> optCap = Lists.newArrayList(createMPCapability(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class), createMPCapability(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class), createMPCapability(Ipv4AddressFamily.class, MplsLabeledVpnSubsequentAddressFamily.class), createMPCapability(Ipv6AddressFamily.class, MplsLabeledVpnSubsequentAddressFamily.class), createMPCapability(Ipv4AddressFamily.class, McastMplsLabeledVpnSubsequentAddressFamily.class), createMPCapability(Ipv6AddressFamily.class, McastMplsLabeledVpnSubsequentAddressFamily.class), createMPCapability(L2vpnAddressFamily.class, EvpnSubsequentAddressFamily.class), createMPCapability(Ipv4AddressFamily.class, RouteTargetConstrainSubsequentAddressFamily.class), createAs4BytesMPCapability(arguments.getAs()));
if (arguments.getMultiPathSupport()) {
optCap.add(createAddPathCapability());
}
final BgpParameters bgpParameters = createBgpParameters(optCap);
final InetSocketAddress localAddress = arguments.getLocalAddresses();
final int port = localAddress.getPort();
InetAddress address = localAddress.getAddress();
int numberOfSpeakers = arguments.getSpeakerCount();
do {
final BGPSessionListener sessionListener = new TestingListener(arguments.getNumberOfPrefixes(), arguments.getExtendedCommunities(), arguments.getMultiPathSupport());
this.listeners.put(address.getHostAddress(), sessionListener);
createPeer(dispatcher, arguments, new InetSocketAddress(address, port), sessionListener, bgpParameters);
numberOfSpeakers--;
address = InetAddresses.increment(address);
} while (numberOfSpeakers > 0);
}
Aggregations