|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +NetDriver is a network device automation framework built on AsyncSSH that provides HTTP RESTful APIs for CLI command execution on network devices. It's organized as a monorepo with a Polylith architecture, consisting of: |
| 8 | + |
| 9 | +- **netdriver-agent**: FastAPI-based REST API service for device connectivity testing and command execution |
| 10 | +- **netdriver-simunet**: SSH server simulation for testing network device terminals |
| 11 | + |
| 12 | +## Key Architecture Patterns |
| 13 | + |
| 14 | +### Polylith Architecture |
| 15 | + |
| 16 | +The project uses a Polylith-inspired structure with `bases/` (applications) and `components/` (shared libraries): |
| 17 | + |
| 18 | +```Text |
| 19 | +bases/netdriver/ |
| 20 | +├── agent/ # REST API application |
| 21 | +└── simunet/ # Simulation network application |
| 22 | +
|
| 23 | +components/netdriver/ |
| 24 | +├── client/ # SSH client with async session management |
| 25 | +├── exception/ # Centralized error handling and error codes |
| 26 | +├── log/ # Logging utilities (Loguru-based) |
| 27 | +├── plugin/ # Plugin system core and engine |
| 28 | +├── plugins/ # Device-specific plugins (Cisco, Huawei, Juniper, etc.) |
| 29 | +├── server/ # SSH server for simulated devices |
| 30 | +├── textfsm/ # Enhanced TextFSM for output parsing |
| 31 | +└── utils/ # Utility functions |
| 32 | +``` |
| 33 | + |
| 34 | +### Session Management |
| 35 | + |
| 36 | +The `SessionPool` (in `components/netdriver/client/pool.py`) is a singleton that: |
| 37 | + |
| 38 | +- Maintains persistent SSH sessions with devices (identified by `protocol:username@ip:port`) |
| 39 | +- Automatically monitors session health and removes closed/expired/idle sessions |
| 40 | +- Implements a command queue per session to prevent concurrent configuration conflicts |
| 41 | +- Uses asyncio locks to ensure thread-safe session operations |
| 42 | + |
| 43 | +### Plugin System |
| 44 | + |
| 45 | +The plugin engine (`components/netdriver/plugin/engine.py`) dynamically loads device plugins at startup: |
| 46 | + |
| 47 | +- Plugins are organized by vendor directory under `components/netdriver/plugins/` |
| 48 | +- Each plugin inherits from `Base` (in `components/netdriver/plugins/base.py`) and implements device-specific behavior |
| 49 | +- Plugins define mode patterns (LOGIN, ENABLE, CONFIG), error patterns, and command handling |
| 50 | +- Plugin resolution: `vendor/model/version` → `vendor/model/base` → `vendor/base/base` |
| 51 | + |
| 52 | +### Device Modes and State Management |
| 53 | + |
| 54 | +Sessions track device state including: |
| 55 | + |
| 56 | +- **Mode**: LOGIN, ENABLE, or CONFIG (defined in `client/mode.py`) |
| 57 | +- **Vsys**: Virtual system context (for multi-context devices like firewalls) |
| 58 | +- Mode switching is handled automatically by the base plugin via `switch_mode()` |
| 59 | + |
| 60 | +## Commands |
| 61 | + |
| 62 | +### Development Environment |
| 63 | + |
| 64 | +```bash |
| 65 | +# Install dependencies |
| 66 | +poetry install |
| 67 | + |
| 68 | +# Install Poetry plugins (if not already installed) |
| 69 | +poetry self add poetry-multiproject-plugin |
| 70 | +poetry self add poetry-polylith-plugin |
| 71 | +``` |
| 72 | + |
| 73 | +### Running Services |
| 74 | + |
| 75 | +```bash |
| 76 | +# Start agent service (REST API on http://localhost:8000) |
| 77 | +poetry run agent |
| 78 | + |
| 79 | +# Start simulation network service (SSH servers on configured ports) |
| 80 | +poetry run simunet |
| 81 | +``` |
| 82 | + |
| 83 | +### Testing |
| 84 | + |
| 85 | +```bash |
| 86 | +# Run all tests |
| 87 | +poetry run pytest |
| 88 | + |
| 89 | +# Run unit tests only |
| 90 | +poetry run pytest -m unit |
| 91 | + |
| 92 | +# Run integration tests only |
| 93 | +poetry run pytest -m integration |
| 94 | + |
| 95 | +# Run specific test file |
| 96 | +poetry run pytest tests/bases/netdriver/agent/test_cisco_nexus.py |
| 97 | +``` |
| 98 | + |
| 99 | +### Configuration |
| 100 | + |
| 101 | +Configuration files in `config/`: |
| 102 | + |
| 103 | +- `config/agent/agent.yml` - Agent service settings (logging, session timeouts, SSH parameters, profiles) |
| 104 | + - Logs are written to `logs/netdriver_agent.log` |
| 105 | +- `config/simunet/simunet.yml` - Simulated device definitions and logging settings |
| 106 | + - Logs are written to `logs/netdriver_simunet.log` |
| 107 | + |
| 108 | +## Development Guidelines |
| 109 | + |
| 110 | +### Adding a New Device Plugin |
| 111 | + |
| 112 | +1. Create vendor directory under `components/netdriver/plugins/` if it doesn't exist |
| 113 | +2. Create plugin file named `{vendor}_{model}.py` (e.g., `cisco_nexus.py`) |
| 114 | +3. Inherit from vendor base class or `Base` plugin |
| 115 | +4. Define `PluginInfo` with vendor, model, version, and description |
| 116 | +5. Implement required abstract methods: |
| 117 | + - `get_mode_prompt_patterns()` - Regex patterns for each mode's prompt |
| 118 | + - `get_more_pattern()` - Pattern for pagination prompts |
| 119 | + - `get_union_pattern()` - Combined pattern for all prompts |
| 120 | + - `get_error_patterns()` - Patterns that indicate command errors |
| 121 | + - `get_ignore_error_patterns()` - Error patterns to ignore |
| 122 | + |
| 123 | +Example: |
| 124 | + |
| 125 | +```python |
| 126 | +from netdriver.plugin.plugin_info import PluginInfo |
| 127 | +from netdriver.plugins.cisco import CiscoBase |
| 128 | + |
| 129 | +class CiscoNexus(CiscoBase): |
| 130 | + info = PluginInfo( |
| 131 | + vendor="cisco", |
| 132 | + model="nexus", |
| 133 | + version="base", |
| 134 | + description="Cisco Nexus Plugin" |
| 135 | + ) |
| 136 | +``` |
| 137 | + |
| 138 | +### Testing Plugins |
| 139 | + |
| 140 | +Integration tests are in `tests/bases/netdriver/agent/` and typically: |
| 141 | + |
| 142 | +1. Start the simunet service with test fixtures in `conftest.py` |
| 143 | +2. Use httpx client to make API calls to the agent |
| 144 | +3. Verify command execution and error handling |
| 145 | + |
| 146 | +### Error Handling |
| 147 | + |
| 148 | +All custom exceptions are in `components/netdriver/exception/errors.py` and inherit from `BaseError`: |
| 149 | + |
| 150 | +- Include HTTP status code and error code |
| 151 | +- For command execution errors, include output |
| 152 | +- Examples: `LoginFailed`, `PluginNotFound`, `ExecCmdTimeout`, `EnableFailed` |
| 153 | + |
| 154 | +### Dependency Injection |
| 155 | + |
| 156 | +The agent uses `dependency-injector` (see `bases/netdriver/agent/containers.py`) to wire: |
| 157 | + |
| 158 | +- Configuration providers |
| 159 | +- Request handlers |
| 160 | +- The container is wired to API modules in `main.py` |
| 161 | + |
| 162 | +### Logging |
| 163 | + |
| 164 | +Uses Loguru configured via `netdriver.log.logman`: |
| 165 | + |
| 166 | +- Correlation ID middleware tracks requests (agent only) |
| 167 | +- Log levels configurable in respective config files |
| 168 | +- Log files are separated by service: |
| 169 | + - Agent: `logs/agent.log` (excludes `netdriver.server` modules in test environment) |
| 170 | + - Simunet: `logs/simunet.log` (only `netdriver.server` modules) |
| 171 | +- Intercepts uvicorn logs for unified output |
| 172 | +- Log rotation: 1 day, retention: 60 days |
| 173 | +- Module filtering: Uses `logger.patch()` in `netdriver.server.device` to ensure correct module identification |
| 174 | +- In test environment: Both handlers are configured to prevent log duplication |
| 175 | + |
| 176 | +## Important Notes |
| 177 | + |
| 178 | +- Python 3.12+ required |
| 179 | +- Uses Poetry for dependency management |
| 180 | +- All SSH operations are async (AsyncSSH-based) |
| 181 | +- Session keys format: `{protocol}:{username}@{ip}:{port}` |
| 182 | +- The agent runs with auto-reload enabled by default (suitable for development) |
| 183 | +- Simulated devices use the plugin system to emulate vendor-specific behavior |
| 184 | +- Configuration profiles support device-specific settings by vendor/model/version or IP address |
0 commit comments