We define an apply-macro for simplified interface configurations.These are expanded into a diverse set of configuration statements.
You can use configuration groups (such as 'interface-details' and 'cos-details' in this example) with commit scripts to retain the details of any configuration expansions in the configuration database, where you can change them more easily.
Source Code and GitHub Links
The source code below is also available from the following GitHub locations:
Example Configuration
01 system {
02 scripts {
03 commit {
04 allow-transients;
05 file params.xsl;
06 }
07 }
08 }
09 interfaces {
10 so-1/2/3 {
11 apply-macro params {
12 clocking internal;
13 description "Link to Hoverville";
14 encapsulation ppp;
15 inet-address 10.1.2.3/28;
16 isis-level-1 enable;
17 isis-level-1-metric 50;
18 isis-level-2-metric 85;
19 }
20 }
21 }
Example Output
01 system {
02 scripts {
03 commit {
04 allow-transients;
05 file params.xsl;
06 }
07 }
08 ## Warning: missing mandatory statement(s): 'root-authentication'
09 }
10 interfaces {
11 so-1/2/3 {
12 apply-groups interface-details; ## 'interface-details' is not defined
13 apply-macro params {
14 clocking internal;
15 description "Link to Hoverville";
16 encapsulation ppp;
17 inet-address 10.1.2.3/28;
18 isis-level-1 enable;
19 isis-level-1-metric 50;
20 isis-level-2-metric 85;
21 }
22 description "Link to Hoverville";
23 encapsulation ppp;
24 unit 0 {
25 family inet {
26 address 10.1.2.3/28;
27 }
28 }
29 }
30 }
31 protocols {
32 rsvp {
33 interface so-1/2/3.0;
34 }
35 isis {
36 interface so-1/2/3.0 {
37 level 1 {
38 enable;
39 metric 50;
40 }
41 level 2 metric 85;
42 }
43 }
44 ldp {
45 interface so-1/2/3.0;
46 }
47 }
48 class-of-service {
49 interfaces {
50 so-1/2/3 {
51 apply-groups cos-details; ## 'cos-details' is not defined
52 }
53 }
54 }
SLAX Script Contents
001 /* Machine Crafted with Care (tm) by slaxWriter */
002 version 1.0;
003
004
005 /*
006 - $Id: params.slax,v 1.1 2007/10/17 18:37:03 phil Exp $
007 -
008 - Copyright (c) 2004-2005, Juniper Networks, Inc.
009 - All rights reserved.
010 -
011 */
012 ns junos = "http://xml.juniper.net/junos/*/junos";
013 ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
014 ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
015
016 import "../import/junos.xsl";
017
018 /*
019 - This example uses an apply-macro to turn simplified interface
020 - configuration into a diverse set of configuration statements.
021 -
022 - Using configuration groups (such as 'interface-details' and
023 - 'cos-details' in this example) with commit scripts can keep the
024 - details of any configuration expansions in the configuration
025 - database, where they can be more easily changed.
026 */
027 match configuration {
028 var $top = .;
029
030 for-each (interfaces/interface/apply-macro[name == "params"]) {
031 var $description = data[name == "description"]/value;
032 var $inet-address = data[name == "inet-address"]/value;
033 var $encapsulation = data[name == "encapsulation"]/value;
034 var $clocking = data[name == "clocking"]/value;
035 var $isis-level-1 = data[name == "isis-level-1"]/value;
036 var $isis-level-1-metric = data[name == "isis-level-1-metric"]/value;
037 var $isis-level-2 = data[name == "isis-level-2"]/value;
038 var $isis-level-2-metric = data[name == "isis-level-2-metric"]/value;
039 var $ifname = ../name _ ".0";
040 <transient-change> {
041 <interfaces> {
042 <interface> {
043 <name> ../name;
044 <apply-groups> {
045 <name> "interface-details";
046 }
047 if ($description) {
048 <description> $description;
049 }
050 <encapsulation> {
051 if (string-length($encapsulation) > 0) {
052 expr $encapsulation;
053
054 } else {
055 expr "cisco-hdlc";
056 }
057 }
058 <unit> {
059 <name> "0";
060 if (string-length($inet-address) > 0) {
061 <family> {
062 <inet> {
063 <address> $inet-address;
064 }
065 }
066 }
067 }
068 }
069 }
070 <protocols> {
071 <rsvp> {
072 <interface> {
073 <name> $ifname;
074 }
075 }
076 <isis> {
077 <interface> {
078 <name> $ifname;
079 if ($isis-level-1 || $isis-level-1-metric) {
080 <level> {
081 <name> "1";
082 if ($isis-level-1) {
083 <xsl:element name = $isis-level-1>;
084 }
085 if ($isis-level-1-metric) {
086 <metric> $isis-level-1-metric;
087 }
088 }
089 }
090 if ($isis-level-2 || $isis-level-2-metric) {
091 <level> {
092 <name> "2";
093 if ($isis-level-2) {
094 <xsl:element name = $isis-level-2>;
095 }
096 if ($isis-level-2-metric) {
097 <metric> $isis-level-2-metric;
098 }
099 }
100 }
101 }
102 }
103 <ldp> {
104 <interface> {
105 <name> $ifname;
106 }
107 }
108 }
109 <class-of-service> {
110 <interfaces> {
111 <name> ../name;
112 <apply-groups> {
113 <name> "cos-details";
114 }
115 }
116 }
117 }
118 }
119 }
XML Script Contents
01 <?xml version="1.0"?>
02 <script version="0.1">
03 <title>params.slax</title>
04 <alternate>params.xsl</alternate>
05 <author>phil</author>
06 <synopsis>
07 Turns simplified interface configuration into diverse configuration
08 </synopsis>
09 <keyword>apply-macro</keyword>
10 <keyword>interface</keyword>
11 <keyword>configuration groups</keyword>
12
13 <description>
14 We define an apply-macro for simplified interface configurations.
15 These are expanded into a diverse set of configuration statements.
16 </description>
17 <implementation>
18 Using configuration groups (such as 'interface-details' and
19 'cos-details' in this example) with commit scripts can keep the
20 details of any configuration expansions in the configuration
21 database, where they can be more easily changed.
22 </implementation>
23
24 <example>
25 <config>params.conf</config>
26 <output>params.output</output>
27 </example>
28 <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
29 src="../../../../../web/leaf.js" type="text/javascript"/>
30 </script>