Skip to content

Remove direct dependency on libyang#4118

Open
bhouse-nexthop wants to merge 1 commit intosonic-net:masterfrom
bhouse-nexthop:bhouse.libyang3
Open

Remove direct dependency on libyang#4118
bhouse-nexthop wants to merge 1 commit intosonic-net:masterfrom
bhouse-nexthop:bhouse.libyang3

Conversation

@bhouse-nexthop
Copy link
Copy Markdown
Contributor

@bhouse-nexthop bhouse-nexthop commented Nov 4, 2025

What I did

sonic-utilities should never call directly into libyang. All functionality it needs should be provided in sonic-yang-mgmt. This resolves issues when libyang changes API/ABI such as happened between libyang1 and libyang2, and again between libyang2 and libyang3.

Depends on sonic-net/sonic-buildimage#24414 and sonic-net/sonic-buildimage#26563
Fixes sonic-net/sonic-buildimage#22385

How I did it

Removed calls to libyang and instead uses new functionality introduced in sonic-yang-mgmt.

Also installs both libyang1 and libyang3 for tests to maintain compatibility with both during the migration.

How to verify it

After sonic-net/sonic-buildimage#24414 merges observe test cases still pass.

Previous command output (if the output of a command-line utility has changed)

N/A

New command output (if the output of a command-line utility has changed)

N/A

@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).

@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).

@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@bhouse-nexthop
Copy link
Copy Markdown
Contributor Author

forced a rebuild, hoping it pulls the updated assets from the merge of sonic-net/sonic-buildimage#24414 so it passes

mssonicbld added a commit to mssonicbld/sonic-buildimage-msft that referenced this pull request Jan 22, 2026
#### Why I did it

This is needed by sonic-utilities to prevent a direct dependency on libyang.  By adding these helpers, sonic-utilities will use only what is provided by sonic-yang-mgmt and not be dependent on the version of libyang installed.

#### How I did it

Added helpers to:
- Load a yang module based on the provided string and return the loaded module name.
- Recursively locate dependencies for a given yang path

#### How to verify it

See included test cases.

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

N/A

#### Tested branch (Please provide the tested image version)

master as of 2025-11-03

#### Description for the changelog

sonic-yang-mgmt: helpers to prevent dependents from calling into libyang

#### Link to config_db schema for YANG module changes
N/A

#### A picture of a cute animal (not mandatory but encouraged)

Fixes #22385
Needed by sonic-net/sonic-utilities#4118
r12f pushed a commit to Azure/sonic-buildimage-msft that referenced this pull request Jan 23, 2026
…om calling into libyang (#1951)

#### Why I did it

This is needed by sonic-utilities to prevent a direct dependency on
libyang. By adding these helpers, sonic-utilities will use only what is
provided by sonic-yang-mgmt and not be dependent on the version of
libyang installed.

#### How I did it

Added helpers to:
- Load a yang module based on the provided string and return the loaded
module name.
- Recursively locate dependencies for a given yang path

#### How to verify it

See included test cases.

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

N/A

#### Tested branch (Please provide the tested image version)

master as of 2025-11-03

#### Description for the changelog

sonic-yang-mgmt: helpers to prevent dependents from calling into libyang

#### Link to config_db schema for YANG module changes
N/A

#### A picture of a cute animal (not mandatory but encouraged)

Fixes #22385
Needed by sonic-net/sonic-utilities#4118
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Comment thread config/config_mgmt.py
qiluo-msft pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Apr 16, 2026
…olation (#26563)

Why I did it
NOTE: This is part of the libyang3 enablement series. These fixes are needed to be able to merge sonic-net/sonic-utilities#4118 to remove direct dependency on libyang from sonic-utilities

Fix uses clause compilation to handle all YANG node types in groupings. Previously, _preProcessYangGrouping only captured leaf, leaf-list, and choice nodes from groupings, silently dropping container, list, and uses children. Similarly, _compileUsesClause only merged those three types. Both now handle all six node types (leaf, leaf-list, choice, container, list, uses). Also add "notification" to the node types traversed by _compileUsesClauseModel so uses clauses within notification nodes are compiled.

Fix sonic-cfg-help parse_leaf to handle union types with a single member. When xmltodict converts a YANG union with one type, it produces a dict instead of a list. The old code assumed a list and would iterate dict keys, producing incorrect results or crashing.

Work item tracking
Microsoft ADO (number only):
How I did it
Rewrite find_data_dependencies() to properly handle container, list, and global "/" xpaths by recursing into data tree descendants via tree_dfs() while preserving per-leaf value matching against backlink targets. The previous implementation lost value matching when called on non-leaf xpaths, causing incorrect dependency results (e.g., returning refs for unrelated ports when querying a specific port's container).

The rewrite splits the logic into helper methods:

_find_data_dependencies_node(): recurses via tree_dfs(), extracts leaf values, finds schema backlinks, and resolves them against the data tree with value filtering
_find_data_dependencies_global(): iterates top-level data nodes via tree_for() and delegates to _find_data_dependencies_node()
_resolve_backlink_data(): matches backlink schema paths against actual data nodes, filtering by the originating leaf's value
Remove the unused error_log_level parameter from loadData(). The parameter was never meaningfully used and added unnecessary API surface.

Restructure tests into independent classes with isolated fixtures:

Test_SonicYang_Loading: class-scoped fixtures for the progressive load/merge/validate pipeline where tests build on prior state
Test_SonicYang: function-scoped fixtures that create a fresh SonicYang instance per test, ensuring each test starts from a clean data tree rather than depending on mutations from prior tests
Test_SonicYang_UsesCompilation: validates grouping preprocessing captures container/list/uses nodes, uses clause compilation merges them correctly, and notification nodes are traversed
Previously all tests shared a single class-scoped SonicYang instance, meaning test_add_node, test_delete_node, and test_set_datanode_value permanently modified the data tree for all subsequent tests. This coupling made it impossible to run tests individually or reorder them without failures, and masked bugs where test expectations depended on accumulated state rather than the config data file.

How to verify it
Run test suite with the changes made in this PR:

Add regression test cases for find_data_dependencies:

List entry xpath (PORT_LIST without leaf suffix) returns the same dependencies as querying the specific leaf
ACL_TABLE_LIST entry returns only ACL_RULE refs matching that specific table name
Fix ACL_TABLE container and global "/" test expectations to only include refs matching existing ACL_TABLE entries
Add test-grouping.yang model and tests exercising uses compilation with container, list, nested uses, and notification nodes. Add unit tests for sonic-cfg-help union type handling covering single-type, multi-type, and non-leafref union members.
@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).

@bhouse-nexthop
Copy link
Copy Markdown
Contributor Author

/azpw run

@mssonicbld
Copy link
Copy Markdown
Collaborator

⚠️ Notice: /azpw run only runs failed jobs now. If you want to trigger a whole pipline run, please rebase your branch or close and reopen the PR.
💡 Tip: You can also use /azpw retry to retry failed jobs directly.

Retrying failed(or canceled) jobs...

sonic-utilities should never call directly into libyang.  All
functionality it needs should be provided in sonic-yang-mgmt.  This
resolves issues when libyang changes API/ABI such as happened
between libyang1 and libyang2, and again between libyang2 and libyang3.

Depends on sonic-net/sonic-buildimage#24414

Signed-off-by: Brad House <[email protected]>
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@bhouse-nexthop
Copy link
Copy Markdown
Contributor Author

@saiarcot895 @qiluo-msft this is the next libyang3 blocker

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.

Enhancement: Upgrade to libyang3

3 participants