diff --git a/src/primaite/simulator/domain/controller.py b/src/primaite/simulator/domain/controller.py index e9f3b26d..bc428743 100644 --- a/src/primaite/simulator/domain/controller.py +++ b/src/primaite/simulator/domain/controller.py @@ -102,7 +102,7 @@ class DomainController(SimComponent): :rtype: Dict """ state = super().describe_state() - state.update({"accounts": {uuid: acct.describe_state() for uuid, acct in self.accounts.items()}}) + state.update({"accounts": {acct.username: acct.describe_state() for acct in self.accounts.values()}}) return state def _register_account(self, account: Account) -> None: diff --git a/src/primaite/simulator/network/container.py b/src/primaite/simulator/network/container.py index e1780448..8d8709d3 100644 --- a/src/primaite/simulator/network/container.py +++ b/src/primaite/simulator/network/container.py @@ -199,10 +199,24 @@ class Network(SimComponent): state = super().describe_state() state.update( { - "nodes": {uuid: node.describe_state() for uuid, node in self.nodes.items()}, - "links": {uuid: link.describe_state() for uuid, link in self.links.items()}, + "nodes": {node.hostname: node.describe_state() for node in self.nodes.values()}, + "links": {}, } ) + # Update the links one-by-one. The key is a 4-tuple of `hostname_a, port_a, hostname_b, port_b` + for uuid, link in self.links.items(): + node_a = link.endpoint_a._connected_node + node_b = link.endpoint_b._connected_node + hostname_a = node_a.hostname if node_a else None + hostname_b = node_b.hostname if node_b else None + port_a = link.endpoint_a._port_num_on_node + port_b = link.endpoint_b._port_num_on_node + state["links"][uuid] = link.describe_state() + state["links"][uuid]["hostname_a"] = hostname_a + state["links"][uuid]["hostname_b"] = hostname_b + state["links"][uuid]["port_a"] = port_a + state["links"][uuid]["port_b"] = port_b + return state def add_node(self, node: Node) -> None: diff --git a/src/primaite/simulator/network/hardware/base.py b/src/primaite/simulator/network/hardware/base.py index a310a3f5..ad3d73aa 100644 --- a/src/primaite/simulator/network/hardware/base.py +++ b/src/primaite/simulator/network/hardware/base.py @@ -91,6 +91,8 @@ class NIC(SimComponent): "Indicates if the NIC supports Wake-on-LAN functionality." _connected_node: Optional[Node] = None "The Node to which the NIC is connected." + _port_num_on_node: Optional[int] = None + "Which port number is assigned on this NIC" _connected_link: Optional[Link] = None "The Link to which the NIC is connected." enabled: bool = False @@ -148,7 +150,7 @@ class NIC(SimComponent): state = super().describe_state() state.update( { - "ip_adress": str(self.ip_address), + "ip_address": str(self.ip_address), "subnet_mask": str(self.subnet_mask), "mac_address": self.mac_address, "speed": self.speed, @@ -311,6 +313,8 @@ class SwitchPort(SimComponent): "The Maximum Transmission Unit (MTU) of the SwitchPort in Bytes. Default is 1500 B" _connected_node: Optional[Node] = None "The Node to which the SwitchPort is connected." + _port_num_on_node: Optional[int] = None + "The port num on the connected node." _connected_link: Optional[Link] = None "The Link to which the SwitchPort is connected." enabled: bool = False @@ -497,8 +501,8 @@ class Link(SimComponent): state = super().describe_state() state.update( { - "endpoint_a": self.endpoint_a.uuid, - "endpoint_b": self.endpoint_b.uuid, + "endpoint_a": self.endpoint_a.uuid, # TODO: consider if using UUID is the best way to do this + "endpoint_b": self.endpoint_b.uuid, # TODO: consider if using UUID is the best way to do this "bandwidth": self.bandwidth, "current_load": self.current_load, } @@ -1094,12 +1098,12 @@ class Node(SimComponent): { "hostname": self.hostname, "operating_state": self.operating_state.value, - "NICs": {uuid: nic.describe_state() for uuid, nic in self.nics.items()}, + "NICs": {eth_num: nic.describe_state() for eth_num, nic in self.ethernet_port.items()}, # "switch_ports": {uuid, sp for uuid, sp in self.switch_ports.items()}, "file_system": self.file_system.describe_state(), - "applications": {uuid: app.describe_state() for uuid, app in self.applications.items()}, - "services": {uuid: svc.describe_state() for uuid, svc in self.services.items()}, - "process": {uuid: proc.describe_state() for uuid, proc in self.processes.items()}, + "applications": {app.name: app.describe_state() for app in self.applications.values()}, + "services": {svc.name: svc.describe_state() for svc in self.services.values()}, + "process": {proc.name: proc.describe_state() for proc in self.processes.values()}, "revealed_to_red": self.revealed_to_red, } ) @@ -1316,6 +1320,7 @@ class Node(SimComponent): self.nics[nic.uuid] = nic self.ethernet_port[len(self.nics)] = nic nic._connected_node = self + nic._port_num_on_node = len(self.nics) nic.parent = self self.sys_log.info(f"Connected NIC {nic}") if self.operating_state == NodeOperatingState.ON: diff --git a/src/primaite/simulator/network/hardware/nodes/switch.py b/src/primaite/simulator/network/hardware/nodes/switch.py index 92999b88..fffae6e2 100644 --- a/src/primaite/simulator/network/hardware/nodes/switch.py +++ b/src/primaite/simulator/network/hardware/nodes/switch.py @@ -30,6 +30,7 @@ class Switch(Node): self.switch_ports = {i: SwitchPort() for i in range(1, self.num_ports + 1)} for port_num, port in self.switch_ports.items(): port._connected_node = self + port._port_num_on_node = port_num port.parent = self port.port_num = port_num diff --git a/src/primaite/simulator/system/processes/process.py b/src/primaite/simulator/system/processes/process.py index ad9af335..b753e3ad 100644 --- a/src/primaite/simulator/system/processes/process.py +++ b/src/primaite/simulator/system/processes/process.py @@ -41,5 +41,5 @@ class Process(Software): :rtype: Dict """ state = super().describe_state() - state.update({"operating_state": self.operating_state.name}) + state.update({"operating_state": self.operating_state.value}) return state