Skip to content

[PrefixListMgr]: Refactor PrefixListMgr to support multiple prefix types via config registry#26937

Merged
StormLiangMS merged 13 commits into
sonic-net:masterfrom
shixizhang:dev/shixz/enhanceprefixlist
May 7, 2026
Merged

[PrefixListMgr]: Refactor PrefixListMgr to support multiple prefix types via config registry#26937
StormLiangMS merged 13 commits into
sonic-net:masterfrom
shixizhang:dev/shixz/enhanceprefixlist

Conversation

@shixizhang
Copy link
Copy Markdown
Contributor

@shixizhang shixizhang commented Apr 22, 2026

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
  • Microsoft ADO (number only): 37638053

How I did it

Registry refactor (ANCHOR_PREFIX behavior unchanged):

  • Added PREFIX_TYPE_CONFIG registry mapping each prefix type to its templates, allowed device types, prefix-list naming lambda, and log labels
  • Added _is_device_allowed() helper using (type, subtype) tuple matching, with None = unrestricted
  • Refactored generate_prefix_list_config to use registry lookup instead of hardcoded checks
  • Renamed handler local prefix_list_nameprefix_type to avoid confusion with data["prefix_list_name"]
  • Used metadata.get("subtype", "") to handle devices without subtype

SUPPRESS_PREFIX type (new):

  • Added SUPPRESS_PREFIX registry entry with allowed_devices: None (any device)
  • Added add_suppress_prefix.conf.j2 / del_suppress_prefix.conf.j2 templates
  • In CLI: replaced global check_spine_router with per-type validate_device_for_type

constants.yml-driven prefix-list name resolution:

  • Prefix-list names resolved from constants.yml (bgp.prefix_list.<type>.ipv4_name / ipv6_name) with registry lambda as fallback
  • Enables downstream repos to override names via constants.yml alone

Backward-compatibility:

  • All 5 existing ANCHOR_PREFIX unit tests pass unchanged
  • PrefixListMgr registered on all devices — ANCHOR_PREFIX on non-spine devices logs a warning (no FRR state change; doesn't occur in practice)
  • CLI status now allowed on any device (read-only)

How to verify it

  • All 15 unit tests pass (5 existing ANCHOR_PREFIX + 2 registry validation + 5 SUPPRESS_PREFIX + 3 constants override/fallback)
  • ANCHOR_PREFIX behavior identical before and after

Which release branch to backport (provide reason below if selected)

  • 202305
  • 202311
  • 202405
  • 202411
  • 202505
  • 202511
  • 202603

Tested branch (Please provide the tested image version)

  • SONiC.20251110.19

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)

枣

…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>
Copilot AI review requested due to automatic review settings April 22, 2026 06:50
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_CONFIG registry and refactors PrefixListMgr to use it for templates, device allowlisting, and log labeling.
  • Updates prefix_list CLI 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.

Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py Outdated
Comment thread dockers/docker-fpm-frr/base_image_files/prefix_list Outdated
Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py Outdated
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@shixizhang shixizhang changed the title [bgpcfgd]: Refactor PrefixListMgr to support multiple prefix types via config registry [PrefixListMgr]: Refactor PrefixListMgr to support multiple prefix types via config registry Apr 22, 2026
- 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>
Copilot AI review requested due to automatic review settings April 22, 2026 07:29
@shixizhang shixizhang force-pushed the dev/shixz/enhanceprefixlist branch from 5c36cae to 8b72f3e Compare April 22, 2026 07:29
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_CONFIG registry 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-list CLI 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.

Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py
Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py
Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py Outdated
…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>
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copilot AI review requested due to automatic review settings April 26, 2026 02:35
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Apr 26, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_CONFIG registry and refactors PrefixListMgr to select templates/device allowlists/naming via registry lookup.
  • Adds SUPPRESS_PREFIX support end-to-end (templates, CLI script support, constants, and unit tests).
  • Registers PrefixListMgr unconditionally in bgpcfgd startup, 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.

Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py
Comment thread src/sonic-bgpcfgd/bgpcfgd/managers_prefix_list.py Outdated
Comment thread src/sonic-bgpcfgd/bgpcfgd/main.py Outdated
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copilot AI review requested due to automatic review settings April 26, 2026 02:54
Copy link
Copy Markdown
Contributor

@StormLiangMS StormLiangMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @deepak-singhal0408 could you help to take a look?

@mssonicbld
Copy link
Copy Markdown
Collaborator

Cherry-pick PR to msft-202603: Azure/sonic-buildimage-msft#2264

@vmittal-msft
Copy link
Copy Markdown
Contributor

@deepak-singhal0408 do we need this for 202511 ? is it must have ?

@deepak-singhal0408
Copy link
Copy Markdown
Contributor

@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,

@mssonicbld
Copy link
Copy Markdown
Collaborator

Cherry-pick PR to msft-202603: Azure/sonic-buildimage-msft#2280

@mssonicbld
Copy link
Copy Markdown
Collaborator

Cherry-pick PR to 202505: #27333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants