When you add a new interface to an Open Shortest Path First (OSPF) or Intermediate System-to-Intermediate System (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 management process (mgd) 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;
}
}
}
}
}
}
}
}
}
To test the example in this section, perform the following steps:
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;
}
}
}
}
- [edit]
- user@host# load merge terminal
- [Type ^D at a new line to end input]
- > Paste the contents of the clipboard here<
- [edit]
- user@host# commit
Script-Generated Configuration
When you issue the show protocols configuration mode command, the following output appears:
[edit]
user@host# show protocols
isis {
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 interfaces
t3-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;
}
}
}