09_Cisco IOS XR Programmability - Student Learning Guide v5.0 - DOCX-DP
09_Cisco IOS XR Programmability - Student Learning Guide v5.0 - DOCX-DP
Introduction
Typically, a network operation center is a heterogeneous mix of various devices at multiple layers of the
network. Such network centers require bulk automated configurations to be accomplished seamlessly. CLIs
are widely used for configuring and extracting the operational details of a router. But the general
mechanism of CLI scraping is not flexible and optimal. Small changes in the configuration require rewriting
scripts multiple times. Bulk configuration changes through CLIs are cumbersome and error-prone. These
limitations restrict automation and scale. To overcome these limitations, you need an automated mechanism
to manage your network.
Cisco IOS XR supports a programmatic way of configuring and collecting operational data of a network
device using data models. They replace the process of manual configuration, which is proprietary, and
highly text-based. The data models are written in an industry-defined language and are used to automate the
configuration task and retrieve operational data across heterogeneous devices in a network. Although
configurations using CLIs are easier and human-readable, automating the configuration using model-driven
programmability results in scalability.
Model-driven programmability provides a simple, flexible, and rich framework for device programmability.
This programmability framework provides multiple choices to interface with an IOS XR device in terms of
transport, protocol, and encoding. These choices are decoupled from the models for greater flexibility.
Data models help to manipulate configuration data and retrieve operational data. The data models replace
the process of manual configuration and are written in an industry-defined language. Although
configurations using CLIs are easier and are humanly readable, automating the configuration by using data
models results in scalability. Data models provide a well-defined hierarchy of the configurational and
operational data of a router. This common framework helps you to program and manage a network with
ease.
Data models handle the following types of data on routers:
• Configuration data: A set of writable data that is required for transforming a system from an initial
default state into its current state. A few examples are configuring entries of the IP routing tables,
configuring the interface MTU to use a specific value, and configuring an Ethernet interface to run at a
given speed.
• Operational state data: A set of data that is obtained by the system at run time and influences the
behavior of the system like that of configuration data. However, in contrast to configuration data,
operational state data is transient. The data is modified by interactions with internal components or
other systems, using specialized protocols. Two examples are entries obtained from routing protocols
such as Open Shortest Path First (OSPF), and attributes of the network interfaces.
Newer application programming interfaces (APIs) require clear and well-defined ways to interact with
them. They should also be able to use custom and open-source tooling to easily consume the APIs.
APIs should support the ability to offer client-side validation. When you use a next-generation approach that
is purely model-driven, it becomes a great by-product. Rather than have the device do error checking, the
management applications that use the device's API and model automatically handle it.
Data needs to be structured such that it is easy to access and modify in a standard way.
Users should have the flexibility to pick and choose from preferred management, transport, and encoding
protocols.
Next-generation APIs should support the ability of multiple types of configurations, making it simpler to
perform backups and restores, but also to improve how changes are made. As a result, changes should have
the ability to be staged and applied as a transaction.
As next-generation programmatic interfaces are being built, there are a few key attributes that must be met:
• They must support various types of transport: HTTP, SSH, and TLS.
• They must be flexible and support various types of encoding formats, such as XML and JSON.
• There must be efficient and easy-to-use tooling that helps consume the new APIs—for example
programming libraries.
• There must be extensible and open APIs: REST, RESTCONF, Network Configuration Protocol
(NETCONF), and Google-defined remote procedure call (gRPC).
Also, they must be model-driven. Being model-driven is what helps any transport, API, encoding, and data
format.
Note NETCONF is not the only protocol. Network Services Orchestrator (NSO) also uses Network Element
Drivers (NEDs) to support other protocols.
The IETF defines the YANG data modeling language in RFC 6020 and augments it with several other
RFCs:
• RFC-7317 YANG for System Management
• RFC-7277 YANG for IP Management
• RFC-7224 IANA Intf. Type YANG Module
• RFC-7223 YANG for Intf. Management
• RFC-6991 Common YANG Data Types
• RFC-6536 NETCONF Access Control
• RFC-6470 NETCONF Base Notifications
• RFC-6244 Architecture for Network Management Using NETCONF and YANG
• RFC-6087 Guidelines for YANG Authors
• RFC-6022 YANG for NETCONF Monitoring
NETCONF Capabilities
NETCONF has many capabilities, some of which perform granular services.
The following are the NETCONF capabilities:
• The distinction between configuration and state data.
• Multiple configuration data stores (candidate, running, startup).
• Configuration change transactions.
• Configuration testing and validation support.
• Selective data retrieval with filtering.
• Streaming and playback of event notifications.
• Extensible procedure call mechanism.
YANG Capabilities
The YANG service model has many capabilities.
These are the YANG capabilities:
• Human readable, easy-to-learn representation.
• Hierarchical configuration data models.
• Reusable types and groupings (structured types).
• Extensibility through augmentation mechanisms.
• Supports the definition of operations (RPCs).
• Formal constraints for configuration validation.
• Data modularity through modules and submodules.
• Versioning rules and development support.
Note YANG defines the data structure and formal constraints for validating device configuration data (XML).
Data Models
The data models characteristics are as follows:
• Describe a constrained set of data in the form of a schema language.
• Use well-defined parameters to standardize the representation of data.
• Are not used to send information to devices.
• Device configuration can be validated against a data model.
• It can define attributes and answers to such questions as the following:
– What is the range of a valid VLAN ID?
– Can a VLAN name contain spaces?
– Should the values be enumerated and support only “up” or “down” for an admin state?
– Should the value be a string or an integer?
Open models were not used to be augmented by a vendor. Instead, vendors have their native models.
Augmentation is used for building a hierarchical view where subtrees are defined in other models.
YANG data models can be of two main classes, open or native.
The figure shows data models for different protocols.
YANG data models can be represented in a hierarchical, tree-based structure with nodes. This
representation makes the models easy to understand.
Each feature has a defined YANG model, which is synthesized from schemas.
A model in a tree format includes:
• Top-level nodes and their subtrees
• Subtrees that augment nodes in other YANG models
• Custom RPCs
YANG defines four node types. Each node has a name. Depending on the node type, the node either defines
a value or contains a set of child nodes.
The node types for data modeling are:
• Leaf node: Contains a single value of a specific type.
• Leaf-list node: This contains a sequence of leaf nodes.
• List node: A node that may exist multiple times in a data tree. A list has no value, but rather a set of
child nodes.
• Container node: Contains a grouping of related nodes that have only child nodes, which can be any of
the four-node types.
The publicly available YANG data model information is located on these websites:
• https://github.com/YangModels/yang
• https://github.com/openconfig/public
The main differences between native and open data models include the following:
• Native data models provide the most configuration and operation coverage.
• Open models map to native data models.
• Departures from open models are specified as a deviation module.
A growing number of major vendors (such as Cisco and many others) already support NETCONF and
YANG to some degree, and the support is growing as the service providers increasingly make NETCONF
and YANG a requirement in their projects.
The module is the base unit of definition in YANG. A module defines a single data model. A module can
define a complete, cohesive model, or augment an existing data model with additional nodes. Submodules
are partial modules that contribute definitions to a module. A module can include any number of
submodules, but each submodule can belong to only one module. The names of all standard modules and
submodules must be unique. It is recommended that developers of enterprise modules choose names for
their modules that will have a low probability of colliding with standard or other enterprise modules—for
example, by using the enterprise or organization name as a prefix for the module name.
YANG models the hierarchical organization of data as a tree in which each node has a name and a value or
a set of child nodes.
YANG Modules
Each module should have the following sections:
• Module name that should be the same as the module’s filename (case-sensitive equality between
module name and filename prevents a warning message when you validate a module).
• Header information describes the module and gives information about the module itself:
– Namespace (mandatory): Defines the Uniform Resource Identifier (URI) of the XML namespace.
– Prefix (mandatory): The prefix statement defines the prefix associated with the module and its
namespace. The prefix statement’s argument is the prefix string that you use as a prefix to access a
module.
– Organization, contact (optional): Describes the module origin (such as a company, or author).
– Description (optional): Describes the module (what it is, what it does, and so on).
• Revision information: Provides versioning of the module and facilitates the importing of modules
based on revisions.
• Imports and includes: The import and include statements make definitions available to other modules
and submodules.
• Type definitions: In addition to built-in data types, you can define custom types and use them later in
the data models.
• Reusable node declarations: In addition to type definitions, you can define and reuse a more complex
set of YANG codes.
• Configuration and operational data declarations: The main part of the module, where you define the
actual data model.
• Action (RPC) and notification declarations: Defines NETCONF RPCs and notifications to use within
the module.
There must not be any circular chains of imports or includes. For example, if submodule “a” includes
submodule “b,” “b” cannot include “a.” When you reference a definition in an external module, you must
use a locally defined prefix, followed by “:” and the external identifier. References to definitions in the local
module can use the prefix notation. Because built-in data types do not belong to any module and have no
prefix, references to built-in data types (for example, int32) cannot use the prefix notation.
YANG Modules and Submodules
The output shows the YANG module “include” option where the submodule is included.
The output shows the YANG module presented in the XML format. The XML format representation is
especially useful in large and complex models where a basic editor is no longer the best choice for viewing
and editing the YANG data model.
The output shows the YANG module presented in the JSON format. The JSON format is popular because it
is simple and easy to read and understand.
The output shows the CLI format. The CLI format is well known for many years.
1. 0Which protocol uses XML-based data encoding and provides mechanisms to install, manipulate,
and delete device configurations?
a. gRPC
b. NETCONF
c. Python
d. UDP
0NETCONF Fundamentals
This topic describes the foundational principles of NETCONF.
NETCONF is an IETF network management protocol designed specifically for configuration management.
The following are the NETCONF capabilities:
• The distinction between configuration and state data.
• Multiple configuration data stores (candidate, running, startup).
• Configuration change transactions.
• Configuration testing and validation support.
• Selective data retrieval with filtering.
• Streaming and playback of event notifications.
• Extensible procedure call mechanism.
NETCONF is a next-generation network management protocol that is designed specifically to help with
transactional-based network management and to improve upon the weaknesses of SNMP. NETCONF also
makes a distinction between configuration and state data.
One of the most unique attributes of NETCONF is that it uses multiple configuration data stores (candidate,
running, startup), though a device is not required to implement this feature to "support" the protocol.
NETCONF utilizes a candidate configuration, which is simply a configuration with all proposed changes
applied in an uncommitted state. It is the equivalent of entering CLI commands that do not take effect right
away. You would then "commit" all the changes as a single transaction. After they are committed, you
would see them in the running configuration.
NETCONF supports device transactions, which means that when you make an API call configuring
multiple objects and one fails, the entire transaction fails, and you do not end up with a partial
configuration. They are also configurable and platform-dependent. You can still force a change so that it
fails on error or rolls back on error.
NETCONF offers the ability to use filters that are based on XML documents or expressions and to
selectively request specific components of configuration or operational state data.
The NETCONF characteristics are as follows:
• Provides mechanisms to create, replace, merge, and delete device configurations.
• Provides mechanisms to retrieve operational data.
• Uses an XML-based data encoding for the configuration data, and protocol messages.
• Uses RPC mechanism to facilitate communication between a client and server.
NETCONF provides mechanisms to create, replace, merge, and delete the configuration of network devices.
NETCONF also provides mechanisms to retrieve operational data. It uses XML data encoding for the
configuration data, and protocol messages. NETCONF uses a simple RPC mechanism to facilitate
communication between a client and a server. The client can be a script or application that runs as part of a
network manager. The server is a network device such as a router.
A NETCONF session is a logical connection between a network configuration application (client) and a
network device (router). The configuration attributes can be changed during any authorized session.
NETCONF is connection oriented, with SSH as the underlying transport. NETCONF sessions are
established with a hello message, where features and capabilities are announced. Sessions are terminated
with the use of close or kill messages.
Operation Description
edit-config Loads all or part of a specified configuration (merge, replace, create, delete, remove).
Get Retrieve all or part of running configuration and device operational data.
Operation Description
unlock Remove lock on entire configuration data store (for example, candidate).
NETCONF defines one or more configuration data stores and allows configuration operations on the data
stores. A configuration data store is a complete set of configuration data that is required for getting a device
from its initial default state into a desired operational state. The configuration data store does not include
state data or executive commands.
NETCONF Example
NETCONF Request (Client to Router)
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<get-config>
<source><running/></source>
<filter>
<cdp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-cdp-cfg"/>
</filter>
</get-config>
</rpc>
The example shows how a NETCONF <get-config> request works with the Cisco Discovery Protocol
feature.
The client initiates a message to get the current configuration of Cisco Discovery Protocol running on the
router. The router then responds with the current Cisco Discovery Protocol configuration.
The <rpc> element in the request and response messages encloses a NETCONF request sent between the
client and the router. The message-id attribute in the <rpc> element is mandatory. This attribute is a string
chosen by the sender, and it encodes an integer. The receiver of the <rpc> element does not decode or
interpret this string but simply saves it to be used in the <rpc-reply> message. The sender must ensure that
the message-id value is normalized. When the client receives information from the server, the <rpc-reply>
message contains the same message-id.
NETCONF Response (Router to Client)
<?xml version="1.0"?>
<rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<cdp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-cdp-cfg">
<timer>10</timer>
<enable>true</enable>
<log-adjacency></log-adjacency>
<hold-time>200</hold-time>
<advertise-v1-only></advertise-v1-only>
</cdp>
</data>
</rpc-reply>
Operation Description
NETCONF Actions
You can perform the following NETCONF actions:
logmsg Cisco-IOS-XR-syslog-act.yang
snmp Cisco-IOS-XR-snmp-test-trap-act.yang
rollback Cisco-IOS-XR-cfgmgr-rollback-act.yang
ping Cisco-IOS-XR-ping-act.yang
Cisco-IOS-XR-ipv4-ping-act.yang
Cisco-IOS-XR-ipv6-ping-act.yang
traceroute Cisco-IOS-XR-traceroute-act.yang
Cisco-IOS-XR-ipv4-traceroute-act.yang
Cisco-IOS-XR-ipv6-traceroute-act.yang
crypto Cisco-IOS-XR-crypto-act.yang
The example shows how a NETCONF action request to run the ping command on a router.
NETCONF Response (Router to Client)
<rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<ping-response xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
<ipv4>
<destination>192.168.1.1</destination>
<repeat-count>5</repeat-count>
<data-size>100</data-size>
<timeout>2</timeout>
The example shows the NETCONF action response from the router.
<pattern>0xabcd</pattern>
<rotate-pattern>0</rotate-pattern>
<reply-list>
<result>!</result>
<result>!</result>
<result>!</result>
<result>!</result>
<result>!</result>
</reply-list>
<hits>5</hits>
<total>5</total>
<success-rate>100</success-rate>
<rtt-min>1</rtt-min>
<rtt-avg>1</rtt-avg>
<rtt-max>1</rtt-max>
</ipv4>
</ping-response>
</rpc-reply>
NETCONF Router Configuration
The following command is an example of router configurations that enable NETCONF.
ssh server v2 ssh server netconf
!
netconf-yang agent
ssh
1. 0What NETCONF edit-config operation will create configuration if the configuration does not exist?
a. Create
b. Delete
c. Merge
d. Remove
e. Replace
0gRPC Fundamentals
This topic describes the foundational principles of gRPC.
The following are gRPC characteristics:
• Provides the infrastructure to build a device management service to exchange configuration and
operational data between a client and a server.
• The structure of the data is defined by YANG models.
• Definition of interface based on Protocol Buffers (Protobuf).
• Requests are encoded and sent to the router with the use of JSON
gRPC is an open-source RPC framework. gRPC provides the infrastructure to build a device management
service to exchange configuration and operational data between a client and a server and the structure of the
data is defined by YANG models.
Cisco gRPC IDL uses the Protocol Buffers interface definition language (IDL) to define service methods,
and define parameters and return types as Protocol Buffers message types. The gRPC requests are encoded
and sent to the router with the use of JSON. Clients can invoke the RPC calls defined in the IDL to program
the router.
Operation* Description
The Network Infrastructure Layer or Service Adaptation Layer has the following characteristics:
• Acts as the bridge between the Application Layer and the HW.
• Presents abstractions to the Application and Protocol Layer.
• Highly Performant API outside the Central Config and Oper Database context.
1. 0Which Cisco IOS XR Service Layer telnet can collect streaming notifications?
a. API for the “Do-it-yourself” system
b. Offload high-level tasks to network OS
c. Offload low-level tasks to network OS
d. Performance
0On-Box Automation Scripts
This topic describes the on-box automation scripts that you can use to automate network operations.
Network automation is imperative to deploy and manage the networks with large-scale cloud-computing
architectures. The automation can be achieved through standard model-driven data models. To cater to the
automation requirements, you can use the Cisco IOS XR infrastructure to make API calls and run scripts
from an external controller. These off-box scripts take advantage of the exposed interfaces such as
NETCONF, SNMP, and SSH to work on the network element. However, there is a need to maintain an
external controller to interact with the router.
To simplify the operational infrastructure, the automation scripts can be run on the router, eliminating the
need for an external controller. The executions of the various types of scripts are faster and reliable as it is
not dependent on the speed or network reachability of the external controller. Most script types interact with
IOS XR Software using standard protocols such as NETCONF. You can download scripts to the router,
configure scripts, view operational data, and set responses to events in the router.
In summary, on-box scripting is similar to off-box scripting, with the exception that the management
software that runs in an external controller is now part of the router software. The scripts programmatically
automate configuration and operational tasks on the network devices. You can create customized scripts that
are based on your network requirement and execute scripts on routers by running Cisco IOS XR operating
system. The packages that support scripting are provided in the software image.
The following are the automation script features:
• Operational simplicity by using automation scripts.
• Model-driven CLI to show YANG operational data.
• Model-driven CLI to display running configuration in XML and JSON formats.
• Manage automation scripts using YANG RPCs.
• Contextual script infrastructure.
Operational simplicity using automation scripts provides you the flexibility to deploy your automation
code on your router instead of running it on external controllers. With automation now available on-box and
integrated into the IOS XR Software, the router processes data locally using Python libraries that provide
direct access to the underlying device operations to execute CLI commands, monitor router configurations,
and status continuously. With on-box automation, you can efficiently control the end-to-end operations
from script enablement to deployment without depending on the connectivity, resource, and speed of an
external controller.
Model-driven CLI to show YANG operational data enables you to use a traditional CLI command to
display YANG data model structures on the router console and obtain operational data from the router in
JSON or XML formats. The functionality helps you transition smoothly between CLI and YANG models,
easing data retrieval from your router and network. This feature introduces the show yang operational
command.
Model-driven CLI to display running configuration in XML and JSON formats enables you to display
the configuration data for Cisco IOS XR platforms in both JSON and XML formats. This feature introduces
the show run | [xml | json] command.
Manage automation scripts using YANG RPCs enables you to use remote procedure calls (RPCs) on
YANG data models to perform the same automated operations as CLIs, such as edit configurations or
retrieve router information.
When you create and run Python scripts on the router, the contextual script infrastructure enables a
contextual interaction between the scripts, the IOS XR Software, and the external servers. This context,
programmed in the script, uses Cisco IOS XR Python packages, modules, and libraries to:
• Obtain operational data from the router.
• Set configurations and conditions.
Scripts Scope
Config Scripts Enforce contextual and conditional changes to configurations, validate configurations before
committing the changes to detect and notify potential errors. If configuration does not comply
with the rules that are defined in the script, an action can be invoked. For example, generate a
warning, syslog message, or halt a commit operation.
Exec Scripts Run operational commands or RPCs, process the output, generate syslogs, configure system,
perform system action commands such as system reload, process restarts, and collect logs for
further evaluation.
Process Scripts Daemonize to continuously run as an agent on the router to execute additional checks outside
traditional ZTP. Daemonized scripts are similar to exec scripts but run continuously. The script
executes operational commands on the router and analyzes the output.
EEM Scripts Run operational commands or RPCs, generate, and determine the next steps like logging the
root cause or changing device configuration. Event policies can upload the output of event
scripts to an on-box or off-box location for further analysis.
Scripts Invoke
Config Scripts All config scripts are processed automatically when commit command is executed on the
router.
Scripts Invoke
Exec Scripts Exec script is invoked manually via CLI command or RPC.
EEM Scripts Event scripts are invoked by defined event policies in response to a system event and allow
for immediate action to take effect.
Scripts Benefit
Config Scripts Simplifies complex configurations and averts potential errors before a configuration is
committed. Ensures that the network configuration complies with rules and policies that are
defined in the script.
Exec Scripts Collects operational information, and decreases the time that is involved in troubleshooting
issues.
Provides flexibility in changing the input parameters for every script run. This fosters dynamic
automation of operational information.
Process Scripts Runs scripts as a daemon to continuously perform tasks that are not transient.
EEM Scripts Automates log collection upon detecting error conditions that are defined by event policies.
Uploads the output of event scripts to an on-box or off-box location for further analysis.
Package Description
asn1crypto Parses and serializes Abstract Syntax Notation One (ASN.1) data structures.
ecdsa Implements Elliptic Curve Digital Signature Algorithm (ECDSA) cryptography library to
create keypairs (signing key and verifying key), sign messages, and verify the signatures.
Package Description
Library Syntax
1. 0What on-box automation script runs as a daemon to continuously perform tasks that are not
transient?
a. Config Scripts
b. EEM Scripts
c. Exec Scripts
d. Process Scripts
0YANG Development Kit
This topic describes YANG Development Kit (YDK) a software development kit that provides APIs that are
modeled in YANG.
The YDK is a software development kit that provides APIs that are modeled in YANG. The main goal of
the YDK is to reduce the learning curve by expressing the model semantics in API and abstracting protocol
and encoding details. YDK can generate APIs in a variety of programming languages by using YANG
models. These APIs can then be used to simplify the implementation of applications for network
automation.
YDK has two main components: an API generator (YDK-gen) and a set of generated APIs. Today, YDK-
gen takes YANG models as input and produces either Python APIs (YDK-Py) or C++ (YDK-Cpp) that
mirror the structure of the models.
These abstractions can help the developer. They isolate developers from the protocol, transport, and
encoding specifics and allow them to focus on what is most important—the data structures and the
automation logic. There is no need to code NETCONF RPC specifics or to manipulate XML directly. A set
of predefined services take care of the management protocol, the transport for your management session,
and the encoding and decoding of your data. Furthermore, the APIs perform some level of local validation
based on information embedded in the YANG model. This means that many errors can be caught locally
without having to communicate with the networking device. The abstractions also make the automation
applications more portable. An application could change providers with minimal changes to the code.
Client-Side Validation
The following are the client-side validation characteristics:
• Model constraints that are used during API generation.
• YDK service will automatically perform local (client-side) validation.
• Config (read-write) versus state (read-only).
• Type check (enum, string, and so on)
• Value check (range, pattern, and so on)
• Semantic check (key uniqueness or presence, mandatory leafs, and so on)
• Model deviation check (unsupported leaf, and so on)
YDK takes care of data validation so that you do not have to. Data models not only define a data hierarchy,
but also specify the constraints associated with the data (such as type, valid values, and ranges). YDK
services automatically perform thorough data validation (types, values, semantics, deviations, and so on).
That validation takes place locally before your data is exchanged with the network device.
Service Providers
from ydk.providers import NetconfServiceProvider
sp_instance = NetconfServiceProvider(address='10.0.0.1',
port=830,
username='test',
password='test',
protocol='ssh')
The first step in any application is to create a service provider instance. In this case, the NETCONF service
provider (defined in ydk.providers.NetconfServiceProvider) is responsible for mapping between the CRUD
service API and the underlying manageability protocol (NETCONF RPCs).
The CRUD service provides methods to create, read, update, and delete entities on a device, making use of
the session provided by a service provider (in this case NETCONF). To use the CRUD service, you must
import the CRUDService class, from ydk.services import CRUDService. Then instantiate the CRUD
service, CRUDService().
NETCONF Fundamentals
1. A
gRPC Fundamentals
1. B
Progress Check
1. B
2. C
3. D
4. B
5. D
6. A, C
7. C