t
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
@@ -0,0 +1 @@
|
|||||||
|
pyaml
|
||||||
+5
-9
@@ -3,11 +3,10 @@ from .domain.models import Topology
|
|||||||
from .parse.convert_raw import (
|
from .parse.convert_raw import (
|
||||||
convert_raw_topology,
|
convert_raw_topology,
|
||||||
)
|
)
|
||||||
|
from .output.graphviz import generate_diagram
|
||||||
|
from .output.file_convert import make_yaml
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# from .make_res import make_result
|
|
||||||
# from .structures import Result
|
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
|
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
|
||||||
)
|
)
|
||||||
@@ -17,13 +16,10 @@ def run():
|
|||||||
raw_devices = parse_csv("data/input/table.csv")
|
raw_devices = parse_csv("data/input/table.csv")
|
||||||
# print(raw_devices)
|
# print(raw_devices)
|
||||||
topology = convert_raw_topology(raw_devices)
|
topology = convert_raw_topology(raw_devices)
|
||||||
print(topology)
|
# print(topology)
|
||||||
|
|
||||||
# 2) make structured result
|
generate_diagram(topology, "data/output/diagram.png")
|
||||||
# result = make_result(devices_raw, network_agg, network_interfaces)
|
make_yaml(topology, "data/output/topology.yaml")
|
||||||
|
|
||||||
# # 3) output (for debug)
|
|
||||||
# print(result)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import yaml
|
||||||
|
|
||||||
|
from ..domain.models import Topology
|
||||||
|
|
||||||
|
"""
|
||||||
|
Example:
|
||||||
|
links:
|
||||||
|
networks:
|
||||||
|
- A: [PC1.eth0, PC2.eth0]
|
||||||
|
meta:
|
||||||
|
id: host-host.csv
|
||||||
|
name: host-host.csv
|
||||||
|
nodes:
|
||||||
|
- role: host
|
||||||
|
name: PC1
|
||||||
|
interfaces:
|
||||||
|
- eth0:
|
||||||
|
- ip: 10.0.12.1/24
|
||||||
|
network: A
|
||||||
|
- role: host
|
||||||
|
name: PC2
|
||||||
|
interfaces:
|
||||||
|
- eth0:
|
||||||
|
- ip: 10.0.12.2/24
|
||||||
|
network: A
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def make_yaml(topology: Topology, output_path: str) -> None:
|
||||||
|
pass
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
from ..domain.models import Topology
|
||||||
|
import graphviz
|
||||||
|
|
||||||
|
|
||||||
|
def generate_diagram(topology: Topology, output_path: str) -> None:
|
||||||
|
dot = graphviz.Graph(name="Network Topology", format="png", engine="neato")
|
||||||
|
dot.attr(overlap="false", splines="true")
|
||||||
|
|
||||||
|
for device in topology.devices.values():
|
||||||
|
shape = "box"
|
||||||
|
dot.node(device.name, label=device.name, shape=shape)
|
||||||
|
|
||||||
|
for network in topology.networks.values():
|
||||||
|
interfaces_with_device = [
|
||||||
|
iface for iface in network.interfaces if iface.device is not None
|
||||||
|
]
|
||||||
|
|
||||||
|
for i, iface_a in enumerate(interfaces_with_device):
|
||||||
|
for iface_b in interfaces_with_device[i + 1 :]:
|
||||||
|
label = network.name or ""
|
||||||
|
dot.edge(iface_a.device.name, iface_b.device.name, label=label)
|
||||||
|
|
||||||
|
output_path = output_path[:-4] if output_path.endswith(".png") else output_path
|
||||||
|
dot.render(output_path, cleanup=True)
|
||||||
|
|
||||||
|
return
|
||||||
@@ -10,6 +10,7 @@ from ..domain.models import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from . import RawDevices
|
from . import RawDevices
|
||||||
|
import logging
|
||||||
|
|
||||||
name_matching = {
|
name_matching = {
|
||||||
"DEVICE_TYPE": "Role",
|
"DEVICE_TYPE": "Role",
|
||||||
@@ -136,12 +137,15 @@ def assign_interfaces_to_networks(
|
|||||||
def convert_raw_topology(raw_devices: list[RawDevices]) -> Topology:
|
def convert_raw_topology(raw_devices: list[RawDevices]) -> Topology:
|
||||||
topology = Topology()
|
topology = Topology()
|
||||||
|
|
||||||
|
logging.info(f"Parsing {len(raw_devices)} raw devices to devices")
|
||||||
devices = parse_devices(raw_devices)
|
devices = parse_devices(raw_devices)
|
||||||
|
logging.info(f"Parsing interfaces from raw devices")
|
||||||
add_interfaces(devices, raw_devices)
|
add_interfaces(devices, raw_devices)
|
||||||
|
|
||||||
for device in devices:
|
for device in devices:
|
||||||
topology.add_device(device)
|
topology.add_device(device)
|
||||||
|
|
||||||
|
logging.info(f"Parsing networks from raw devices")
|
||||||
networks = parse_networks(devices, raw_devices)
|
networks = parse_networks(devices, raw_devices)
|
||||||
assign_interfaces_to_networks(networks, devices)
|
assign_interfaces_to_networks(networks, devices)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user