SDN-实验7:基于REST API的SDN北向应用实践

1.编写Python程序,调用OpenDaylight的北向接口实现以下功能
(1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;


(2) 下发指令删除s1上的流表数据。
编写delete.py,调用OpenDaylight的北向接口下发指令删除s1上的流表数据
`import requests
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 = requests.delete(url=url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print(response.content)(3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。 编写timeout.py及timeout.json,调用OpenDaylight的北向接口下发硬超时流表,实现拓扑内主机h1和h3网络中断20s timeout.py#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
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("./timeout.json") as file:
str = file.read()
headers = {'Content-Type': 'application/json'}
res = requests.put(url, str, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content)timeout.json{
"flow": [
{
"id": "1",
"match": {
"in-port": "1",
"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": "flow",
"priority": "65535",
"hard-timeout": "20",
"cookie": "2",
"table_id": "0"
}
]
}(4) 获取s1上活动的流表数。 编写get_flows.py,调用OpenDaylight的北向接口获取s1上活动的流表数 get_flows.pyimport requests
from requests.auth import HTTPBasicAuth

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'
headers = {'Content-Type': 'application/json'}
response = requests.get(url=url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print(response.content)2.编写Python程序,调用Ryu的北向接口实现以下功能 (1) 实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。 编写ryu_timeout.py及ryu_timeout.json,调用Ryu的北向接口实现硬超时流表下发 ryu_timeout.pyimport requests
from requests.auth import HTTPBasicAuth

if name == 'main':
url = 'http://127.0.0.1:8080/stats/flowentry/add'
headers = {'Content-Type': 'application/json'}
json = open('ryu_timeout.json').read()
response = requests.post(url, data=json, headers=headers)
print(response.content)ryu_timeout.json{
"dpid": 1,
"cookie": 1,
"cookie_mask": 1,
"table_id": 0,
"hard_timeout": 20,
"priority": 65535,
"flags": 1,
"match":{
"in_port":1
},
"actions":[]
}`
打开ryu
ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest
创建拓扑
sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
运行ryu_timeout.py
 实现硬超时功能

posted @ 2022-11-27 22:49  xyvuu  阅读(56)  评论(0编辑  收藏  举报