daemon: added type hinting to DistributedControll and removed bad logic looking for tunnels during add_link

This commit is contained in:
Blake Harnden 2020-06-09 21:03:19 -07:00
parent 32ad8a9b68
commit 452e0720f2
3 changed files with 18 additions and 70 deletions

View file

@ -37,10 +37,10 @@ class DistributedServer:
:param name: convenience name to associate with host
:param host: host to connect to
"""
self.name = name
self.host = host
self.conn = Connection(host, user="root")
self.lock = threading.Lock()
self.name: str = name
self.host: str = host
self.conn: Connection = Connection(host, user="root")
self.lock: threading.Lock = threading.Lock()
def remote_cmd(
self, cmd: str, env: Dict[str, str] = None, cwd: str = None, wait: bool = True
@ -117,10 +117,10 @@ class DistributedController:
:param session: session
"""
self.session = session
self.servers = OrderedDict()
self.tunnels = {}
self.address = self.session.options.get_config(
self.session: "Session" = session
self.servers: Dict[str, DistributedServer] = OrderedDict()
self.tunnels: Dict[int, Tuple[GreTap, GreTap]] = {}
self.address: str = self.session.options.get_config(
"distributed_address", default=None
)
@ -178,13 +178,10 @@ class DistributedController:
"""
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
if not isinstance(node, CoreNetwork):
continue
if isinstance(node, CtrlNet) and node.serverintf is not None:
continue
for name in self.servers:
server = self.servers[name]
self.create_gre_tunnel(node, server)
@ -195,7 +192,6 @@ class DistributedController:
"""
Create gre tunnel using a pair of gre taps between the local and remote server.
:param node: node to create gre tunnel for
:param server: server to create
tunnel for
@ -243,15 +239,3 @@ class DistributedController:
(self.session.id << 16) ^ utils.hashkey(n1_id) ^ (utils.hashkey(n2_id) << 8)
)
return key & 0xFFFFFFFF
def get_tunnel(self, n1_id: int, n2_id: int) -> GreTap:
"""
Return the GreTap between two nodes if it exists.
:param n1_id: node one id
:param n2_id: node two id
:return: gre tap between nodes or None
"""
key = self.tunnel_key(n1_id, n2_id)
logging.debug("checking for tunnel key(%s) in: %s", key, self.tunnels)
return self.tunnels.get(key)

View file

@ -42,7 +42,7 @@ from core.location.geo import GeoLocation
from core.location.mobility import BasicRangeModel, MobilityManager
from core.nodes.base import CoreNetworkBase, CoreNode, CoreNodeBase, NodeBase
from core.nodes.docker import DockerNode
from core.nodes.interface import CoreInterface, GreTap
from core.nodes.interface import CoreInterface
from core.nodes.lxd import LxcNode
from core.nodes.network import (
CtrlNet,
@ -200,7 +200,6 @@ class Session:
Optional[CoreNode],
Optional[CoreNetworkBase],
Optional[CoreNetworkBase],
GreTap,
]:
"""
Convenience method for retrieving nodes within link data.
@ -221,23 +220,6 @@ class Session:
node_one = self.get_node(node_one_id, NodeBase)
node_two = self.get_node(node_two_id, NodeBase)
# both node ids are provided
tunnel = self.distributed.get_tunnel(node_one_id, node_two_id)
logging.debug("tunnel between nodes: %s", tunnel)
if isinstance(tunnel, GreTapBridge):
net_one = tunnel
if tunnel.remotenum == node_one_id:
node_one = None
else:
node_two = None
# physical node connected via gre tap tunnel
# TODO: double check this cases type
elif tunnel:
if tunnel.remotenum == node_one_id:
node_one = None
else:
node_two = None
if isinstance(node_one, CoreNetworkBase):
if not net_one:
net_one = node_one
@ -253,14 +235,13 @@ class Session:
node_two = None
logging.debug(
"link node types n1(%s) n2(%s) net1(%s) net2(%s) tunnel(%s)",
"link node types n1(%s) n2(%s) net1(%s) net2(%s)",
node_one,
node_two,
net_one,
net_two,
tunnel,
)
return node_one, node_two, net_one, net_two, tunnel
return node_one, node_two, net_one, net_two
def _link_wireless(self, objects: Iterable[CoreNodeBase], connect: bool) -> None:
"""
@ -326,7 +307,7 @@ class Session:
options = LinkOptions()
# get node objects identified by link data
node_one, node_two, net_one, net_two, tunnel = self._link_nodes(
node_one, node_two, net_one, net_two = self._link_nodes(
node_one_id, node_two_id
)
@ -415,23 +396,6 @@ class Session:
net_two.setkey(key)
if addresses:
net_two.addrconfig(addresses)
# physical node connected with tunnel
if not net_one and not net_two and (node_one or node_two):
if node_one and isinstance(node_one, PhysicalNode):
logging.info("adding link for physical node: %s", node_one.name)
addresses = interface_one.get_addresses()
node_one.adoptnetif(
tunnel, interface_one.id, interface_one.mac, addresses
)
node_one.linkconfig(tunnel, options)
elif node_two and isinstance(node_two, PhysicalNode):
logging.info("adding link for physical node: %s", node_two.name)
addresses = interface_two.get_addresses()
node_two.adoptnetif(
tunnel, interface_two.id, interface_two.mac, addresses
)
node_two.linkconfig(tunnel, options)
finally:
if node_one:
node_one.lock.release()
@ -461,7 +425,7 @@ class Session:
:raises core.CoreError: when no common network is found for link being deleted
"""
# get node objects identified by link data
node_one, node_two, net_one, net_two, _tunnel = self._link_nodes(
node_one, node_two, net_one, net_two = self._link_nodes(
node_one_id, node_two_id
)
@ -573,7 +537,7 @@ class Session:
options = LinkOptions()
# get node objects identified by link data
node_one, node_two, net_one, net_two, _tunnel = self._link_nodes(
node_one, node_two, net_one, net_two = self._link_nodes(
node_one_id, node_two_id
)

View file

@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List
from core.config import ConfigurableManager, ConfigurableOptions, Configuration
from core.emulator.enumerations import ConfigDataTypes, RegisterTlvs
@ -10,8 +10,8 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
Provides session configuration.
"""
name = "session"
options = [
name: str = "session"
options: List[Configuration] = [
Configuration(
_id="controlnet", _type=ConfigDataTypes.STRING, label="Control Network"
),
@ -57,7 +57,7 @@ class SessionConfig(ConfigurableManager, ConfigurableOptions):
label="SDT3D URL",
),
]
config_type = RegisterTlvs.UTILITY
config_type: RegisterTlvs = RegisterTlvs.UTILITY
def __init__(self) -> None:
super().__init__()