#2248 - synced wth dev

This commit is contained in:
Chris McCarthy
2024-02-08 15:27:02 +00:00
35 changed files with 1688 additions and 1035 deletions

View File

@@ -78,12 +78,12 @@ class FileSystem(SimComponent):
self._delete_manager.add_request(
name="file",
request_type=RequestType(
func=lambda request, context: self.delete_file_by_id(folder_uuid=request[0], file_uuid=request[1])
func=lambda request, context: self.delete_file(folder_name=request[0], file_name=request[1])
),
)
self._delete_manager.add_request(
name="folder",
request_type=RequestType(func=lambda request, context: self.delete_folder_by_id(folder_uuid=request[0])),
request_type=RequestType(func=lambda request, context: self.delete_folder(folder_name=request[0])),
)
rm.add_request(
name="delete",
@@ -171,7 +171,7 @@ class FileSystem(SimComponent):
self.folders[folder.uuid] = folder
self._folder_request_manager.add_request(
name=folder.uuid, request_type=RequestType(func=folder._request_manager)
name=folder.name, request_type=RequestType(func=folder._request_manager)
)
return folder
@@ -280,7 +280,7 @@ class FileSystem(SimComponent):
sys_log=self.sys_log,
)
folder.add_file(file)
self._file_request_manager.add_request(name=file.uuid, request_type=RequestType(func=file._request_manager))
self._file_request_manager.add_request(name=file.name, request_type=RequestType(func=file._request_manager))
return file
def get_file(self, folder_name: str, file_name: str) -> Optional[File]:

View File

@@ -257,7 +257,7 @@ class Folder(FileSystemItemABC):
# add to list
self.files[file.uuid] = file
self._file_request_manager.add_request(file.uuid, RequestType(func=file._request_manager))
self._file_request_manager.add_request(file.name, RequestType(func=file._request_manager))
file.folder = self
def remove_file(self, file: Optional[File]):

View File

