1. 程式人生 > 實用技巧 >實驗 7:OpenDaylight 實驗——Python 中的 REST API 呼叫

實驗 7:OpenDaylight 實驗——Python 中的 REST API 呼叫

實驗目的

對 Python 呼叫 OpenDaylight 的 REST API 方法有初步瞭解。

實驗任務

本實驗需要用另一種方法完成上一個實驗相同的功能,即通過 Python 程式呼叫OpenDaylight 的北向介面下發關於硬超時的流表,實現拓撲內主機在一定時內的網路通斷。實驗拓撲如下:

實驗步驟

1. 實驗環境

安裝了 Ubuntu 18.04.5 Desktop amd64 的虛擬機器

2. 實驗過程

(1)生成拓撲

sudo mn --topo=single,3 --controller=remote,ip=127.0.0.1,port=6633 --switch ovsk,protocols=OpenFlow13


生成拓撲的方法同之前的實驗一致。

(2)編寫 Python 程式碼和 JSON 格式的請求內容

vim odlnorth.py

#!/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('hardtimeout.json') as f: 
        jstr = f.read() 
    resp = http_put(url,jstr) 
    print(resp.content)

由於是下發流表,因此需要有 http 請求的具體內容,同之前的 Postman一樣,也是以 JSON 格式來表示。
vim hardtimeout.json
檔案中不包含 http 請求的 url 地址:

{ 
    "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": "10", 
            "cookie": "2", 
            "table_id": "0" 
        } 
    ] 
}

(3)執行 Python 程式碼,完成流表下發

python odlnorth.py

結果驗證方法同實驗 6。

提示:執行程式碼如果報錯:ImportError: No module named requests,可嘗試
如下方法。
sudo apt-get install pythonpip // 如果 pip 沒裝的話需要執行

sudo pip install requests

選做題

vim odlnorth.py 編輯程式碼

#!/usr/bin/python 
 
import requests 
from requests.auth import HTTPBasicAuth 
import json
 
def http_put(url,jstr): 
    url= url 
    headers = {'Content-Type':'application/json'} 
    resp = requests.get(url,jstr,headers=headers,auth=HTTPBasicAuth('admin', 'admin')) 
    return resp

def get_flow_table_conut(res):
    return len(res["flow-node-inventory:flow"])
     
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('hardtimeout.json') as f: 
        jstr = f.read() 
    resp = http_put(url,jstr) 
    print(resp.content)
    print(get_flow_table_conut(json.loads(resp.content)))

就是把上面那個程式碼的 put 改成 get,獲得流表項,然後解析返回的資料,獲取流表數目
執行截圖

實驗要求

  • 1.根據實驗步驟復現上述實驗,指導書僅提供參考方法,如果你對 Python 比較
    熟悉,且有其他方法,歡迎使用。

  • 2.利用Python程式獲取本實驗拓撲中的交換機s1中活動的流表數目active-flows。

  • 3.在部落格園發表一篇部落格,記錄主要步驟,如果完成了選做題,請在部落格標題上標註,並提交程式到 GitHub,展示執行結果

總結

本次實驗較為簡單,根據老師所提供的實驗講義即可完成。