From ec8637540f403a607a18f3f77ee6c10c141e4291 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Mon, 7 Dec 2020 19:20:52 -0600 Subject: Module Manager: Support augmenting models This is very early code, but seems to work. --- ncserver/base/modman.py | 28 ++++++++++++++++++++++++++++ ncserver/util.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 ncserver/util.py diff --git a/ncserver/base/modman.py b/ncserver/base/modman.py index d8659a2..5033afa 100644 --- a/ncserver/base/modman.py +++ b/ncserver/base/modman.py @@ -146,6 +146,25 @@ class ModuleManager: return None + def _augments_for_ns(self, namespace): + """Find modules that augment the specified namespace. + + :param str namespace: + The namespace of the model whose augments are desired. + + :returns: + An Iterable of modules that augment the namespace. + The Iterable may be empty. + """ + augments = list() + + for mod in self.modules.values(): + if getattr(mod, 'M_AUGMENTS', None) is not None and \ + namespace in mod.M_AUGMENTS: + augments.append(mod) + + return augments + def load_module(self, name): """Load a module. @@ -325,6 +344,15 @@ class ModuleManager: raise error.OperationNotSupportedAppError(rpc) self.logger.debug('Dispatching edit-config to %s', module.M_NAME) module.edit(session, rpc, child, def_op) + for augment in self._augments_for_ns(namespace): + try: + self.logger.debug( + 'Augment: Dispatching edit-config for %s to %s', + namespace, augment.M_NAME + ) + augment.edit(session, rpc, child, def_op) + except error.OperationNotSupportedAppError: + continue MODMAN = ModuleManager() diff --git a/ncserver/util.py b/ncserver/util.py new file mode 100644 index 0000000..209d4d9 --- /dev/null +++ b/ncserver/util.py @@ -0,0 +1,36 @@ +""" +NETCONF for APK Distributions server: + Higher-level utility functions. + +Copyright © 2020 Adélie Software in the Public Benefit, Inc. + +Released under the terms of the NCSA license. See the LICENSE file included +with this source distribution for more information. + +SPDX-License-Identifier: NCSA +""" + + +from lxml.etree import QName +from netconf import error + +from ncserver.base.modman import MODMAN + + +def maybe_raise_on_invalid_node(xmlns, rpc, node): + """Determine if this node should cause the module in namespace to raise. + + :param str xmlns: + The xmlns of the current module. + + :param rpc: + The RPC causing this lookup to happen. + + :param node: + The node in question. + """ + nodens = QName(node).namespace + if nodens == xmlns: + raise error.UnknownElementAppError(rpc, node) + if nodens not in (mod.M_PREFIX for mod in MODMAN._augments_for_ns(xmlns)): + raise error.UnknownNamespaceAppError(rpc, node, None) -- cgit v1.2.3-70-g09d2