Network Validation
This guide covers PyPtP's validation system for checking network data quality. The validator detects issues like disconnected elements, invalid coordinates, missing presentations, and data inconsistencies.
Full Example
View the complete code: 04_validation.py
Running Validation
Basic Usage
python
from pyptp import NetworkMV, configure_logging
from pyptp.validator import CheckRunner, Severity
configure_logging(level="INFO")
# Load network
network = NetworkMV.from_file("network.vnf")
# Run all validators
runner = CheckRunner(network)
report = runner.run()The CheckRunner executes all registered validation checks and returns a ValidationReport containing any issues found.
Quick Summary
python
from pyptp.ptp_log import logger
logger.info("Validation: {}", report.summary())The summary() method returns a concise string like "3 errors, 12 warnings".
Understanding Severity Levels
python
# Count issues by severity
error_count = sum(1 for issue in report.issues if issue.severity == Severity.ERROR)
warning_count = sum(1 for issue in report.issues if issue.severity == Severity.WARNING)
logger.info(" Errors: {}", error_count)
logger.info(" Warnings: {}", warning_count)Severity Definitions
| Level | Meaning | Action Required |
|---|---|---|
ERROR | Network will not function correctly | Must fix before use |
WARNING | Potential issue or suboptimal configuration | Review and fix if relevant |
INFO | Informational note | Optional improvements |
Common Error Types
| Issue | Description |
|---|---|
| Missing node reference | Branch references a node that doesn't exist |
| Disconnected element | Element not connected to any node |
| Invalid GUID | Malformed or duplicate GUID |
| Missing presentation | Element has no visual representation |
Common Warnings
| Issue | Description |
|---|---|
| Coordinate mismatch | Branch corner doesn't align with node position |
| Unused node | Node with no connected elements |
| Duplicate name | Multiple elements share the same name |
Inspecting Issues
Iterate Critical Issues
python
for issue in report.issues:
if issue.severity == Severity.ERROR:
logger.error("ERROR: {}", issue.message)
logger.error(" Element: {} {}", issue.object_type, issue.object_id)Issue Properties
| Property | Description |
|---|---|
severity | Severity.ERROR, WARNING, or INFO |
code | Machine-readable issue identifier |
message | Human-readable description |
object_type | Element type (e.g., "Node", "Line") |
object_id | GUID of the affected element |
Filter by Issue Code
python
# Find all coordinate mismatch issues
coord_issues = [
issue for issue in report.issues
if issue.code == "BRANCH_CORNER_MISMATCH"
]Exporting Reports
JSON Export
python
from pathlib import Path
Path("validation_report.json").write_text(
str(report.to_json()),
encoding="utf-8"
)The JSON format is useful for:
- Automated CI/CD pipelines
- Integration with issue tracking systems
- Historical comparison of validation results
Programmatic Access
python
# Check if network passes validation
is_valid = error_count == 0
logger.info("Network valid: {}", is_valid)
# Use in automation
if not is_valid:
raise ValueError(f"Network has {error_count} validation errors")Validation in Workflows
Pre-Save Validation
python
def save_if_valid(network, path):
"""Only save network if it passes validation."""
report = CheckRunner(network).run()
errors = sum(1 for i in report.issues if i.severity == Severity.ERROR)
if errors > 0:
logger.error("Cannot save: {} errors found", errors)
return False
network.save(path)
logger.info("Network saved to {}", path)
return TrueCI/CD Integration
python
import sys
def validate_network(vnf_path):
"""Exit with error code if validation fails."""
network = NetworkMV.from_file(vnf_path)
report = CheckRunner(network).run()
errors = sum(1 for i in report.issues if i.severity == Severity.ERROR)
if errors > 0:
for issue in report.issues:
if issue.severity == Severity.ERROR:
print(f"ERROR: {issue.message}")
sys.exit(1)
print("Validation passed")
sys.exit(0)Complete Example
python
"""Validate network data quality."""
from pathlib import Path
from pyptp import NetworkMV, configure_logging
from pyptp.ptp_log import logger
from pyptp.validator import CheckRunner, Severity
configure_logging(level="INFO")
# Load network
network = NetworkMV.from_file("network.vnf")
# Run validation
runner = CheckRunner(network)
report = runner.run()
# Check results
logger.info("Validation: {}", report.summary())
# Count issues by severity
error_count = sum(1 for issue in report.issues if issue.severity == Severity.ERROR)
warning_count = sum(1 for issue in report.issues if issue.severity == Severity.WARNING)
logger.info(" Errors: {}", error_count)
logger.info(" Warnings: {}", warning_count)
# Show critical issues
for issue in report.issues:
if issue.severity == Severity.ERROR:
logger.error("ERROR: {}", issue.message)
logger.error(" Element: {} {}", issue.object_type, issue.object_id)
# Export report
Path("validation_report.txt").write_text(str(report.to_json()), encoding="utf-8")
# Check if network is valid
is_valid = error_count == 0
logger.info("Network valid: {}", is_valid)