Example: Finding LSPs to Multiple Destinations Using an Op Script

This sample script checks for label-switched paths (LSPs) to multiple destinations.

XSLT Syntax

<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:junos="http://xml.juniper.net/junos/*/junos"
    xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm"
    xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0" version="1.0">
 
    <xsl:variable name="arguments">
        <argument>
            <name>address</name>
            <description>LSP endpoint</description>
        </argument>
    </xsl:variable>
    <xsl:param name="address"/>
    <xsl:template match="/">
        <op-script-output>
            <xsl:choose>
                <xsl:when test="$address = ''">
                    <xnm:error>
                        <message>missing mandatory argument 'address'</message>
                    </xnm:error>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="get-configuration">
                        <get-configuration database="committed">
                            <configuration>
                                <protocols>
                                    <mpls/>
                                </protocols>
                            </configuration>
                        </get-configuration>
                    </xsl:variable>
                    <xsl:variable name="config"
                                 select="jcs:invoke($get-configuration)"/>
                    <xsl:variable name="mpls" select="$config/protocols/mpls"/>
                    <xsl:variable name="get-route-information">
                        <get-route-information>
                            <terse/>
                            <destination>
                                <xsl:value-of select="$address"/>
                            </destination>
                        </get-route-information>
                    </xsl:variable>
                    <xsl:variable name="rpc-out"
                                      select="jcs:invoke($get-route-information)"/>
                    <xsl:choose>
                        <xsl:when test="$rpc-out//xnm:error">
                            <xsl:copy-of select="$rpc-out//xnm:error"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:for-each select="$rpc-out/route-table/rt/rt-destination">
                                <xsl:choose>
                                    <xsl:when test="contains(.,'/32')">
                                        <xsl:variable name="dest"
                                                  select="substring-before(.,'/')"/>
                                        <xsl:variable name="lsp"
                                              select="$mpls/label-switched-path[to = $dest]"/>
                                        <xsl:choose>
                                            <xsl:when test="$lsp">
                                                <output>
                                                  <xsl:value-of select="concat('Found: ', $dest,
                                                                      ' (',$lsp/to, ') --&gt; ', $lsp/name)"/>
                                                </output>
                                            </xsl:when>
                                            <xsl:otherwise>
                                                <xsl:variable name="name"
                                                                        select="jcs:hostname($dest)"/>
                                                <output>
                                                    <xsl:value-of select="concat('Name: ', $name)"/>
                                                </output>
                                                <output>
                                                    <xsl:value-of select="concat('Missing: ', $dest)"/>
                                                </output>
                                            </xsl:otherwise>
                                        </xsl:choose>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <output>
                                            <xsl:value-of select="concat('Not a host route: ', .)"/>
                                        </output>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:for-each>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </op-script-output>
    </xsl:template>
</xsl:stylesheet>

SLAX Syntax

version 1.0;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
 
var $arguments = {
    <argument> {
        <name> "address";
        <description> "LSP endpoint";
    }
}
param $address;
match / {
    <op-script-output> {
        if ($address = '') {
            <xnm:error> {
                <message> "missing mandatory argument 'address'";
            }
        } else {
            var $get-configuration = {
                <get-configuration database="committed"> {
                    <configuration> {
                        <protocols> {
                            <mpls>;
                        }
                    }
                }
            }
            var $config = jcs:invoke($get-configuration);
            var $mpls = $config/protocols/mpls;
            var $get-route-information = {
                <get-route-information> {
                    <terse>;
                    <destination> $address;
                }
            }
            var $rpc-out = jcs:invoke($get-route-information);
            if ($rpc-out//xnm:error) {
                copy-of $rpc-out//xnm:error;
            } else {
                for-each ($rpc-out/route-table/rt/rt-destination) {
                    if (contains(.,'/32')) {
                        var $dest = substring-before(.,'/');
                        var $lsp = $mpls/label-switched-path[to = $dest];
                        if ($lsp) {
                            <output> 'Found: ' _ $dest _ ' (' _ $lsp/to _ ') - -> ' _
                                     $lsp/name;
                        } else {
                            var $name = jcs:hostname($dest);
                            <output> 'Name: ' _ $name;
                            <output> 'Missing: ' _ $dest;
                        }
                    } else {
                        <output> 'Not a host route: ' _ .;
                    }
                }
            }
        }
    }
}

Testing the ex-lsp Script

To test the ex-lsp script, perform the following steps:

  1. Copy the XSLT or SLAX script from Example: Finding LSPs to Multiple Destinations Using an Op Script into a text file, name the file ex-lsp.xsl or ex-lsp.slax as appropriate, and copy it to the /var/db/scripts/op directory on the device.
  2. In configuration mode, include the file ex-lsp.extension statement at the [edit system scripts op] hierarchy level, substituting .slax or .xsl for extension as appropriate.

    [edit system scripts op]file ex-lsp.(slax | xsl);
  3. Issue the commit and-quit command.

    [edit]user@host# commit and-quit

When you issue the op ex-lsp address address operational mode command, you should see output similar to the following:


user@R4> op ex-lsp address 10.168.215.0/24
Found: 192.168.215.1 (10.168.215.1) --> R4>R1
Found: 192.168.215.2 (10.168.215.2) --> R4>R2
Name: R3
Missing: 10.168.215.3
Name: R5
Missing: 10.168.215.4
Name: R6
Missing: 10.168.215.5