Add sonic validate command for YANG-based config_db.json validation#2191
Open
Add sonic validate command for YANG-based config_db.json validation#2191
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="osism/commands/sonic.py" line_range="1353-1354" />
<code_context>
+ f"(supported: {', '.join(SUPPORTED_HWSKUS)})."
+ )
+ return None
+ config_version = None
+ if device.custom_fields.get("sonic_parameters", {}).get("config_version"):
+ config_version = device.custom_fields["sonic_parameters"]["config_version"]
+ return generate_sonic_config(device, hwsku, None, config_version)
</code_context>
<issue_to_address>
**issue (bug_risk):** Access to device.custom_fields can raise AttributeError when the attribute is missing.
In `_config_from_generate`, this block assumes `device.custom_fields` always exists:
```python
config_version = None
if device.custom_fields.get("sonic_parameters", {}).get("config_version"):
config_version = device.custom_fields["sonic_parameters"]["config_version"]
```
If a device lacks `custom_fields`, this will raise `AttributeError` instead of using your existing guarded logic. Please either reuse the earlier `hasattr(device, "custom_fields")` check here, or factor out something like `sonic_params = getattr(device, "custom_fields", {}).get("sonic_parameters", {})` and use it for both `hwsku` and `config_version` to keep error handling consistent.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Comment on lines
+1353
to
+1354
| config_version = None | ||
| if device.custom_fields.get("sonic_parameters", {}).get("config_version"): |
There was a problem hiding this comment.
issue (bug_risk): Access to device.custom_fields can raise AttributeError when the attribute is missing.
In _config_from_generate, this block assumes device.custom_fields always exists:
config_version = None
if device.custom_fields.get("sonic_parameters", {}).get("config_version"):
config_version = device.custom_fields["sonic_parameters"]["config_version"]If a device lacks custom_fields, this will raise AttributeError instead of using your existing guarded logic. Please either reuse the earlier hasattr(device, "custom_fields") check here, or factor out something like sonic_params = getattr(device, "custom_fields", {}).get("sonic_parameters", {}) and use it for both hwsku and config_version to keep error handling consistent.
bf3accc to
9f0a1f6
Compare
Validates generated SONiC ConfigDB JSON against the bundled SONiC YANG models so config drift, type errors and broken leafrefs are caught before configs are pushed to switches. Sources can be a local file, NetBox local_context, the export directory or a fresh in-memory generation. Pulls in sonic-yang-mgmt as a VCS install (not on PyPI), pinned to sonic-buildimage release branch 202511. Renders the three previously missing YANG models (sonic-extension, sonic-types, sonic-policer) from their upstream Jinja templates so the bundled model set is loadable by libyang. AI-assisted: Claude Code Signed-off-by: Christian Berendt <berendt@osism.tech>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Validates generated SONiC ConfigDB JSON against the bundled SONiC YANG models so config drift, type errors and broken leafrefs are caught before configs are pushed to switches. Sources can be a local file, NetBox local_context, the export directory or a fresh in-memory generation.
Pulls in sonic-yang-mgmt as a VCS install (not on PyPI), pinned to sonic-buildimage release branch 202511. Renders the three previously missing YANG models (sonic-extension, sonic-types, sonic-policer) from their upstream Jinja templates so the bundled model set is loadable by libyang.
AI-assisted: Claude Code