Subject: [Netconf] Open source filtering code?

From: Christian Hopps <[email-masked]>
Date: Sat, 24 Feb 2018 14:35:00 -0500

I've developed a basic python based netconf server and client (https://github.com/choppsv1/netconf), but it currently lacks any decent filtering capability. I was wondering can anyone point me at any open source (or way to use open source) that implements netconf/yang subtree/xpath filtering?

For this project something simple that takes a result and prunes it down would probably work OK.

FWIW I looked at lxml and xpath thinking that I could translate subtree filters to xpath first and then use xpath; however, lxml xpath is not really designed to support returning results from the root of the tree. Failing to find anything else I'll probably try and make that work anyway.

Thanks,
Chris.

---

From: Christian Hopps <[email-masked]>
Date: Sat, 24 Feb 2018 18:41:50 -0500

Still interested in any answers to this query; however, I thought I would followup..

A somewhat sub-optimal but working solution using lxml for pruning results turned out to be pretty simple after all. :)

`data` is the data lxlm.Element containing the get[-config] result, and `xpath` is an xpath string.

def xpath_filter_result(data, xpath):

    # First get a copy we can safely modify.
    data = copy.deepcopy(data)
    results = data.xpath(xpath, namespaces=NSMAP)

    # Mark the tree up
    for result in results:
        # Mark all children
        for elm in result.iterdescendants():
            elm.attrib['__filter_marked__'] = ""
        # Mark this element and all parents
        while result is not data:
            result.attrib['__filter_marked__'] = ""
            result = result.getparent()

    def prunedecendants(elm):
        for child in elm.getchildren():
            if '__filter_marked__' not in child.attrib:
                elm.remove(child)
            else:
                # Recurse
                prunedecendants(child)
                # Remove the mark
                del child.attrib['__filter_marked__']

    prunedecendants(data)

    return data

Thanks,
Chris.

---

From: Mikael Abrahamsson <[email-masked]>
Date: Mon, 26 Feb 2018 09:38:15 +0100 (CET)

> Still interested in any answers to this query; however, I thought I would 

Have you looked at libyang ?

https://github.com/CESNET/libyang

It's heavily used in Sysrepo/Netopeer2 for all the YANG related functions.

-- 
Mikael Abrahamsson    email: [email-masked]

---

From: Radek Krejčí <[email-masked]>
Date: Wed, 28 Feb 2018 09:47:18 +0100

Hi,
NETCONF filtering is actually implemented in our Netopeer2 NETCONF server (where we use libyang to represent YANG data). libyang implements XPath on its data structures representing YANG data, so XPath filtering is really straightforward. Subtree filtering is implemented by transforming subtree filter into XPath expressions. libyang has also Python bindings, so you could use it even in Python code.

Regards,
Radek