0% found this document useful (0 votes)
117 views

Devnet 2367

DEVNET-2367

Uploaded by

Ala Jebnoun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Devnet 2367

DEVNET-2367

Uploaded by

Ala Jebnoun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

#CLUS

Advanced XML
Templates

Fatih Ayvaz, Cisco CX Solutions Architect

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

Enhancements  Some flow-control already


exists (namespaces, foreach,
when)

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

Processing  when, foreach attributes


deprecated, but no removal
Functions scheduled

 Explicitly changing XPath


context (set-context-node,
set-root-node, save-context,
switch-context)
 Copy configuration subtree
(copy-tree)

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>

• <?else?> clause is optional


#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 9
Example: foreach
<vrf>
<name>VPN{/vpn-number}</name>
<ip-route-forwarding-list foreach=”{tunnel}">

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>

• Any number of tags inside <?foreach?> block is allowed

#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>

• Variable is not visible after <?end?>


• Can be a while-loop if no start and step expressions are specified:
<?for ;{expression};?>

#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>

• Variable is visible after <?end?>, but not after </interface>


• Variable value is always a string
• {$x > $y} is false when x=11, y=9

#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 -->

• Changes the root context node used to evaluate absolute XPath


expressions, but not current context node
• The argument for set-root-node is evaluated in a special context where the
root is the root of the datastore, but current node is unchanged

#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 -->

• The argument is the identifier of the context, can be any


• If a name already exists, then <?save-context name?> replaces the stored
context

#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;
}
}

• A template could look like:


<dst>
<?copy-tree {/src}?>
</dst>
• The source tree is a subset of the destination tree

#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>

• The equivalent of the Maapi.copy_tree() API call


• Source path is the argument
• Destination path is determined by the position of the instruction in the
template
• Disadvantage: runtime error if source and destination data models do not
match

#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

Webex Teams will be moderated cs.co/ciscolivebot#DEVNET-2367


by the speaker until June 16, 2019.

#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

Meet the engineer


Related sessions
1:1 meetings

#CLUS DEVNET-2367 © 2019 Cisco and/or its affiliates. All rights reserved. Cisco Public 22
Thank you

#CLUS
#CLUS

You might also like