[j-nsp] How to query the results tree from a commit script?

Phil Shafer phil at juniper.net
Wed May 23 09:13:29 EDT 2012


Tore Anderson writes:
>  if(/commit-script-input/configuration/interfaces/interface[name == $iface]/unit[name =
>= $unit] ||
>     /commit-script-results/transient-change/interfaces/interface[name == $iface]/unit[n
>ame == $unit]) {

XPath expressions like "/foo" refer to the input document, where
"commit-script-results" is the output, so it won't be available for
you.  In fact XSLT gives no means of inspecting the output tree,
since it wants to allow streaming processing.

So you'll need to save the values in order to get this to work.  As
Curtis mentioned, mvars will allow this in the future, and I'd
second Benny's suggestion that you save these values, but an
alternative is to generate the list once in a quick pass over the
config and then use this data as a key for later config generation.

Something like:

match configuration {
    var $vpn-units := { call generate-vpn-units(); }

    if ($if-you-need-it) {
        var $ifname = $whatever;
        call generate-vpn($ifname, $unit = $vpn-units/interface[name == $ifname]/unit);
    }
}

template generate-vpn-units () {
    /*
     * We pick our unit number for each interface here, using
     * the value of the last unit plus one.  If/when we need
     * it in the future, we can all agree to use this number.
     */
    for-each (interfaces/interface) {
        <interface> {
            <name> name;
            <unit> unit[position() == last()]/name + 1;
        }
    }
}

This assumes that unit numbers are sorted, which is true except in
the case of config groups (IIRC).  We can add "sort unit/name;" if
this is needed (aka <xsl:sort select="unit/name">).

Thanks,
 Phil


More information about the juniper-nsp mailing list