add examples, convert models
This commit is contained in:
@@ -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,,,,,,,
|
||||
|
@@ -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
|
||||
|
@@ -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,,,,
|
||||
|
+2
-1
@@ -1 +1,2 @@
|
||||
pyaml
|
||||
pyaml
|
||||
py-d2
|
||||
@@ -5,6 +5,8 @@ from typing import Any, Dict, List, Optional
|
||||
|
||||
class Interface:
|
||||
name: str
|
||||
adapter: Optional[str]
|
||||
master_interface: Optional[str]
|
||||
ip_address: Optional[str]
|
||||
network: Optional[str]
|
||||
default_gateway: Optional[str]
|
||||
|
||||
@@ -15,6 +15,8 @@ from . import RawDevices
|
||||
name_matching = {
|
||||
"DEVICE_TYPE": "Role",
|
||||
"DEVICE_NAME": "Name",
|
||||
"ADAPTER": "Adapter",
|
||||
"MASTER": "Master Interface",
|
||||
"INTERFACE_NAME": "Interface",
|
||||
"NETWORK_NAME": "Network",
|
||||
"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]:
|
||||
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"
|
||||
)
|
||||
|
||||
interface = Interface(
|
||||
name=raw_device.fields.get(name_matching["INTERFACE_NAME"], "").strip(),
|
||||
ip_address=raw_device.fields.get(name_matching["IP_ADDRESS"], "").strip(),
|
||||
network=raw_device.fields.get(name_matching["NETWORK_NAME"], "").strip(),
|
||||
default_gateway=raw_device.fields.get(
|
||||
name_matching["DEFAULT_GATEWAY"], ""
|
||||
).strip(),
|
||||
)
|
||||
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(
|
||||
name=_get_from_field(raw_device.fields, "INTERFACE_NAME"),
|
||||
adapter=_get_from_field(raw_device.fields, "ADAPTER"),
|
||||
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"),
|
||||
)
|
||||
device.add_interface(interface)
|
||||
|
||||
return
|
||||
|
||||
|
||||
def parse_networks(
|
||||
devices: list[Device], raw_devices: list[RawDevices]
|
||||
) -> list[Network]:
|
||||
def parse_networks(raw_devices: list[RawDevices]) -> list[Network]:
|
||||
networks = []
|
||||
|
||||
for raw_device in raw_devices:
|
||||
@@ -146,7 +160,7 @@ def convert_raw_topology(raw_devices: list[RawDevices]) -> Topology:
|
||||
topology.add_device(device)
|
||||
|
||||
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)
|
||||
|
||||
for network in networks:
|
||||
|
||||
Reference in New Issue
Block a user