Re: [netmod] Open source filtering code?

Christian Hopps <chopps@chopps.org> Sat, 24 February 2018 23:41 UTC

Return-Path: <chopps@chopps.org>
X-Original-To: netmod@ietfa.amsl.com
Delivered-To: netmod@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id C6B0312D946; Sat, 24 Feb 2018 15:41:54 -0800 (PST)
X-Quarantine-ID: <RTD19QSz8EHQ>
X-Virus-Scanned: amavisd-new at amsl.com
X-Amavis-Alert: BANNED, message contains text/plain,.exe
X-Spam-Flag: NO
X-Spam-Score: -1.909
X-Spam-Level:
X-Spam-Status: No, score=-1.909 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id RTD19QSz8EHQ; Sat, 24 Feb 2018 15:41:53 -0800 (PST)
Received: from smtp.chopps.org (smtp.chopps.org [54.88.81.56]) by ietfa.amsl.com (Postfix) with ESMTP id 2B472126C22; Sat, 24 Feb 2018 15:41:53 -0800 (PST)
Received: from tops.chopps.org (47-50-69-38.static.klmz.mi.charter.com [47.50.69.38]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by smtp.chopps.org (Postfix) with ESMTPSA id 81C8F62A28; Sat, 24 Feb 2018 23:41:52 +0000 (UTC)
References: <87o9ke9o63.fsf@chopps.org>
User-agent: mu4e 1.0; emacs 25.3.1
From: Christian Hopps <chopps@chopps.org>
To: "netconf\@ietf.org" <netconf@ietf.org>
Cc: "netmod\@ietf.org" <netmod@ietf.org>
In-reply-to: <87o9ke9o63.fsf@chopps.org>
Date: Sat, 24 Feb 2018 18:41:50 -0500
Message-ID: <87vaemarb5.fsf@chopps.org>
MIME-Version: 1.0
Content-Type: text/plain; format=flowed
Archived-At: <https://mailarchive.ietf.org/arch/msg/netmod/3A1JUgCyQSMMdshjn4L_Wt-oM24>
Subject: Re: [netmod] Open source filtering code?
X-BeenThere: netmod@ietf.org
X-Mailman-Version: 2.1.22
Precedence: list
List-Id: NETMOD WG list <netmod.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/options/netmod>, <mailto:netmod-request@ietf.org?subject=unsubscribe>
List-Archive: <https://mailarchive.ietf.org/arch/browse/netmod/>
List-Post: <mailto:netmod@ietf.org>
List-Help: <mailto:netmod-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/netmod>, <mailto:netmod-request@ietf.org?subject=subscribe>
X-List-Received-Date: Sat, 24 Feb 2018 23:41:55 -0000

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.


Christian Hopps <chopps@chopps.org> writes:

> 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.