实验7:基于REST API的SDN北向应用实践
一、实验目的
能够编写程序调用OpenDaylight REST API实现特定网络功能;
能够编写程序调用Ryu REST API实现特定网络功能。
二、实验环境
下载虚拟机软件Oracle VisualBox或VMware;
在虚拟机中安装Ubuntu 20.04 Desktop amd64,并完整安装Mininet、OpenDaylight(Carbon版本)、Postman和Ryu;
三、实验要求
(一)基本要求
编写Python程序,调用OpenDaylight的北向接口实现以下功能
(1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;
生成拓扑并连接控制器:sudo mn --topo=single,3 --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
运行 karaf 启动 ODL: ./distribution-karaf-0.6.4-Carbon/bin/karaf
浏览器访问:http://127.0.0.1:8181/index.html 访问查看。
(2) 下发指令删除s1上的流表数据。
delete.py代码
复制代码
1 #!/usr/bin/python
2 import requests
3 from requests.auth import HTTPBasicAuth
4 if __name__ == "__main__":
5 url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/'
6 headers = {'Content-Type': 'application/json'}
7 res = requests.delete(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
8 print (res.content)
1 #!/usr/bin/python
2 import requests
3 from requests.auth import HTTPBasicAuth
4 if __name__ == "__main__":
5 url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1'
6 with open("./timeout.json") as file:
7 str = file.read()
8 headers = {'Content-Type': 'application/json'}
9 res = requests.put(url, str, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
10 print (res.content)
# timeout.json
2 {
3 "flow": [
4 {
5 "id": "1",
6 "match": {
7 "in-port": "1",
8 "ethernet-match": {
9 "ethernet-type": {
10 "type": "0x0800"
11 }
12 },
13 "ipv4-destination": "10.0.0.3/32"
14 },
15 "instructions": {
16 "instruction": [
17 {
18 "order": "0",
19 "apply-actions": {
20 "action": [
21 {
22 "order": "0",
23 "drop-action": {}
24 }
25 ]
26 }
27 }
28 ]
29 },
30 "flow-name": "flow",
31 "priority": "65535",
32 "hard-timeout": "20",
33 "cookie": "2",
34 "table_id": "0"
35 }
36 ]
37 }
(4) 获取s1上活动的流表数。
get.py代码
1 #!/usr/bin/python
2 import requests
3 from requests.auth import HTTPBasicAuth
4 if __name__ == "__main__":
5 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'
6 headers = {'Content-Type': 'application/json'}
7 res = requests.get(url,headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
8 print (res.content)
编写Python程序,调用Ryu的北向接口实现以下功能
(1) 实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。
①生成拓扑并连接控制器: sudo mn --topo=single,3 --controller=remote,ip=127.0.0.1,port=6633 --
switch ovsk,protocols=OpenFlow13
②运行ryu:ryu-manager ryu/ryu/app/ofctl_rest.py ryu/ryu/app/simple_switch_13.py
1 #!/usr/bin/python
2 import requests
3 if __name__ == "__main__":
4 url = 'http://127.0.0.1:8080/stats/flowentry/add'
5 with open("./ryu_timeout.json") as file:
6 str = file.read()
7 headers = {'Content-Type': 'application/json'}
8 res = requests.post(url, str, headers=headers)
9 print (res.content)
# ryu_timeout.json
2 {
3 "dpid": 1,
4 "cookie": 1,
5 "cookie_mask": 1,
6 "table_id": 0,
7 "hard_timeout": 20,
8 "priority": 65535,
9 "flags": 1,
10 "match":{
11 "in_port":1
12 },
13 "actions":[]
14 }
创建拓扑图:sudo mn --custom mytopo.py --topo mytopo --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
(三)个人总结
这次遇到了类似环境搭错了的问题,很难解决,最后选择重新搭建环境,比如在下发指令删除s1上的流表数据的时候,一直出现错误,最后更改python3才行,还有后面的步骤因为没有清理流表陷入无误区