Add methods for adding/removing nodes form network
This commit is contained in:
@@ -137,6 +137,7 @@ class SimComponent(BaseModel):
|
||||
kwargs["uuid"] = str(uuid4())
|
||||
super().__init__(**kwargs)
|
||||
self.action_manager: Optional[ActionManager] = None
|
||||
self._parent: Optional["SimComponent"] = None
|
||||
|
||||
@abstractmethod
|
||||
def describe_state(self) -> Dict:
|
||||
@@ -187,3 +188,24 @@ class SimComponent(BaseModel):
|
||||
Override this method with anything that needs to happen within the component for it to be reset.
|
||||
"""
|
||||
pass
|
||||
|
||||
@property
|
||||
def parent(self) -> "SimComponent":
|
||||
"""Reference to the parent object which manages this object.
|
||||
|
||||
:return: Parent object.
|
||||
:rtype: SimComponent
|
||||
"""
|
||||
return self._parent
|
||||
|
||||
@parent.setter
|
||||
def parent(self, new_parent: "SimComponent") -> None:
|
||||
if self._parent:
|
||||
msg = f"Overwriting parent of {self}, {self._parent} with {new_parent}"
|
||||
_LOGGER.warn(msg)
|
||||
raise RuntimeWarning(msg)
|
||||
self._parent = new_parent
|
||||
|
||||
@parent.deleter
|
||||
def parent(self) -> None:
|
||||
self._parent = None
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from typing import Dict
|
||||
from typing import Any, Dict
|
||||
|
||||
from primaite import getLogger
|
||||
from primaite.simulator.core import Action, ActionManager, AllowAllValidator, SimComponent
|
||||
from primaite.simulator.network.hardware.base import Link, Node
|
||||
|
||||
_LOGGER = getLogger(__name__)
|
||||
|
||||
|
||||
class NetworkContainer(SimComponent):
|
||||
"""Top level container object representing the physical network."""
|
||||
@@ -40,3 +43,45 @@ class NetworkContainer(SimComponent):
|
||||
}
|
||||
)
|
||||
return state
|
||||
|
||||
def add_node(self, node: Node) -> None:
|
||||
"""
|
||||
Add an existing node to the network.
|
||||
|
||||
:param node: Node instance that the network should keep track of.
|
||||
:type node: Node
|
||||
"""
|
||||
if node in self:
|
||||
_LOGGER.warning(f"Can't add node {node}. It is already in the network.")
|
||||
self.nodes[node.uuid] = node
|
||||
node.parent = self
|
||||
|
||||
def remove_node(self, node: Node) -> None:
|
||||
"""
|
||||
Remove a node from the network.
|
||||
|
||||
:param node: Node instance that is currently part of the network that should be removed.
|
||||
:type node: Node
|
||||
"""
|
||||
if node not in self:
|
||||
_LOGGER.warning(f"Can't remove node {node}. It's not in the network.")
|
||||
del self.nodes[node.uuid]
|
||||
del node.parent # misleading?
|
||||
|
||||
def connect_nodes(self, node1: Node, node2: Node) -> None:
|
||||
"""TODO."""
|
||||
# I think we should not be forcing users to add and remove individual links.
|
||||
# Clearly if a link exists between two nodes in the network, then the link is also part of the network.
|
||||
# I'm just not sure how we ought to handle link creation as it requires an unoccupied network device on the node
|
||||
raise NotImplementedError
|
||||
|
||||
def disconnect_nodes(self, node1: Node, node2: Node) -> None:
|
||||
"""TODO."""
|
||||
raise NotImplementedError
|
||||
|
||||
def __contains__(self, item: Any) -> bool:
|
||||
if isinstance(item, Node):
|
||||
return item.uuid in self.nodes
|
||||
elif isinstance(item, Link):
|
||||
return item.uuid in self.links
|
||||
raise TypeError("")
|
||||
|
||||
Reference in New Issue
Block a user