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

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

一、实验目的

  • 能够编写程序调用OpenDaylight REST API实现特定网络功能;
  • 能够编写程序调用Ryu REST API实现特定网络功能。

二、实验环境

  • 下载虚拟机软件Oracle VisualBox或VMware;
  • 在虚拟机中安装Ubuntu 20.04 Desktop amd64,并完整安装Mininet、OpenDaylight(Carbon版本)、Postman和Ryu;

三、实验要求

(一)基本要求:编写Python程序,调用OpenDaylight的北向接口实现以下功能

  1. 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight

     

     

  2. 下发指令删除s1上的流表数据

     

     

点击查看代码
  1. 下发硬超时流表,实现拓扑内主机h1和h3网络中断10s

     

     

点击查看代码点击查看代码4. 获取s1上活动的流表数

 

 

点击查看代码

编写Python程序,调用Ryu的北向接口实现以下功能

  1. 实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。

     

     


     

     


     

     

点击查看代码点击查看代码
  1. 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。提示:拓扑生成后需连接Ryu,且Ryu应能够提供REST API服务
    VLAN_ID Hosts
    0 h1 h3
    1 h2 h4

     

     

点击查看代码点击查看代码

(二)进阶要求

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

 

 

点击查看代码
# getall.py
import requests
import time
import re


class GetNodes:
    def __init__(self, ip):
        self.ip = ip
        
    def get_switch_id(self):
        url = 'http://' + self.ip + '/stats/switches'
        re_switch_id = requests.get(url=url).json()
        switch_id_hex = []
        for i in re_switch_id:
            switch_id_hex.append(hex(i))

        return switch_id_hex

    def getflow(self):
        url = 'http://' + self.ip + '/stats/flow/%d'
        switch_list = self.get_switch_id()
        ret_flow = []
        for switch in switch_list:
            new_url = format(url % int(switch, 16))
            re_switch_flow = requests.get(url=new_url).json()
            ret_flow.append(re_switch_flow)
        return ret_flow

    def show(self):
        flow_list = self.getflow()
        for flow in flow_list:
            for dpid in flow.keys():
                dp_id = dpid
                switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id))        
                print('s'+switchnum,end = " ")
                switchnum = int(switchnum)
            for list_table in flow.values():
                for table in list_table:          
                    string1 = str(table)
                    if re.search("'dl_vlan': '(.*?)'", string1) is not None:
                       num = re.search("'dl_vlan': '(.*?)'", string1).group(1);
                       if num == '0' and switchnum == 1:
                          print('h1',end = " ")
                       if num == '1' and switchnum == 1:
                          print('h2',end = " ")
                       if num == '0' and switchnum == 2:
                          print('h3',end = " ")
                       if num == '1' and switchnum == 2:
                          print('h4',end = " ")
        print("")
        flow_list = self.getflow()
        for flow in flow_list:
            for dpid in flow.keys():
                dp_id = dpid
                print('switch_name:s{1}'.format(hex(int(dp_id)), int(dp_id)))
            for list_table in flow.values():
                for table in list_table:
                    print(table)
s1 = GetNodes("127.0.0.1:8080")
s1.show()


(三)个人总结

    本次实验难度较难,花费了不少时间,进一步学习了用OpenDaylight和Ryu的rest API来实现特定的要求,也还进一步学习了python的request库。同时在实验中遇到问题时,我查看了相关文档,进行了相关的搜索,锻炼了信息检索和阅读文档的能力。

posted @ 2022-11-27 17:58  世界的浪流者  阅读(49)  评论(0编辑  收藏  举报