RESTCONF: IOS-XE Interface and Static Route configuration
Today, we’re going to configure a static route and an interface IP address within an IOS-XE platform using RESTCONF.
If you’re not already familiar, RESTCONF (defined in RFC 8040) is an HTTP-based protocol that uses YANG modeling to configure network devices. RESTCONF is an alternative to using NETCONF.
NOTE: This blog post serves as a lab guide for working through a variety of tasks while testing your current knowledge. This blog post is not meant to serve as a theoretical introduction to the material. To get the most out of the material, you should already have a firm grasp on networking fundamentals and beginner level knowledge of Python, YANG, JSON and HTTP. In the solution, I will be using VS Code, Postman, Python requests and JSON libraries, and SecureCRT to connect to the command line of the CSR1000v.
During this lab, we will be using the DevNet Always-on IOS-XE on CSR Sandbox with the latest code (17.3.1 at the time of this post).
Problem 1: Configure a Static Route on a CSR1000v using RESTCONF
To begin, we’ll ease into the coding by using Postman to make the API calls. If you have a solid grasp on using Python to do this with a library such as the requests library, feel free to skip tasks 1 and 2.
Task 1:
Use Postman to pull down the native YANG model from the CSR1000v. Explore the YANG model and what options are available.
Task 2:
Update the URL of the get request to only pull down the IP route information. Investigate the options like setting a next hop IP address, using an interface, giving the route a name and setting an administrative distance. See how these options are modeled.
Task 3:
Switch to Python and create a new file (name=get_static_route.py). Configure a script to pull down the same information we explored in Postman. Hint: use Postman to generate the code and manipulate it inside Python. Ensure you are using the auth=("username","password") and verify=False parameters inside your request.
Task 4:
Copy this code to a new file (name=set_static_route.py).
Create a Python dictionary and input the necessary parameters to configure the following static route:
ip route 10.100.100.0 255.255.255.0 17.17.17.17 name restconf-test-route
Hint: configure the route and see how the router represents the parameters in YANG. Don’t copy/paste, but re-create to ensure you understand the specific structure.
Task 5:
Create the proper call, ensuring you are using the proper method, to the CSR1000v with the requests library inside Python and configure the static route. Print the response code and ensure you receive a 204.
Task 6:
Clean-up your code with pylint, put it into a function and ensure it will be called when the file is executed.
Task 7:
As this is a public lab environment, ensure you clean-up your configuration. Delete the static route.
Problem 2: Set An IP address For Interface GigabitEthernet3 on the CSR1000v.
In this activity, we’ll dive straight into Python without using Postman as we already have code created for requests to the CSR1000v.
TASK 1:
Create a new IOS-XE Python YANG exploration script (name=get_interface_gig3.py) to retrieve all interface configurations from the CSR1000v. Execute the script and explore the output.
Now update your script to pull the GigabitEthernet3 primary IP address configuration specifically. Does this fail? Why? What can the response code tell you?
Task 2:
Copy this code to a new file (name=set_gig3_ip.py).
Create a Python dictionary and input the necessary parameters to configure GigabitEthernet3 in the following manner:
interface GigabitEthernet3
ip address 10.20.20.254 255.255.255.0
Hint: configure the interface and see how the router represents the parameters in YANG. Don’t copy/paste, but re-create to ensure you understand the specific structure.
Task 3:
Create the proper call, ensuring you are using the proper method, to the CSR1000v with the requests library inside Python and configure the interface with the IP address and subnet mask.
Once you have successfully configured the device with an IP address and subnet mask, try to re-run the script with a different method. Which one is successful?
Task 4:
Build in error handling into your Python script to handle the situation discovered in both Task 1 and Task 3. Use simple print statements to output which method was ran successfully.
Hint: you should have a total of three methods. You can also use try/except and if/else statements.
Task 5:
Run manual unit tests to ensure you’ve covered all cases (witnessed all three print statements from Task 4).
Task 6:
Create an interactive method for user input so that any user could run the script, type in an IP address and subnet mask. Re-run your manual unit testing to ensure that all methods still work.
Task 7:
Clean-up your code with pylint, put it into a function and ensure it will be called when the file is executed.
Task 8:
Remove your configuration. Create a separate Python script to delete the IP address from GigabitEthernet3 (name=delete_gig3_ip.py). Ensure you receive a 204 status code. Verify the IP address has been deleted with the get_interface_gig3.py script you built in Task 1.
Solution:
Solution Python scripts can be found on my GitHub.
A video walkthrough of the solution can be found below: