Fun with Python and ODL
Getting started on a project to capture the state of an Opendaylight controller node. I’ve learned the basics of programming and python a few different times now, but with out a project to do none of it ever sticks!
So… Opendaylight, let’s do some programming with Python!
To start what I’ll do is: 1. Import the requests library to make an HTTP call. 2. Define the API path we want to call. 3. Make a HTTP Get request using the default user-name and password. 4. Display the result.
import requests
#ODL query of Operational Inventory
operational_db = 'http://10.10.10.1:8181/restconf/operational/opendaylight-inventory:nodes/'
#Make a get call with default auth password admin:admin
r = requests.get(operational_db,auth=('admin','admin'))
#print the json output that is returned
print(r.text)
We then are returned:
{"nodes":{"node":[{"id":"openflow:8398923990168109056","node-connector":[{"id":"openflow:8398923990168109056:1","flow-node-inventory:supported":"one-gb-hd forty-gb-fd","flow-node-inventory:peer-features":"one-gb-hd forty-gb-fd","flow-node-inventory:advertised-features":"","flow-node-inventory:port-number":"1","flow-node-inventory:hardware-address":"74:8e:f8:de:a1:7a","flow-node-inventory:current-speed":1000000,"flow-node-inventory:current-feature":"one-gb-hd forty-gb-fd","flow-node-inventory:maximum-speed":1000000,"flow-node-inventory:name":"eth1/1/1","flow-node-inventory:state":{"link-down":true,"blocked":false,"live":false},"flow-node-inventory:configuration":"","opendaylight-port-statistics:flow-capable-node-connector-statistics":{"collision-count":0,"transmit-drops":0,"receive-frame-error":0,"transmit-errors":0,"bytes":{"received":0,"transmitted":0},"receive-crc-error":0,"duration":{"second":0,"nanosecond":0},"receive-errors":0,"receive-drops":0,"receive-over-run-error":0,"packets":{"received":0,"transmitted":0}}},{"id":"openflow:8398923990168109056:31","flow-node-inventory:supported":"one-gb-hd forty-gb-fd","flow-node-inventory:peer-features":"one-gb-hd forty-gb-fd","flow-node-inventory:advertised-features":"","flow-node-inventory:port-number":"31","flow-node-inventory:hardware-address":"74:8e:f8:de:a1:98","flow-node-inventory:current-speed":1000000,"flow-node-inventory:current-feature":"one-gb-hd forty-gb-fd","flow-node-inventory:maximum-speed":1000000,"flow-node-inventory:name":"eth1/1/31","flow-node-inventory:state":{"link-down":true,"blocked":false,"live":false},"flow-node-inventory:configuration":"","opendaylight-port-statistics:flow-capable-node-connector-statistics":{"collision-count":0,"transmit-drops":0,"receive-frame-error":0,"transmit-errors":0,"bytes":{"received":0,"transmitted":0},"receive-crc-error":0,"duration":{"second":0,"nanosecond":0},"receive-errors":0,"receive-drops":0,"receive-over-run-error":0,"packets":{"received":0,"transmitted":0}}},{"id":"openflow:8398923990168109056:32","flow-node-inventory:supported":"one-gb-hd forty-gb-fd","flow-node-inventory:peer-features":"one-gb-hd forty-gb-fd","flow-node-inventory:advertised-features":"","flow-node-inventory:port-number":"32","flow-node-inventory:hardware-address":"74:8e:f8:de:a1:99","flow-node-inventory:current-speed":1000000,"flow-node-inventory:current-feature":"one-gb-hd forty-gb-fd","flow-node-inventory:maximum-speed":1000000,"flow-node-inventory:name":"eth1/1/32","flow-node-inventory:state":{"link-down":true,"blocked":false,"live":false},"flow-node-inventory:configuration":"","opendaylight-port-statistics:flow-capable-node-connector-statistics":{"collision-count":0,"transmit-drops":0,"receive-frame-error":0,"transmit-errors":0,"bytes":{"received":0,"transmitted":0},"receive-crc-error":0,"duration":{"second":0,"nanosecond":0},"receive-errors":0,"receive-drops":0,"receive-over-run-error":0,"packets":{"received":0,"transmitted":0}}},{"id":"openflow:8398923990168109056:16","flow-node-inventory:supported":"one-gb-hd forty-gb-fd","flow-node-inventory:peer-features":"one-gb-hd forty-gb-fd","flow-node-inventory:advertised-features":"","flow-node-inventory:port-number":"16","flow-node-inventory:hardware-address":"74:8e:f8:de:a1:89","flow-node-inventory:current-speed":1000000,"flow-node-inventory:current-feature":"one-gb-hd forty-gb-fd","flow-node-inventory:maximum-speed":1000000,"flow-node-inventory:name":"eth1/1/16","flow-node-inventory:state":{"link-down":true,"blocked":false,"live":false},"flow-node-inventory:configuration":"","opendaylight-port-statistics:flow-capable-node-connector-statistics":{"collision-count":0,"transmit-drops":0,"receive-frame-error":0,"transmit-errors":0,"bytes":{"received":0,"transmitted":0},"receive-crc-error":0,"duration":{"second":0,"nanosecond":0},"receive-errors":0,"receive-drops":0,"receive-over-run-error":0,"packets":{"received":0,"transmitted":0}}},{"id":"openflow:8398923990168109056:2","flow-node-inventory:supported":"one-gb-hd forty-gb-fd","flow-node-inventory:peer-features":"one-gb-hd forty-gb-fd","flow-node-inventory:advertised-features":"","flow-node-inventory:port-number":"2","flow-node-inventory:hardware-address":"74:8e:f8:de:a1:7b","flow-node-inventory:current-speed":1000000,"flow-node-inventory:current-feature":"one-gb-hd forty-gb-fd","flow-node-inventory:maximum-speed":1000000,"flow-node-inventory:name":"eth1/1/2","flow-node-inventory:state":{"link-down":true,"blocked":false,"live":false},"flow-node-inventory:configuration":"","opendaylight-port-statistics:flow-capable-node-connector-statistics":{"collision-count":0,"transmit-drops":0,"receive-frame-error":0,"transmit-errors":0,"bytes":{"received":0,"transmitted":0},"receive-crc-error":0,"duration":{"second":0,"nanosecond":0},"receive-errors":0,"receive-drops":0,"receive-over-run-error":0,"packets":{"received":0,"transmitted":0}}}],"flow-node-inventory:snapshot-gathering-status-start":{"begin":"2016-11-11T08:21:01.567-08:00"},"flow-node-inventory:snapshot-gathering-status-end":{"end":"2016-11-11T08:21:02.068-08:00","succeeded":true},"flow-node-inventory:table":[{"id":0,"flow":[{"id":"L2switch-0","opendaylight-flow-statistics:flow-statistics":{"packet-count":0,"duration":{"nanosecond":711000000,"second":607124},"byte-count":0},"priority":100,"table_id":0,"hard-timeout":0,"match":{"ethernet-match":{"ethernet-type":{"type":35020}}},"cookie":3098476543630901248,"flags":"","instructions":{"instruction":[{"order":0,"apply-actions":{"action":[{"order":0,"output-action":{"max-length":65535,"output-node-connector":"CONTROLLER"}}]}}]},"idle-timeout":0}]}],"flow-node-inventory:hardware":"FastIron","flow-node-inventory:description":"None","flow-node-inventory:switch-features":{"max_tables":1,"max_buffers":0,"capabilities":["flow-node-inventory:flow-feature-capability-flow-stats","flow-node-inventory:flow-feature-capability-group-stats","flow-node-inventory:flow-feature-capability-queue-stats","flow-node-inventory:flow-feature-capability-port-stats"]},"flow-node-inventory:manufacturer":"Brocade Communications, Inc","flow-node-inventory:serial-number":"None","flow-node-inventory:software":"FI 8.0.30","flow-node-inventory:ip-address":"10.10.10.2","opendaylight-meter-statistics:meter-features":{"meter-capabilities-supported":["opendaylight-meter-types:meter-kbps","opendaylight-meter-types:meter-stats","opendaylight-meter-types:meter-burst"],"max_bands":2,"meter-band-supported":["opendaylight-meter-types:meter-band-drop","opendaylight-meter-types:meter-band-dscp-remark"],"max_meter":1024,"max_color":3},"opendaylight-group-statistics:group-features":{"group-capabilities-supported":["opendaylight-group-types:select-liveness"],"actions":[1],"group-types-supported":["opendaylight-group-types:group-ff","opendaylight-group-types:group-all","opendaylight-group-types:group-select","opendaylight-group-types:group-indirect"],"max-groups":[512,0]}}]}}
Next step, parse that json!