DEVNET USE
DEVNET USE
Software Studio I
Introduction to DevNet, APIs, and Data Formats
What is Cisco DevNet?
Cisco DevNet is Cisco’s developer program designed to help network engineers, software
developers, and IT professionals learn how to automate and program networks. It provides
tools, resources, and sandboxes (virtual environments) to practice coding, working with APIs,
and building network automation solutions.
1. Scalability – Networks are growing rapidly, and manually configuring devices is slow
and prone to errors. Automation allows companies to manage thousands of devices
efficiently.
5. Integration with Cloud and DevOps – Modern networks must integrate with cloud
environments, requiring programmable APIs for seamless communication.
Companies like Google, Amazon, and Microsoft use network automation to manage
thousands of servers across the globe.
2. Self-Healing Networks
Modern networks can automatically detect failures and reroute traffic without
human intervention.
Example: If a cable breaks in an ISP’s network, automation tools can redirect traffic
through an alternate path.
Cloud services use automation to deploy virtual networks, set up firewalls, and
manage security policies on demand.
Large organizations use Cisco Meraki to manage thousands of Wi-Fi access points
from a single dashboard.
Example: Schools and hospitals can automatically optimize their Wi-Fi networks for
better coverage and security.
What is an API?
An API (Application Programming Interface) is a set of rules and protocols that allows
different software applications to communicate with each other. Think of it as a "messenger"
that takes requests from one system, delivers them to another, and then brings the response
back to the original system.
1. An API is a set of functions and procedures that allow the creation of applications that
access the features or data of an operating system, application, or other service
(Chandra, 2019).
2. APIs are interfaces that enable software components to interact with each other,
defining the methods and data formats that applications can use to request and
exchange information (Fielding, 2000).
3. APIs act as a bridge between different software systems, allowing them to share data
and functionality without requiring developers to understand the internal workings of
the systems they are interacting with (Richardson & Ruby, 2007).
Imagine you’re using a weather app on your phone. The app doesn’t generate weather data
itself—it uses an API to request data from a weather service. The API sends the request to the
weather service’s server, retrieves the data (e.g., temperature, humidity), and delivers it back
to the app in a format the app can display.
In a networking context, APIs are used to manage and monitor network devices. For
example:
A network administrator might use an API to retrieve a list of all devices connected to
a Cisco DNA Center.
APIs are the backbone of network automation and programmability. Here’s why they’re so
important in the context of DevNet:
What is a CLI?
A CLI (Command-Line Interface) is a text-based interface used to interact with software or
devices. You type commands, and the system executes them. For example, in a Cisco router,
you might use the CLI to configure interfaces, check device status, or troubleshoot issues.
GET https://<DNA-Center-IP>/api/v1/network-device
{
"devices": [
{
"id": "1",
"name": "Router1",
"ip": "192.168.1.1"
},
{
"id": "2",
"name": "Switch1",
"ip": "192.168.1.2"
}
]
}
Scalability Manual, not ideal for large- Ideal for managing large-scale
scale networks networks
2. Scalability: APIs are designed to handle large-scale operations, making them ideal
for enterprise networks.
3. Integration: APIs enable different systems (e.g., Cisco DNA Center, Meraki, ACI) to
communicate and share data seamlessly.
Data Formats
Data formats are standardized ways of organizing and representing data. They ensure that
data can be easily understood, transmitted, and processed by different systems. In APIs, data
formats like JSON, XML, and YAML are used to structure the data being sent and received.
JSON is a lightweight, text-based format that is easy for humans to read and write, and easy
for machines to parse and generate. It is widely used in APIs because of its simplicity and
compatibility with web technologies.
JSON is a text-based data interchange format that is easy for humans to read and write and
easy for machines to parse and generate (Crockford, 2006).
Example of JSON:
{
"device": {
"id": "1",
"name": "Router1",
"ip": "192.168.1.1",
"status": "active"
}
}
Key Features:
XML is a markup language that defines rules for encoding documents in a format that is both
human-readable and machine-readable. It is more verbose than JSON but is highly flexible
and widely used in legacy systems.
XML is a markup language that defines a set of rules for encoding documents in a format that
is both human-readable and machine-readable (Bray et al., 2000).
Example of XML:
<device>
<id>1</id>
<name>Router1</name>
<ip>192.168.1.1</ip>
<status>active</status>
</device>
Key Features:
YAML is a human-readable data serialization format that is often used for configuration files
and data exchange. It is less verbose than XML and easier to read than JSON for complex
data structures.
YAML is a human-readable data serialization format that is often used for configuration files
and data exchange (Ben-Kiki et al., 2009).
Example of YAML:
device:
id: 1
name: Router1
ip: 192.168.1.1
status: active
Key Features:
1. Interoperability: Data formats ensure that different systems can understand and
process the same data.
2. Readability: JSON and YAML are easy for humans to read, making debugging and
development faster.
3. Efficiency: JSON is lightweight, making it ideal for transmitting data over networks.
4. Flexibility: XML and YAML support complex data structures, making them suitable
for advanced use cases.
Python is one of the most widely used programming languages in the world of network
automation and DevOps. This is because it’s so popular for working with APIs and data
formats. Here are other reasons:
1. Ease of Use:
Python has a simple and readable syntax, making it easy for beginners to learn and use.
For example, parsing JSON in Python is straightforward:
3. Cross-Platform Compatibility:
Python runs on almost any operating system, making it a versatile choice for network
automation tasks. Whether you’re working on Windows, Linux, or macOS, Python scripts
will run seamlessly.
JSON is natively supported in Python through the json module. This module allows you to
convert JSON data into Python dictionaries and vice versa.
import json
# JSON data as a string
json_data = '''
{
"device": {
"id": "1",
"name": "Router1",
"ip": "192.168.1.1",
"status": "active"
}
}
'''
# Accessing data
print(data["device"]["name"]) # Output: Router1
Key Functions:
Python’s xml.etree.ElementTree module is commonly used to parse XML data. It allows you
to navigate and extract data from XML documents.
import xml.etree.ElementTree as ET
# Parse XML
root = ET.fromstring(xml_data)
# Accessing data
print(root.find("name").text) # Output: Router1
Key Functions:
For YAML, you’ll need to install the PyYAML library, which is not included in Python’s
standard library. This library allows you to parse YAML data into Python dictionaries.
import yaml
Key Functions:
1. Data Extraction: Parsing allows you to extract specific information from API
responses (e.g., device status, IP addresses).
2. Configuration Management: You can read and write configuration files in JSON,
XML, or YAML format.
3. Integration: Parsing enables you to integrate data from different systems into your
Python scripts.
Imagine you’re responsible for managing a network with hundreds of devices (routers,
switches, firewalls, etc.). Manually checking the status of each device would be time-
consuming and prone to errors. Instead, you can use Python and APIs to automate this task.
1. Goal:
Write a Python script that retrieves the status of all network devices (e.g., CPU usage,
memory usage, interface status) and generates a report.
2. Steps:
o Use the requests library to interact with the API of your network management
platform (e.g., Cisco DNA Center or Meraki).
o Parse the API response (usually in JSON format) to extract relevant data.
o Generate a report or trigger alerts if any device is experiencing issues.
import requests
Output:
2. Run the test (it should fail because the code isn’t written yet).
Advantages of TDD?:
Example:
Let’s say you’re writing a Python function to add two numbers. Using TDD, you’d
start by writing a test:
import unittest
class TestMathOperations(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5) # Test the add function
if __name__ == "__main__":
unittest.main()
Then, you’d write the add function to make the test pass:
1. Agile
Key Principles:
Advantages:
Disadvantages:
Example:
A network automation team uses Agile to develop a script for automating VLAN
configurations. They deliver a basic version in the first sprint and add features (e.g.,
error handling, logging) in subsequent sprints.
2. Lean
Lean focuses on delivering value to the customer while minimizing waste (e.g., unnecessary
code, and delays). It emphasizes continuous improvement and efficiency.
Key Principles:
o Eliminate Waste: Remove anything that doesn’t add value to the customer.
Advantages:
Disadvantages:
Example:
A network automation team uses Lean to streamline their configuration backup script.
They identify and remove unnecessary steps (e.g., redundant error checks) to make
the script faster and more efficient.
3. Waterfall
Waterfall is a linear and sequential approach to software development. Each phase (e.g.,
design, development, testing) is completed before moving to the next.
Key Principles:
Advantages:
o Simple to understand and manage.
Disadvantages:
Example:
A network automation team uses Waterfall to develop a script for deploying firewall
rules. They complete the design, development, and testing phases in sequence, with
no overlap.
Code Organization
Organizing your code into modules and packages makes it easier to maintain, reuse, and
collaborate on. Here’s how to structure your Python projects effectively.
1. Modules
Example:
# network_utils.py
def is_valid_ip(ip):
# Function to validate IP addresses
pass
def ping(host):
# Function to ping a host
Pass
2. Packages
my_project/
├── main.py
└── utils/
├── __init__.py
├── network_utils.py
└── file_utils.py
Example:
# main.py
from utils.network_utils import is_valid_ip, ping
print(is_valid_ip("192.168.1.1"))
3. Best Practices:
Write clear and concise documentation for each module and function.
Example:
A team uses Agile to develop a network monitoring tool. In the first sprint, they
deliver a basic script to collect device status. In subsequent sprints, they add features
like alerting, reporting, and integration with other tools.
Lean is ideal for optimizing network automation processes and eliminating waste. It works
well when:
Efficiency is Critical: Lean helps identify and remove unnecessary steps, such as
redundant error checks or inefficient code.
Customer Value: Lean ensures that the automation delivers real value to the
organization, such as reducing manual effort or improving network reliability.
Example:
A team uses Lean to streamline their configuration deployment script. They identify
bottlenecks (e.g., slow API calls) and optimize the script to reduce deployment time.
Waterfall is less common in network automation but can be useful in specific scenarios:
a. MVC (Model-View-Controller)
MVC separates an application into three components:
Model: Manages the data and business logic.
Controller: Acts as an intermediary between the Model and View, handling user input and
updating the Model.
Example:
In a network automation tool:
Model: A Python class that retrieves device configurations from a database or API.
Controller: A script that processes user commands and updates the Model.
b. Observer Pattern
The Observer pattern defines a one-to-many dependency between objects. When one object
changes state, all its dependents are notified and updated automatically.
Example:
In a network monitoring tool:
When the device’s status changes, all observers are notified and updated.
Key Concepts:
git init
git status
# Add files to the staging area
Example Workflow:
git add .
git commit -m "Added new script for VLAN automation"
3. Push the branch to the remote repository:
4. Merge the branch into the main branch (e.g., main or master):
A remote repository (e.g., on GitHub, GitLab, or Bitbucket) serves as the central hub for your
project. Team members can clone the repository, make changes, and push their updates.
Steps:
3. Add files, make changes, and push them to the remote repository:
git add .
Instead of working directly on the main branch, each team member should create a separate
branch for their work. This keeps the main branch stable and makes it easier to review
changes.
Steps:
git add .
git commit -m "Added new script for VLAN automation"
3. Push the branch to the remote repository:
Steps:
2. Discuss the changes with your team and address any feedback.
3. Merge the branch into main once the changes are approved:
4. Resolve Conflicts
Conflicts occur when two people modify the same part of a file. Git helps you resolve
conflicts by highlighting the differences and allowing you to choose which changes to keep.
Steps:
1. Pull the latest changes from main before starting your work:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> branch-name
3. Edit the file to resolve the conflict, then commit the changes:
3. Both Alice and Bob push their branches to the remote repository:
4. They open pull requests, review each other’s changes, and merge them into main.
Conflicts occur when two people modify the same part of a file, and Git can’t automatically
merge the changes. While conflicts are inevitable in team projects, following these best
practices can help minimize them:
Break your work into small, manageable tasks (e.g., one script or feature per branch).
This reduces the likelihood of overlapping changes and makes conflicts easier to
resolve.
Always create a new branch for each feature, bug fix, or task.
Before starting work, pull the latest changes from the main branch:
Regularly pull changes from main into your feature branch to stay up-to-date:
Let your team know what you’re working on to avoid overlapping changes.
Use tools like GitHub Issues or project boards to track tasks and progress.
Organize your code into modules and packages (as we discussed earlier).
This reduces the chances of multiple people editing the same file.
Use Git’s conflict markers to identify the conflicting changes and decide which ones
to keep.
Always open a pull request (PR) before merging your changes into main.
Key Concepts:
HTTP Methods:
python
import requests
HTTP Fundamentals
HTTP (Hypertext Transfer Protocol) is the foundation of data communication on the web.
Understanding HTTP is essential for working with REST APIs.
Key Concepts:
o Request: A message sent by the client (e.g., a browser or script) to the server.
Status Code: Indicates the result of the request (e.g., 200 for success,
404 for not found).
import requests
response = requests.get("https://api.example.com/devices")
print(f"Status Code: {response.status_code}")
print(f"Headers: {response.headers}")
print(f"Body: {response.json()}")
The pizza shop sends an automatic notification (webhook) to the delivery service.
This way, the delivery system instantly knows when to start preparing your order without
refreshing the website!
Think of APIs like checking your email manually, while webhooks are like getting email
notifications.
API Documentation
API documentation provides details about how to use an API, including:
Endpoints: Available URLs and methods.
Parameters: Required and optional parameters.
Authentication: How to authenticate requests.
Examples: Sample requests and responses.
a. Example API Documentation:
Endpoint: GET /devices
Description: Retrieve a list of devices.
Parameters: None
Response:
json[
{"id": 1, "name": "Router1", "status": "up"},
{"id": 2, "name": "Switch1", "status": "down"}
]