實驗7:基於REST API的SDN北向應用實踐


實驗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版本)、Postman和Ryu;

三、實驗要求

(一)基本要求

1.OpenDaylight

(1) 利用Mininet平台搭建下圖所示網絡拓撲,並連接OpenDaylight;



(2) 編寫Python程序,調用OpenDaylight的北向接口下發指令刪除s1上的流表數據。

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
def http_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 = http_delete(url)
    print (resp.content)

(3) 編寫Python程序,調用OpenDaylight的北向接口下發硬超時流表,實現拓撲內主機h1和h3網絡中斷20s。

#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
def http_put(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('h_timeout.json') as f:
        jstr = f.read()
    resp = http_put(url,jstr)
    print (resp.content)
{
    "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": "flow1",
            "priority": "65535",
            "hard-timeout": "20",
            "cookie": "2",
            "table_id": "0"
        }
    ]
}

(4) 編寫Python程序,調用OpenDaylight的北向接口獲取s1上活動的流表數。

#!/usr/bin/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/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0'
    resp = http_get(url)
    res = json.loads(resp.text)
    # print(resp.text)
    print(len(res['flow-node-inventory:table'][0]['flow']))


注:delete.py之前已經執行過了,所以報錯。

2.Ryu

(1) 編寫Python程序,調用Ryu的北向接口,實現上述OpenDaylight實驗拓撲上相同的硬超時流表下發。

#!/usr/bin/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('ryu_h_timeout.json') as f:
        jstr = f.read()
    resp = http_post(url,jstr)
    print (resp.content)
{
    "dpid": 1,
    "cookie": 1,
    "cookie_mask": 1,
    "table_id": 0,
    "hard_timeout": 20,
    "priority": 65535,
    "flags": 1,
    "match":{
        "in_port":1
    },
    "actions":[
        {
            "type":"OUTPUT",
            "port": 2
        }
    ]
 }

四、個人總結

本次實驗倒是沒有出現軟件方面的問題,就是代碼還是不太會改,基本上復制同學的。第二個ryu實驗用的系統內置的simple_switch_13.py,

因為需要使用到ofctl_rest.py,所以要注意在ryu下的app文件夾打開終端,避免出現找不到目錄的情況。每段代碼中最后一段是json文件,因為
之前用postman直接寫可視化界面上,一開始我還不知道寫文件里。
這次實驗操作是使用 python 向指定 url 發送請求來調用 REST API,用代碼表示總的來說比之前可視化操作反而看起來清晰很多。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM