This document demonstrates the use of Python programming to provision a MPLS L3VPN on a service Provider Edge (PE) router using REST API. This example uses Cisco CSR1000v (IOS-XE) routers as PE routers.
Contributed by: Anuradha Perera
Edited by: Kumar Sridhar
REST API management access to CSR1000v routers (pleae refer to the References at the end of this document).
Python (Version 2.x or 3.x) and "Requests" Python library installed on the computer used for configuring the routers.
Some basic knowledge Python programming.
In this example focus is on configuring required MPLS L3VPN service parameters on PE-1 router, which are highlighted in pink color.
The Configuration task is divided in to a number of sub-tasks and each sub-task is implemented under a user defined function. In this way functions can be re-used when required.
All the functions use "Requests" library to access REST APIs on the router and data format is JSON. In HTTP requests “verify” parameter is set to “False” to ignore validating SSL certificate.
1. Retrieve token-id
Before proceeding with any configuration on a router you need to have a valid token-id obtained from the router. This function initiates HTTP request to authenticate and obtain a token-id so that it can invoke other APIs using this token. Response of this request includes a token-id.
def getToken (ip, port, username, password):
url = "https://" + ip + ":"+ port + "/api/v1/auth/token-services"
username = "cisco" # username to login.This should be configured with privilege level 15.
password = "cisco" # password associated with username
tokenID = <value returned> # Token ID obtained from router using getToken function
vrfName = "VRF-A" # name of the VRF
RD = "3:3" # Route Distinguisher for VRF
importRT = "34:34" # Import Route Target
exportRT = "34:34" # export Route Target
interfaceName = "GigabitEthernet3" # name of the customer edge (CE) facing interface
interfaceIP = "192.168.13.3" # IP address of CE facing interface
interfaceSubnet = "255.255.255.0" # subnet of CE facing interface
ASN = "34" # BGP AS number of PE router
neighbourIP = "192.168.13.1" # BGP peering IP of CE router
remoteAS = "11" # AS number of CE router
In all the above functions, dedicated APIs were called for each configuation setp. The example below demonstrates how to pass IOS-XE CLI, in general, in the body of REST API call. This can be used as a workaround to automate if particular API is not available. In the above functions 'content-type' is set to 'application/json', but in the below example, 'content-type' is set to 'text/plain' as it is parsing standard CLI input.
This exmaple defines interface description for interface GigabitEthernet3. Configuration can be customized by changing the "cliInput" parameter.
def passCLIInput (ip, port, tokenID):
url = "https://" + ip + ":"+ port + "/api/v1/global/running-config"