[PrefixListMgr]: Refactor PrefixListMgr to support multiple prefix types via config registry#26937
Conversation
…a config registry
Currently PrefixListMgr only supports ANCHOR_PREFIX with hardcoded
device-type checks and template references. This makes it impossible
for downstream forks to add new prefix types without modifying core
logic.
Refactor PrefixListMgr to use a data-driven PREFIX_TYPE_CONFIG registry
that maps each prefix type to its templates, allowed devices, prefix-list
naming, and log labels. This is a purely structural refactor — ANCHOR_PREFIX
behavior, log messages, device gating, and template rendering remain
functionally identical.
Changes:
- Add PREFIX_TYPE_CONFIG registry with ANCHOR_PREFIX as the sole entry
- Refactor __init__ to load templates dynamically from registry
- Refactor generate_prefix_list_config to be data-driven via registry
lookup instead of hardcoded ANCHOR_PREFIX checks
- Add _is_device_allowed() helper for unified device-type validation
- Rename handler local variable prefix_list_name to prefix_type to
avoid confusion with data["prefix_list_name"] (the FRR prefix-list
name set by the registry lambda)
- Use metadata.get("subtype", "") instead of metadata["subtype"] to
handle devices without subtype field
- Rename check_spine_router to check_supported_device in prefix_list
CLI script and add validate_device_for_type() with per-type device
validation via prefix_type_devices associative array
- Add read_device_metadata() with caching to avoid duplicate
sonic-cfggen calls
- Add 2 new unit tests: test_unsupported_prefix_type and
test_anchor_prefix_wrong_device
- All 5 existing ANCHOR_PREFIX unit tests pass unchanged
Signed-off-by: Shixi Zhang <shixz@microsoft.com>
|
/azp run Azure.sonic-buildimage |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
Refactors bgpcfgd’s PrefixListMgr to move prefix-list generation and device gating from hardcoded logic to a registry-driven configuration, with corresponding updates to the prefix_list CLI helper and unit tests.
Changes:
- Introduces a
PREFIX_TYPE_CONFIGregistry and refactorsPrefixListMgrto use it for templates, device allowlisting, and log labeling. - Updates
prefix_listCLI script to add cached device metadata reads and per-prefix-type device validation. - Extends unit tests to cover unsupported prefix types and unsupported device types for
ANCHOR_PREFIX.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py |
Adds registry-driven prefix type configuration and refactors config generation/device gating accordingly. |
src/sonic-bgpcfgd/tests/test_prefix_list.py |
Adds unit tests for unsupported prefix types and wrong-device scenarios. |
dockers/docker-fpm-frr/base_image_files/prefix_list |
Refactors device checking, adds cached metadata reads and per-prefix-type device validation. |
|
/azp run Azure.sonic-buildimage |
|
Azure Pipelines successfully started running 1 pipeline(s). |
- Move metadata["bgp_asn"] inside try/except KeyError block to gracefully log and return False if bgp_asn is missing, instead of raising an unhandled KeyError - Use PREFIX_TYPE_CONFIG.values() instead of .items() since the key is unused in the __init__ template loading loop Signed-off-by: Shixi Zhang <shixz@microsoft.com>
5c36cae to
8b72f3e
Compare
|
/azp run Azure.sonic-buildimage |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
Refactors PrefixListMgr in sonic-bgpcfgd to be driven by a per-prefix-type registry rather than hardcoded ANCHOR_PREFIX logic, and updates the prefix-list CLI/device validation plus unit tests accordingly.
Changes:
- Introduces
PREFIX_TYPE_CONFIGregistry and updates PrefixListMgr handlers to select templates/device-allowlists by prefix type. - Adds unit tests for unsupported prefix type and unsupported device gating.
- Refactors the
prefix-listCLI script to cache device metadata and validate per-prefix-type device support.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py | Moves prefix-list generation logic to a registry-driven model and unifies device gating. |
| src/sonic-bgpcfgd/tests/test_prefix_list.py | Adds coverage for unsupported prefix types and wrong-device handling. |
| dockers/docker-fpm-frr/base_image_files/prefix_list | Adds cached device metadata read and per-prefix-type device validation for add/remove. |
…itionally Add generic SUPPRESS_PREFIX prefix type to the PREFIX_TYPE_CONFIG registry, enabling downstream forks to dynamically manage prefix-lists via CONFIG_DB for route suppression use cases. Changes: - Add SUPPRESS_PREFIX entry to PREFIX_TYPE_CONFIG with unrestricted device access (allowed_devices: None), generic templates, and generic prefix-list naming lambda - Add None check in _is_device_allowed() to support unrestricted prefix types - Pass device_type and device_subtype to template data dict, enabling template-level conditional logic by device role - Register PrefixListMgr unconditionally in main.py — per-prefix-type device validation handled by _is_device_allowed in the registry - Remove check_supported_device global gate from prefix_list CLI, replaced by per-type validate_device_for_type with early return for unrestricted types - Add SUPPRESS_PREFIX to supported_prefix_types and prefix_type_devices in prefix_list CLI - Add generic add/del suppress_prefix Jinja2 templates - Fix sys.modules mock ordering in test_prefix_list.py - Add 5 new SUPPRESS_PREFIX unit tests (ipv4/ipv6 add/del + any-device) - Rename constructor_unsupported_device to constructor_generic_device using ToRRouter for broader test coverage - All 12 tests pass (5 existing ANCHOR_PREFIX + 7 new) Signed-off-by: Shixi Zhang <shixz@microsoft.com>
|
/azp run Azure.sonic-buildimage |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run Azure.sonic-buildimage |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
Refactors PrefixListMgr in sonic-bgpcfgd to be data-driven via a per-prefix-type registry, enabling support for multiple prefix types (including a new SUPPRESS_PREFIX) while keeping existing ANCHOR_PREFIX behavior intended to remain unchanged.
Changes:
- Introduces
PREFIX_TYPE_CONFIGregistry and refactorsPrefixListMgrto select templates/device allowlists/naming via registry lookup. - Adds
SUPPRESS_PREFIXsupport end-to-end (templates, CLI script support, constants, and unit tests). - Registers
PrefixListMgrunconditionally inbgpcfgdstartup, relying on per-type validation to gate behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py | Adds registry-driven prefix-type behavior and name override support. |
| src/sonic-bgpcfgd/bgpcfgd/main.py | Registers PrefixListMgr unconditionally and adjusts enablement logging. |
| dockers/docker-fpm-frr/base_image_files/prefix_list | Extends CLI to support SUPPRESS_PREFIX and moves to per-type device validation. |
| dockers/docker-fpm-frr/frr/bgpd/suppress_prefix/add_suppress_prefix.conf.j2 | New Jinja2 template for adding suppress prefix-list entries. |
| dockers/docker-fpm-frr/frr/bgpd/suppress_prefix/del_suppress_prefix.conf.j2 | New Jinja2 template for removing suppress prefix-list entries. |
| files/image_config/constants/constants.yml | Adds constants entries for prefix-list naming overrides. |
| src/sonic-bgpcfgd/tests/test_prefix_list.py | Adds unit tests for unsupported types, device gating, and SUPPRESS_PREFIX behavior. |
|
/azp run Azure.sonic-buildimage |
|
Azure Pipelines successfully started running 1 pipeline(s). |
StormLiangMS
left a comment
There was a problem hiding this comment.
LGTM. @deepak-singhal0408 could you help to take a look?
|
Cherry-pick PR to msft-202603: Azure/sonic-buildimage-msft#2264 |
|
@deepak-singhal0408 do we need this for 202511 ? is it must have ? |
yes @vmittal-msft.. this is for an issue seen in production.. the change is very pointed and should be safe, |
|
Cherry-pick PR to msft-202603: Azure/sonic-buildimage-msft#2280 |
|
Cherry-pick PR to 202505: #27333 |
Why I did it
Currently PrefixListMgr only supports ANCHOR_PREFIX with hardcoded device-type checks and template references. This refactor makes PrefixListMgr data-driven and extensible, and adds a generic SUPPRESS_PREFIX type for route suppression use cases.
Work item tracking
How I did it
Registry refactor (ANCHOR_PREFIX behavior unchanged):
PREFIX_TYPE_CONFIGregistry mapping each prefix type to its templates, allowed device types, prefix-list naming lambda, and log labels_is_device_allowed()helper using(type, subtype)tuple matching, withNone= unrestrictedgenerate_prefix_list_configto use registry lookup instead of hardcoded checksprefix_list_name→prefix_typeto avoid confusion withdata["prefix_list_name"]metadata.get("subtype", "")to handle devices without subtypeSUPPRESS_PREFIX type (new):
SUPPRESS_PREFIXregistry entry withallowed_devices: None(any device)add_suppress_prefix.conf.j2/del_suppress_prefix.conf.j2templatescheck_spine_routerwith per-typevalidate_device_for_typeconstants.yml-driven prefix-list name resolution:
constants.yml(bgp.prefix_list.<type>.ipv4_name/ipv6_name) with registry lambda as fallbackconstants.ymlaloneBackward-compatibility:
statusnow allowed on any device (read-only)How to verify it
Which release branch to backport (provide reason below if selected)
Tested branch (Please provide the tested image version)
Description for the changelog
Refactor PrefixListMgr to use a data-driven config registry with constants.yml-driven prefix-list name resolution, and add generic SUPPRESS_PREFIX type.
Link to config_db schema for YANG module changes
No YANG model changes. Uses existing PREFIX_LIST table:
Prefix list
A picture of a cute animal (not mandatory but encouraged)