Example: Controlling LDP Configuration

If you want to enable LDP on an interface, you must configure the interface at both the [edit protocols routing-protocol-name] and [edit protocols ldp] hierarchy levels. This example shows you how to use commit scripts to ensure that the interface is configured at both levels.

This example tests for interfaces that are configured at either the [edit protocols ospf] or [edit protocols isis] hierarchy level but not at the [edit protocols ldp] hierarchy level. If LDP is not enabled on the device, there is no problem; otherwise, a warning is emitted with the message that the interface does not have LDP enabled.

In case you want some interfaces to be exempt from the LDP test, this script allows you to tag those interfaces as not requiring LDP by including the apply-macro no-ldp statement at the [edit protocols isis interface interface-name] or [edit protocols ospf area area-id interface interface-name] hierarchy level. For example:

[edit]protocols {isis {interface so-0/1/2.0 {apply-macro no-ldp;}}}

If the apply-macro no-ldp statement is included, the warning is not emitted.

A second test ensures that all LDP-enabled interfaces are configured for an interior gateway protocol (IGP). As for LDP, you can exempt some interfaces from the test by including the apply-macro no-igp statement at the [edit protocols ldp interface interface-name] hierarchy level. If that statement is not included and no IGP is configured, a warning is emitted.

XSLT Syntax

<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet version="1.0"
    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">
    <xsl:import href="../import/junos.xsl"/>
 
    <xsl:template match="configuration">
        <xsl:variable name="ldp" select="protocols/ldp"/>
        <xsl:variable name="isis" select="protocols/isis"/>
        <xsl:variable name="ospf" select="protocols/ospf"/>
        <xsl:if test="$ldp">
            <xsl:for-each select="$isis/interface/name |
                                         $ospf/area/interface/name">
                <xsl:variable name="ifname" select="."/>
                <xsl:if test="not(../apply-macro[name = 'no-ldp'])
                                         and not($ldp/interface[name = $ifname])">
                    <xnm:warning>
                        <xsl:call-template name="jcs:edit-path"/>
                        <xsl:call-template name="jcs:statement"/>
                        <message>ldp not enabled for this interface</message>
                    </xnm:warning>
                </xsl:if>
            </xsl:for-each>
            <xsl:for-each select="protocols/ldp/interface/name">
                <xsl:variable name="ifname" select="."/>
                <xsl:if test="not(apply-macro[name = 'no-igp'])
                                         and not($isis/interface[name = $ifname])
                                         and not($ospf/area/interface[name = $ifname])">
                    <xnm:warning>
                        <xsl:call-template name="jcs:edit-path"/>
                        <xsl:call-template name="jcs:statement"/>
                        <message>
                            <xsl:text>ldp-enabled interface does not have </xsl:text>
                            <xsl:text>an IGP configured</xsl:text>
                        </message>
                    </xnm:warning>
                </xsl:if>
            </xsl:for-each>
        </xsl:if>
    </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";
import "../import/junos.xsl";
 
apply-macro no-ldp;match configuration {
    var $ldp = protocols/ldp;
    var $isis = protocols/isis;
    var $ospf = protocols/ospf;
    if ($ldp) {
        for-each ($isis/interface/name | $ospf/area/interface/name) {
            var $ifname = .;
            if (not(../apply-macro[name = 'no-ldp']) and not($ldp/interface[name =
                         $ifname])) {
                <xnm:warning> {
                    call jcs:edit-path();
                    call jcs:statement();
                    <message> "ldp not enabled for this interface";
                }
            }
        }
        for-each (protocols/ldp/interface/name) {
            var $ifname = .;
            if (not(apply-macro[name = 'no-igp']) and not($isis/interface[name =
                                $ifname]) and not($ospf/area/interface[name = $ifname])) {
                <xnm:warning> {
                    call jcs:edit-path();
                    call jcs:statement();
                    <message> {
                        expr "ldp-enabled interface does not have ";
                        expr "an IGP configured";
                    }
                }
            }
        }
    }
}

Testing the ex-ldp Script

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

  1. Copy the XSLT or SLAX script from Example: Controlling LDP Configuration into a text file, name the file ex-ldp.xsl or ex-ldp.slax as appropriate, and copy it to the /var/db/scripts/commit directory on the device.
  2. Select the following configuration stanzas, and press Ctrl+c to copy them to the clipboard. If you are using the SLAX version of the script, change the filename at the [edit system scripts commit file] hierarchy level to ex-ldp.slax.

    system {scripts {commit {file ex-ldp.xsl;}}}protocols {isis {interface so-1/2/2.0 {apply-macro no-ldp;}interface so-1/2/3.0;}ospf {area 10.4.0.0 {interface ge-3/2/1.0;interface ge-2/2/1.0;}}ldp { interface ge-1/2/1.0;interface ge-2/2/1.0;}}
  3. In configuration mode, issue the load merge terminal command to merge the stanzas into your device configuration:

    [edit]user@host# load merge terminal[Type ^D at a new line to end input]... Paste the contents of the clipboard here ...
    1. At the prompt, paste the contents of the clipboard using the mouse and the paste icon.
    2. Press Enter.
    3. Press Ctrl+d.
  4. Issue the commit command. The following output appears:

    [edit]user@host# commit
    [edit protocols ospf area 10.4.0.0 interface so-1/2/3.0]
        'interface so-1/2/3.0;'
            warning: LDP not enabled for this interface
    [edit protocols ospf area 10.4.0.0 interface ge-3/2/1.0]
        'interface ge-3/2/1.0;'
            warning: LDP not enabled for this interface
    [edit protocols ldp interface ge-1/2/1.0]
        'interface ge-1/2/1.0;'
            warning: LDP-enabled interface does not have an IGP configured
    commit complete