You are to use PyBGPStream. You are given a chronologically sorted list of absolute (also called "fully qualified") path names. These are RIB files, so you will pass “rib-file" in your call to set_data_interface_option(). Using the data from the cache files, calculate the percentage growth in advertised prefixes for each AS over the entire timespan
represented by the snapshots by completing the function top_10_ases_by_prefix_growth(). An AS is not guaranteed to appear in every snapshot, nor it is guaranteed to
appear in the first and last snapshots.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you implement the top_10_ases_by_prefix_growth() function using PyBGPStream to analyze RIB files and calculate the percentage growth in advertised prefixes for each AS.
Here's the implementation:
python
import pybgpstream
from collections import defaultdict
def top_10_ases_by_prefix_growth(rib_files):
"""
Calculate the percentage growth in advertised prefixes for each AS over the timespan
represented by the RIB files.
Args:
rib_files: A chronologically sorted list of absolute path names to RIB files
Returns:
A list of tuples (asn, growth_percentage) for the top 10 ASes with the highest
percentage growth, sorted in descending order of growth percentage
"""
# Dictionary to store prefixes advertised by each AS in each snapshot
as_prefixes_by_snapshot = []
# Process each RIB file
for rib_file in rib_files:
# Create a new stream
stream = pybgpstream.BGPStream()
# Configure the stream to use the RIB file
stream.set_data_interface_option("singlefile", "rib-file", rib_file)
# Dictionary to store prefixes for each AS in this snapshot
current_snapshot_prefixes = defaultdict(set)
# Process records in the stream
for record in stream:
for elem in record:
# Get the AS path
as_path = elem.fields["as-path"].split(" ")
# Skip if AS path is empty
if not as_path or as_path[0] == "":
continue
# Get the origin AS (last AS in the path)
origin_as = as_path[-1]
# Try to convert to integer (skip if not a valid AS number)
try:
origin_as = int(origin_as)
except ValueError:
continue
# Get the prefix
prefix = elem.fields["prefix"]
# Add the prefix to the set of prefixes for this AS
current_snapshot_prefixes[origin_as].add(prefix)
# Add the current snapshot's data to our list
as_prefixes_by_snapshot.append(current_snapshot_prefixes)
# Calculate growth for each AS
as_growth = {}
# Get all unique ASes across all snapshots
all_ases = set()
for snapshot in as_prefixes_by_snapshot:
all_ases.update(snapshot.keys())
# For each AS, find its first and last appearance and calculate growth
for asn in all_ases:
first_count = None
last_count = None
# Find first appearance
for snapshot in as_prefixes_by_snapshot:
if asn in snapshot and first_count is None:
first_count = len(snapshot[asn])
# Find last appearance
for snapshot in reversed(as_prefixes_by_snapshot):
if asn in snapshot and last_count is None:
last_count = len(snapshot[asn])
# Calculate growth percentage if both first and last counts exist
if first_count is not None and last_count is not None and first_count > 0:
growth_percentage = ((last_count - first_count) / first_count) * 100
as_growth[asn] = growth_percentage
# Sort ASes by growth percentage in descending order and take top 10
top_10 = sorted(as_growth.items(), key=lambda x: x[1], reverse=True)[:10]
return top_10
This implementation:
Processes each RIB file using PyBGPStream
For each snapshot, collects the set of prefixes advertised by each AS
Calculates the percentage growth for each AS by comparing its first and last appearance in the snapshots
Returns the top 10 ASes with the highest percentage growth
The function handles cases where:
An AS might not appear in every snapshot
An AS might not appear in the first or last snapshot
The AS path might be empty or invalid
The growth percentage is calculated as: ((last_count - first_count) / first_count) * 100, where first_count and last_count are the number of prefixes advertised by the AS in its first and last appearance, respectively.