Skip to content

Add sonic validate command for YANG-based config_db.json validation#2191

Open
berendt wants to merge 1 commit intomainfrom
sonic-validate
Open

Add sonic validate command for YANG-based config_db.json validation#2191
berendt wants to merge 1 commit intomainfrom
sonic-validate

Conversation

@berendt
Copy link
Copy Markdown
Member

@berendt berendt commented Apr 24, 2026

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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread osism/commands/sonic.py
Comment on lines +1353 to +1354
config_version = None
if device.custom_fields.get("sonic_parameters", {}).get("config_version"):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@berendt berendt force-pushed the sonic-validate branch 3 times, most recently from bf3accc to 9f0a1f6 Compare April 24, 2026 11:25
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>
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.

1 participant