|
| 1 | +""" |
| 2 | +VPP Interface Configuration Endpoints |
| 3 | +
|
| 4 | +All VPP (Vector Packet Processing) interface endpoints for VyOS 1.5+. |
| 5 | +VPP supports seven interface types under `interfaces vpp`: |
| 6 | + bonding (vppbondN), bridge (vppbrN), gre (vppgreN), ipip (vppipipN), |
| 7 | + loopback (vpploN), vxlan (vppvxlanN), xconnect (vppxconN) |
| 8 | +
|
| 9 | +Multi-parameter builder operations encode extra parameters as colon-separated |
| 10 | +values in the `value` field, e.g. "vlan_id:address" for vif address ops. |
| 11 | +""" |
| 12 | + |
| 13 | +import inspect |
| 14 | +import logging |
| 15 | +from typing import Dict, List, Optional, Any |
| 16 | + |
| 17 | +from fastapi import APIRouter, HTTPException, Request |
| 18 | +from pydantic import BaseModel, Field |
| 19 | +from starlette.concurrency import run_in_threadpool |
| 20 | + |
| 21 | +from fastapi_permissions import require_read_permission, require_write_permission |
| 22 | +from rbac_permissions import FeatureGroup |
| 23 | +from session_vyos_service import get_session_vyos_service |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +router = APIRouter(prefix="/vyos/vpp", tags=["vpp-interface"]) |
| 28 | + |
| 29 | + |
| 30 | +# ============================================================================= |
| 31 | +# Request / Response Models |
| 32 | +# ============================================================================= |
| 33 | + |
| 34 | + |
| 35 | +class BatchOperation(BaseModel): |
| 36 | + op: str = Field(..., description="Builder method name") |
| 37 | + value: Optional[str] = Field(None, description="Value (colon-separated for multi-param ops)") |
| 38 | + |
| 39 | + |
| 40 | +class BatchRequest(BaseModel): |
| 41 | + interface: str = Field(..., description="Interface name (e.g., vppbond0, vppgre1)") |
| 42 | + operations: List[BatchOperation] |
| 43 | + |
| 44 | + |
| 45 | +class VyOSResponse(BaseModel): |
| 46 | + success: bool |
| 47 | + data: Optional[Dict[str, Any]] = None |
| 48 | + error: Optional[str] = None |
| 49 | + |
| 50 | + |
| 51 | +# ---- Per-type config models ------------------------------------------------- |
| 52 | + |
| 53 | + |
| 54 | +class VifConfig(BaseModel): |
| 55 | + vlan_id: str |
| 56 | + description: Optional[str] = None |
| 57 | + disabled: bool = False |
| 58 | + addresses: List[str] = Field(default_factory=list) |
| 59 | + mtu: Optional[str] = None |
| 60 | + |
| 61 | + |
| 62 | +class BridgeMember(BaseModel): |
| 63 | + interface: str |
| 64 | + bvi: bool = False |
| 65 | + |
| 66 | + |
| 67 | +class BondingConfig(BaseModel): |
| 68 | + name: str |
| 69 | + description: Optional[str] = None |
| 70 | + disabled: bool = False |
| 71 | + mode: Optional[str] = None |
| 72 | + hash_policy: Optional[str] = None |
| 73 | + mac: Optional[str] = None |
| 74 | + mtu: Optional[str] = None |
| 75 | + addresses: List[str] = Field(default_factory=list) |
| 76 | + members: List[str] = Field(default_factory=list) |
| 77 | + vif: List[VifConfig] = Field(default_factory=list) |
| 78 | + |
| 79 | + |
| 80 | +class BridgeConfig(BaseModel): |
| 81 | + name: str |
| 82 | + description: Optional[str] = None |
| 83 | + members: List[BridgeMember] = Field(default_factory=list) |
| 84 | + |
| 85 | + |
| 86 | +class GreConfig(BaseModel): |
| 87 | + name: str |
| 88 | + description: Optional[str] = None |
| 89 | + disabled: bool = False |
| 90 | + addresses: List[str] = Field(default_factory=list) |
| 91 | + mtu: Optional[str] = None |
| 92 | + remote: Optional[str] = None |
| 93 | + source_address: Optional[str] = None |
| 94 | + tunnel_type: Optional[str] = None |
| 95 | + key: Optional[str] = None |
| 96 | + |
| 97 | + |
| 98 | +class IpipConfig(BaseModel): |
| 99 | + name: str |
| 100 | + description: Optional[str] = None |
| 101 | + disabled: bool = False |
| 102 | + addresses: List[str] = Field(default_factory=list) |
| 103 | + mtu: Optional[str] = None |
| 104 | + remote: Optional[str] = None |
| 105 | + source_address: Optional[str] = None |
| 106 | + |
| 107 | + |
| 108 | +class LoopbackConfig(BaseModel): |
| 109 | + name: str |
| 110 | + description: Optional[str] = None |
| 111 | + disabled: bool = False |
| 112 | + addresses: List[str] = Field(default_factory=list) |
| 113 | + mtu: Optional[str] = None |
| 114 | + vif: List[VifConfig] = Field(default_factory=list) |
| 115 | + |
| 116 | + |
| 117 | +class VxlanConfig(BaseModel): |
| 118 | + name: str |
| 119 | + description: Optional[str] = None |
| 120 | + disabled: bool = False |
| 121 | + addresses: List[str] = Field(default_factory=list) |
| 122 | + mtu: Optional[str] = None |
| 123 | + remote: Optional[str] = None |
| 124 | + source_address: Optional[str] = None |
| 125 | + vni: Optional[str] = None |
| 126 | + |
| 127 | + |
| 128 | +class XconnectConfig(BaseModel): |
| 129 | + name: str |
| 130 | + description: Optional[str] = None |
| 131 | + disabled: bool = False |
| 132 | + members: List[str] = Field(default_factory=list) |
| 133 | + |
| 134 | + |
| 135 | +class VppConfigResponse(BaseModel): |
| 136 | + bonding: List[BondingConfig] = Field(default_factory=list) |
| 137 | + bridge: List[BridgeConfig] = Field(default_factory=list) |
| 138 | + gre: List[GreConfig] = Field(default_factory=list) |
| 139 | + ipip: List[IpipConfig] = Field(default_factory=list) |
| 140 | + loopback: List[LoopbackConfig] = Field(default_factory=list) |
| 141 | + vxlan: List[VxlanConfig] = Field(default_factory=list) |
| 142 | + xconnect: List[XconnectConfig] = Field(default_factory=list) |
| 143 | + total: int = 0 |
| 144 | + |
| 145 | + |
| 146 | +# ============================================================================= |
| 147 | +# Endpoints |
| 148 | +# ============================================================================= |
| 149 | + |
| 150 | + |
| 151 | +@router.get("/capabilities") |
| 152 | +async def get_capabilities(request: Request) -> Dict[str, Any]: |
| 153 | + """Return version-aware feature capabilities for VPP interfaces.""" |
| 154 | + await require_read_permission(request, FeatureGroup.INTERFACES) |
| 155 | + service = get_session_vyos_service(request) |
| 156 | + from vyos_builders.interfaces.vpp import VppInterfaceBuilderMixin |
| 157 | + builder = VppInterfaceBuilderMixin(version=service.get_version()) |
| 158 | + return builder.get_capabilities() |
| 159 | + |
| 160 | + |
| 161 | +@router.get("/config", response_model=VppConfigResponse) |
| 162 | +async def get_config(http_request: Request, refresh: bool = False) -> VppConfigResponse: |
| 163 | + """Get all VPP interface configurations from VyOS.""" |
| 164 | + await require_read_permission(http_request, FeatureGroup.INTERFACES) |
| 165 | + try: |
| 166 | + service = get_session_vyos_service(http_request) |
| 167 | + full_config = await run_in_threadpool(service.get_full_config, refresh) |
| 168 | + raw_config = full_config.get("interfaces", {}).get("vpp", {}) |
| 169 | + |
| 170 | + from vyos_mappers.interfaces.vpp_versions import get_vpp_mapper |
| 171 | + mapper = get_vpp_mapper(service.get_version()) |
| 172 | + parsed = mapper.parse_all_vpp_interfaces(raw_config) |
| 173 | + return VppConfigResponse(**parsed) |
| 174 | + except Exception: |
| 175 | + logger.exception("Unhandled error in get_config") |
| 176 | + raise HTTPException(status_code=500, detail="Internal server error") |
| 177 | + |
| 178 | + |
| 179 | +@router.post("/batch", response_model=VyOSResponse) |
| 180 | +async def batch_configure(http_request: Request, request: BatchRequest) -> VyOSResponse: |
| 181 | + """ |
| 182 | + Configure a VPP interface using batch operations. |
| 183 | +
|
| 184 | + Operation names match builder methods exactly, e.g.: |
| 185 | + set_bonding_description, set_gre_remote, delete_vxlan_vni |
| 186 | +
|
| 187 | + Multi-parameter operations use colon-separated `value`: |
| 188 | + set_bonding_vif_address → value="100:192.0.2.1/24" (vlan_id:address) |
| 189 | + set_bonding_vif_mtu → value="100:1500" (vlan_id:mtu) |
| 190 | + set_loopback_vif_address → value="10:10.0.0.1/24" (vlan_id:address) |
| 191 | + """ |
| 192 | + await require_write_permission(http_request, FeatureGroup.INTERFACES) |
| 193 | + |
| 194 | + try: |
| 195 | + service = get_session_vyos_service(http_request) |
| 196 | + batch = service.create_vpp_batch() |
| 197 | + |
| 198 | + for op in request.operations: |
| 199 | + if op.op in batch._INTERNAL_BUILDER_METHODS: |
| 200 | + raise HTTPException( |
| 201 | + status_code=400, |
| 202 | + detail=f"Operation '{op.op}' is not a valid interface operation", |
| 203 | + ) |
| 204 | + |
| 205 | + method = getattr(batch, op.op, None) |
| 206 | + if method is None: |
| 207 | + raise HTTPException( |
| 208 | + status_code=400, |
| 209 | + detail=f"Unsupported operation: {op.op}", |
| 210 | + ) |
| 211 | + |
| 212 | + sig = inspect.signature(method) |
| 213 | + params = [p for p in sig.parameters.keys() if p != "self"] |
| 214 | + |
| 215 | + if len(params) == 1: |
| 216 | + method(request.interface) |
| 217 | + elif len(params) == 2: |
| 218 | + if op.value is None: |
| 219 | + raise HTTPException( |
| 220 | + status_code=400, |
| 221 | + detail=f"Operation '{op.op}' requires a value", |
| 222 | + ) |
| 223 | + method(request.interface, op.value) |
| 224 | + elif len(params) == 3: |
| 225 | + if op.value is None: |
| 226 | + raise HTTPException( |
| 227 | + status_code=400, |
| 228 | + detail=f"Operation '{op.op}' requires a value in 'param1:param2' format", |
| 229 | + ) |
| 230 | + parts = op.value.split(":", 1) |
| 231 | + if len(parts) != 2: |
| 232 | + raise HTTPException( |
| 233 | + status_code=400, |
| 234 | + detail=f"Operation '{op.op}' requires value in 'param1:param2' format", |
| 235 | + ) |
| 236 | + method(request.interface, parts[0], parts[1]) |
| 237 | + elif len(params) == 4: |
| 238 | + if op.value is None: |
| 239 | + raise HTTPException( |
| 240 | + status_code=400, |
| 241 | + detail=f"Operation '{op.op}' requires a value in 'param1:param2:param3' format", |
| 242 | + ) |
| 243 | + parts = op.value.split(":", 2) |
| 244 | + if len(parts) != 3: |
| 245 | + raise HTTPException( |
| 246 | + status_code=400, |
| 247 | + detail=f"Operation '{op.op}' requires value in 'param1:param2:param3' format", |
| 248 | + ) |
| 249 | + method(request.interface, parts[0], parts[1], parts[2]) |
| 250 | + else: |
| 251 | + raise HTTPException( |
| 252 | + status_code=400, |
| 253 | + detail=f"Operation '{op.op}' has unexpected signature", |
| 254 | + ) |
| 255 | + |
| 256 | + response = service.execute_batch(batch) |
| 257 | + return VyOSResponse( |
| 258 | + success=response.status == 200, |
| 259 | + data=response.result if isinstance(response.result, dict) else None, |
| 260 | + error=response.error if response.error else None, |
| 261 | + ) |
| 262 | + except HTTPException: |
| 263 | + raise |
| 264 | + except Exception: |
| 265 | + logger.exception("Unhandled error in batch_configure") |
| 266 | + raise HTTPException(status_code=500, detail="Internal server error") |
0 commit comments