[Contents] [Prev] [Next] [Index] [Report an Error]

Prepending a Global Policy

For most configuration objects, the order in which the object or its children are created is not significant, because the JUNOS configuration management software stores and displays configuration objects in predetermined positions in the configuration hierarchy. However, some configuration objects—such as routing policies and firewall filters—consist of elements that must be processed and analyzed sequentially in order to produce the intended routing behavior.

This example ensures that a Border Gateway Protocol (BGP) global import policy is applied to all your BGP imports before any other import policies are applied.

This example automatically prepends the bgp_global_import policy in front of any other BGP import policies. If the bgp_global_import policy statement is not included in the configuration, an error message is emitted, and the commit operation fails.

Otherwise, the commit script uses the insert="before" JUNOScript attribute and the position() XSLT function to control the position of the global BGP policy in relation to any other applied policies. The insert="before" attribute inserts the bgp_global_import policy in front of the first preexisting BGP import policy.

If there is no preexisting default BGP import policy, the global policy is included in the configuration.

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:if               test="not(policy-options/policy-statement[name='bgp_global_import'])">
            <xnm:error>
                <message>Policy error: Policy bgp_global_import required</message>
            </xnm:error>
        </xsl:if>

        <xsl:for-each select="protocols/bgp | protocols/bgp/group |                                                                        protocols/bgp/group/neighbor">
            <xsl:variable name="first" select="import[position() = 1]"/>
            <xsl:if test="$first">
                <xsl:call-template name="jcs:emit-change">
                    <xsl:with-param name="tag" select="'transient-change'"/>
                    <xsl:with-param name="content">
                        <import insert="before"                                                      name="{$first}">bgp_global_import</import>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:if>
        </xsl:for-each>

        <xsl:for-each select="protocols/bgp">
            <xsl:if test="not(import)">
                <xsl:call-template name="jcs:emit-change">
                    <xsl:with-param name="tag" select="'transient-change'"/>
                    <xsl:with-param name="content">
                        <import>bgp_global_import</import>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:if>
        </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 {
    if (not(policy-options/policy-statement[name='bgp_global_import'])) {
        <xnm:error> {
            <message> "Policy error: Policy bgp_global_import required";
        }
    }
    for-each (protocols/bgp | protocols/bgp/group |                                     protocols/bgp/group/neighbor) {
        var $first = import[position() = 1];

        if ($first) {
            call jcs:emit-change($tag = 'transient-change') {
                with $content = {
                    <import insert="before" name="{$first}"> "bgp_global_import";
                }
            }
        }
    }
    for-each (protocols/bgp) {
        if (not(import)) {
            call jcs:emit-change($tag = 'transient-change') {
                with $content = {
                    <import> "bgp_global_import";
                }
            }
        }
    }
}

[Contents] [Prev] [Next] [Index] [Report an Error]