add examples, convert models

This commit is contained in:
2026-03-10 03:32:51 +03:00
parent 324524c66a
commit a7d5c0617d
6 changed files with 54 additions and 13 deletions
+10
View File
@@ -0,0 +1,10 @@
Name,Role,Adapter,Interface,Master Interface,Network,VLAN,Network IP,Mask,Device IP,Default Gateway
PC1,Host,Adapter1,eth1,vlan7,A,,,,,,
PC1,Host,,vlan7,,7,10.10.10.0,/24,10.10.10.7,0.0.0.0,
PC2,Host,Adapter1,eth1,vlan9,B,,,,,,
PC2,Host,,vlan9,,9,10.10.10.0,/24,10.10.10.9,0.0.0.0,
com,Switch,Adapter1,eth2,vlan7,A,7,,,,,
com,Switch,Adapter2,eth3,vlan9,B,9,,,,,
com,Switch,,vlan7,bridge,,,,,,
com,Switch,,vlan9,bridge,,,,,,
com,Switch,,bridge,,,,,,,
1 Name,Role,Adapter,Interface,Master Interface,Network,VLAN,Network IP,Mask,Device IP,Default Gateway
2 PC1,Host,Adapter1,eth1,vlan7,A,,,,,,
3 PC1,Host,,vlan7,,7,10.10.10.0,/24,10.10.10.7,0.0.0.0,
4 PC2,Host,Adapter1,eth1,vlan9,B,,,,,,
5 PC2,Host,,vlan9,,9,10.10.10.0,/24,10.10.10.9,0.0.0.0,
6 com,Switch,Adapter1,eth2,vlan7,A,7,,,,,
7 com,Switch,Adapter2,eth3,vlan9,B,9,,,,,
8 com,Switch,,vlan7,bridge,,,,,,
9 com,Switch,,vlan9,bridge,,,,,,
10 com,Switch,,bridge,,,,,,,
+3
View File
@@ -0,0 +1,3 @@
Name,Role,Adapter,Interface,Network,Network IP,Mask,Device IP,Default Gateway
PC1,Host,Adapter1,eth1,A,10.0.0.0,/24,10.0.0.1,0.0.0.0
PC2,Host,Adapter1,eth1,A,10.0.0.0,/24,10.0.0.2,0.0.0.0
1 Name Role Adapter Interface Network Network IP Mask Device IP Default Gateway
2 PC1 Host Adapter1 eth1 A 10.0.0.0 /24 10.0.0.1 0.0.0.0
3 PC2 Host Adapter1 eth1 A 10.0.0.0 /24 10.0.0.2 0.0.0.0
+11
View File
@@ -0,0 +1,11 @@
Name,Role,Adapter,Interface,Network,VLAN,Network IP,Mask,Device IP,Default Gateway
PC1,Host,Adapter1,eth1,A,4,10.0.0.0,/24,10.0.0.1,0.0.0.0
PC2,Host,Adapter1,eth1,B,2,10.0.0.0,/24,10.0.0.2,0.0.0.0
PC3,Host,Adapter1,eth1,C,3,10.0.0.0,/24,10.0.0.3,0.0.0.0
PC4,Host,Adapter1,eth1,D,4,10.0.0.0,/24,10.0.0.4,0.0.0.0
com_left,Switch,Adapter1,eth1,tr,trunk,,,,
com_left,Switch,Adapter2,eth2,A,4,,,,
com_left,Switch,Adapter3,eth3,B,2,,,,
com_right,Switch,Adapter1,eth1,tr,trunk,,,,
com_right,Switch,Adapter2,eth2,D,4,,,,
com_right,Switch,Adapter3,eth3,C,3,,,,
1 Name Role Adapter Interface Network VLAN Network IP Mask Device IP Default Gateway
2 PC1 Host Adapter1 eth1 A 4 10.0.0.0 /24 10.0.0.1 0.0.0.0
3 PC2 Host Adapter1 eth1 B 2 10.0.0.0 /24 10.0.0.2 0.0.0.0
4 PC3 Host Adapter1 eth1 C 3 10.0.0.0 /24 10.0.0.3 0.0.0.0
5 PC4 Host Adapter1 eth1 D 4 10.0.0.0 /24 10.0.0.4 0.0.0.0
6 com_left Switch Adapter1 eth1 tr trunk
7 com_left Switch Adapter2 eth2 A 4
8 com_left Switch Adapter3 eth3 B 2
9 com_right Switch Adapter1 eth1 tr trunk
10 com_right Switch Adapter2 eth2 D 4
11 com_right Switch Adapter3 eth3 C 3
+1
View File
@@ -1 +1,2 @@
pyaml pyaml
py-d2
+2
View File
@@ -5,6 +5,8 @@ from typing import Any, Dict, List, Optional
class Interface: class Interface:
name: str name: str
adapter: Optional[str]
master_interface: Optional[str]
ip_address: Optional[str] ip_address: Optional[str]
network: Optional[str] network: Optional[str]
default_gateway: Optional[str] default_gateway: Optional[str]
+24 -10
View File
@@ -15,6 +15,8 @@ from . import RawDevices
name_matching = { name_matching = {
"DEVICE_TYPE": "Role", "DEVICE_TYPE": "Role",
"DEVICE_NAME": "Name", "DEVICE_NAME": "Name",
"ADAPTER": "Adapter",
"MASTER": "Master Interface",
"INTERFACE_NAME": "Interface", "INTERFACE_NAME": "Interface",
"NETWORK_NAME": "Network", "NETWORK_NAME": "Network",
"VLAN": "VLAN", "VLAN": "VLAN",
@@ -30,6 +32,10 @@ name_matching = {
} }
def _get_from_field(fields: dict, field_name: str) -> str:
return fields.get(name_matching[field_name], "").strip()
def parse_devices(raw_devices: list[RawDevices]) -> list[Device]: def parse_devices(raw_devices: list[RawDevices]) -> list[Device]:
devices = [] devices = []
@@ -73,22 +79,30 @@ def add_interfaces(devices: list[Device], raw_devices: list[RawDevices]) -> None
f"Device with name '{device_name}' not found for interface parsing" f"Device with name '{device_name}' not found for interface parsing"
) )
if not raw_device.fields.get(name_matching["ADAPTER"], "").strip():
interface = VirtualInterface(
name=_get_from_field(raw_device.fields, "INTERFACE_NAME"),
master_interface=_get_from_field(raw_device.fields, "MASTER"),
ip_address=_get_from_field(raw_device.fields, "IP_ADDRESS"),
network=_get_from_field(raw_device.fields, "NETWORK_NAME"),
default_gateway=_get_from_field(raw_device.fields, "DEFAULT_GATEWAY"),
)
else:
interface = Interface( interface = Interface(
name=raw_device.fields.get(name_matching["INTERFACE_NAME"], "").strip(), name=_get_from_field(raw_device.fields, "INTERFACE_NAME"),
ip_address=raw_device.fields.get(name_matching["IP_ADDRESS"], "").strip(), adapter=_get_from_field(raw_device.fields, "ADAPTER"),
network=raw_device.fields.get(name_matching["NETWORK_NAME"], "").strip(), master_interface=_get_from_field(raw_device.fields, "MASTER"),
default_gateway=raw_device.fields.get( ip_address=_get_from_field(raw_device.fields, "IP_ADDRESS"),
name_matching["DEFAULT_GATEWAY"], "" network=_get_from_field(raw_device.fields, "NETWORK_NAME"),
).strip(), default_gateway=_get_from_field(raw_device.fields, "DEFAULT_GATEWAY"),
) )
device.add_interface(interface) device.add_interface(interface)
return return
def parse_networks( def parse_networks(raw_devices: list[RawDevices]) -> list[Network]:
devices: list[Device], raw_devices: list[RawDevices]
) -> list[Network]:
networks = [] networks = []
for raw_device in raw_devices: for raw_device in raw_devices:
@@ -146,7 +160,7 @@ def convert_raw_topology(raw_devices: list[RawDevices]) -> Topology:
topology.add_device(device) topology.add_device(device)
logging.info(f"Parsing networks from raw devices") logging.info(f"Parsing networks from raw devices")
networks = parse_networks(devices, raw_devices) networks = parse_networks(raw_devices)
assign_interfaces_to_networks(networks, devices) assign_interfaces_to_networks(networks, devices)
for network in networks: for network in networks: