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

(一)基本要求

  1. 编写Python程序,调用OpenDaylight的北向接口实现以下功能
    (1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;

    (2) 下发指令删除s1上的流表数据。

  2. #!/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. (3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。

     

  4. # timeout.py
    import requests
    from requests.auth import HTTPBasicAuth
    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 file:
    str = file.read()
    headers = {'Content-Type': 'application/json'}
    res = requests.put(url, str, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    print (res.content)

  5. # 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"
    }
    ]
    }

  6. (4) 获取s1上活动的流表数。

    # getflow.py
    import requests
    from requests.auth import HTTPBasicAuth
    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'
    headers = {'Content-Type': 'application/json'}
    res = requests.get(url,headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    print (res.content)

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

    (2) 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。

     

     

    提示:拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务

posted @ 2022-11-03 08:04  林凡23  阅读(42)  评论(0)    收藏  举报