实验7:基于REST API的SDN北向应用实践
实验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)
(3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。
timeout.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/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)
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上活动的流表数。
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
ryu_timeout.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)
1 # 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 }
(2) 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。
提示:拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务
VLAN_ID | Hosts |
---|---|
0 | h1 h3 |
1 | h2 h4 |
建立拓扑图:mytopo.py代码
1 from mininet.topo import Topo 2 3 class MyTopo(Topo): 4 def __init__(self): 5 # initilaize topology 6 Topo.__init__(self) 7 8 self.addSwitch("s1") 9 self.addSwitch("s2") 10 11 self.addHost("h1") 12 self.addHost("h2") 13 self.addHost("h3") 14 self.addHost("h4") 15 16 self.addLink("s1", "h1") 17 self.addLink("s1", "h2") 18 self.addLink("s2", "h3") 19 self.addLink("s2", "h4") 20 self.addLink("s1", "s2") 21 22 topos = {'mytopo': (lambda: MyTopo())}
vlan.py代码
1 import json 2 import requests 3 4 if __name__ == "__main__": 5 url = 'http://127.0.0.1:8080/stats/flowentry/add' 6 headers = {'Content-Type': 'application/json'} 7 flow1 = { 8 "dpid": 1, 9 "priority": 1, 10 "match":{ 11 "in_port": 1 12 }, 13 "actions":[ 14 { 15 "type": "PUSH_VLAN", 16 "ethertype": 33024 17 }, 18 { 19 "type": "SET_FIELD", 20 "field": "vlan_vid", 21 "value": 4096 22 }, 23 { 24 "type": "OUTPUT", 25 "port": 3 26 } 27 ] 28 } 29 flow2 = { 30 "dpid": 1, 31 "priority": 1, 32 "match":{ 33 "in_port": 2 34 }, 35 "actions":[ 36 { 37 "type": "PUSH_VLAN", 38 "ethertype": 33024 39 }, 40 { 41 "type": "SET_FIELD", 42 "field": "vlan_vid", 43 "value": 4097 44 }, 45 { 46 "type": "OUTPUT", 47 "port": 3 48 } 49 ] 50 } 51 flow3 = { 52 "dpid": 1, 53 "priority": 1, 54 "match":{ 55 "vlan_vid": 0 56 }, 57 "actions":[ 58 { 59 "type": "POP_VLAN", 60 "ethertype": 33024 61 }, 62 { 63 "type": "OUTPUT", 64 "port": 1 65 } 66 ] 67 } 68 flow4 = { 69 "dpid": 1, 70 "priority": 1, 71 "match": { 72 "vlan_vid": 1 73 }, 74 "actions": [ 75 { 76 "type": "POP_VLAN", 77 "ethertype": 33024 78 }, 79 { 80 "type": "OUTPUT", 81 "port": 2 82 } 83 ] 84 } 85 flow5 = { 86 "dpid": 2, 87 "priority": 1, 88 "match": { 89 "in_port": 1 90 }, 91 "actions": [ 92 { 93 "type": "PUSH_VLAN", 94 "ethertype": 33024 95 }, 96 { 97 "type": "SET_FIELD", 98 "field": "vlan_vid", 99 "value": 4096 100 }, 101 { 102 "type": "OUTPUT", 103 "port": 3 104 } 105 ] 106 } 107 flow6 = { 108 "dpid": 2, 109 "priority": 1, 110 "match": { 111 "in_port": 2 112 }, 113 "actions": [ 114 { 115 "type": "PUSH_VLAN", 116 "ethertype": 33024 117 }, 118 { 119 "type": "SET_FIELD", 120 "field": "vlan_vid", 121 "value": 4097 122 }, 123 { 124 "type": "OUTPUT", 125 "port": 3 126 } 127 ] 128 } 129 flow7 = { 130 "dpid": 2, 131 "priority": 1, 132 "match": { 133 "vlan_vid": 0 134 }, 135 "actions": [ 136 { 137 "type": "POP_VLAN", 138 "ethertype": 33024 139 }, 140 { 141 "type": "OUTPUT", 142 "port": 1 143 } 144 ] 145 } 146 flow8 = { 147 "dpid": 2, 148 "priority": 1, 149 "match": { 150 "vlan_vid": 1 151 }, 152 "actions": [ 153 { 154 "type": "POP_VLAN", 155 "ethertype": 33024 156 }, 157 { 158 "type": "OUTPUT", 159 "port": 2 160 } 161 ] 162 } 163 res1 = requests.post(url, json.dumps(flow1), headers=headers) 164 res2 = requests.post(url, json.dumps(flow2), headers=headers) 165 res3 = requests.post(url, json.dumps(flow3), headers=headers) 166 res4 = requests.post(url, json.dumps(flow4), headers=headers) 167 res5 = requests.post(url, json.dumps(flow5), headers=headers) 168 res6 = requests.post(url, json.dumps(flow6), headers=headers) 169 res7 = requests.post(url, json.dumps(flow7), headers=headers) 170 res8 = requests.post(url, json.dumps(flow8), headers=headers)
创建拓扑图:sudo mn --custom mytopo.py --topo mytopo --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
(二)进阶要求
OpenDaylight或Ryu任选其一,编程实现查看前序VLAN实验拓扑中所有节点(含交换机、主机)的名称,以及显示每台交换机的所有流表项。
getall.py代码
1 import requests 2 import time 3 import re 4 5 class GetNodes: 6 def __init__(self, ip): 7 self.ip = ip 8 9 def get_switch_id(self): 10 url = 'http://' + self.ip + '/stats/switches' 11 re_switch_id = requests.get(url=url).json() 12 switch_id_hex = [] 13 for i in re_switch_id: 14 switch_id_hex.append(hex(i)) 15 16 return switch_id_hex 17 18 def getflow(self): 19 url = 'http://' + self.ip + '/stats/flow/%d' 20 switch_list = self.get_switch_id() 21 ret_flow = [] 22 for switch in switch_list: 23 new_url = format(url % int(switch, 16)) 24 re_switch_flow = requests.get(url=new_url).json() 25 ret_flow.append(re_switch_flow) 26 return ret_flow 27 28 def show(self): 29 flow_list = self.getflow() 30 for flow in flow_list: 31 for dpid in flow.keys(): 32 dp_id = dpid 33 switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id)) 34 print('s'+switchnum,end = " ") 35 switchnum = int(switchnum) 36 for list_table in flow.values(): 37 for table in list_table: 38 string1 = str(table) 39 if re.search("'dl_vlan': '(.*?)'", string1) is not None: 40 num = re.search("'dl_vlan': '(.*?)'", string1).group(1); 41 if num == '0' and switchnum == 1: 42 print('h1',end = " ") 43 if num == '1' and switchnum == 1: 44 print('h2',end = " ") 45 if num == '0' and switchnum == 2: 46 print('h3',end = " ") 47 if num == '1' and switchnum == 2: 48 print('h4',end = " ") 49 print("") 50 flow_list = self.getflow() 51 for flow in flow_list: 52 for dpid in flow.keys(): 53 dp_id = dpid 54 print('switch_name:s{1}'.format(hex(int(dp_id)), int(dp_id))) 55 for list_table in flow.values(): 56 for table in list_table: 57 print(table) 58 s1 = GetNodes("127.0.0.1:8080") 59 s1.show()
(三)个人总结
通过本次实验,我已经能够大概编写程序调用OpenDaylight REST API实现特定网络功能和Ryu REST API实现特定网络功能。对我来说,实验还是挺难的,实验过程中出现了很多问题,比如在下发指令删除s1上的流表数据的时候,一直出现错误,最后更改python运行方式才行,因为python和python3是不兼容的,所以一些可以在python上运行的代码不一定可以在python3上运行;第二个问题是ryu一直运行不出来,出现“OSError: [Errno 98] Address already in use"的错误,最后发现需要把OpenDaylight 控制器关掉,才可以运行。总的来说,实验有难度,需要有很好的基础,并且结合前几次的实验。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!