added type hinting for core.nodes
This commit is contained in:
parent
4e71759ac9
commit
c0fcc91d10
10 changed files with 534 additions and 360 deletions
|
@ -4,18 +4,31 @@ virtual ethernet classes that implement the interfaces available under Linux.
|
|||
|
||||
import logging
|
||||
import time
|
||||
from typing import TYPE_CHECKING, Callable, Dict, List, Tuple
|
||||
|
||||
from core import utils
|
||||
from core.errors import CoreCommandError
|
||||
from core.nodes.netclient import get_net_client
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.emulator.distributed import DistributedServer
|
||||
from core.emulator.session import Session
|
||||
from core.nodes.base import CoreNetworkBase, CoreNode
|
||||
|
||||
|
||||
class CoreInterface:
|
||||
"""
|
||||
Base class for network interfaces.
|
||||
"""
|
||||
|
||||
def __init__(self, session, node, name, mtu, server=None):
|
||||
def __init__(
|
||||
self,
|
||||
session: "Session",
|
||||
node: "CoreNode",
|
||||
name: str,
|
||||
mtu: int,
|
||||
server: "DistributedServer" = None,
|
||||
) -> None:
|
||||
"""
|
||||
Creates a CoreInterface instance.
|
||||
|
||||
|
@ -50,7 +63,14 @@ class CoreInterface:
|
|||
use_ovs = session.options.get_config("ovs") == "True"
|
||||
self.net_client = get_net_client(use_ovs, self.host_cmd)
|
||||
|
||||
def host_cmd(self, args, env=None, cwd=None, wait=True, shell=False):
|
||||
def host_cmd(
|
||||
self,
|
||||
args: str,
|
||||
env: Dict[str, str] = None,
|
||||
cwd: str = None,
|
||||
wait: bool = True,
|
||||
shell: bool = False,
|
||||
) -> str:
|
||||
"""
|
||||
Runs a command on the host system or distributed server.
|
||||
|
||||
|
@ -68,7 +88,7 @@ class CoreInterface:
|
|||
else:
|
||||
return self.server.remote_cmd(args, env, cwd, wait)
|
||||
|
||||
def startup(self):
|
||||
def startup(self) -> None:
|
||||
"""
|
||||
Startup method for the interface.
|
||||
|
||||
|
@ -76,7 +96,7 @@ class CoreInterface:
|
|||
"""
|
||||
pass
|
||||
|
||||
def shutdown(self):
|
||||
def shutdown(self) -> None:
|
||||
"""
|
||||
Shutdown method for the interface.
|
||||
|
||||
|
@ -84,7 +104,7 @@ class CoreInterface:
|
|||
"""
|
||||
pass
|
||||
|
||||
def attachnet(self, net):
|
||||
def attachnet(self, net: "CoreNetworkBase") -> None:
|
||||
"""
|
||||
Attach network.
|
||||
|
||||
|
@ -98,7 +118,7 @@ class CoreInterface:
|
|||
net.attach(self)
|
||||
self.net = net
|
||||
|
||||
def detachnet(self):
|
||||
def detachnet(self) -> None:
|
||||
"""
|
||||
Detach from a network.
|
||||
|
||||
|
@ -107,7 +127,7 @@ class CoreInterface:
|
|||
if self.net is not None:
|
||||
self.net.detach(self)
|
||||
|
||||
def addaddr(self, addr):
|
||||
def addaddr(self, addr: str) -> None:
|
||||
"""
|
||||
Add address.
|
||||
|
||||
|
@ -117,7 +137,7 @@ class CoreInterface:
|
|||
addr = utils.validate_ip(addr)
|
||||
self.addrlist.append(addr)
|
||||
|
||||
def deladdr(self, addr):
|
||||
def deladdr(self, addr: str) -> None:
|
||||
"""
|
||||
Delete address.
|
||||
|
||||
|
@ -126,7 +146,7 @@ class CoreInterface:
|
|||
"""
|
||||
self.addrlist.remove(addr)
|
||||
|
||||
def sethwaddr(self, addr):
|
||||
def sethwaddr(self, addr: str) -> None:
|
||||
"""
|
||||
Set hardware address.
|
||||
|
||||
|
@ -136,7 +156,7 @@ class CoreInterface:
|
|||
addr = utils.validate_mac(addr)
|
||||
self.hwaddr = addr
|
||||
|
||||
def getparam(self, key):
|
||||
def getparam(self, key: str) -> float:
|
||||
"""
|
||||
Retrieve a parameter from the, or None if the parameter does not exist.
|
||||
|
||||
|
@ -145,7 +165,7 @@ class CoreInterface:
|
|||
"""
|
||||
return self._params.get(key)
|
||||
|
||||
def getparams(self):
|
||||
def getparams(self) -> List[Tuple[str, float]]:
|
||||
"""
|
||||
Return (key, value) pairs for parameters.
|
||||
"""
|
||||
|
@ -154,7 +174,7 @@ class CoreInterface:
|
|||
parameters.append((k, self._params[k]))
|
||||
return parameters
|
||||
|
||||
def setparam(self, key, value):
|
||||
def setparam(self, key: str, value: float) -> bool:
|
||||
"""
|
||||
Set a parameter value, returns True if the parameter has changed.
|
||||
|
||||
|
@ -174,7 +194,7 @@ class CoreInterface:
|
|||
self._params[key] = value
|
||||
return True
|
||||
|
||||
def swapparams(self, name):
|
||||
def swapparams(self, name: str) -> None:
|
||||
"""
|
||||
Swap out parameters dict for name. If name does not exist,
|
||||
intialize it. This is for supporting separate upstream/downstream
|
||||
|
@ -189,7 +209,7 @@ class CoreInterface:
|
|||
self._params = getattr(self, name)
|
||||
setattr(self, name, tmp)
|
||||
|
||||
def setposition(self, x, y, z):
|
||||
def setposition(self, x: float, y: float, z: float) -> None:
|
||||
"""
|
||||
Dispatch position hook handler.
|
||||
|
||||
|
@ -200,7 +220,7 @@ class CoreInterface:
|
|||
"""
|
||||
self.poshook(self, x, y, z)
|
||||
|
||||
def __lt__(self, other):
|
||||
def __lt__(self, other: "CoreInterface") -> bool:
|
||||
"""
|
||||
Used for comparisons of this object.
|
||||
|
||||
|
@ -217,8 +237,15 @@ class Veth(CoreInterface):
|
|||
"""
|
||||
|
||||
def __init__(
|
||||
self, session, node, name, localname, mtu=1500, server=None, start=True
|
||||
):
|
||||
self,
|
||||
session: "Session",
|
||||
node: "CoreNode",
|
||||
name: str,
|
||||
localname: str,
|
||||
mtu: int = 1500,
|
||||
server: "DistributedServer" = None,
|
||||
start: bool = True,
|
||||
) -> None:
|
||||
"""
|
||||
Creates a VEth instance.
|
||||
|
||||
|
@ -239,7 +266,7 @@ class Veth(CoreInterface):
|
|||
if start:
|
||||
self.startup()
|
||||
|
||||
def startup(self):
|
||||
def startup(self) -> None:
|
||||
"""
|
||||
Interface startup logic.
|
||||
|
||||
|
@ -250,7 +277,7 @@ class Veth(CoreInterface):
|
|||
self.net_client.device_up(self.localname)
|
||||
self.up = True
|
||||
|
||||
def shutdown(self):
|
||||
def shutdown(self) -> None:
|
||||
"""
|
||||
Interface shutdown logic.
|
||||
|
||||
|
@ -280,8 +307,15 @@ class TunTap(CoreInterface):
|
|||
"""
|
||||
|
||||
def __init__(
|
||||
self, session, node, name, localname, mtu=1500, server=None, start=True
|
||||
):
|
||||
self,
|
||||
session: "Session",
|
||||
node: "CoreNode",
|
||||
name: str,
|
||||
localname: str,
|
||||
mtu: int = 1500,
|
||||
server: "DistributedServer" = None,
|
||||
start: bool = True,
|
||||
) -> None:
|
||||
"""
|
||||
Create a TunTap instance.
|
||||
|
||||
|
@ -301,7 +335,7 @@ class TunTap(CoreInterface):
|
|||
if start:
|
||||
self.startup()
|
||||
|
||||
def startup(self):
|
||||
def startup(self) -> None:
|
||||
"""
|
||||
Startup logic for a tunnel tap.
|
||||
|
||||
|
@ -315,7 +349,7 @@ class TunTap(CoreInterface):
|
|||
# self.install()
|
||||
self.up = True
|
||||
|
||||
def shutdown(self):
|
||||
def shutdown(self) -> None:
|
||||
"""
|
||||
Shutdown functionality for a tunnel tap.
|
||||
|
||||
|
@ -331,7 +365,9 @@ class TunTap(CoreInterface):
|
|||
|
||||
self.up = False
|
||||
|
||||
def waitfor(self, func, attempts=10, maxretrydelay=0.25):
|
||||
def waitfor(
|
||||
self, func: Callable, attempts: int = 10, maxretrydelay: float = 0.25
|
||||
) -> bool:
|
||||
"""
|
||||
Wait for func() to return zero with exponential backoff.
|
||||
|
||||
|
@ -362,7 +398,7 @@ class TunTap(CoreInterface):
|
|||
|
||||
return result
|
||||
|
||||
def waitfordevicelocal(self):
|
||||
def waitfordevicelocal(self) -> None:
|
||||
"""
|
||||
Check for presence of a local device - tap device may not
|
||||
appear right away waits
|
||||
|
@ -381,7 +417,7 @@ class TunTap(CoreInterface):
|
|||
|
||||
self.waitfor(localdevexists)
|
||||
|
||||
def waitfordevicenode(self):
|
||||
def waitfordevicenode(self) -> None:
|
||||
"""
|
||||
Check for presence of a node device - tap device may not appear right away waits.
|
||||
|
||||
|
@ -412,7 +448,7 @@ class TunTap(CoreInterface):
|
|||
else:
|
||||
raise RuntimeError("node device failed to exist")
|
||||
|
||||
def install(self):
|
||||
def install(self) -> None:
|
||||
"""
|
||||
Install this TAP into its namespace. This is not done from the
|
||||
startup() method but called at a later time when a userspace
|
||||
|
@ -428,7 +464,7 @@ class TunTap(CoreInterface):
|
|||
self.node.node_net_client.device_name(self.localname, self.name)
|
||||
self.node.node_net_client.device_up(self.name)
|
||||
|
||||
def setaddrs(self):
|
||||
def setaddrs(self) -> None:
|
||||
"""
|
||||
Set interface addresses based on self.addrlist.
|
||||
|
||||
|
@ -448,18 +484,18 @@ class GreTap(CoreInterface):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
node=None,
|
||||
name=None,
|
||||
session=None,
|
||||
mtu=1458,
|
||||
remoteip=None,
|
||||
_id=None,
|
||||
localip=None,
|
||||
ttl=255,
|
||||
key=None,
|
||||
start=True,
|
||||
server=None,
|
||||
):
|
||||
node: "CoreNode" = None,
|
||||
name: str = None,
|
||||
session: "Session" = None,
|
||||
mtu: int = 1458,
|
||||
remoteip: str = None,
|
||||
_id: int = None,
|
||||
localip: str = None,
|
||||
ttl: int = 255,
|
||||
key: int = None,
|
||||
start: bool = True,
|
||||
server: "DistributedServer" = None,
|
||||
) -> None:
|
||||
"""
|
||||
Creates a GreTap instance.
|
||||
|
||||
|
@ -497,7 +533,7 @@ class GreTap(CoreInterface):
|
|||
self.net_client.device_up(self.localname)
|
||||
self.up = True
|
||||
|
||||
def shutdown(self):
|
||||
def shutdown(self) -> None:
|
||||
"""
|
||||
Shutdown logic for a GreTap.
|
||||
|
||||
|
@ -512,7 +548,7 @@ class GreTap(CoreInterface):
|
|||
|
||||
self.localname = None
|
||||
|
||||
def data(self, message_type):
|
||||
def data(self, message_type: int) -> None:
|
||||
"""
|
||||
Data for a gre tap.
|
||||
|
||||
|
@ -521,7 +557,7 @@ class GreTap(CoreInterface):
|
|||
"""
|
||||
return None
|
||||
|
||||
def all_link_data(self, flags):
|
||||
def all_link_data(self, flags: int) -> List:
|
||||
"""
|
||||
Retrieve link data.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue