Skip to content

Validation rule checking if access port is associated with just single network#690

Open
mkilar123 wants to merge 5 commits intonetascode:developfrom
mkilar123:validate_access
Open

Validation rule checking if access port is associated with just single network#690
mkilar123 wants to merge 5 commits intonetascode:developfrom
mkilar123:validate_access

Conversation

@mkilar123
Copy link
Copy Markdown
Collaborator

@mkilar123 mkilar123 commented Dec 10, 2025

Related Issue(s)

Fixes #695

Related Collection Role

  • cisco.nac_dc_vxlan.validate
  • cisco.nac_dc_vxlan.dtc.create
  • cisco.nac_dc_vxlan.dtc.deploy
  • cisco.nac_dc_vxlan.dtc.remove
  • other

Related Data Model Element

  • vxlan.fabric
  • vxlan.global
  • vxlan.topology
  • vxlan.underlay
  • vxlan.overlay
  • vxlan.overlay_extensions
  • vxlan.policy
  • vxlan.multisite
  • defaults.vxlan
  • other

Proposed Changes

Adding a validation rule which checks if an access interface is mapped to maximum one network:
Checked conditions:

  • If the access interface has "access_vlan" defined - it cannot be associated with any network_attach_group
  • If the access interface has no "access_vlan" defined - it can be associated with maximum one network_attach_group
  • If network_attach_group contains an access port - it can be associated with maximum one network
    The checks also apply to interfaces on TORs

Test Notes

Tests run:

  • access interface without "access_vlan" defined referenced max once in network_attach_groups

  • access interface with"access_vlan" defined not referenced in any network_attach_groups

  • TOR access interface without "access_vlan" defined referenced max once in network_attach_groups

  • TOR access interface with"access_vlan" defined not referenced in any network_attach_groups

  • access interface with"access_vlan" defined referenced only once in network_attach_groups

  • TOR access interface with"access_vlan" defined referenced only once in network_attach_groups

  • access interface without "access_vlan" defined referenced multiple times in network_attach_groups

  • TOR access interface without "access_vlan" defined referenced multiple times in network_attach_groups

  • network_attach_group containing access port referenced by one network

  • network_attach_group containing access port referenced by multiple networks

    • network_attach_group without access port referenced by multiple networks

Cisco Nexus Dashboard Version

Checklist

  • Latest commit is rebased from develop with merge conflicts resolved
  • New or updates to documentation has been made accordingly
  • Assigned the proper reviewers

@mkilar123 mkilar123 requested a review from a team as a code owner December 10, 2025 11:20
@mkilar123 mkilar123 requested a review from skaszlik December 10, 2025 11:20
@mkilar123 mkilar123 added enhancement New feature or request ready for review PR Ready for Review labels Dec 10, 2025
Copy link
Copy Markdown
Collaborator

@juburnet juburnet left a comment

Choose a reason for hiding this comment

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

Comprehensive Review: PR #690 - Validation Rule for Access Interface VLAN Assignments

🎯 Overview

This PR introduces a new HIGH severity validation rule (ID: 405) to enforce proper access interface to network attach group mappings. The implementation addresses a critical configuration gap for both switch and TOR topologies.


✅ Strengths

  1. Clear business logic: The rule enforces three distinct constraints that prevent VLAN configuration conflicts
  2. Comprehensive coverage: Handles both switch (2-tuple) and TOR (3-tuple) interface references
  3. Descriptive error messages: Each validation failure provides actionable context (hostname, interface, networks)
  4. Defensive programming: Uses utility methods data_model_key_check() and safeget() for safe dictionary access
  5. Well-documented: Docstrings explain the purpose of each helper method
  6. CI passing: All 23 checks pass, indicating no regressions

🔴 Critical Issues (Must Fix Before Merge)

1. Missing None Check in Interface Info Access (Lines 237, 277)

Location: validate_access_interface_references()

Issue: After retrieving interface_info from access_interfaces_map, the code immediately accesses interface_info['has_access_vlan'] without validating the dictionary structure.

