added type hinting to core.xml
This commit is contained in:
parent
02156867e2
commit
8cd8b2ae2c
3 changed files with 178 additions and 93 deletions
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
import socket
|
||||
from typing import TYPE_CHECKING, List, Tuple
|
||||
|
||||
import netaddr
|
||||
from lxml import etree
|
||||
|
@ -7,26 +8,40 @@ from lxml import etree
|
|||
from core import utils
|
||||
from core.constants import IP_BIN
|
||||
from core.emane.nodes import EmaneNet
|
||||
from core.nodes.base import CoreNodeBase
|
||||
from core.nodes.base import CoreNodeBase, NodeBase
|
||||
from core.nodes.interface import CoreInterface
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.emulator.session import Session
|
||||
|
||||
|
||||
def add_type(parent_element, name):
|
||||
def add_type(parent_element: etree.Element, name: str) -> None:
|
||||
type_element = etree.SubElement(parent_element, "type")
|
||||
type_element.text = name
|
||||
|
||||
|
||||
def add_address(parent_element, address_type, address, interface_name=None):
|
||||
def add_address(
|
||||
parent_element: etree.Element,
|
||||
address_type: str,
|
||||
address: str,
|
||||
interface_name: str = None,
|
||||
) -> None:
|
||||
address_element = etree.SubElement(parent_element, "address", type=address_type)
|
||||
address_element.text = address
|
||||
if interface_name is not None:
|
||||
address_element.set("iface", interface_name)
|
||||
|
||||
|
||||
def add_mapping(parent_element, maptype, mapref):
|
||||
def add_mapping(parent_element: etree.Element, maptype: str, mapref: str) -> None:
|
||||
etree.SubElement(parent_element, "mapping", type=maptype, ref=mapref)
|
||||
|
||||
|
||||
def add_emane_interface(host_element, netif, platform_name="p1", transport_name="t1"):
|
||||
def add_emane_interface(
|
||||
host_element: etree.Element,
|
||||
netif: CoreInterface,
|
||||
platform_name: str = "p1",
|
||||
transport_name: str = "t1",
|
||||
) -> etree.Element:
|
||||
nem_id = netif.net.nemidmap[netif]
|
||||
host_id = host_element.get("id")
|
||||
|
||||
|
@ -54,7 +69,7 @@ def add_emane_interface(host_element, netif, platform_name="p1", transport_name=
|
|||
return platform_element
|
||||
|
||||
|
||||
def get_address_type(address):
|
||||
def get_address_type(address: str) -> str:
|
||||
addr, _slash, _prefixlen = address.partition("/")
|
||||
if netaddr.valid_ipv4(addr):
|
||||
address_type = "IPv4"
|
||||
|
@ -65,7 +80,7 @@ def get_address_type(address):
|
|||
return address_type
|
||||
|
||||
|
||||
def get_ipv4_addresses(hostname):
|
||||
def get_ipv4_addresses(hostname: str) -> List[Tuple[str, str]]:
|
||||
if hostname == "localhost":
|
||||
addresses = []
|
||||
args = f"{IP_BIN} -o -f inet address show"
|
||||
|
@ -85,7 +100,7 @@ def get_ipv4_addresses(hostname):
|
|||
|
||||
|
||||
class CoreXmlDeployment:
|
||||
def __init__(self, session, scenario):
|
||||
def __init__(self, session: "Session", scenario: etree.Element) -> None:
|
||||
self.session = session
|
||||
self.scenario = scenario
|
||||
self.root = etree.SubElement(
|
||||
|
@ -93,17 +108,17 @@ class CoreXmlDeployment:
|
|||
)
|
||||
self.add_deployment()
|
||||
|
||||
def find_device(self, name):
|
||||
def find_device(self, name: str) -> etree.Element:
|
||||
device = self.scenario.find(f"devices/device[@name='{name}']")
|
||||
return device
|
||||
|
||||
def find_interface(self, device, name):
|
||||
def find_interface(self, device: NodeBase, name: str) -> etree.Element:
|
||||
interface = self.scenario.find(
|
||||
f"devices/device[@name='{device.name}']/interfaces/interface[@name='{name}']"
|
||||
)
|
||||
return interface
|
||||
|
||||
def add_deployment(self):
|
||||
def add_deployment(self) -> None:
|
||||
physical_host = self.add_physical_host(socket.gethostname())
|
||||
|
||||
for node_id in self.session.nodes:
|
||||
|
@ -111,7 +126,7 @@ class CoreXmlDeployment:
|
|||
if isinstance(node, CoreNodeBase):
|
||||
self.add_virtual_host(physical_host, node)
|
||||
|
||||
def add_physical_host(self, name):
|
||||
def add_physical_host(self, name: str) -> etree.Element:
|
||||
# add host
|
||||
root_id = self.root.get("id")
|
||||
host_id = f"{root_id}/{name}"
|
||||
|
@ -126,7 +141,7 @@ class CoreXmlDeployment:
|
|||
|
||||
return host_element
|
||||
|
||||
def add_virtual_host(self, physical_host, node):
|
||||
def add_virtual_host(self, physical_host: etree.Element, node: NodeBase) -> None:
|
||||
if not isinstance(node, CoreNodeBase):
|
||||
raise TypeError(f"invalid node type: {node}")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue