158 lines
5.5 KiB
Python
158 lines
5.5 KiB
Python
|
|
"""ODF 光纤配线单元管理系统 - FastAPI 应用入口。
|
|||
|
|
|
|||
|
|
定义所有 RESTful API 路由,提供对 ODF 机架、配线单元、端口和
|
|||
|
|
跳接连接的管理接口。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from typing import Optional
|
|||
|
|
|
|||
|
|
from fastapi import FastAPI, HTTPException
|
|||
|
|
|
|||
|
|
from app.models import PortType
|
|||
|
|
from app.schemas import (
|
|||
|
|
RackCreate,
|
|||
|
|
RackResponse,
|
|||
|
|
RackListResponse,
|
|||
|
|
UnitCreate,
|
|||
|
|
UnitResponse,
|
|||
|
|
PortResponse,
|
|||
|
|
PortDetail,
|
|||
|
|
FreePortResponse,
|
|||
|
|
ConnectionCreate,
|
|||
|
|
ConnectionResponse,
|
|||
|
|
)
|
|||
|
|
from app.services import service
|
|||
|
|
|
|||
|
|
app = FastAPI(
|
|||
|
|
title="ODF 光纤配线单元管理系统",
|
|||
|
|
description="提供对 ODF 光纤配线架、配线单元、光纤端口和跳接连接的统一管理能力。",
|
|||
|
|
version="1.0.0",
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ==================== 机架 API ====================
|
|||
|
|
|
|||
|
|
@app.get("/api/racks", response_model=list[RackListResponse], summary="获取所有机架列表")
|
|||
|
|
def list_racks():
|
|||
|
|
"""获取所有 ODF 机架的简要列表,包含每个机架下的单元数量。"""
|
|||
|
|
return service.list_racks()
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.get("/api/racks/{rack_id}", response_model=RackResponse, summary="获取机架详情")
|
|||
|
|
def get_rack(rack_id: str):
|
|||
|
|
"""根据机架 ID 获取机架的详细信息,包含所有配线单元和端口。"""
|
|||
|
|
rack = service.get_rack(rack_id)
|
|||
|
|
if not rack:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"机架 {rack_id} 不存在")
|
|||
|
|
return rack
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.post("/api/racks", response_model=RackResponse, status_code=201, summary="创建新机架")
|
|||
|
|
def create_rack(body: RackCreate):
|
|||
|
|
"""创建一个新的空 ODF 机架,不含配线单元。"""
|
|||
|
|
rack = service.create_rack(body.rack_name, body.location)
|
|||
|
|
return rack
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.delete("/api/racks/{rack_id}", status_code=204, summary="删除机架")
|
|||
|
|
def delete_rack(rack_id: str):
|
|||
|
|
"""删除指定机架及其包含的所有配线单元和端口。"""
|
|||
|
|
ok = service.delete_rack(rack_id)
|
|||
|
|
if not ok:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"机架 {rack_id} 不存在")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ==================== 配线单元 API ====================
|
|||
|
|
|
|||
|
|
@app.post("/api/racks/{rack_id}/units", response_model=UnitResponse, status_code=201, summary="创建配线单元")
|
|||
|
|
def create_unit(rack_id: str, body: UnitCreate):
|
|||
|
|
"""在指定机架下创建一个新的配线单元,并自动生成指定数量的端口。"""
|
|||
|
|
unit = service.create_unit(
|
|||
|
|
rack_id=rack_id,
|
|||
|
|
unit_number=body.unit_number,
|
|||
|
|
unit_name=body.unit_name,
|
|||
|
|
position=body.position,
|
|||
|
|
port_count=body.port_count,
|
|||
|
|
port_type=body.port_type,
|
|||
|
|
)
|
|||
|
|
if not unit:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"机架 {rack_id} 不存在")
|
|||
|
|
return unit
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.delete("/api/units/{unit_id}", status_code=204, summary="删除配线单元")
|
|||
|
|
def delete_unit(unit_id: str):
|
|||
|
|
"""删除指定配线单元及其所有端口。"""
|
|||
|
|
ok = service.delete_unit(unit_id)
|
|||
|
|
if not ok:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"单元 {unit_id} 不存在")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ==================== 端口 API ====================
|
|||
|
|
|
|||
|
|
@app.get("/api/ports/free", response_model=list[FreePortResponse], summary="查询空闲端口")
|
|||
|
|
def get_free_ports(rack_id: Optional[str] = None):
|
|||
|
|
"""查询所有空闲端口,可选择按机架过滤。"""
|
|||
|
|
return service.get_free_ports(rack_id)
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.get("/api/ports/{port_id}", response_model=PortDetail, summary="获取端口详情")
|
|||
|
|
def get_port_detail(port_id: str):
|
|||
|
|
"""获取指定端口的详细信息,包含所属单元和机架信息。"""
|
|||
|
|
detail = service.get_port_detail(port_id)
|
|||
|
|
if not detail:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"端口 {port_id} 不存在")
|
|||
|
|
return detail
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ==================== 跳接连接 API ====================
|
|||
|
|
|
|||
|
|
@app.get("/api/connections", response_model=list[ConnectionResponse], summary="获取所有跳接连接")
|
|||
|
|
def list_connections():
|
|||
|
|
"""获取所有跳接连接列表。"""
|
|||
|
|
return service.list_connections()
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.post("/api/connections", response_model=ConnectionResponse, status_code=201, summary="创建跳接连接")
|
|||
|
|
def create_connection(body: ConnectionCreate):
|
|||
|
|
"""在两个空闲端口之间创建跳接连接。两个端口状态必须为空闲(FREE)。"""
|
|||
|
|
conn = service.create_connection(
|
|||
|
|
port_a_id=body.port_a_id,
|
|||
|
|
port_b_id=body.port_b_id,
|
|||
|
|
fiber_length=body.fiber_length,
|
|||
|
|
remark=body.remark,
|
|||
|
|
)
|
|||
|
|
if not conn:
|
|||
|
|
raise HTTPException(
|
|||
|
|
status_code=400,
|
|||
|
|
detail="创建连接失败,请检查端口是否存在、是否空闲、是否同一端口",
|
|||
|
|
)
|
|||
|
|
return conn
|
|||
|
|
|
|||
|
|
|
|||
|
|
@app.delete("/api/connections/{connection_id}", status_code=204, summary="删除跳接连接")
|
|||
|
|
def delete_connection(connection_id: str):
|
|||
|
|
"""删除指定跳接连接,并自动释放两个端口的状态为空闲。"""
|
|||
|
|
ok = service.delete_connection(connection_id)
|
|||
|
|
if not ok:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"连接 {connection_id} 不存在")
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ==================== 路径查询 API ====================
|
|||
|
|
|
|||
|
|
@app.get("/api/ports/{port_id}/path", summary="查询端口连接路径")
|
|||
|
|
def get_port_path(port_id: str):
|
|||
|
|
"""查询指定端口的连接路径信息,包括对端端口和连接详情。"""
|
|||
|
|
result = service.get_port_path(port_id)
|
|||
|
|
if not result:
|
|||
|
|
raise HTTPException(status_code=404, detail=f"端口 {port_id} 不存在")
|
|||
|
|
return result
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ==================== 健康检查 ====================
|
|||
|
|
|
|||
|
|
@app.get("/api/health", summary="健康检查")
|
|||
|
|
def health_check():
|
|||
|
|
"""服务健康检查接口。"""
|
|||
|
|
return {"status": "ok", "service": "ODF光纤配线单元管理系统"}
|