|
| 1 | +"""Pydantic v2 unified config schema for CORNET experiments.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any, Literal |
| 7 | + |
| 8 | +from pydantic import BaseModel, ConfigDict, field_validator, model_validator |
| 9 | + |
| 10 | + |
| 11 | +class ConfigValidationError(ValueError): |
| 12 | + """Raised when a unified config fails schema validation.""" |
| 13 | + |
| 14 | + |
| 15 | +# ── Node models ────────────────────────────────────────────────────────────── |
| 16 | + |
| 17 | +class ContainerConfig(BaseModel): |
| 18 | + image: str |
| 19 | + environment: dict[str, str] = {} |
| 20 | + volumes: list[str] = [] |
| 21 | + |
| 22 | + |
| 23 | +class NodeConfig(BaseModel): |
| 24 | + name: str |
| 25 | + type: Literal["MOBILE", "STATIC", "UE", "GNB", "EPC"] = "MOBILE" |
| 26 | + container: ContainerConfig | None = None |
| 27 | + # Arbitrary extra keys (e.g. position, speed) are allowed |
| 28 | + model_config = ConfigDict(extra="allow") |
| 29 | + |
| 30 | + |
| 31 | +# ── Network section ─────────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +class MininetConfig(BaseModel): |
| 34 | + wmediumd: bool = False |
| 35 | + ssid: str = "cornet-net" |
| 36 | + mode: str = "g" |
| 37 | + channel: int = 1 |
| 38 | + |
| 39 | + |
| 40 | +class NetworkConfig(BaseModel): |
| 41 | + plugin: str |
| 42 | + type: Literal["ns3", "mininet", "ns3+mininet"] |
| 43 | + nodes: list[NodeConfig] = [] |
| 44 | + mininet: MininetConfig | None = None |
| 45 | + # NS-3 specific keys forwarded as-is |
| 46 | + model_config = ConfigDict(extra="allow") |
| 47 | + |
| 48 | + |
| 49 | +# ── Robot section ───────────────────────────────────────────────────────────── |
| 50 | + |
| 51 | +class ModelConfig(BaseModel): |
| 52 | + type: Literal["urdf", "sdf"] |
| 53 | + path: str # relative to task dir or absolute |
| 54 | + |
| 55 | + |
| 56 | +class PoseConfig(BaseModel): |
| 57 | + x: float = 0.0 |
| 58 | + y: float = 0.0 |
| 59 | + z: float = 0.0 |
| 60 | + roll: float = 0.0 |
| 61 | + pitch: float = 0.0 |
| 62 | + yaw: float = 0.0 |
| 63 | + |
| 64 | + |
| 65 | +class RobotEntry(BaseModel): |
| 66 | + name: str |
| 67 | + model: ModelConfig |
| 68 | + pose: PoseConfig = PoseConfig() |
| 69 | + ros_namespace: str | None = None |
| 70 | + |
| 71 | + |
| 72 | +class RobotConfig(BaseModel): |
| 73 | + plugin: str |
| 74 | + launch_file: str | None = None |
| 75 | + world: str | None = None |
| 76 | + robots: list[RobotEntry] = [] |
| 77 | + |
| 78 | + |
| 79 | +# ── Experiment section ──────────────────────────────────────────────────────── |
| 80 | + |
| 81 | +class SweepConfig(BaseModel): |
| 82 | + axes: dict[str, list[Any]] # e.g. {"network.numerology": [2,4], "network.bandwidth": [20,40]} |
| 83 | + parallel: bool = False |
| 84 | + repeats: int = 1 |
| 85 | + |
| 86 | + @field_validator("repeats") |
| 87 | + @classmethod |
| 88 | + def _positive_repeats(cls, v: int) -> int: |
| 89 | + if v < 1: |
| 90 | + raise ConfigValidationError("sweep.repeats must be >= 1") |
| 91 | + return v |
| 92 | + |
| 93 | + |
| 94 | +class ExperimentConfig(BaseModel): |
| 95 | + name: str |
| 96 | + duration: float # seconds |
| 97 | + output_dir: str = "results" |
| 98 | + primary_metric: str | None = None |
| 99 | + higher_is_better: bool = False |
| 100 | + sweep: SweepConfig | None = None |
| 101 | + |
| 102 | + |
| 103 | +# ── Top-level ───────────────────────────────────────────────────────────────── |
| 104 | + |
| 105 | +class UnifiedConfig(BaseModel): |
| 106 | + """Root config model for CORNET unified schema (``_schema: unified-v1``).""" |
| 107 | + |
| 108 | + _schema: str = "unified-v1" # literal marker; not validated by pydantic |
| 109 | + network: NetworkConfig |
| 110 | + robot: RobotConfig |
| 111 | + experiment: ExperimentConfig |
| 112 | + |
| 113 | + @model_validator(mode="after") |
| 114 | + def _cross_validate(self) -> "UnifiedConfig": |
| 115 | + # Mininet wmediumd requires Docker support — warn but don't error |
| 116 | + return self |
0 commit comments