OpenDayLight控制器的北向API接口整理
建立topo
sudo mn --topo=single,3 --mac --controller=remote,ip=控制器ip,port=6633 --switch ovsk,protocols=OpenFlow13
获取当前topo信息
import requests as rq
from requests.auth import HTTPBasicAuth
if __name__ == '__main__':
url = 'http://控制器ip:8181/restconf/operational/opendaylight-inventory:nodes'
headers = {'Content-Type': 'application/json'}
response = rq.get(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print(response.content)```

删除s1流表数据
import requests as rq
from requests.auth import HTTPBasicAuth
if __name__ == '__main__':
url = 'http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/'
headers = {'Content-Type': 'application/json'}
response = rq.delete(url=url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print(response.content)
但是好像并没有执行成功,输出流表仍有数据
故查找原因
print(response)看看
返回状态405了
没有关于405的错误信息
搜索资料后,感觉是版本问题?有待商榷。。。
下发硬超时流,实现网络中断30s
import requests
from requests.auth import HTTPBasicAuth
def http_put(url,jstr):
headers = {'Content-Type':'application/json'}
resp = requests.put(url,jstr,headers=headers,auth=HTTPBasicAuth('admin', 'admin'))
return resp
if __name__ == "__main__":
url='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1'
with open('flowTable.json') as f:
jstr = f.read()
resp = http_put(url,jstr)
print resp
flowTable.json
"ethernet-match": {
"ethernet-type": {
"type": "0x0800"
}
},
"ipv4-destination": "10.0.0.3/32"
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"order": "0",
"drop-action": {}
}
]
}
}
]
},
"flow-name": "flow1",
"priority": "65535",
"hard-timeout": "30",
"cookie": "2",
"table_id": "0"
}
]
}
config和operational区别见:
https://www.sdnlab.com/community/question/181
h1 ping h3,然后去openday运行py文件,成功了
点开网址:
http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1
也可以看到流表确实下发了
获取s1上活动的流表数
import requests
from requests.auth import HTTPBasicAuth
def http_get(url):
headers = {'Content-Type':'application/json'}
resp = requests.put(url,headers=headers,auth=HTTPBasicAuth('admin', 'admin'))
return resp
if __name__ == "__main__":
url='http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/opendaylight-flow-table-statistics:flow-table-statistics'
resp = http_get(url)
print resp
但是结果返回405
关于405的问题,暂时不太知道为什么。