daemon: removed usages of getattr and hasattr where possible

This commit is contained in:
Blake Harnden 2021-01-12 21:30:48 -08:00
parent 6ef458fc74
commit c2a40dbb6b
9 changed files with 30 additions and 28 deletions

View file

@ -25,7 +25,9 @@ from core.emulator.session import Session
from core.errors import CoreError
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
from core.nodes.base import CoreNode, CoreNodeBase, NodeBase
from core.nodes.docker import DockerNode
from core.nodes.interface import CoreInterface
from core.nodes.lxd import LxcNode
from core.nodes.network import WlanNode
from core.services.coreservices import CoreService
@ -264,19 +266,22 @@ def get_node_proto(session: Session, node: NodeBase) -> core_pb2.Node:
geo = core_pb2.Geo(
lat=node.position.lat, lon=node.position.lon, alt=node.position.alt
)
services = getattr(node, "services", [])
if services is None:
services = []
services = [x.name for x in services]
config_services = getattr(node, "config_services", {})
config_services = [x for x in config_services]
services = [x.name for x in node.services]
model = node.type
node_dir = None
config_services = []
if isinstance(node, CoreNodeBase):
node_dir = node.nodedir
config_services = [x for x in node.config_services]
channel = None
if isinstance(node, CoreNode):
channel = node.ctrlchnlname
emane_model = None
if isinstance(node, EmaneNet):
emane_model = node.model.name
model = getattr(node, "type", None)
node_dir = getattr(node, "nodedir", None)
channel = getattr(node, "ctrlchnlname", None)
image = getattr(node, "image", None)
image = None
if isinstance(node, (DockerNode, LxcNode)):
image = node.image
return core_pb2.Node(
id=node.id,
name=node.name,

View file

@ -103,8 +103,7 @@ class FRRZebra(ConfigService):
ip4s.append(str(ip4.ip))
for ip6 in iface.ip6s:
ip6s.append(str(ip6.ip))
is_control = getattr(iface, "control", False)
ifaces.append((iface, ip4s, ip6s, is_control))
ifaces.append((iface, ip4s, ip6s, iface.control))
return dict(
frr_conf=frr_conf,

View file

@ -104,8 +104,7 @@ class Zebra(ConfigService):
ip4s.append(str(ip4.ip))
for ip6 in iface.ip6s:
ip6s.append(str(ip6.ip))
is_control = getattr(iface, "control", False)
ifaces.append((iface, ip4s, ip6s, is_control))
ifaces.append((iface, ip4s, ip6s, iface.control))
return dict(
quagga_bin_search=quagga_bin_search,

View file

@ -159,7 +159,7 @@ class NodeBase(abc.ABC):
ifaces = []
for iface_id in sorted(self.ifaces):
iface = self.ifaces[iface_id]
if not control and getattr(iface, "control", False):
if not control and iface.control:
continue
ifaces.append(iface)
return ifaces

View file

@ -73,6 +73,7 @@ class CoreInterface:
self.net_client: LinuxNetClient = get_net_client(
self.session.use_ovs(), self.host_cmd
)
self.control: bool = False
def host_cmd(
self,

View file

@ -66,7 +66,7 @@ class FRRZebra(CoreService):
for iface in node.get_ifaces():
cfg += "interface %s\n" % iface.name
# include control interfaces in addressing but not routing daemons
if hasattr(iface, "control") and iface.control is True:
if iface.control:
cfg += " "
cfg += "\n ".join(map(cls.addrstr, iface.ips()))
cfg += "\n"

View file

@ -63,7 +63,7 @@ class Zebra(CoreService):
for iface in node.get_ifaces():
cfg += "interface %s\n" % iface.name
# include control interfaces in addressing but not routing daemons
if getattr(iface, "control", False):
if iface.control:
cfg += " "
cfg += "\n ".join(map(cls.addrstr, iface.ips()))
cfg += "\n"

View file

@ -581,7 +581,7 @@ if [ "x$1" = "xstart" ]; then
"""
for iface in node.get_ifaces():
if hasattr(iface, "control") and iface.control is True:
if iface.control:
cfg += "# "
redir = "< /dev/null"
cfg += "tcpdump ${DUMPOPTS} -w %s.%s.pcap -i %s %s &\n" % (

View file

@ -13,7 +13,7 @@ from core.errors import CoreXmlError
from core.nodes.base import CoreNodeBase, NodeBase
from core.nodes.docker import DockerNode
from core.nodes.lxd import LxcNode
from core.nodes.network import CtrlNet, WlanNode
from core.nodes.network import CtrlNet, GreTapBridge, WlanNode
from core.services.coreservices import CoreService
if TYPE_CHECKING:
@ -253,15 +253,13 @@ class DeviceElement(NodeElement):
class NetworkElement(NodeElement):
def __init__(self, session: "Session", node: NodeBase) -> None:
super().__init__(session, node, "network")
model = getattr(self.node, "model", None)
if model:
add_attribute(self.element, "model", model.name)
mobility = getattr(self.node, "mobility", None)
if mobility:
add_attribute(self.element, "mobility", mobility.name)
grekey = getattr(self.node, "grekey", None)
if grekey and grekey is not None:
add_attribute(self.element, "grekey", grekey)
if isinstance(self.node, (WlanNode, EmaneNet)):
if self.node.model:
add_attribute(self.element, "model", self.node.model.name)
if self.node.mobility:
add_attribute(self.element, "mobility", self.node.mobility.name)
if isinstance(self.node, GreTapBridge):
add_attribute(self.element, "grekey", self.node.grekey)
self.add_type()
def add_type(self) -> None: