Skip to content

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

LevelMeaningAction Required
ERRORNetwork will not function correctlyMust fix before use
WARNINGPotential issue or suboptimal configurationReview and fix if relevant
INFOInformational noteOptional improvements

Common Error Types

IssueDescription
Missing node referenceBranch references a node that doesn't exist
Disconnected elementElement not connected to any node
Invalid GUIDMalformed or duplicate GUID
Missing presentationElement has no visual representation

Common Warnings

IssueDescription
Coordinate mismatchBranch corner doesn't align with node position
Unused nodeNode with no connected elements
Duplicate nameMultiple 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

PropertyDescription
severitySeverity.ERROR, WARNING, or INFO
codeMachine-readable issue identifier
messageHuman-readable description
object_typeElement type (e.g., "Node", "Line")
object_idGUID 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 True

CI/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)

Next Steps