ENCOR Chapter 28
ENCOR Chapter 28
Network Programmability
Concepts
Instructor Materials
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2
Chapter 28 Content (Cont.)
GitHub - This section illustrates different use cases for version control and the
power of community code sharing.
Basic Python Components and Scripts - This section illustrates the components
of Python scripts and how to interpret them.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 3
Command-Line Interface
(CLI)
The biggest flaw with using the CLI to manage a network is misconfiguration.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 4
Command-Line Interface (CLI)
Command-Line Interface (CLI)
The most glaring and biggest flaws with using the CLI to manage a network is misconfiguration.
When businesses have increased complexity in their networks, the cost of something failing can
be very high due to the increased time it takes to troubleshoot the issues in a complex network.
There are tools that can assist in reducing the number of outages that are caused by human error
due to misconfigurations in the CLI.
Table 28-2 CLI PROs and CONs
PROs CONs
Well known and documented Difficult to scale
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 6
Application Programming Interface
Northbound and Southbound APIs
A method of communicating with and configuring a network is the use
of application programming interfaces (APIs).
The two most common APIs: the Northbound and Southbound APIs
which are used in network automation.
functions that most applications CREATE Inserts data in a database or application Updating a customer’s home address in a
or databases use to store or alter database
READ Retrieves data from a database or Pulling up a customer’s home address from a
data. These functions are called application database
“CRUD” functions: CREATE, UPDATE Modifies or replaces data in a database Changing a street address stored in a
READ, UPDATE, and DELETE. or application database
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 9
Application Programming Interface
Introduction to Postman
One of the most important pieces of interacting with
any software using APIs is testing. Testing code
helps ensure that developers are accomplishing the
outcome that was intended when executing the
code.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 10
Application Programming Interface
Introduction to Postman (Cont.)
The Postman application has various sections that you can interact with.
The focus here is on using the Builder portion of the dashboard.
• History - The history tab shows a list of all the recent API calls made.
• Collections - API calls can be stored in groups, called collections,
that are specific to a user’s needs.
• New Tab - Tabs provide another very convenient way to work with
various API calls. Each tab can have its own API call and parameters
that are completely independent of any other tab.
• URL Bar - Each tab has its own URL bar to be able to use a specific
API. Remember that an API call using REST is very much like an
HTTP transaction. Each API call in a RESTful API maps to an
individual URL for a particular function. This means every
configuration change or poll to retrieve data a user makes in a REST
API has a unique URL—whether it is a GET, POST, PUT, PATCH, or
DELETE function.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 11
Application Programming Interface
Data Formats XML and JSON
XML and JSON are two of the most common data formats that are used with APIs. Extensible Markup
Language (XML) is commonly used when constructing web services.
XML is a tag-based language. For example, a start tag named interface is represented as <interface>
and the end tag for <interface> would be </interface>.
Inside the start tag and end tag, you can use different code and parameters. Example 28-1 shows a
snippet of XML output with both start and end tags as well as some configuration parameters.
JSON uses objects for its format. Each JSON object starts with a { and ends with a }.
Example 28-3 shows how JSON can be used to represent the same username example shown for
XML in Example 28-1. You can see that it has four separate key/value pairs, one for each user’s
name.
In this JSON code snippet, you can see that the first key is user, and the value for that key is a unique
username, root.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 13
Application Programming Interface
HTTP Status Codes
Now that the XML and JSON data formats have been explained, it is important to circle back to using the
REST API and the associated responses and outcomes of doing so.
First, we need to look at the HTTP response status codes. Most internet users have experienced the
dreaded “404 Not Found” error when navigating to a website.
Table 28-5 lists the most common HTTP status codes as well as the reasons users may receive each one.
HTTP Status Code Result Common Reason for Response Code
404 Not Found Page at HTTP URL location does not exist or is hidden
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 14
Application Programming Interface
Cisco DNA Center APIs
The Cisco DNA Center controller expects all incoming data from the REST API to be in JSON format.
The HTTP POST function is used to send the credentials to the Cisco DNA Center controller.
Cisco DNA Center uses basic authentication to pass a username and password to the Cisco DNA
Center Token API to authenticate users. This API is used to authenticate a user to the Cisco DNA
Center controller to make additional API calls.
The key steps necessary to successfully set up the API call in Postman are as follows:
The Network Device API allows users to retrieve a list of devices that are currently in inventory that
are being managed by the Cisco DNA Center controller.
You need to prepare Postman to use the token that was generated when you successfully
authenticated:
Step 1. Copy the token you received earlier and click a new tab in Postman.
Step 2. In the URL bar enter https://sandboxdnac.cisco.com/api/v1/network-device
Step 3. Select the HTTP GET operation from the dropdown box.
Step 4. Select the Headers tab and enter Content-Type as the key.
Step 5. Select application/json as the value.
Step 6. Add another key and enter X-Auth-Token.
Step 7. Paste the token in as the value.
Step 8. Click Send to pass the token to the Cisco DNA Center controller and perform an HTTP GET
to retrieve a device inventory list.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 16
Application Programming Interface
Cisco DNA Center APIs (Cont.)
Within a few moments an API call can be used to gather
that data for the entire network.
When using APIs, it is common to manipulate data by
using filters and offsets. If a user wants to leverage the
Network Device API to gather information on only the
second device in the inventory. This is where the API
documentation becomes so valuable. Most APIs have
documentation that explains what they can be used to
accomplish.
In Postman, it is possible to modify the Network Device
API URL and add ?limit=1 to the end of the URL to show
only a single device in the inventory. It is also possible to
add the &offset=2 command to the end of the URL to state
that only the second device in the inventory should be
shown. These query parameters are part of the API.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 17
Application Programming Interface
Cisco vManage APIs
There are various APIs available in the Cisco SD-WAN and the vManage controller.
With a Cisco SD-WAN API you need to provide login credentials to the API in order to be able
to utilize the API calls.
Some key pieces of information are necessary to successfully set up the API call in Postman:
• The URL bar must have the API call to target the Authentication API
• The HTTP POST operation is used to send the username and password to Cisco vManage
• The Headers Content-Type key must be application/x-www-form-urlencoded
• The body must contain keys with the j_username devnetuser and thej_password Cisco123!
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 18
Data Models and Supporting
Protocols
This section provides a high-level overview of some of the most common data models and
tools and how they are leveraged in a programmatic approach:
• Yet Another Next Generation (YANG) modeling language
• Network Configuration Protocol (NETCONF)
• RESTCONF
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 19
Data Models and Supporting Protocols
YANG Data Models
YANG uses data models. Data models are used to describe whatever
can be configured, monitored or executed on a device.
Some of the key differences between SNMP and NETCONF are listed in Table 28-6.
One of the most important differences is that SNMP can’t distinguish between configuration data and
operational data, but NETCONF can. Another key differentiator is that NETCONF uses paths to
describe resources, whereas SNMP uses object identifiers (OIDs).
A NETCONF path can be similar to interfaces/interface/eth0, which is much more descriptive than what
you would expect from SNMP. Feature SNMP NETCONF
NETCONF exchanges information called capabilities when the TCP connection has been made.
Capabilities tell the client what the device it’s connected to can do. Furthermore, other information can
be gathered by using the common NETCONF operations shown in Table 28-7.
Information and configurations are stored in datastores. Datastores can be manipulated by using the
NETCONF operations listing in Table 28-7. NETCONF uses Remote Procedure Call (RPC) messages in
XML format to send the information between hosts.
NETCONF Description
Operation
<get> Requests running configuration and state information of the device
<get-config> Requests some or all of the configuration from a datastore
<edit-config> Edits a configuration datastore by using CRUD operations
<copy-config> Copies the configuration to another datastore
<delete-config> Deletes the configuration
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 23
Data Models and Supporting Protocols
NETCONF (Cont.)
Example 28-9 shows an example of an OSPF
NETCONF RPC message that provides the OSPF
routing configuration of an IOS XE device.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 24
Data Models and Supporting Protocols
RESTCONF
RESTCONF is used to programmatically interface with data
defined in YANG models while also using the datastore concepts
defined in NETCONF.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 26
Cisco DevNet
DEVNET API Labs
This section provides a high-level overview of DevNet,
including the different sections of DevNet and some of
the labs and content that are available.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 29
GitHub
GitHub Repositories and Version Control
One of the most commonly adopted ways of using version control is by using GitHub.
GitHub is a hosted web-based repository for code.
Using GitHub offers: easiest ways to track changes in your files, collaborate with other
developers, and share code with the online community.
GitHub provides a guide that steps through how to
create a repository, start a branch, add comments,
and open a pull request.
Projects are repositories that contain code files.
GitHub provides a single pane to create, edit, and
share code files.
Figure 28-21 shows a repository called ENCORE
that contains three files: ENCORE.txt,
JSON_Example.txt, README.md
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 30
GitHub
GitHub Repositories and Version Control (Cont.)
GitHub gives a summary of commit logs, so when
you save a change in one of your files or create a
new file, GitHub shows details about it on the
main repository page.
In the JSON_Example.txt, for example, GitHub
shows its contents and how to edit the file in the
repository.
Figure 28-22 shows the contents of the
JSON_Example.txt file and the options available
with the file.
This editor is very similar to any text editor.
Other GitHub users and developers can contribute
to this code or add and delete lines of code based
on the code that was originally created.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 31
Basic Python Components
and Scripts
• Python has become one of the most common programming languages in terms of
network programmability.
• This section leverages the new knowledge you have gained in this chapter about APIs,
HTTP operations, DevNet, and GitHub.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 32
Basic Python Components and Scripts
Interpreting Python Scripts
When you understand the basics of interpreting Custom - This is used in the event that there is
what a Python script is designed to do, it will be already a Cisco DNA Center installed either in a
easier to understand and leverage other scripts lab or another facility, and it needs to be
that are available. accessed using this script.
The variables shown are in the JSON data format that was discussed earlier in this chapter.
Remember that JSON uses key/value pairs and is extremely easy to read and interpret.
In Example 28-13, you can see the key/value pair “username”: “devnetuser”. The structure used to
hold all the key/value pairs in this script is called a dictionary. In this particular Python script, the
dictionary is named dnac. The dictionary named dnac contains multiple key/value pairs, and it starts
and ends with curly braces ({})
username devnetuser Username to log in to Cisco DNA Center sandbox (via API or GUI)
password Cisco123! Password to log in to Cisco DNA Center sandbox (via API or GUI)
Table 28-8 Python Variables for Cisco DNA Center Sandbox in Env_Lab.py
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 34
Basic Python Components and Scripts
Interpreting Python Scripts (Cont.)
In the get_dnac_device.py script.
The first section of code tells the Python
interpreter what modules this particular
script will use.
Think of a module as a collection of
actions and instructions. To better explain
the contents in this script, comments are
inserted throughout the script to help
document each section.
Example 28-17 shows the first section of
the get_dnac_devices.py with comments.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 35
Basic Python Components and Scripts
Interpreting Python Scripts (Cont.)
Modules help Python understand
what it is capable of.
If a developer tried to do an HTTP
GET request without having the
Requests modules imported, it
would be difficult for Python to
understand how to interpret the
HTTP call. Although there are other
ways of doing HTTP calls from
Python, the Requests modules
greatly simplify this process.
Example 28-18 shows the second
section of the get_dnac_devices.py
script.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 36
Basic Python Components and Scripts
Interpreting Python Scripts (Cont.)
Functions are blocks of code that are built to perform specific actions. Functions are very structured in
nature and can often be reused later on within a Python script.
Some functions are built into Python and do not have to be created. The print function, which can be
used to print data to a terminal screen. You can see the print function at the end of the
get_dnac_devices.py script.
In order to execute any API calls to Cisco DNA Center, you must be authenticated, using the Token
API.
Example 28-19 shows the use of the Token API within a Python script. (Recall that you saw this API
used with Postman earlier in the chapter.)
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 37
Basic Python Components and Scripts
Interpreting Python Scripts (Cont.)
The script shown in Example 28-20 ties the Token API to the Network Device API call to retrieve
the information from Cisco DNA Center.
The line that says header [“x-auth-token”] = token is mapping the JSON response from the
previous example, which is the token, into the header called x-auth-token. In addition, the URL
for the API has changed to network_device, and the response is sending a requests.get to that
URL.
This is exactly the same example used with Postman earlier in this chapter.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 38
Basic Python Components and Scripts
Interpreting Python Scripts (Cont.)
The final section of get_dnac_devices.py shows code that ties the dnac dictionary that is in the
Env_Lab.py script to the dnac_login function covered earlier. In addition, the print function takes
the response received from the response.get that was sent to the Network Device API and puts it
into the table format that was specified earlier in the script with the name dnac_devices.
Example 28-21 shows the final lines of code in the script.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 39
Prepare for the Exam
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 40
Prepare for the Exam
Key Topics for Chapter 28
Description
HTTP Functions and Use Cases
CRUD Functions and Use Cases
HTTP Status Codes
Steps to authenticate to Cisco DNA Center using a POST operation and basic
authentication
Steps to leverage the Network Device API to retrieve a device inventory from Cisco DNA
Center
Using the offset and limit filters with the Network Device API when gathering device
inventory
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 41
Prepare for the Exam
Key Terms for Chapter 28
Key Terms
Application Programming Interface (API)
command-line interface (CLI)
DevNet
Extensible Markup Language (XML)
GitHub
Java-Script Object (JSON)
NETCONF
Python
RESTCONF
Yang Model
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 42