Skip to content

Moved create-only to YANG - #4733

Open
bradleymoon-nexthop wants to merge 4 commits into
sonic-net:masterfrom
nexthop-ai:moved-create-only-to-yang
Open

Moved create-only to YANG#4733
bradleymoon-nexthop wants to merge 4 commits into
sonic-net:masterfrom
nexthop-ai:moved-create-only-to-yang

Conversation

@bradleymoon-nexthop

@bradleymoon-nexthop bradleymoon-nexthop commented Jul 30, 2026

Copy link
Copy Markdown

PR Description: Move create-only Fields to YANG Models

Why I did it

Currently, the CreateOnlyFilter inside generic_config_updater/patch_sorter.py utilizes a static, hardcoded list of lists (self.patterns) to identify which table fields are immutable (create-only). This makes the system rigid, difficult to maintain, and divorces schema properties from the validation layer as new tables or attributes are added to SONiC.

To solve this, this PR refactors CreateOnlyFilter to dynamically discover create-only annotations directly from the compiled YANG models using the custom ext:create-only YANG extension introduced in sonic-net/sonic-mgmt-common/pull/234

How I did it

  • De-coupled direct libyang dependencies: Avoided direct imports of libyang inside sonic-utilities (conforming to .semgrep/no-direct-libyang.yml isolation guidelines). Instead, delegated all recursive schema-walking logic to the sonic_yang module through the new sy.get_create_only_fields() accessor.
  • Transition Floor & Backward Compatibility:
    • Retained the original 26 hardcoded patterns as _CREATE_ONLY_FIELDS_FALLBACK to act as a fallback floor.
    • Rather than completely replacing the fallback, the YANG-discovered patterns are unioned with _CREATE_ONLY_FIELDS_FALLBACK. This ensures that if the system has an incomplete/partially annotated model set (e.g., during separate backports or transition windows on release branches), the baseline create-only protection remains active while logging a warning for any missing annotations.
    • Added an AttributeError catch block in _discover_create_only_fields to safely handle environments where config_wrapper is None (such as bare CLI contexts or unit test runs), defaulting cleanly to the fallback list.
    • Added in-place de-duplication of the final patterns to prevent duplicate runs across duplicate path matches.
  • Performance Optimization: Memoized the discovery logic and shared a single PathAddressing instance across the three create-only filtering classes. This ensures that the schema subtree is walked once per process rather than multiple times per patch operation, avoiding FFI/libyang traversal overhead.

How to verify it

  • Run the updated and renamed GCU unit tests:
    pytest tests/generic_config_updater/patch_sorter_test.py
  • Verified that the test suite exercises the following scenarios:
    1. Parity validation: Confirming the fallback list and YANG-derived fields match expected create-only schemas.
    2. Spec-validated Mock testing: Ensuring the spec=[] mock exercises the fallback-only path safely when get_create_only_fields() is unavailable on-box during the migration window.
    3. No-accessor transition check: Asserting correct fallback engagement without crash when sonic_yang is initialized but the model package hasn't been upgraded.

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 30, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: bradleymoon-nexthop / name: Bradley Moon (501c037)
  • ✅ login: bradleymoon-nexthop / name: bradleymoon-nexthop (937a9cc)

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@bradleymoon-nexthop
bradleymoon-nexthop force-pushed the moved-create-only-to-yang branch from 3080294 to 6a2c2b5 Compare July 30, 2026 17:43
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@bradleymoon-nexthop
bradleymoon-nexthop force-pushed the moved-create-only-to-yang branch from 6a2c2b5 to 501c037 Compare July 30, 2026 17:46
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@bradleymoon-nexthop
bradleymoon-nexthop force-pushed the moved-create-only-to-yang branch from 32dac5a to 5701059 Compare July 30, 2026 17:51
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@bradleymoon-nexthop
bradleymoon-nexthop marked this pull request as ready for review July 30, 2026 17:52
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

Signed-off-by: bradleymoon-nexthop <bradleymoon@nexthop.ai>
Signed-off-by: Bradley Moon  <bradleymoon@nexthop.ai>
Keep YANG create-only discovery with the fallback floor, and drop the
unused private-only GCU_FIELD_OP_CONF_FILE constant from the public port.

Signed-off-by: Bradley Moon  <bradleymoon@nexthop.ai>
Signed-off-by: Bradley Moon  <bradleymoon@nexthop.ai>
Signed-off-by: Bradley Moon  <bradleymoon@nexthop.ai>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@bradleymoon-nexthop
bradleymoon-nexthop force-pushed the moved-create-only-to-yang branch from ec5a12d to 99c7b88 Compare July 30, 2026 17:57
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks!

---Powered by SONiC BuildBot

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants