Skip to content

Instantly share code, notes, and snippets.

@fredhsu
Last active May 7, 2018 13:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fredhsu/5462867 to your computer and use it in GitHub Desktop.
Save fredhsu/5462867 to your computer and use it in GitHub Desktop.
OpenDayLight REST API call using Python getting flow statistics
import httplib2
import json
h = httplib2.Http(".cache")
h.add_credentials('admin', 'admin')
#resp, content = h.request('http://10.55.17.20:8080/controller/nb/v2/statistics/default/flowstats', "GET")
# Updated 8 SEP 2013 to reflect new REST API
resp, content = h.request('http://10.55.17.20:8080/controller/nb/v2/statistics/default/flow', "GET")
allFlowStats = json.loads(content)
flowStats = allFlowStats['flowStatistics']
# These JSON dumps were handy when trying to parse the responses
#print json.dumps(flowStats[0]['flowStat'][1], indent = 2)
#print json.dumps(flowStats[4], indent = 2)
for fs in flowStats:
print "\nSwitch ID : " + fs['node']['id']
print '{0:8} {1:8} {2:5} {3:15}'.format('Count', 'Action', 'Port', 'DestIP')
for aFlow in fs['flowStat']:
count = aFlow['packetCount']
actions = aFlow['flow']['actions']
actionType = ''
actionPort = ''
#print actions
if(type(actions) == type(list())):
actionType = actions[1]['type']
actionPort = actions[1]['port']['id']
else:
actionType = actions['type']
actionPort = actions['port']['id']
dst = aFlow['flow']['match']['matchField'][0]['value']
print '{0:8} {1:8} {2:5} {3:15}'.format(count, actionType, actionPort, dst)
@rewtelize
Copy link

Is this a reactive flow? Where do you leave this file? How is it called by the main programm?

Thank you in advantage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment