Find overlapping allocations in the right context
Compare network ranges within a VRF while keeping separate routing contexts independent.
An address needs its routing context
The same private address range can legitimately appear in separate routing contexts. A Virtual Routing and Forwarding instance, or VRF, keeps routing tables separate. This exercise compares allocations only when their normalized VRF labels match. An overlap across two different labels is not automatically an error in this exported plan.
Parse prefixes as networks with strict=True. Unlike the previous descriptive lesson, this allocation policy rejects host bits rather than silently changing what someone requested. Compare only networks of the same IP version.
Compare ranges, not strings
A /25 can overlap a /24 even though the text differs. Conversely, similar-looking prefixes can be disjoint. Network objects expose overlaps() to answer the range question. Consider every unordered pair once, including exact duplicates represented by different allocation IDs.
Allocation IDs are unique after stripping and casefolding. Duplicate IDs make evidence ambiguous and must raise an error before a report is returned. Normalize the VRF labels using the same rule, and leave the original input untouched.
Order the two IDs within each pair, then sort the final report by VRF and IDs. The result is a review list for an address plan. It does not prove that traffic currently conflicts or that the exported VRF labels reflect a live device's configuration.
from ipaddress import ip_network
print(ip_network("192.0.2.0/24").overlaps(ip_network("192.0.2.128/25")))
print(ip_network("192.0.2.0/25").overlaps(ip_network("192.0.2.128/25")))
Put it into practice.
Define allocation_conflicts(rows). Input rows have string id, vrf, prefix. Normalize id and vrf with strip().casefold(); empty labels, duplicate IDs, zone IDs and invalid/non-network prefixes raise ValueError. Parse prefixes strictly. Return one {vrf, left, right} row for each overlapping unordered pair in the same VRF and IP family, including identical ranges. left/right are normalized IDs in lexical order. Sort results by vrf, left, right and preserve inputs.
See an explained solution
from ipaddress import ip_network
from itertools import combinations
def allocation_conflicts(rows):
parsed, seen, result = [], set(), []
for row in rows:
identifier, vrf = row["id"].strip().casefold(), row["vrf"].strip().casefold()
if not identifier or not vrf or identifier in seen or "%" in row["prefix"]:
raise ValueError("Invalid or duplicate allocation")
network = ip_network(row["prefix"].strip(), strict=True)
seen.add(identifier)
parsed.append((identifier, vrf, network))
for left, right in combinations(sorted(parsed), 2):
if left[1] == right[1] and left[2].version == right[2].version and left[2].overlaps(right[2]):
result.append({"vrf":left[1], "left":left[0], "right":right[0]})
return sorted(result,key=lambda row:(row["vrf"],row["left"],row["right"]))
if __name__ == "__main__":
print(allocation_conflicts([{ "id":"edge", "vrf":"blue", "prefix":"192.0.2.0/24" }, {"id":"branch", "vrf":"blue", "prefix":"192.0.2.128/25"}]))
Network comparisons detect containment and equality, while the VRF and address-family keys keep unrelated allocations separate.