实验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
启动OpenDaylight
./distribution-karaf-0.6.4-Carbon/bin/karafz
在浏览器访问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 5 def http_delete(url): 6 url= url 7 headers = {'Content-Type':'application/json'} 8 resp = requests.delete(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin')) 9 return resp 10 11 if __name__ == "__main__": 12 url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/' 13 resp = http_delete(url) 14 print(resp.content)
(3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。
timeOut.py
1 # timeout.py 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
1 # 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上活动的流表数。
getflow.py
1 # getflow.py 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实验拓扑上相同的硬超时流表下发。
(2) 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。
提示:拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务
VLAN_ID | Hosts |
---|---|
0 | h1 h3 |
1 | h2 h4 |
(二)进阶要求
OpenDaylight或Ryu任选其一,编程实现查看前序VLAN实验拓扑中所有节点(含交换机、主机)的名称,以及显示每台交换机的所有流表项。
(三)实验报告
- 请用Markdown排版;
- 将所有本实验相关文件保存在目录/home/用户名/学号/lab7/中;
- (一)只需要提交实现相应Python代码和执行结果截图,其余文字请勿赘述;
- (二)不做必须要求,有完成的同学请提交Python代码和运行结果,文件保存目录参照要求2。
- 个人总结,包括但不限于实验难度、实验过程遇到的困难及解决办法,个人感想,不少于200字。