基于REST API的SDN北向应用实践
Posted 爱打瞌睡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于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/karaf
(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)
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)
(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)
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)
(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)
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)
基本要求
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)
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": []
}
"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": []
}
(2)
利用Mininet平台搭建下图所示网络拓扑,要求支持OpenFlow 1.3协议,主机名、交换机名以及端口对应正确。拓扑
生成后需连接Ryu,且Ryu应能够提供REST API服务。
连接Ryu:ryu-manager ryu/ryu/app/gui_topology/gui_topology.py --observe-links
(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
"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
个人总结:这次的实验相对前面几次实验是比较难的、比较麻烦的,尝试了很多次,按照前面完成的同学的思路,一步一步来,其中,编写python和json文件时,要用到的是nano ***.py/json。做完实验还是不知道代码是什么意思,然后opendalight部分的中断h1 ping h3 20s时有点瑕疵,我只中断了10s,所以运行了2次python t1.py代码。然后ryu部分利用mininet平台搭建网络拓扑时,出现 File exists 问题,再来就是Shell脚本,这是完全没看懂,就借鉴了同学的代码。对于我来说,代码这块是个问题,所以以后要努努力,不会敲代码,但是至少也要读懂代码。
SDN实验---Ryu的应用开发北向接口RESTAPI
一:推文
软件定义网络基础---REST API概述
软件定义网络基础---REST API的设计规范
二:掌握Ryu基本RESTAPI使用方法
(一)Ryu的RESTAPI
(二) REST应用样例:app/ofctl_rest.py
1.所提供的样例API接口
# REST API # # Retrieve the switch stats # # get the list of all switches # GET /stats/switches # # get the desc stats of the switch # GET /stats/desc/<dpid> # # get flows desc stats of the switch # GET /stats/flowdesc/<dpid> # # get flows desc stats of the switch filtered by the fields # POST /stats/flowdesc/<dpid> # # get flows stats of the switch # GET /stats/flow/<dpid> # # get flows stats of the switch filtered by the fields # POST /stats/flow/<dpid> # # get aggregate flows stats of the switch # GET /stats/aggregateflow/<dpid> # # get aggregate flows stats of the switch filtered by the fields # POST /stats/aggregateflow/<dpid> # # get table stats of the switch # GET /stats/table/<dpid> # # get table features stats of the switch # GET /stats/tablefeatures/<dpid> # # get ports stats of the switch # GET /stats/port/<dpid>[/<port>] # Note: Specification of port number is optional # # get queues stats of the switch # GET /stats/queue/<dpid>[/<port>[/<queue_id>]] # Note: Specification of port number and queue id are optional # If you want to omitting the port number and setting the queue id, # please specify the keyword "ALL" to the port number # e.g. GET /stats/queue/1/ALL/1 # # get queues config stats of the switch # GET /stats/queueconfig/<dpid>[/<port>] # Note: Specification of port number is optional # # get queues desc stats of the switch # GET /stats/queuedesc/<dpid>[/<port>[/<queue_id>]] # Note: Specification of port number and queue id are optional # If you want to omitting the port number and setting the queue id, # please specify the keyword "ALL" to the port number # e.g. GET /stats/queuedesc/1/ALL/1 # # get meter features stats of the switch # GET /stats/meterfeatures/<dpid> # # get meter config stats of the switch # GET /stats/meterconfig/<dpid>[/<meter_id>] # Note: Specification of meter id is optional # # get meter desc stats of the switch # GET /stats/meterdesc/<dpid>[/<meter_id>] # Note: Specification of meter id is optional # # get meters stats of the switch # GET /stats/meter/<dpid>[/<meter_id>] # Note: Specification of meter id is optional # # get group features stats of the switch # GET /stats/groupfeatures/<dpid> # # get groups desc stats of the switch # GET /stats/groupdesc/<dpid>[/<group_id>] # Note: Specification of group id is optional (OpenFlow 1.5 or later) # # get groups stats of the switch # GET /stats/group/<dpid>[/<group_id>] # Note: Specification of group id is optional # # get ports description of the switch # GET /stats/portdesc/<dpid>[/<port_no>] # Note: Specification of port number is optional (OpenFlow 1.5 or later) # Update the switch stats # # add a flow entry # POST /stats/flowentry/add # # modify all matching flow entries # POST /stats/flowentry/modify # # modify flow entry strictly matching wildcards and priority # POST /stats/flowentry/modify_strict # # delete all matching flow entries # POST /stats/flowentry/delete # # delete flow entry strictly matching wildcards and priority # POST /stats/flowentry/delete_strict # # delete all flow entries of the switch # DELETE /stats/flowentry/clear/<dpid> # # add a meter entry # POST /stats/meterentry/add # # modify a meter entry # POST /stats/meterentry/modify # # delete a meter entry # POST /stats/meterentry/delete # # add a group entry # POST /stats/groupentry/add # # modify a group entry # POST /stats/groupentry/modify # # delete a group entry # POST /stats/groupentry/delete # # modify behavior of the physical port # POST /stats/portdesc/modify # # modify role of controller # POST /stats/role # # # send a experimeter message # POST /stats/experimenter/<dpid>
(三)Ryu官方文档:https://ryu.readthedocs.io/en/latest/app/ofctl_rest.html
三:实验开始
(一)开启Ryu控制器
ryu-manager ofctl_rest.py simple_switch_13.py --observe-links --verbose #除了使用RESTAPI之外,我们还要保证ryu控制器正常处理交换机逻辑
(二)开启Mininet
sudo mn --controller=remote --mac --topo=tree,2,2
(三)使用Curl发送URL请求
1.获取到所有交换机Get the list of all switches which connected to the controller.
$ curl -X GET http://localhost:8080/stats/switches
2.获取交换机的描述信息Get the desc stats of the switch which specified with Datapath ID in URI.
$ curl -X GET http://localhost:8080/stats/desc/1
3.其他类似
(四)Postman实验(更美观)
1.安装postman
2.获取交换机信息
以上是关于基于REST API的SDN北向应用实践的主要内容,如果未能解决你的问题,请参考以下文章
编写 Node.js Rest API 的 10 个最佳实践