Skip to content

[cvl] Enforce mandatory field validation for OP_CREATE operations#212

Open
Sumalatha-G-ML wants to merge 1 commit intosonic-net:masterfrom
Sumalatha-G-ML:fix-issue-2283-cvl-mandatory-validation
Open

[cvl] Enforce mandatory field validation for OP_CREATE operations#212
Sumalatha-G-ML wants to merge 1 commit intosonic-net:masterfrom
Sumalatha-G-ML:fix-issue-2283-cvl-mandatory-validation

Conversation

@Sumalatha-G-ML
Copy link
Copy Markdown

Issue: CVL's ValidateEditConfig() does not enforce YANG mandatory true fields on OP_CREATE operations, allowing invalid configurations to be written to CONFIG_DB.

Root Cause:
CVL performs mandatory field validation only for OP_DELETE operations but skips this critical check for OP_CREATE operations. This allows entries to be created without required mandatory fields defined in YANG models.

Solution:

  • Added checkMandatoryFieldsForCreate() function to validate mandatory fields
  • Function iterates through YANG leaf nodes and checks if mandatory fields exist
  • Returns CVL_SEMANTIC_ERROR with clear error message if mandatory field is missing
  • Called during OP_CREATE case in ValidateEditConfig()

Impact:

  • Prevents invalid configurations from being written to CONFIG_DB
  • Provides clear error messages indicating which mandatory field is missing
  • Consistent with existing OP_DELETE mandatory field validation
  • No hardware dependency - pure software validation

Testing:

  • CREATE operations with missing mandatory fields will now fail with proper error
  • CREATE operations with all mandatory fields will continue to work normally
  • Error includes table name, key, field name, and descriptive message

Fixes: sonic-net/SONiC#2283

Issue: CVL's ValidateEditConfig() does not enforce YANG mandatory true fields
on OP_CREATE operations, allowing invalid configurations to be written to
CONFIG_DB.

Root Cause:
CVL performs mandatory field validation only for OP_DELETE operations but
skips this critical check for OP_CREATE operations. This allows entries to
be created without required mandatory fields defined in YANG models.

Solution:
- Added checkMandatoryFieldsForCreate() function to validate mandatory fields
- Function iterates through YANG leaf nodes and checks if mandatory fields exist
- Returns CVL_SEMANTIC_ERROR with clear error message if mandatory field is missing
- Called during OP_CREATE case in ValidateEditConfig()

Impact:
- Prevents invalid configurations from being written to CONFIG_DB
- Provides clear error messages indicating which mandatory field is missing
- Consistent with existing OP_DELETE mandatory field validation
- No hardware dependency - pure software validation

Testing:
- CREATE operations with missing mandatory fields will now fail with proper error
- CREATE operations with all mandatory fields will continue to work normally
- Error includes table name, key, field name, and descriptive message

Fixes: sonic-net/SONiC#2283

Signed-off-by: Sumalatha G <[email protected]>
@mssonicbld
Copy link
Copy Markdown

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@MrSa3dola
Copy link
Copy Markdown

Hi, faraazbrcm replied to me with the following fix

"Mandatory validation is a syntax validation, CVL relies on libyang for this. Ygot does not support such validation.
Are you using latest version of code?. In Latest I see mandatory check is enabled"

And I added the following fix

grep -n 'lyd_check_mandatory_tree' /sonic/src/libyang/libyang/src/tree_data.c | head -5

If you see `int lyd_check_mandatory_tree` (no API prefix), then the patch is NOT applied.
You need to apply the fix to the source:

sed -i 's/^int$/API int/' /sonic/src/libyang/libyang/src/tree_data.c

Then verify:

grep -B1 'lyd_check_mandatory_tree' /sonic/src/libyang/libyang/src/tree_data.c | head -3

It must show `API int` on the line before `lyd_check_mandatory_tree`.

Apply the above fix right after the relevant comment in the source file.

I tried it, and it’s working now — it checks both the mandatory fields and the when conditions as well.

@Sumalatha-G-ML
Copy link
Copy Markdown
Author

Thank you for sharing @faraazbrcm's feedback about using libyang's lyd_check_mandatory_tree instead.

But I have below questions

  1. Does the libyang fix already exist in master branch?
  2. Should I close this PR if the issue is already fixed?
  3. Or should I update this PR to apply the libyang patch instead of CVL code changes?

@faraazbrcm - Which approach do you prefer?

@MrSa3dola
Copy link
Copy Markdown

MrSa3dola commented Apr 16, 2026

@Sumalatha-G-ML You can mention your issue in the main repo here for faster reply.

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.

CVL does not enforce YANG mandatory field validation on OP_CREATE (sonic-mgmt-common #210)

3 participants