本篇博文主要介绍在已经完成搭建OpenDayLight的系统上使用Python3调用Rest Api来获取网络中的拓扑信息。本篇博文采用了Basic Auth来进行的认证。
要点
1.Java版本见:Java版本
2.本项目将在GitHub上持续更新,项目地址
3.采用了Basic Auth完成了用户认证功能
4.本项目采用了httplib2的库进行的Http的请求功能
5.Python版博文将持续更新
~~~~~~~~~~~~~~~~(分割线)
当前已经完成代码如下:目前只完成了拓扑结构的获取
import httplib2
class OdlUtil:
url = ''
def __init__(self, host, port):
self.url = 'http://' + host + ':' + str(port)
'''
得到网络中的拓扑信息
'''
def get_topology(self, container_name='default', username="admin", password="admin"):
http = httplib2.Http()
http.add_credentials(name=username, password=password)
headers = {'Accept': 'application/json'}
response, content = http.request(uri=self.url + '/controller/nb/v2/topology/' + str(container_name), headers=headers)
return content.decode()
调用方式如下:
odl = OdlUtil('127.0.0.1', '8080')
print(odl.get_topology(username="admin", password="admin"))
结果如下:
{“edgeProperties”:[{“properties”:{“timeStamp”:{“value”:1479215165778,”name”:”creation”},”bandwidth”:{“value”:10000000000},”name”:{“value”:”s2-eth3”},”state”:{“value”:1},”config”:{“value”:1}},”edge”:{“tailNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:01”,”type”:”OF”},”id”:”1”,”type”:”OF”},”headNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:02”,”type”:”OF”},”id”:”1”,”type”:”OF”}}},{“properties”:{“timeStamp”:{“value”:1479215165779,”name”:”creation”},”bandwidth”:{“value”:10000000000},”name”:{“value”:”s3-eth3”},”state”:{“value”:1},”config”:{“value”:1}},”edge”:{“tailNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:01”,”type”:”OF”},”id”:”2”,”type”:”OF”},”headNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:03”,”type”:”OF”},”id”:”1”,”type”:”OF”}}},{“properties”:{“timeStamp”:{“value”:1479215165873,”name”:”creation”},”bandwidth”:{“value”:10000000000},”name”:{“value”:”s1-eth1”},”state”:{“value”:1},”config”:{“value”:1}},”edge”:{“tailNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:02”,”type”:”OF”},”id”:”1”,”type”:”OF”},”headNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:01”,”type”:”OF”},”id”:”1”,”type”:”OF”}}},{“properties”:{“timeStamp”:{“value”:1479215165777,”name”:”creation”},”bandwidth”:{“value”:10000000000},”name”:{“value”:”s1-eth2”},”state”:{“value”:1},”config”:{“value”:1}},”edge”:{“tailNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:03”,”type”:”OF”},”id”:”1”,”type”:”OF”},”headNodeConnector”:{“node”:{“id”:”00:00:00:00:00:00:00:01”,”type”:”OF”},”id”:”2”,”type”:”OF”}}}]}
如果需要返回Xml格式,只需要将 headers = {‘Accept’: ‘application/json’}改成headers = {‘Accept’: ‘application/xml’}即可。