From d570f9d051b8445be5fc764d827de1cab424a5b8 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Tue, 8 Dec 2020 19:17:56 -0600 Subject: Factor get_nmsa into a util module for DRY --- ncserver/module/interfaces.py | 22 +++++++--------------- ncserver/module/ip.py | 14 +++----------- ncserver/util.py | 8 ++++++++ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/ncserver/module/interfaces.py b/ncserver/module/interfaces.py index c91c318..d760e0b 100644 --- a/ncserver/module/interfaces.py +++ b/ncserver/module/interfaces.py @@ -18,9 +18,9 @@ from lxml.etree import QName # pylint: disable=E0611 from netconf import error, util from ncserver.base.log import log_config_change -from ncserver.base.modman import MODMAN from ncserver.base.util import _, node_operation -from ncserver.util import maybe_raise_on_invalid_node, system_boot_time +from ncserver.util import get_nmsa, maybe_raise_on_invalid_node, \ + system_boot_time LOGGER = logging.getLogger(__name__) @@ -102,14 +102,6 @@ def _add_iface_contents(container, ifaces): ifname.name, type_num) -def _get_nmsa(): - """Retrieve our NMSA module handle.""" - nmsa_ns = "http://netconf.adelielinux.org/ns/netmgmt" - nmsa = MODMAN._module_for_ns(nmsa_ns) # pylint: disable=W0212 - - return nmsa - - def _log_iface_change(session, iface: str, info: str): """Log a change to an interface. @@ -128,7 +120,7 @@ def _log_iface_change(session, iface: str, info: str): def running(node): """Retrieve the service configuration for this device.""" ifaces = util.subelm(node, 'if:interfaces') - nmsa = _get_nmsa() + nmsa = get_nmsa() if nmsa is None: # We can't get any parameters if an NMSA module isn't loaded. @@ -186,7 +178,7 @@ def operational(node): ifaces = util.subelm(node, 'if:interfaces') _add_iface_contents(ifaces, pathlib.Path('/sys/class/net').iterdir()) - nmsa = _get_nmsa() + nmsa = get_nmsa() for iface in ifaces.iterchildren(): name = iface.find('{'+M_NS+'}name').text @@ -241,7 +233,7 @@ def operational(node): def _edit_description(session, rpc, node, def_op, iface: str): """Edit the description for an interface.""" operation = node_operation(node, def_op) - nmsa = _get_nmsa() + nmsa = get_nmsa() already = nmsa.get_param(iface, 'description') is not None if operation == 'create' and already: @@ -265,7 +257,7 @@ def _edit_description(session, rpc, node, def_op, iface: str): def _edit_enabled(session, rpc, node, def_op, iface: str): """Edit the enabled property of an interface.""" operation = node_operation(node, def_op) - nmsa = _get_nmsa() + nmsa = get_nmsa() if operation == 'create': raise error.DataExistsAppError(rpc) @@ -283,7 +275,7 @@ def edit(session, rpc, node, def_op): """Edit the interface configuration for this device.""" methods = {'description': _edit_description, 'enabled': _edit_enabled} - nmsa = _get_nmsa() + nmsa = get_nmsa() # We can't edit if we don't have an NMSA module loaded. # This guarantees that none of our child functions need to test for nmsa. if nmsa is None: diff --git a/ncserver/module/ip.py b/ncserver/module/ip.py index e6a2de1..cd4cde0 100644 --- a/ncserver/module/ip.py +++ b/ncserver/module/ip.py @@ -15,8 +15,8 @@ import logging from lxml.etree import QName # pylint: disable=E0611 from netconf import util -from ncserver.base.modman import MODMAN from ncserver.base.util import _ +from ncserver.util import get_nmsa LOGGER = logging.getLogger(__name__) @@ -76,14 +76,6 @@ def _get_ifaces(node): return ifaces -def _get_nmsa(): - """Retrieve our NMSA module handle.""" - nmsa_ns = "http://netconf.adelielinux.org/ns/netmgmt" - nmsa = MODMAN._module_for_ns(nmsa_ns) # pylint: disable=W0212 - - return nmsa - - def from_bool(value: bool) -> str: """Turn a Python bool into an XML bool.""" return str(value).lower() @@ -141,7 +133,7 @@ def _add_ipv6(iface, getter): def running(node): """Retrieve the IP configuration for this device.""" ifaces = _get_ifaces(node) - nmsa = _get_nmsa() + nmsa = get_nmsa() if None in (ifaces, nmsa): # We can't retrieve configuration unless both the ietf-interfaces and @@ -183,7 +175,7 @@ def running(node): def operational(node): """Retrieve the IP state for this device.""" ifaces = _get_ifaces(node) - nmsa = _get_nmsa() + nmsa = get_nmsa() if None in (ifaces, nmsa): # We can't retrieve configuration unless both the ietf-interfaces and diff --git a/ncserver/util.py b/ncserver/util.py index 7349baf..229fbf1 100644 --- a/ncserver/util.py +++ b/ncserver/util.py @@ -47,3 +47,11 @@ def system_boot_time() -> str: raw = upfile.read().split(' ')[0] boot = floor(time.time() - float(raw)) return yang_dt_for_timestamp(boot) + + +def get_nmsa(): + """Retrieve our NMSA module handle.""" + nmsa_ns = "http://netconf.adelielinux.org/ns/netmgmt" + nmsa = MODMAN._module_for_ns(nmsa_ns) # pylint: disable=W0212 + + return nmsa -- cgit v1.2.3-70-g09d2