Risk: If interface_info is an empty dict or malformed, this will raise a KeyError.

Recommendation:

interface_info = access_interfaces_map[switch_key]
has_access_vlan = interface_info.get('has_access_vlan', False)
if has_access_vlan is None:
    # Log warning or skip
    continue

🟡 Should Fix (Not Blocking)

2. Code Duplication: Switch vs TOR Validation (Lines 233-259 vs 261-301)

Issue: Nearly identical validation logic repeated for switches and TORs with only minor string formatting differences.

Impact: Maintenance burden - bug fixes must be applied twice.

Recommendation: Extract common validation logic into a shared helper method:

@classmethod
def _validate_interface_vlan_reference(cls, hostname, interface_name, has_access_vlan, 
                                        reference_count, group_names, device_type='switch', parent_leaf=None):
    """
    Shared validation logic for both switches and TORs
    """
    # Common validation with parameterized error messages

3. Inefficient Iteration Pattern (Lines 138-205)

Issue: The code iterates through all interfaces and then filters by mode == 'access', rather than pre-filtering.

Impact: Minor performance overhead when processing large switch configs.

Recommendation: If the data model allows, consider pre-filtering access interfaces before iteration.


🟢 Consider for Improvement

4. Confusing Variable Naming (Line 263)

Location: TOR validation block

Issue: Variable group_names actually contains network names, not group names:

for ref_key, group_names in interface_references.items():
    # ...
    network_list = ', '.join(group_names)  # These are network names, not group names

Recommendation: Rename to network_names for clarity.

5. Utility Method Redundancy (Lines 343-373)

Issue: data_model_key_check() and safeget() may duplicate functionality from other validation rules.

Recommendation: Check if similar utilities exist in other rules (e.g., 401, 402, 403, 404). If so, consider extracting to a shared validation_utils.py module.


💭 Questions / Needs Discussion

6. Validation Coverage: Trunk Ports

Question: This rule only validates access mode interfaces. Should there be validation for trunk ports that might also reference network_attach_groups?

Context: Line 155 explicitly filters for mode == 'access'. Is this intentional scope, or should trunk ports be covered in a separate rule?

7. Error Reporting: Aggregation vs Individual

Observation: The rule appends individual error messages to cls.results. For large deployments with many violations, this could produce hundreds of error lines.

Question: Should there be a configurable limit or summary aggregation (e.g., "Found 47 access interface violations across 12 switches. See details below.")?


🧪 Testing Recommendations

Suggested Test Cases (If Not Already Covered):

  1. Edge case: Access interface with access_vlan referenced in multiple network attach groups
  2. Edge case: TOR interface referenced from multiple leaf switches
  3. Edge case: Network attach group referenced by 3+ networks (not just 2)
  4. Edge case: Empty access_interfaces list on a switch
  5. Edge case: Malformed interface definition missing expected keys
  6. Regression: Ensure rule doesn't fire false positives on trunk interfaces

📊 Code Quality Metrics

Metric Value Status
Lines Added 367
Lines Deleted 0 ✅ (New feature)
Cyclomatic Complexity Medium ⚠️ (Could be reduced with refactoring)
Documentation Good
Error Messages Excellent
Test Coverage Unknown ❓ (No test files in PR)

🎬 Recommendation

Status: ⚠️ REQUEST CHANGES

Reason: Critical issue #1 (missing None check) could cause runtime errors in edge cases.

Action Items:

  1. 🔴 Add defensive None/KeyError handling for interface_info['has_access_vlan'] access
  2. 🟡 Consider refactoring duplicated validation logic
  3. 💭 Clarify scope: Should trunk ports be validated in a future rule?

Once issue #1 is addressed, this will be APPROVED - the core logic is sound and the feature is valuable.


🔗 Related Context

  • Related Issue: #695
  • Rule ID: 405
  • Severity: HIGH
  • Applies to: common_vxlan validation

Copy link
Copy Markdown
Collaborator

@juburnet juburnet left a comment

Choose a reason for hiding this comment

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

Summary

I've completed a comprehensive review of this PR. The validation rule is well-designed and addresses an important configuration gap for access interface VLAN assignments. However, there is one critical issue that must be fixed before merge.

Decision: REQUEST CHANGES

Critical Issue: Lines 237 and 277 directly access interface_info['has_access_vlan'] without validation, which could raise KeyError if the dictionary structure is malformed. Please see inline comments for the fix.

Once this is addressed, the PR will be ready for approval. The core logic is sound, error messages are excellent, and all CI checks pass.

Additional Non-Blocking Suggestions

  • Consider refactoring duplicated validation logic between switch and TOR handling (see inline comment)
  • Minor variable naming improvements for clarity

Thank you for this contribution!

@mkilar123 mkilar123 requested a review from juburnet February 17, 2026 15:40
Copy link
Copy Markdown
Collaborator

@juburnet juburnet left a comment

Choose a reason for hiding this comment

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

🔍 Comprehensive Code Review Summary

Overall Assessment

This PR introduces a HIGH severity validation rule (ID: 405) to enforce proper access interface to network attach group mappings. The implementation addresses issue #695 and provides valuable protection against VLAN configuration conflicts in both switch and TOR topologies.

What Changed Since Last Review (2026-02-12)

The author has addressed the critical issue from the previous review by adding defensive validation for malformed interface data (lines 235-241). Two commits on 2026-02-17 added the necessary structure validation that prevents potential KeyError exceptions.


✅ Strengths

  1. Critical Issue Resolved: The missing None check identified in the previous review has been properly addressed
  2. Clear Business Logic: Three distinct validation constraints prevent VLAN configuration conflicts
  3. Comprehensive Coverage: Handles both switch (2-tuple) and TOR (3-tuple) interface references
  4. Excellent Error Messages: Actionable error messages with specific context (hostname, interface, networks)
  5. Defensive Programming: Uses utility methods data_model_key_check() and safeget() for safe dictionary access
  6. Multi-overlay Support: Correctly handles both vxlan.overlay and vxlan.multisite.overlay paths
  7. CI Clean: All 23 checks passing with no regressions
  8. Well Documented: Clear docstrings explain the purpose and logic of each method

🟡 Non-Blocking Improvements

1. Code Duplication (Lines 247-267 vs 289-309)

Severity: 🟡 MEDIUM
Impact: Maintenance burden - bug fixes must be applied twice

The validation logic for switches and TORs is nearly identical (~95% overlap). Only error message formatting differs.

Suggestion: Extract common validation into a helper method (can be done in follow-up PR).

2. Utility Method Redundancy (Lines 350-381)

Severity: 🟢 LOW
Scope: Project-wide issue, not specific to this PR

Both data_model_key_check() and safeget() are duplicated in rules 401, 402, 403, and now 405. These should be extracted to a shared validation_utils.py module.

Recommendation: Address in a separate project-wide refactoring PR.

3. Minor Code Clarity (Line 88)

Severity: 🟢 LOW

interface_references = all_interface_references

This redundant assignment creates an alias that doesn't add clarity. Consider using all_interface_references directly.


🛡️ Security Assessment

Status: ✅ No Security Issues Detected

  • ✅ No user input processing
  • ✅ No command injection vectors
  • ✅ No SQL/database queries
  • ✅ No file system operations
  • ✅ No network requests
  • ✅ Pure data validation logic

🧪 Testing

CI Status: ✅ All 23 Checks Passing

  • Ansible-Lint: ✅ PASS
  • Build collection (multiple Python/Ansible versions): ✅ PASS
  • Sanity tests (multiple Python/Ansible versions): ✅ PASS

Manual Test Coverage (from PR description)

The author tested extensive scenarios including:

  • Access interfaces with/without access_vlan defined
  • TOR access interfaces
  • Multiple network attach group references
  • Network attach groups with access ports

Recommendation

