(一)基本要求
-
编写Python程序,调用OpenDaylight的北向接口实现以下功能
(1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;(2) 下发指令删除s1上的流表数据。
-
#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuthdef http_delete(url):
url= url
headers = {'Content-Type':'application/json'}
resp = requests.delete(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
return respif __name__ == "__main__":
url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/'
resp = http_delete(url)
print(resp.content) -
(3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。
-
# timeout.py
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上活动的流表数。
# getflow.py
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/flow-node-inventory:table/0/opendaylight-flow-table-statistics:flow-table-statistics'
headers = {'Content-Type': 'application/json'}
res = requests.get(url,headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content) -
编写Python程序,调用Ryu的北向接口实现以下功能
(1) 实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。(2) 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。
二)进阶要求
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()