Devnet 2367
Devnet 2367
Advanced XML
Templates
DEVNET-2367
#CLUS
Agenda
• Background and Problem Statement
• NSO Template Language Enhancements
• Limitations
• Example Usage
• Demonstration
• Conclusion
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 3
“Templates” is a powerful
mechanism for implementing
services
Allows to declare target
Background configuration
Simplifies the task of writing a
service
Simplifies understanding the
service maintenance
DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 4
Disadvantage: limited
functionality
Many services cannot be
implemented in templates
Previous solution: use both
Problem Java/Python and templates
Invoke templates multiple times
per service: context switching
The logic of the service is split
between code and templates
Harder to understand
DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 5
Extend the template
language
Template Make it applicable to a wider
Language range of problems
DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 6
Add more features using XML
Processing Instructions:
Setting variables at runtime
More control flow statements:
if, for, foreach
DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 7
XML Processing instructions
is an instruction for the XML
handler on how to process
the XML data
Limitation Not stored in CDB
The feature is only applicable in
XML templates, not CDB
templates
DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 8
Example: if
<interface>
<name>{/interface}</name>
<shutdown when="{shutdown}"/>
Before: <address when="{not(shutdown)}">{address}</address>
<mask when="{not(shutdown)}">{mask}</mask>
</interface>
<interface>
<name>{/interface}</name>
<?if {shutdown}?>
<shutdown/>
Now: <?else?>
<address>{address}</address>
<mask>{mask}</mask>
<?end?>
</interface>
Before:
<prefix>{network}</prefix>
<mask>{netmask}</mask>
<forwarding-address>10.255.254.{(tunnel-number - 1)*4+2}</forwarding-address>
</ip-route-forwarding-list>
</vrf>
<vrf>
<name>VPN{/vpn-number}</name>
<?foreach {tunnel}?>
<ip-route-forwarding-list>
Now:
<prefix>{network}</prefix>
<mask>{netmask}</mask>
<forwarding-address>10.255.254.{(tunnel-number – 1)*4/2}</forwarding-address>
</ip-route-forwarding-list>
<?end?>
</vrf>
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 10
Example: for
Before: Not possible to iterate by variable.
<interface xmlns="urn:ios">
<?for i=0; {$i < 4}; i={$i + 1}?>
<FastEthernet tags="nocreate">
<name>0/{$i}</name>
Now: <shutdown tags="merge"/>
</FastEthernet>
<?end?>
<!-- i doesn’t exist here anymore -->
</interface>
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 11
Example: set
Before: Not possible to assign variable at runtime
<interface xmlns="urn:ios">
<?set i=0?>
<?for ;{$i < 4};?>
<FastEthernet tags="nocreate">
<name>0/{$i}</name>
Now: <shutdown tags="merge"/>
</FastEthernet>
<?set i={$i + 1}?>
<?end?>
<!-- i=‘4’ here -->
</interface>
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 12
Example: set-context-node
<device-list foreach=”{/devices}">
Before: <name>{device}</name>
</device-list>
<?set-context-node {/devices}?>
<device-list>
Now: <name>{device}</name>
</device-list>
<!-- context node is /devices until the following closing tag -->
• Only changes the current context node used to evaluate relative XPath
expressions
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 13
Example: set-root-node
Before: Not possible to change the root node.
<?set-root-node {/devices}?>
<device-list>
Now: <name>{/device}</name>
</device-list>
<!-- root node is /devices until the following closing tag -->
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 14
Example: save-context, switch-context
Before: Not possible to switch between multiple contexts.
<?save-context mycontext?>
<!-- both root and current context nodes saved -->
<?set-root-node {/other-tree}?>
<?set-context-node {/other-subtree}?>
Now: <dst>
…
</dst>
<?switch-context mycontext?>
<!-- both root and current context nodes restored -->
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 15
Example: copy-tree
container src { container dst {
list foo { list foo {
key bar; key bar;
leaf bar { leaf bar {
type string; type string;
} }
} }
leaf baz { leaf baz {
type uint16; type uint16;
} }
} leaf foobar {
type string;
}
}
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 16
Example: copy-tree
<dst>
<foo>
Before:
<bar>{/src/foo/bar}</bar>
</foo>
<baz>{/src/baz}</baz>
</dst>
<dst>
Now: <?copy-tree {/src}?>
</dst>
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 17
Debugging
ncs% set some-service some-data
[ok]
ncs% commit dry-run | debug template
Processing instruction 'if': evaluating the condition (from file "tst2.xml", line 4)
Evaluating conditional expression "boolean(. != 'z')" (from file "tst2.xml", line 4)
Context node: /tst2[id='1']/ll1[.='x']
Result: true – continuing
Processing instruction 'else': skipping (from file "tst2.xml", line 19)
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 18
Demo
Cisco Webex Teams
Questions?
Use Cisco Webex Teams to chat
with the speaker after the session
How
1 Find this session in the Cisco Live Mobile App
2 Click “Join the Discussion”
3 Install Webex Teams or go directly to the team space
4 Enter messages/questions in the team space
#CLUS © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 20
Complete your
online session • Please complete your session survey
evaluation after each session. Your feedback
is very important.
• Complete a minimum of 4 session
surveys and the Overall Conference
survey (starting on Thursday) to
receive your Cisco Live water bottle.
• All surveys can be taken in the Cisco Live
Mobile App or by logging in to the Session
Catalog on ciscolive.cisco.com/us.
Cisco Live sessions will be available for viewing
on demand after the event at ciscolive.cisco.com.
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 21
Continue your education
Demos in the
Walk-in labs
Cisco campus
#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 22
Thank you
#CLUS
#CLUS