126 lines
3.7 KiB
Python
126 lines
3.7 KiB
Python
|
|
# models/base_switch.py
|
||
|
|
from typing import List, Optional
|
||
|
|
|
||
|
|
class PowerSupply:
|
||
|
|
def __init__(self, id: int, state: str, mode: Optional[str] = None):
|
||
|
|
self.id = id
|
||
|
|
self.state = state
|
||
|
|
self.mode = mode
|
||
|
|
self.summary = f"Alimentation {id} en état {state}"
|
||
|
|
|
||
|
|
def to_dict(self):
|
||
|
|
return {
|
||
|
|
"object": "power_supply",
|
||
|
|
"id": self.id,
|
||
|
|
"state": self.state,
|
||
|
|
"mode": self.mode,
|
||
|
|
"summary": self.summary,
|
||
|
|
}
|
||
|
|
|
||
|
|
class Fan:
|
||
|
|
def __init__(self, id: int, state: str):
|
||
|
|
self.id = id
|
||
|
|
self.state = state
|
||
|
|
self.summary = f"Ventilateur {id} en état {state}"
|
||
|
|
|
||
|
|
def to_dict(self):
|
||
|
|
return {
|
||
|
|
"object": "fan",
|
||
|
|
"id": self.id,
|
||
|
|
"state": self.state,
|
||
|
|
"summary": self.summary,
|
||
|
|
}
|
||
|
|
|
||
|
|
class MACEntry:
|
||
|
|
def __init__(self, mac: str, vlan: int, port: str, type_: str):
|
||
|
|
self.mac = mac
|
||
|
|
self.vlan = vlan
|
||
|
|
self.port = port
|
||
|
|
self.type = type_
|
||
|
|
|
||
|
|
def to_dict(self):
|
||
|
|
return {
|
||
|
|
"object": "mac_entry",
|
||
|
|
"mac": self.mac,
|
||
|
|
"vlan": self.vlan,
|
||
|
|
"port": self.port,
|
||
|
|
"type": self.type,
|
||
|
|
}
|
||
|
|
|
||
|
|
class Port:
|
||
|
|
def __init__(self, name: str):
|
||
|
|
self.object = "port"
|
||
|
|
self.name = name
|
||
|
|
self.status = None
|
||
|
|
self.speed = None
|
||
|
|
self.vlan = None
|
||
|
|
self.link_type = None
|
||
|
|
self.trunk_vlans = []
|
||
|
|
self.bpdu_protection = False
|
||
|
|
self.portfast = False
|
||
|
|
self.loopback_detection = None
|
||
|
|
self.mac_addresses = []
|
||
|
|
self.summary = ""
|
||
|
|
|
||
|
|
def to_dict(self):
|
||
|
|
return {
|
||
|
|
"object": "port",
|
||
|
|
"name": self.name,
|
||
|
|
"status": self.status,
|
||
|
|
"speed": self.speed,
|
||
|
|
"vlan": self.vlan,
|
||
|
|
"link_type": self.link_type,
|
||
|
|
"trunk_vlans": self.trunk_vlans if self.link_type == "trunk" else None,
|
||
|
|
"bpdu_protection": self.bpdu_protection,
|
||
|
|
"portfast": self.portfast,
|
||
|
|
"loopback_detection": self.loopback_detection,
|
||
|
|
"mac_addresses": [m.to_dict() for m in self.mac_addresses],
|
||
|
|
"summary": self.summary,
|
||
|
|
}
|
||
|
|
|
||
|
|
class VLAN:
|
||
|
|
def __init__(self, vlan_id: int, name: Optional[str] = None, description: Optional[str] = None):
|
||
|
|
self.vlan_id = vlan_id
|
||
|
|
self.name = name
|
||
|
|
self.description = description
|
||
|
|
self.summary = f"VLAN {vlan_id} : {name or 'non nommé'}"
|
||
|
|
|
||
|
|
def to_dict(self):
|
||
|
|
return {
|
||
|
|
"object": "vlan",
|
||
|
|
"vlan_id": self.vlan_id,
|
||
|
|
"name": self.name,
|
||
|
|
"description": self.description,
|
||
|
|
"summary": self.summary,
|
||
|
|
}
|
||
|
|
|
||
|
|
class Switch:
|
||
|
|
def __init__(self):
|
||
|
|
self.hostname = None
|
||
|
|
self.model = None
|
||
|
|
self.firmware_version = None
|
||
|
|
self.boot_image = None
|
||
|
|
self.uptime = None
|
||
|
|
self.ports: List[Port] = []
|
||
|
|
self.vlans: List[VLAN] = []
|
||
|
|
self.fans: List[Fan] = []
|
||
|
|
self.power_supplies: List[PowerSupply] = []
|
||
|
|
self.mac_table: List[MACEntry] = []
|
||
|
|
self.summary = ""
|
||
|
|
|
||
|
|
def to_dict(self):
|
||
|
|
return {
|
||
|
|
"object": "switch",
|
||
|
|
"hostname": self.hostname,
|
||
|
|
"model": self.model,
|
||
|
|
"firmware_version": self.firmware_version,
|
||
|
|
"boot_image": self.boot_image,
|
||
|
|
"uptime": self.uptime,
|
||
|
|
"summary": self.summary,
|
||
|
|
"fans": [f.to_dict() for f in self.fans],
|
||
|
|
"power_supplies": [p.to_dict() for p in self.power_supplies],
|
||
|
|
"ports": [p.to_dict() for p in self.ports],
|
||
|
|
"vlans": [v.to_dict() for v in self.vlans],
|
||
|
|
"mac_table": [m.to_dict() for m in self.mac_table],
|
||
|
|
}
|