Example: Configuring an Interior Gateway Protocol on an Interface

When you add a new interface to an OSPF or IS-IS domain, you must configure the interface at multiple hierarchy levels, including [edit interfaces] and [edit protocols]. This example uses a macro to automatically include the interface at the [edit protocols] hierarchy level and to configure the proper interior gateway protocol (IGP) on the interface, either OSPF or IS-IS, depending on the content of an apply-macro statement that you include in the interface configuration. This macro allows you to perform more configuration tasks at a single hierarchy level.

In this example, the Junos OS management (mgd) process inspects the configuration, looking for apply-macro statements. For each apply-macro ifclass statement included at the [edit interfaces interface-name unit logical-unit-number] hierarchy level, the script tests whether the role parameter is defined as cpe. If so, the script checks the igp parameter.

If the igp parameter is defined as isis, the script includes the relevant interface name at the [edit protocols isis interface] hierarchy level.

If the igp parameter is defined as ospf, the script includes the relevant interface name at the [edit protocols ospf area address interface] hierarchy level. For OSPF, the script references the area parameter to determine the correct subnet address of the area.

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:for-each
                       select="interfaces/interface/unit/apply-macro[name = 'ifclass']">
            <xsl:variable name="role" select="data[name='role']/value"/>
            <xsl:variable name="igp" select="data[name='igp']/value"/>
            <xsl:variable name="ifname">
                <xsl:value-of select="../../name"/>
                <xsl:text>.</xsl:text>
                <xsl:value-of select="../name"/>
            </xsl:variable>
            <xsl:choose>
                <xsl:when test="$role = 'cpe'">
                    <change>
                        <xsl:choose>
                            <xsl:when test="$igp = 'isis'">
                                <protocols>
                                    <isis>
                                        <interface>
                                            <name><xsl:value-of select="$ifname"/></name>
                                        </interface>
                                    </isis>
                                </protocols>
                            </xsl:when>
                            <xsl:when test="$igp = 'ospf'">
                                <protocols>
                                    <ospf>
                                        <area>
                                            <name>
                                                <xsl:value-of select="data[name='area']/value"/>
                                            </name>
                                            <interface>
                                                <name><xsl:value-of select="$ifname"/></name>
                                            </interface>
                                        </area>
                                    </ospf>
                                </protocols>
                            </xsl:when>
                        </xsl:choose>
                    </change>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </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";
 
match configuration {
    for-each (interfaces/interface/unit/apply-macro[name = 'ifclass']) {
        var $role = data[name='role']/value;
        var $igp = data[name='igp']/value;
        var $ifname = {
            expr ../../name;
            expr ".";
            expr ../name;
        }
        if ($role = 'cpe') {
            <change> {
                if ($igp = 'isis') {
                    <protocols> {
                        <isis> {
                            <interface> {
                                <name> $ifname;
                            }
                        }
                    }
                }
                else if ($igp = 'ospf') {
                    <protocols> {
                        <ospf> {
                            <area> {
                                <name> data[name='area']/value;
                                <interface> {
                                    <name> $ifname;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Testing the ex-if-class Script

To test the ex-if-class script, perform the following steps:

  1. Copy the XSLT or SLAX script from Example: Configuring an Interior Gateway Protocol on an Interface into a text file, name the file ex-if-class.xsl or ex-if-class.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-if-class.slax.

    system {scripts {commit {file ex-if-class.xsl;}}}interfaces {so-1/2/3 {unit 0 {apply-macro ifclass {area 10.4.0.0;igp ospf;role cpe;}}}t3-0/0/0 {unit 0 {apply-macro ifclass {igp isis;role cpe;}}}}
  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.

    [edit]
    user@host# commit

Script-Generated Configuration

When you issue the show protocols configuration mode command, the following output appears:

[edit]user@host# show protocolsisis {interface t3-0/0/0.0;}ospf {area 10.4.0.0 {interface so-1/2/3.0;}}

Manual Configuration

When you issue the show interfaces configuration mode command, the following output appears:

[edit]user@host# show interfacest3-0/0/0 {unit 0 {apply-macro ifclass {igp isis;role cpe;}}}so-1/2/3 {unit 0 {apply-macro ifclass {area 10.4.0.0;igp ospf;role cpe;}}}