summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2020-11-19 13:03:32 -0600
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2020-11-19 13:03:32 -0600
commita8c7c3fbf37a9eedc96e63d3a946c3a795006f30 (patch)
tree99bb5b05389fbe7cd095731bb9a1b50f46068e25
parent8a48c830b2091e21f2be5ef73c34a4cdd9d106cd (diff)
downloadnetconfapk-a8c7c3fbf37a9eedc96e63d3a946c3a795006f30.tar.gz
netconfapk-a8c7c3fbf37a9eedc96e63d3a946c3a795006f30.tar.bz2
netconfapk-a8c7c3fbf37a9eedc96e63d3a946c3a795006f30.tar.xz
netconfapk-a8c7c3fbf37a9eedc96e63d3a946c3a795006f30.zip
ifupdown-ng NMSA: Add initial parameter handling
-rw-r--r--ncserver/module/nms_ifupdownng.py91
1 files changed, 83 insertions, 8 deletions
diff --git a/ncserver/module/nms_ifupdownng.py b/ncserver/module/nms_ifupdownng.py
index 23795f6..b4e1c1f 100644
--- a/ncserver/module/nms_ifupdownng.py
+++ b/ncserver/module/nms_ifupdownng.py
@@ -51,6 +51,11 @@ _TRANSACTION = False
"""Determines if a transaction is in progress."""
+#############################
+# I N T E R N A L #
+#############################
+
+
def _load_config():
"""Load the current active configuration from /e/n/i."""
global _CONFIG # pylint: disable=W0603
@@ -76,7 +81,7 @@ def _load_config():
_CONFIG = yaml.safe_load(rawyaml)
-def _save():
+def _save_unlocked():
"""Save changes to the configuration."""
eni = ""
for iface in _CONFIG.keys():
@@ -99,6 +104,14 @@ def _save():
conf_file.write(eni[:-1])
+def _save():
+ """Save configuration changes, if a transaction is not in progress."""
+ if _TRANSACTION:
+ return
+
+ _save_unlocked()
+
+
def _find_one(iface: str, key: str):
"""Find a single instance of configuration +key+ for +iface+."""
if iface not in _CONFIG.keys():
@@ -125,6 +138,52 @@ def _find_many(iface: str, key: str) -> list:
return ret
+#############################
+# P A R A M E T E R S #
+#############################
+
+
+def get_desc(iface: str):
+ """Retrieve the description for the specified interface."""
+ return _find_one(iface, "netconf-description")
+
+
+def set_desc(iface: str, value: str):
+ """Set the description for the specified interface."""
+ iface_cfg = _CONFIG[iface]
+ for item in iface_cfg:
+ if 'netconf-description' in item.keys():
+ item['netconf-description'] = value
+ _save()
+ return
+
+ iface_cfg.append({'netconf-description': value})
+ _save()
+ return
+
+
+def unset_desc(iface: str):
+ """Unset the description for the specified interface."""
+ iface_cfg = _CONFIG[iface]
+ for item in iface_cfg:
+ if 'netconf-description' in item.keys():
+ iface_cfg.remove(item)
+ _save()
+ return
+
+
+_PARAMETERS = {
+ # "name": (getter, setter, unsetter)
+ "description": (get_desc, set_desc, unset_desc)
+}
+"""Describes all supported parameters and their methods."""
+
+
+#############################
+# P U B L I C A P I #
+#############################
+
+
def interface_list():
"""Return a list of configured interfaces."""
_load_config()
@@ -159,7 +218,7 @@ def commit():
if not _TRANSACTION:
LOGGER.warning(_("commit when no transaction is in progress"))
- _save()
+ _save_unlocked()
_TRANSACTION = False
@@ -174,20 +233,30 @@ def get_param(iface: str, parameter: str):
)
return None
- # implement this.
- raise NotImplementedError
+ if parameter not in _PARAMETERS.keys():
+ LOGGER.error(_("requested non-existent parameter %s for interface %s"),
+ parameter, iface)
+ return None
+
+ return _PARAMETERS[parameter][0](iface)
def set_param(iface: str, parameter: str, value):
"""Set the parameter for the specified interface."""
_load_config()
+ if parameter not in _PARAMETERS.keys():
+ LOGGER.error(
+ _("attempted to set non-existent parameter %s for interface %s"),
+ parameter, iface
+ )
+ return
+
# Allow creation of new interfaces from NETCONF.
if iface not in _CONFIG.keys():
_CONFIG[iface] = list()
- # implement this.
- raise NotImplementedError
+ _PARAMETERS[parameter][1](iface, value)
def unset_param(iface: str, parameter: str):
@@ -201,8 +270,14 @@ def unset_param(iface: str, parameter: str):
)
return
- # implement this.
- raise NotImplementedError
+ if parameter not in _PARAMETERS.keys():
+ LOGGER.error(
+ _("attempted to unset non-existent parameter %s for interface %s"),
+ parameter, iface
+ )
+ return
+
+ _PARAMETERS[parameter][2](iface)
def list_addresses(iface: str) -> list: