[j-nsp] EX-series automation, NETCONF woes

Ross Vandegrift ross at kallisti.us
Fri Mar 5 07:51:37 EST 2010

On Fri, Mar 05, 2010 at 02:26:59PM +0300, Alexandre Snarskii wrote:
> On Wed, Jan 28, 2009 at 03:40:10PM -0500, Ross Vandegrift wrote:
> > Can you do that without providing a map that maps the abbreviated
> > namespace back to the fully-qualified namespace?  If so, I'd love to
> > know how.
> Just run into the same problem myself, and workaround is here:
> you can use local-name() function to do namespace-agnostic 
> searches, and you can use partial checks on namespaces to be
> sure that you getting what you want. 
> Example: iterate over physical interfaces:  
>     <xsl:for-each select="//*[local-name()='physical-interface' and 
>         starts-with(namespace-uri(), 'http://xml.juniper.net/junos/')]">
> Select operational state: 
>         <xsl:variable name="oper"
>             select="normalize-space(*[local-name()='oper-status'])"/>
> Select dropped packets for first queue: 
>         select="normalize-space(*[local-name()='queue-counters']/*[local-name()='queue' and *[local-name()='queue-number']=0]/*[local-name()='queue-counters-red-packets'])"/>
> Syntax is ugly, but it works... 

I've been meaning to write up my solution to this, but haven't had the
chance.  It turns out that lxml exposes namespace assignments in a
programmatically accessible manner.

Building the map I mention above ends up being very easy and means I
can rid myself of all the absurd XSLT mangling I had to do.  I wish
the exposure of the namespace information had been more clearly
documented - or that I had noticed it earlier!

For Python folks, suppose that ncdoc is a netconf document.  Then
here's the quick version of how to build an appropriate map of
specific namespaces from a given JUNOS response:

namepsaces = set()
nsmap = dict()

for i in ncdoc.getiterator():
for ns in namespaces:
	shortname = ns.split('/')[-1]

# Now you can use normal XML tree traversal techniques
e = nsdoc.getroot()
name = e.xpath("./junos-interface:name/text()", namespaces=nsmap)
operstat = e.xpath("./junos-interface:oper-status/text()", namespaces=nsmap)

This should works for any JUNOS platform that uses the consistent
namespace abbreviations we're all used to reading.  Hopefully that's
all of them, forever :)


Ross Vandegrift
ross at kallisti.us

"If the fight gets hot, the songs get hotter.  If the going gets tough,
the songs get tougher."
	--Woody Guthrie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <https://puck.nether.net/pipermail/juniper-nsp/attachments/20100305/b6d6016f/attachment-0001.bin>

More information about the juniper-nsp mailing list