一.实验目的
1.能够编写程序调用OpenDaylight REST API实现特定网络功能
2.能够编写程序调用Ryu REST API实现特定网络功能。
二.实验环境
1.下载虚拟机软件Oracle VisualBox或VMware;
2.在虚拟机中安装Ubuntu 20.04 Desktop amd64,并完整安装Mininet、OpenDaylight(Carbon版本)、Postman和Ryu;
三.
(一)基本要求
编写Python程序,调用OpenDaylight的北向接口实现以下功能
(1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;
运行 karaf 启动 ODL(不能用超级权限) $ ./distribution-karaf-0.6.4-Carbon/bin/karaf //Carbon 版本 $ ./distribution-karaf-0.4.4-Beryllium-SR4/bin/karaf //Beryllium 版本
生成拓扑 sudo mn --topo=single,3 --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
浏览器访问:http://控制器 IP 地址:8181/index.html,本实验中 ODL 若安装在本地,则使 用 http://127.0.0.1:8181/index.html 访问。 用户名 admin,密码 admin
然后在生成拓扑的输入pingall
然后在ODL刷新
观察生成的拓扑
(2) 下发指令删除s1上的流表数据。
python代码
#!/usr/bin/python import requests from requests.auth import HTTPBasicAuth def delete(url): url = url headers = {'Content-Type':'application/json'} resp = requests.delete(url,headers=headers,auth=HTTPBasicAuth('admin', 'admin')) return resp if __name__ == "__main__": url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/' resp = delete(url) print (resp.content)
然后运行
(3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。
先在生成的拓扑下输入命令
h1 ping h3
python 代码为
1.(3) #!/usr/bin/python import requests from requests.auth import HTTPBasicAuth def out(url,jstr): url = url headers = {'Content-Type':'application/json'} resp = requests.put(url,jstr,headers=headers,auth=HTTPBasicAuth('admin', 'admin')) return resp 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 f: j = f.read() resp = out(url,j) print (resp.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" } ] }
然后运行后观察h1 ping h3 的情况
(4) 获取s1上活动的流表数。
python代码为
#!/usr/bin/python import requests from requests.auth import HTTPBasicAuth def getflowNum(url): url = url headers = {'Content-Type':'application/json'} resp = requests.get(url,headers=headers,auth=HTTPBasicAuth('admin', 'admin')) return resp 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' resp = getflowNum(url) print (resp.content)
得到以下情况
2.编写Python程序,调用Ryu的北向接口实现以下功能
(1)实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。
输入命令为
ryu-manager ryu/ryu/app/ofctl_rest.py ryu/ryu/app/simple_switch_13.py
打开ryu
运行拓扑为
生成拓扑 sudo mn --topo=single,3 --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
在拓扑中输入 h1 ping h3 命令
然后python代码为
import requests from requests.auth import HTTPBasicAuth if __name__ == '__main__': url = 'http://127.0.0.1:8080/stats/flowentry/add' headers = {'Content-Type': 'application/json'} json = open('ryutimeout.json').read() response = requests.post(url, data=json, headers=headers) print(response.content)
ryutimeout.json这个文件为
{ "dpid": 1, "cookie": 1, "cookie_mask": 1, "table_id": 0, "hard_timeout": 20, "priority": 65535, "flags": 1, "match":{ "in_port":1 }, "actions":[] }
观察 h1 ping h3 的情况
(2)参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。
VLAN_ID | Hosts |
---|---|
0 | h1 h3 |
1 | h2 h4 |
用Python代码建拓扑
ryu_topo.py为
rom mininet.topo import Topo class ryu_topo(Topo): def __init__(self): Topo.__init__(self) self.addSwitch("s1") self.addSwitch("s2") self.addHost("h1") self.addHost("h2") self.addHost("h3") self.addHost("h4") self.addLink("s1","h1") self.addLink("s1","h2") self.addLink("s2","h3") self.addLink("s2","h4") self.addLink("s1","s2") topos = {'ryu_topo': (lambda: ryu_topo())}
运行拓扑
sudo mn --custom ryu_topo.py --topo ryu_topo --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13
输入pingall
创建一个shell
ryu_shell.sh为
# To set VLAN ID to non-VLAN-tagged frame(s1从1、2口收到h1、h2发来的包并打上vlan_tag,从3口转发) curl -X POST -d '{ "dpid": 1, "priority": 1, "match":{ "in_port": 1 }, "actions":[ { "type": "PUSH_VLAN", # Push a new VLAN tag if a input frame is non-VLAN-tagged "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "SET_FIELD", "field": "vlan_vid", # Set VLAN ID "value": 4096 # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096) }, { "type": "OUTPUT", "port": 3 } ] }' http://localhost:8080/stats/flowentry/add curl -X POST -d '{ "dpid": 1, "priority": 1, "match":{ "in_port": 2 }, "actions":[ { "type": "PUSH_VLAN", # Push a new VLAN tag if a input frame is non-VLAN-tagged "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "SET_FIELD", "field": "vlan_vid", # Set VLAN ID "value": 4097 # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096) }, { "type": "OUTPUT", "port": 3 } ] }' http://localhost:8080/stats/flowentry/add #Pop the outer VLAN tag( 将s1发回至h1、h2的包从1、2号端口脱掉vlan_tag) curl -X POST -d '{ "dpid": 1, "priority": 1, "match":{ "vlan_vid": 0 }, "actions":[ { "type": "POP_VLAN", # Pop the outer VLAN tag "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "OUTPUT", "port": 1 } ] }' http://localhost:8080/stats/flowentry/add curl -X POST -d '{ "dpid": 1, "priority": 1, "match":{ "vlan_vid": 1 }, "actions":[ { "type": "POP_VLAN", # Pop the outer VLAN tag "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "OUTPUT", "port": 2 } ] }' http://localhost:8080/stats/flowentry/add # To set VLAN ID to non-VLAN-tagged frame(s2从1、2口收到h3、h4发来的包并打上vlan_tag,从3口转发) curl -X POST -d '{ "dpid": 2, "priority": 1, "match":{ "in_port": 1 }, "actions":[ { "type": "PUSH_VLAN", # Push a new VLAN tag if a input frame is non-VLAN-tagged "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "SET_FIELD", "field": "vlan_vid", # Set VLAN ID "value": 4096 # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096) }, { "type": "OUTPUT", "port": 3 } ] }' http://localhost:8080/stats/flowentry/add curl -X POST -d '{ "dpid": 2, "priority": 1, "match":{ "in_port": 2 }, "actions":[ { "type": "PUSH_VLAN", # Push a new VLAN tag if a input frame is non-VLAN-tagged "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "SET_FIELD", "field": "vlan_vid", # Set VLAN ID "value": 4097 # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096) }, { "type": "OUTPUT", "port": 3 } ] }' http://localhost:8080/stats/flowentry/add #Pop the outer VLAN tag( 将s2发回至h3、h4的包从1、2号端口脱掉vlan_tag) curl -X POST -d '{ "dpid": 2, "priority": 1, "match":{ "vlan_vid": 0 }, "actions":[ { "type": "POP_VLAN", # Pop the outer VLAN tag "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "OUTPUT", "port": 1 } ] }' http://localhost:8080/stats/flowentry/add curl -X POST -d '{ "dpid": 2, "priority": 1, "match":{ "vlan_vid": 1 }, "actions":[ { "type": "POP_VLAN", # Pop the outer VLAN tag "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame }, { "type": "OUTPUT", "port": 2 } ] }' http://localhost:8080/stats/flowentry/add
然后在sudo chmod +x ryu_shell.sh 赋予脚本执行
然后执行脚本
执行脚本之后
然后pingall
提示:拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务
(二)进阶要求
OpenDaylight或Ryu任选其一,编程实现查看前序VLAN实验拓扑中所有节点(含交换机、主机)的名称,以及显示每台交换机的所有流表项。
所以Python3代码为
def get_switch_id(self): url = 'http://' + self.ip + '/stats/switches' re_switch_id = requests.get(url=url).json() switch_id_hex = [] for i in re_switch_id: switch_id_hex.append(hex(i)) return switch_id_hex
所以python3代码为
def getflow(self): url = 'http://' + self.ip + '/stats/flow/%d' switch_list = self.get_switch_id() ret_flow = [] for switch in switch_list: new_url = format(url % int(switch, 16)) re_switch_flow = requests.get(url=new_url).json() ret_flow.append(re_switch_flow) return ret_flow
全部的pyhton3代码为
import requests
import time
import re
class GetNodes:
def __init__(self, ip):
self.ip = ip
def get_switch_id(self):
url = 'http://' + self.ip + '/stats/switches'
re_switch_id = requests.get(url=url).json()
switch_id_hex = []
for i in re_switch_id:
switch_id_hex.append(hex(i))
return switch_id_hex
def getflow(self):
url = 'http://' + self.ip + '/stats/flow/%d'
switch_list = self.get_switch_id()
ret_flow = []
for switch in switch_list:
new_url = format(url % int(switch, 16))
re_switch_flow = requests.get(url=new_url).json()
ret_flow.append(re_switch_flow)
return ret_flow
def show(self):
flow_list = self.getflow()
for flow in flow_list:
for dpid in flow.keys():
dp_id = dpid
switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id))
print('s'+switchnum,end = " ")
switchnum = int(switchnum)
for list_table in flow.values():
for table in list_table:
string1 = str(table)
if re.search("'dl_vlan': '(.*?)'", string1) is not None:
num = re.search("'dl_vlan': '(.*?)'", string1).group(1);
if num == '0' and switchnum == 1:
print('h1',end = " ")
if num == '1' and switchnum == 1:
print('h2',end = " ")
if num == '0' and switchnum == 2:
print('h3',end = " ")
if num == '1' and switchnum == 2:
print('h4',end = " ")
print("")
flow_list = self.getflow()
for flow in flow_list:
for dpid in flow.keys():
dp_id = dpid
print('switch_name:s{1}'.format(hex(int(dp_id)), int(dp_id)))
for list_table in flow.values():
for table in list_table:
print(table)
s1 = GetNodes("127.0.0.1:8080")
s1.show()
然后运行结果
个人总结:
这次主要就是看着文档,写url就行了。北向接口是实现控制器和开发者之间的交互。这次用的是python3来写的,python可能会报错。以前发送请求可以通过postman发送的,现在通过Python代码实现的,开发者和控制器之间通过编程来实现。基础就是前面的实现转变成python代码,前面odl和ryu实现,现在再做一遍,然后进阶实现的内容很多,一个一个慢慢去文档找,然后url输入,循环遍历,然后就能达到效果。ryu由python实现,然后支持of v1.0,v1.2 v1.3。ODL是有java实现的。两个控制器再一次进行操作,来对比他们之间的不同,对前面实验的复习。
标签:url,self,VLAN,flow,REST,switch,API,SDN,id From: https://www.cnblogs.com/battlefield/p/16846113.html