summaryrefslogtreecommitdiff
path: root/ncserver/util.py
blob: 3897d2c93db340074aa69daeec2d13025db82023 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
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 math import floor
import time

from lxml.etree import QName  # pylint: disable=E0611
from netconf import error

from ncserver.base.modman import MODMAN
from ncserver.base.util import yang_dt_for_timestamp


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)
    # pylint: disable=W0212
    if nodens not in (mod.M_NS for mod in MODMAN._augments_for_ns(xmlns)):
        raise error.UnknownNamespaceAppError(rpc, node, None)


def system_boot_time() -> str:
    """Return the system's boot time."""
    with open('/proc/uptime', 'r') as upfile:
        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