Compare VLAN sets by device and interface
Produce a reviewable VLAN difference without inventing changes for missing observations.
VLAN lists are sets under this policy
For this exported port policy, VLAN order and repeated entries have no meaning. Convert each validated list to a set before comparing it. A text comparison would incorrectly report a change when the same VLANs appear in a different order.
An expected-minus-observed difference describes additions; observed-minus-expected describes removals. Sort the resulting integers numerically. Keep VLAN 10 distinct from VLAN 100 rather than matching their textual prefixes.
Missing observation is a review item
Only desired ports are in scope. An extra observed interface is not automatically a problem. If a desired port has no observation, return a review action with empty add/remove lists: there is not enough evidence to calculate a safe change. A known empty VLAN list is different from a missing port record.
Validate every list and pair identity in both inputs before returning a plan. The teaching policy accepts whole VLAN integers from 1 through 4094; Boolean values are not VLAN IDs even though Python treats bool as an integer subclass. Duplicate device/interface pairs are ambiguous and fail validation.
The output describes intended differences only. It cannot apply a configuration, determine trunk/access semantics for a particular vendor, or prove that a remote device will accept the change. Those decisions require a separate environment and adapter.
observed = {10, 100}
desired = {10, 20}
print("Add:", sorted(desired - observed))
print("Remove:", sorted(observed - desired))
Put it into practice.
Define vlan_drift(observed, desired). Rows contain device, interface, vlans. Normalize identity with strip().casefold(); reject empty/duplicate pairs or non-list VLANs / values outside integer 1..4094 (including bool) with ValueError. Treat VLAN lists as sets. For desired ports only, omit equal sets; known differences return device, interface, action:"change", add, remove with numeric sorted differences. Missing observed port returns action:"review" and empty lists. Sort by device/interface and preserve inputs.
See an explained solution
def vlan_drift(observed, desired):
def index(rows):
result = {}
for row in rows:
key = (row["device"].strip().casefold(), row["interface"].strip().casefold())
values = row["vlans"]
if not all(key) or key in result or not isinstance(values,list) or any(type(v) is not int or not 1 <= v <= 4094 for v in values):
raise ValueError("Invalid or duplicate port policy")
result[key] = set(values)
return result
current, policy = index(observed), index(desired)
result = []
for (device, interface), expected in sorted(policy.items()):
actual = current.get((device,interface))
if actual is None:
result.append({"device":device,"interface":interface,"action":"review","add":[],"remove":[]})
elif actual != expected:
result.append({"device":device,"interface":interface,"action":"change","add":sorted(expected-actual),"remove":sorted(actual-expected)})
return result
if __name__ == "__main__":
print(vlan_drift([{ "device":"edge-a", "interface":"wan0", "vlans":[10,100]}], [{"device":"edge-a", "interface":"wan0", "vlans":[10,20]}]))
The plan ignores irrelevant ordering and duplicate VLAN entries, yet preserves the important difference between an empty observed set and unavailable evidence.