实验7:基于REST API的SDN北向应用实践

实验目的

1. 能够编写程序调用OpenDaylight REST API实现特定网络功能;

2. 能够编写程序调用Ryu REST API实现特定网络功能。

实验环境

1. 下载虚拟机软件Oracle VisualBox或VMware;

2. 在虚拟机中安装Ubuntu 20.04 Desktop amd64,并完整安装Mininet、OpenDaylight(Carbon或Berrylium版本)、Postman和Ryu;3

基本要求

1.OpenDaylight(1)

利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;

建立拓扑:sudo mn --topo=single,3 --mac --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow10连接open daylight:./211906129/opendaylight/distribution-karaf-0.6.4-Carbon/bin/raf

1.png

(2)编写Python程序,调用OpenDaylight的北向接口下发指令删除s1上的流表数据。

nano t1.py

def http_detele(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/'
with open('t1.json') as f:
jstr = f.read()
resp = http_detele(url)
print(resp.content)
2.png

(3)编写Python程序,调用OpenDaylight的北向接口下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。

Python:

nano t1.py

import requests

import json

from requests.auth import HTTPBasicAuth

def http_get(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'
with open('t1.json') as f:
jstr = f.read()
resp = http_get(url)
print(resp.content)
3.png

(4)编写Python程序,调用OpenDaylight的北向接口获取s1上活动的流表数。

python:

import requests

import json

from requests.auth import HTTPBasicAuth

def http_get(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'
with open('t1.json') as f:
jstr = f.read()
resp = http_get(url)
print(resp.content)

0.png

基本要求2.Ryu

(1)编写Python程序,调用Ryu的北向接口,实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。

python:

import requests

from requests.auth import HTTPBasicAuth

def http_post(url,jstr):

url= url
headers = {'Content-Type':'application/json'}
resp = requests.post(url,jstr,headers=headers)
return resp

if name == "main":

url='http://127.0.0.1:8080/stats/flowentry/add'
with open('r1.json') as f:
jstr = f.read()
resp = http_post(url,jstr)
print (resp.content)

json:

{

"dpid": 1,
"match": {
"in-port": "1",
"dl_dst": "00:00:00:00:00:03"

},

"priority": 65535,
"hard_timeout": 20,
"cookie": "2",
"table_id": 0,
"actions": []

}

4.png

5.png
6.png

(3)整理一个Shell脚本,参考Ryu REST API的文档,利用curl命令,实现和实验2相同的VLAN

curl -X POST -d '{
"dpid": 1,
"match": {
"in_port": 1
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type":"OUTPUT",
"port": 3
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add

curl -X POST -d '
{
"dpid": 1,
"priority":5,
"match": {
"in_port": 2
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type":"OUTPUT",
"port": 3
}
]
}' http://localhost:8080/stats/flowentry/add

curl -X POST -d '
{
"dpid": 1,
"match": {
"vlan_vid": 0
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 1
}
],
"priority":5
}
' http://localhost:8080/stats/flowentry/add

curl -X POST -d'
{
"dpid": 1,
"match": {
"vlan_vid": 1
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 2
}
],
"priority":5
}
' http://localhost:8080/stats/flowentry/add
curl -X POST -d '{
"dpid": 2,
"match": {
"in_port": 1
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type":"OUTPUT",
"port": 3
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add

curl -X POST -d '
{
"dpid": 2,
"match": {
"in_port": 2
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type":"OUTPUT",
"port": 3
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add

curl -X POST -d '
{
"dpid": 2,
"match": {
"vlan_vid": 0
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 1
}
],
"priority":5
}
' http://localhost:8080/stats/flowentry/add

curl -X POST -d'
{
"dpid": 2,
"match": {
"vlan_vid": 1
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type":"OUTPUT",
"port": 2
}
],
"priority":5
}' http://localhost:8080/stats/flowentry/add
7.png

个人总结

这次的实验相对前面几次实验是比较难的、比较麻烦的,尝试了很多次,按照前面完成的同学的思路,一步一步来,其中,编写python和json文件时,要用到的是nano ***.py/json。做完实验还是不知道代码是什么意思,然后opendalight部分的中断h1 ping h3 20s时有点瑕疵,我只中断了10s,所以运行了2次python t1.py代码。然后ryu部分利用mininet平台搭建网络拓扑时,出现 File exists 问题,再来就是Shell脚本,这是完全没看懂,就借鉴了同学的代码。对于我来说,代码这块是个问题,所以以后要努努力,不会敲代码,但是至少也要读懂代码。

posted @ 2021-10-25 16:47  JosephNanking  阅读(88)  评论(0)    收藏  举报