Python REST Library for Opendaylight

In this blog, I will cover Python REST library that I wrote to simplify writing Python ODL applications.

In an earlier blog, I had indicated that the primary programming language choice for ODL applications is either Java or Python.

Following is my experience in Python programming with Opendaylight.

  • Python programming model for ODL is using REST api to talk to Controller, so control is limited to what REST api provides. There are few things that are not possible(or atleast I have not figured out) like sending/receiving packets from the Controller, getting asynchronous event based callback to Python.
  • REST api returns data either in xml or json format. Python has in-built json parser that makes it very easy to manipulate data coming back from the controller.
  • Python has a lot of standard libraries so there is a lot of code reuse.
  • There is plenty of documentation and tutorials in the web for Python, so learning Python is straightforward. For folks familiar with C, Perl or any scripting language, learning Python should be straightforward. Python supports object-oriented programming though I have not used it.

Following example shows how straightforward it is to use GET and PUT requests using Python.

h = httplib2.Http(".cache")
h.add_credentials('admin', 'admin')
resp, content = h.request('', "GET")
allFlowStats = json.loads(content)
flowStats = allFlowStats['flowStatistics']

Using http2 library in Python, we can request for a particular url and then parse the json data. We have the flowStats in dictionary format after this.

url = 'http://localhost:8080/controller/nb/v2/flow/default/node/OF/' + nodeid + '/staticFlow/flowtest'
flowtest = {"installInHw":"true","name":"flowtest","node":{"id":"00:00:00:00:00:00:00:03","type":"OF"},"IngressPort":"1","priority":"500","nwSrc":"","actions":"OUTPUT=1"}
resp, content = h.request(
      uri = url,
      method = 'PUT',
      headers={'Content-Type' : 'application/json'},

The above example shows how to add a flow using PUT request.

As I started writing some ODL applications using Python, I realized that it will be good to have a library for common functions that applications can reuse. I have imported the library in the application code to reuse the functions.

I have created a library for the common functionality in following modules.

  • Switch Manager
  • Topology Manager
  • Host tracker
  • Statistics
  • Flow programmer

Current functionality in the library includes:

  • Get all flows
  • Get flow stats
  • Get node stats
  • Get all topology edges
  • Get hosts
  • Get containers
  • Delete flow
  • Add flow

For complete code, please refer Python ODL REST library code. I have also added a test module to test the library code in the same file. I have included a logging library and logs can be enabled to debug issues. To turn on debugging,  run the program like “python <programname> debug”.


One thought on “Python REST Library for Opendaylight

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s