Consider adding automated unit tests for this rule if the project supports it (could be a future enhancement).


📊 Code Quality Metrics

Metric Value Status
Lines Added 375
Lines Deleted 0 ✅ (New feature)
Cyclomatic Complexity Medium ⚠️ (Could be reduced with refactoring)
Code Duplication ~25% ⚠️ (Switch/TOR blocks nearly identical)
Documentation Excellent
Error Messages Excellent
Defensive Programming Good
CI Checks 23/23 passing

🎯 Compliance with Project Patterns

Compared to existing validation rules (401, 402, 403):

  • ✅ Follows same class structure
  • ✅ Uses same utility methods
  • ✅ Returns list of error strings
  • ✅ Uses HIGH severity appropriately
  • ✅ Consistent code style

💭 Optional Discussion Points

  1. Testing Strategy: Are there automated tests for validation rules in this project? If so, should this rule include tests?

  2. Refactoring Scope: Would you prefer to address code duplication before merge, or handle it in a follow-up PR?

  3. Utility Module: Would the team be interested in extracting shared utilities to a common module in a future PR?


✅ Final Decision: APPROVE

Rationale

  1. ✅ Critical issue from previous review has been addressed
  2. ✅ All CI checks passing
  3. ✅ Core validation logic is sound and valuable
  4. ✅ No security concerns
  5. ✅ Remaining issues are non-blocking code quality improvements

What This PR Delivers

  • Prevents access interfaces with access_vlan from being referenced in network attach groups
  • Ensures access interfaces without access_vlan are referenced at most once
  • Validates network attach groups with access ports aren't used by multiple networks
  • Provides clear, actionable error messages for misconfigurations

Post-Merge Opportunities

  • Refactor duplicated validation logic (optional enhancement)
  • Extract utility methods to shared module (project-wide improvement)
  • Add automated tests (if project infrastructure supports it)

Thank you for your contribution and responsiveness to feedback! 🎉

cls.validate_access_interface_references(
access_interfaces_map,
interface_references
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 Minor Code Clarity

This line creates a redundant alias that doesn't add clarity:

interface_references = all_interface_references

Suggestion: Consider using all_interface_references directly throughout the rest of the method to avoid the extra variable assignment.

This is a very minor point and doesn't affect functionality - just a small readability improvement.

network_list = ', '.join(referenced_networks)
cls.results.append(
f"Access interface '{interface_name}' on switch '{hostname}' has 'access_vlan' defined "
f"and cannot be referenced in network_attach_groups. "
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Code Duplication Opportunity

The validation logic here (lines 247-267) is nearly identical to the TOR validation logic (lines 289-309). The only difference is error message formatting.

Impact: Maintenance burden - any bug fixes or logic changes must be applied in two places.

Suggestion: Consider extracting common validation into a helper method. This refactoring can be done in a follow-up PR if you prefer to keep this PR focused on the core functionality.

Example approach:

@classmethod
def _validate_interface_vlan_constraint(cls, hostname, interface_name, has_access_vlan, 
                                         reference_count, referenced_networks, 
                                         device_type='switch', parent_leaf=None):
    # Shared validation logic with parameterized error messages
    ...

Utility function to check if keys exist in nested dictionary
"""
dm_key_dict = {'keys_found': [], 'keys_not_found': [], 'keys_data': [], 'keys_no_data': []}
for key in keys:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 Project-Wide Improvement Opportunity

Both data_model_key_check() and safeget() utility methods are duplicated across multiple validation rules (401, 402, 403, and now 405).

Recommendation: Consider extracting these to a shared validation_utils.py module in a future PR. This would:

  • Reduce code duplication
  • Make updates easier (change once, apply everywhere)
  • Improve maintainability

This is a project-wide improvement opportunity, not specific to this PR. Could be addressed separately to keep this PR focused on the validation rule functionality.

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

Labels

enhancement New feature or request ready for review PR Ready for Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a validation rule that would prevent attaching multiple networks to an access port

2 participants