实验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;
(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实验拓扑上相同的硬超时流表下发。ryu_timeOut.py
# ryu_timeout.py import requests if __name__ == "__main__": url = 'http://127.0.0.1:8080/stats/flowentry/add' with open("./ryu_timeOut.json") as file: str = file.read() headers = {'Content-Type': 'application/json'} res = requests.post(url, str, headers=headers) print (res.content)
ryu_timeOut.json
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 ] 15 16 }
(2) 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。
提示:拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务
VLAN_ID | Hosts |
---|---|
0 | h1 h3 |
1 | h2 h4 |
topo.py
1 #!/usr/bin/env python 2 from mininet.topo import Topo 3 4 class MyTopo(Topo): 5 def __init__(self): 6 # initilaize topology 7 Topo.__init__(self) 8 9 self.addSwitch("s1") 10 self.addSwitch("s2") 11 12 self.addHost("h1") 13 self.addHost("h2") 14 self.addHost("h3") 15 self.addHost("h4") 16 17 self.addLink("s1", "h1") 18 self.addLink("s1", "h2") 19 self.addLink("s2", "h3") 20 self.addLink("s2", "h4") 21 self.addLink("s1", "s2") 22 23 topos = {'mytopo': (lambda: MyTopo())}
(二)进阶要求
OpenDaylight或Ryu任选其一,编程实现查看前序VLAN实验拓扑中所有节点(含交换机、主机)的名称,以及显示每台交换机的所有流表项。
GetNode.py
1 import requests 2 import time 3 import re 4 5 6 class GetNodes: 7 def __init__(self, ip): 8 self.ip = ip 9 10 def get_switch_id(self): 11 url = 'http://' + self.ip + '/stats/switches' 12 re_switch_id = requests.get(url=url).json() 13 switch_id_hex = [] 14 for i in re_switch_id: 15 switch_id_hex.append(hex(i)) 16 17 return switch_id_hex 18 19 def getflow(self): 20 url = 'http://' + self.ip + '/stats/flow/%d' 21 switch_list = self.get_switch_id() 22 ret_flow = [] 23 for switch in switch_list: 24 new_url = format(url % int(switch, 16)) 25 re_switch_flow = requests.get(url=new_url).json() 26 ret_flow.append(re_switch_flow) 27 return ret_flow 28 29 def show(self): 30 flow_list = self.getflow() 31 for flow in flow_list: 32 for dpid in flow.keys(): 33 dp_id = dpid 34 switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id)) 35 print('s'+switchnum,end = " ") 36 switchnum = int(switchnum) 37 for list_table in flow.values(): 38 for table in list_table: 39 string1 = str(table) 40 if re.search("'dl_vlan': '(.*?)'", string1) is not None: 41 num = re.search("'dl_vlan': '(.*?)'", string1).group(1); 42 if num == '0' and switchnum == 1: 43 print('h1',end = " ") 44 if num == '1' and switchnum == 1: 45 print('h2',end = " ") 46 if num == '0' and switchnum == 2: 47 print('h3',end = " ") 48 if num == '1' and switchnum == 2: 49 print('h4',end = " ") 50 print("") 51 flow_list = self.getflow() 52 for flow in flow_list: 53 for dpid in flow.keys(): 54 dp_id = dpid 55 print('switch_name:s{1}'.format(hex(int(dp_id)), int(dp_id))) 56 for list_table in flow.values(): 57 for table in list_table: 58 print(table) 59 s1 = GetNodes("127.0.0.1:8080") 60 s1.show()
(三)实验报告
这次的实验对我来说难度还是比较大的,主要是代码的编写,通过编写代码来实现网络的超时功能,对于我来说还是有很大的挑战。也是在查阅资料和请教同学的情况下才完成了这次实验。
标签:__,url,self,flow,REST,API,requests,SDN,id From: https://www.cnblogs.com/zwf6698/p/16852593.html