首页 > 其他分享 >实验7:基于REST API的SDN北向应用实践..

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

时间:2022-11-03 08:23:11浏览次数:38  
标签:__ .. url flow REST switch API table id

(一)基本要求

  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配置。

     

    二)进阶要求

    OpenDaylight或Ryu任选其一,编程实现查看前序VLAN实验拓扑中所有节点(含交换机、主机)的名称,以及显示每台交换机的所有流表项。

    GetNode.py

    复制代码
     1 import requests
     2 import time
     3 import re
     4 
     5 
     6 class GetNodes:
     7     def __init__(self, ip):
     8         self.ip = ip
     9         
    10     def get_switch_id(self):
    11         url = 'http://' + self.ip + '/stats/switches'
    12         re_switch_id = requests.get(url=url).json()
    13         switch_id_hex = []
    14         for i in re_switch_id:
    15             switch_id_hex.append(hex(i))
    16 
    17         return switch_id_hex
    18 
    19     def getflow(self):
    20         url = 'http://' + self.ip + '/stats/flow/%d'
    21         switch_list = self.get_switch_id()
    22         ret_flow = []
    23         for switch in switch_list:
    24             new_url = format(url % int(switch, 16))
    25             re_switch_flow = requests.get(url=new_url).json()
    26             ret_flow.append(re_switch_flow)
    27         return ret_flow
    28 
    29     def show(self):
    30         flow_list = self.getflow()
    31         for flow in flow_list:
    32             for dpid in flow.keys():
    33                 dp_id = dpid
    34                 switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id))        
    35                 print('s'+switchnum,end = " ")
    36                 switchnum = int(switchnum)
    37             for list_table in flow.values():
    38                 for table in list_table:          
    39                     string1 = str(table)
    40                     if re.search("'dl_vlan': '(.*?)'", string1) is not None:
    41                        num = re.search("'dl_vlan': '(.*?)'", string1).group(1);
    42                        if num == '0' and switchnum == 1:
    43                           print('h1',end = " ")
    44                        if num == '1' and switchnum == 1:
    45                           print('h2',end = " ")
    46                        if num == '0' and switchnum == 2:
    47                           print('h3',end = " ")
    48                        if num == '1' and switchnum == 2:
    49                           print('h4',end = " ")
    50         print("")
    51         flow_list = self.getflow()
    52         for flow in flow_list:
    53             for dpid in flow.keys():
    54                 dp_id = dpid
    55                 print('switch_name:s{1}'.format(hex(int(dp_id)), int(dp_id)))
    56             for list_table in flow.values():
    57                 for table in list_table:
    58                     print(table)
    59 s1 = GetNodes("127.0.0.1:8080")
    60 s1.show()
    复制代码
(三)实验小结 在利用curl命令删除流表时,需要使用sudo apt install curl安装才可以使用,还有就是编写Python程序生成拓扑时,命令一直错误,后面发现是创建指令没有写完整的程序名。同时也复习了关于Ryu的运行方面的操作命令,以及关于.sh脚本的命令。ryu实验中创建的拓扑无法连接上控制器,需要使用命令ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest打开Ryu控制器。

标签:__,..,url,flow,REST,switch,API,table,id
From: https://www.cnblogs.com/vghf/p/16853180.html

相关文章

  • 实验7:基于REST API的SDN北向应用实践 实验
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境下载虚拟机软件OracleVisualBox或......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境下载虚拟机软件OracleVisualBox......
  • 实验7:基于REST API的SDN北向应用实践.
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;(2)下发指令删除s1上的流表数据。#!/......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;(2)下发指令删除s1上的流表数据。(3)下......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;(2)下发指令删除s1上的流表数据。创建odl......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境下载虚拟机软件OracleVisualBo......
  • Vue项目中定时器的问题...
    Vue项目中定时器的问题...比如说我们现在在a页面写一个定时,让他每秒钟处理操作,比如:每秒钟打印一个1,然后跳转到b页面,此时可以看到,定时器依然在执行。这样是非常消耗性能的......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境下载虚拟机软件OracleVisualBox或......