To solve this problem using PyBGPStream, we need to:
- Parse each RIB file snapshot.
- For each snapshot, collect the set of unique prefixes advertised by each origin AS.
- Track the first and last snapshot where each AS appears, and the corresponding prefix sets.
- Compute the percentage growth in the number of unique prefixes from the first to the last appearance.
- Sort the ASes by growth and return the top 10 with the largest growth, sorted from smallest to largest.
Here is the implementation of the function top_10_ases_by_prefix_growth():
from _pybgpstream import BGPStream, BGPRecord, BGPElement
from collections import defaultdict
from typing import List, Tuple
def top_10_ases_by_prefix_growth(rib_file_paths: List[str]) -> List[Tuple[int, float]]:
"""
Given a list of RIB file paths (chronologically sorted), compute the top 10 ASes
with the largest percentage growth in advertised prefixes from their first to last appearance.
Returns:
A list of tuples (asn, percentage_growth), sorted from smallest to largest growth.
"""
# Dictionary to store prefix sets per AS per snapshot index
as_prefixes = defaultdict(lambda: dict()) # asn -> {snapshot_index: set(prefixes)}
for snapshot_index, rib_file in enumerate(rib_file_paths):
stream = BGPStream()
stream.set_data_interface("singlefile")
stream.set_data_interface_option("singlefile", "rib-file", rib_file)
stream.start()
for rec in stream:
if rec.status != "valid":
continue
for elem in rec:
if elem.type != "R":
continue
prefix = elem.fields.get("prefix")
origin_ases = elem.fields.get("as-path", "").split()
if not origin_ases:
continue
origin_as = int(origin_ases[-1])
if snapshot_index not in as_prefixes[origin_as]:
as_prefixes[origin_as][snapshot_index] = set()
as_prefixes[origin_as][snapshot_index].add(prefix)
# Compute growth per AS
growths = []
for asn, snapshots in as_prefixes.items():
sorted_snapshots = sorted(snapshots.keys())
if len(sorted_snapshots) < 2:
continue # Need at least two snapshots to compute growth
first_idx = sorted_snapshots[0]
last_idx = sorted_snapshots[-1]
first_prefixes = snapshots[first_idx]
last_prefixes = snapshots[last_idx]
first_count = len(first_prefixes)
last_count = len(last_prefixes)
if first_count == 0:
continue # Avoid division by zero
growth = ((last_count - first_count) / first_count) * 100
growths.append((asn, growth))
# Get top 10 ASes by growth, sorted from smallest to largest
top_10 = sorted(growths, key=lambda x: -x[1])[:10]
return sorted(top_10, key=lambda x: x[1])
Explanation:
- We use a defaultdict to store, for each AS, a mapping from snapshot index to the set of prefixes it advertised in that snapshot.
- For each RIB file, we parse the BGP elements and extract the origin AS (last AS in the AS path) and the prefix.
- After collecting all data, we compute the percentage growth in prefix count for each AS from its first to last appearance.
- We sort the ASes by growth descending, take the top 10, and then sort those 10 from smallest to largest growth.
Note: This function assumes that rib_file_paths is a list of valid RIB file paths sorted chronologically.