@@ -236,7 +236,7 @@ class Network(SimComponent):
node.parent = self
self._nx_graph.add_node(node.hostname)
_LOGGER.debug(f"Added node {node.uuid} to Network {self.uuid}")
self._node_request_manager.add_request(name=node.uuid, request_type=RequestType(func=node._request_manager))
self._node_request_manager.add_request(name=node.hostname, request_type=RequestType(func=node._request_manager))
def get_node_by_hostname(self, hostname: str) -> Optional[Node]:
"""
@@ -261,7 +261,7 @@ class Network(SimComponent):
:type node: Node
"""
if node not in self:
_LOGGER.warning(f"Can't remove node {node.uuid}. It's not in the network.")
_LOGGER.warning(f"Can't remove node {node.hostname}. It's not in the network.")
return
self.nodes.pop(node.uuid)
for i, _node in self._node_id_map.items():
@@ -269,8 +269,8 @@ class Network(SimComponent):
self._node_id_map.pop(i)
break
node.parent = None
_LOGGER.info(f"Removed node {node.uuid} from network {self.uuid}")
self._node_request_manager.remove_request(name=node.uuid)
self._node_request_manager.remove_request(name=node.hostname)
_LOGGER.info(f"Removed node {node.hostname} from network {self.uuid}")
def connect(
self, endpoint_a: Union[WiredNetworkInterface], endpoint_b: Union[WiredNetworkInterface], **kwargs

View File

@@ -1056,18 +1056,17 @@ class Node(SimComponent):
:param network_interface: The NIC to connect.
:raise NetworkError: If the NIC is already connected.
"""
if network_interface.uuid not in self.network_interfaces:
if network_interface.uuid not in self.network_interface:
self.network_interfaces[network_interface.uuid] = network_interface
self.network_interface[len(self.network_interfaces)] = network_interface
new_nic_num = len(self.network_interfaces)
self.network_interface[new_nic_num] = network_interface
network_interface._connected_node = self
network_interface.port_num = len(self.network_interfaces)
network_interface._port_num_on_node = new_nic_num
network_interface.parent = self
self.sys_log.info(f"Connected Network Interface {network_interface}")
if self.operating_state == NodeOperatingState.ON:
network_interface.enable()
self._nic_request_manager.add_request(
network_interface.uuid, RequestType(func=network_interface._request_manager)
)
self._nic_request_manager.add_request(new_nic_num, RequestType(func=network_interface._request_manager))
else:
msg = f"Cannot connect NIC {network_interface} as it is already connected"
self.sys_log.logger.error(msg)
@@ -1083,17 +1082,20 @@ class Node(SimComponent):
if isinstance(network_interface, str):
network_interface = self.network_interfaces.get(network_interface)
if network_interface or network_interface.uuid in self.network_interfaces:
for port, _nic in self.network_interface.items():
if network_interface == _nic:
network_interface_num = -1
for port, _network_interface in self.network_interface.items():
if network_interface == _network_interface:
self.network_interface.pop(port)
network_interface_num = port
break
self.network_interfaces.pop(network_interface.uuid)
network_interface.parent = None
network_interface.disable()
self.sys_log.info(f"Disconnected Network Interface {network_interface}")
self._nic_request_manager.remove_request(network_interface.uuid)
if network_interface_num != -1:
self._nic_request_manager.remove_request(network_interface_num)
else:
msg = f"Cannot disconnect NIC {network_interface} as it is not connected"
msg = f"Cannot disconnect Network Interface {network_interface} as it is not connected"
self.sys_log.logger.error(msg)
raise NetworkError(msg)
@@ -1142,12 +1144,14 @@ class Node(SimComponent):
:type service: Service
"""
if service in self:
_LOGGER.warning(f"Can't add service {service.uuid} to node {self.uuid}. It's already installed.")
_LOGGER.warning(f"Can't add service {service.name} to node {self.hostname}. It's already installed.")
return
self.services[service.uuid] = service
service.parent = self
service.install() # Perform any additional setup, such as creating files for this service on the node.
self._service_request_manager.add_request(service.uuid, RequestType(func=service._request_manager))
self.sys_log.info(f"Installed service {service.name}")
_LOGGER.debug(f"Added service {service.name} to node {self.hostname}")
self._service_request_manager.add_request(service.name, RequestType(func=service._request_manager))
def uninstall_service(self, service: Service) -> None:
"""
@@ -1157,14 +1161,14 @@ class Node(SimComponent):
:type service: Service
"""
if service not in self:
_LOGGER.warning(f"Can't remove service {service.uuid} from node {self.uuid}. It's not installed.")
_LOGGER.warning(f"Can't remove service {service.name} from node {self.hostname}. It's not installed.")
return
service.uninstall() # Perform additional teardown, such as removing files or restarting the machine.
self.services.pop(service.uuid)
service.parent = None
self.sys_log.info(f"Uninstalled service {service.name}")
_LOGGER.info(f"Removed service {service.uuid} from node {self.uuid}")
self._service_request_manager.remove_request(service.uuid)
_LOGGER.info(f"Removed service {service.name} from node {self.hostname}")
self._service_request_manager.remove_request(service.name)
def install_application(self, application: Application) -> None:
"""
@@ -1174,11 +1178,15 @@ class Node(SimComponent):
:type application: Application
"""
if application in self:
_LOGGER.warning(f"Can't add application {application.uuid} to node {self.uuid}. It's already installed.")
_LOGGER.warning(
f"Can't add application {application.name} to node {self.hostname}. It's already installed."
)
return
self.applications[application.uuid] = application
application.parent = self
self._application_request_manager.add_request(application.uuid, RequestType(func=application._request_manager))
self.sys_log.info(f"Installed application {application.name}")
_LOGGER.debug(f"Added application {application.name} to node {self.hostname}")
self._application_request_manager.add_request(application.name, RequestType(func=application._request_manager))
def uninstall_application(self, application: Application) -> None:
"""
@@ -1188,13 +1196,15 @@ class Node(SimComponent):
:type application: Application
"""
if application not in self:
_LOGGER.warning(f"Can't remove application {application.uuid} from node {self.uuid}. It's not installed.")
_LOGGER.warning(
f"Can't remove application {application.name} from node {self.hostname}. It's not installed."
)
return
self.applications.pop(application.uuid)
application.parent = None
self.sys_log.info(f"Uninstalled application {application.name}")
_LOGGER.info(f"Removed application {application.uuid} from node {self.uuid}")
self._application_request_manager.remove_request(application.uuid)
_LOGGER.info(f"Removed application {application.name} from node {self.hostname}")
self._application_request_manager.remove_request(application.name)
def _shut_down_actions(self):
"""Actions to perform when the node is shut down."""

View File

@@ -201,6 +201,15 @@ class AccessControlList(SimComponent):
"""
return self._acl
@property
def num_rules(self) -> int:
"""
Get the number of rules in the ACL.
:return: The number of rules in the ACL.
"""
return len([rule for rule in self._acl if rule is not None])
def add_rule(
self,
action: ACLAction,
@@ -357,6 +366,15 @@ class AccessControlList(SimComponent):
)
print(table)
@property
def num_rules(self) -> int:
"""
Get the number of rules in the ACL.
:return: The number of rules in the ACL.
"""
return len([rule for rule in self._acl if rule is not None])
class RouteEntry(SimComponent):
"""

View File

@@ -72,6 +72,7 @@ class WebBrowser(Application):
"""
state = super().describe_state()
state["last_response_status_code"] = self.latest_response.status_code if self.latest_response else None
return state
def reset_component_for_episode(self, episode: int):
"""Reset the original state of the SimComponent."""