Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

test the top-level of a result from jcs:invoke()

‎12-10-2015 08:10 AM

 

I'm quite new to Junos scripting, SLAX, and XPath, so I know this will be an easy question to answer.  I am having some difficutly finding the right search terms to get the answer on my own, however.

 

if I have the following:

    /* build rpc call for getting route from route-table */
    var $table_route_rpc = {
      <get-route-information> {
        <destination> $route;
        <extensive> ;
        <exact> ;
        <table> $vrf _ ".inet.0";
      }
    }
    
    /* run the rpc call */
    var $route_results = jcs:invoke($table_route_rpc) ;

When the user has specified the VRF and route correctly, I know I can do things like:

if ($route_results/route-information/route-table/rt/rt-entry/age[@junos:seconds > 3600]) {
  /* route is older than an hour */
}

but if the user mistypes the VRF, $route_results will have <xnm:error> as the top-level element.  Iwant to test for that condition, but I'm not finding a good way to find the element-name of the top of $route_results.  The best thing that I've come up with, which works but looks wonky, is 

if ($route_results/../xnm:error) {
  /* error message */
}

tHanks for the help. Every post is a learning opportunity.

3 REPLIES 3
Highlighted
Junos Automation (Scripting)

Re: test the top-level of a result from jcs:invoke()

[ Edited ]
‎12-10-2015 08:21 AM

The most reliable method I've found for figuring this out just insert  a debug line in your script that does a "copy-of $route_results".   Then invoke the script to force an error, then pipe the output through | display xml.

 

That will show you the error XML that you can parse.

 

/doug

--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST
Highlighted
Junos Automation (Scripting)

Re: test the top-level of a result from jcs:invoke()

‎12-10-2015 09:09 AM

Thanks for the reponse.

 

When there is an error with the input, in the form of a bad table name, the output looks like:

 

        <xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm">
            <source-daemon xmlns="">
                routing
            </source-daemon>
            <message xmlns="">
                No routing tables matching specification.
            </message>
        </xnm:error>

 

So, I do think I COULD match on $route_results/message to get the same effect, but I'm wondering what the best way to match on the <xnm:error> element, which is the very top (root?) of $route_results .

 

Something like:

if ($route_results context_root == 'xnm:error') {
  /* do things */
}

 

Thanks for your time!

Highlighted
Junos Automation (Scripting)

Re: test the top-level of a result from jcs:invoke()

‎12-10-2015 10:57 AM

Not sure if it's *best*, but here's an example of how I usually do error checking/processing:

var $rs = jcs:execute( $con, $copy-rpc );
if ($rs//self::xnm:error) {
    var $errmsg = "Copy error: " _ translate($rs//self::xnm:error/message,"\n","");
    expr util:emit-msg('error',$errmsg);
...

/* 
 * the translate() function is just in there b/c that particular error msg includes
 * line breaks.
 */

 

If you're doing much in was of SLAX, then I strongly recommend downloading a copy of Curtis Call's excellent SLAX 1.0 Reference Guide. It's the best documentation available for all of the libslax 1.0 features, and includes examples.

 

It's at http://www.juniper.net/us/en/training/jnbooks/day-one/automation-series/junos-automation-slax/

 

See also the collection of links at http://forums.juniper.net/t5/Automation/Where-can-I-learn-more-about-SLAX/ta-p/279431

 

 

Good luck!

/doug

--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST
Feedback