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

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

时间:2022-10-23 20:24:23浏览次数:46  
标签:__ url flow REST headers API SDN type requests

基础要求

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

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

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

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) 下发硬超时流表,实现拓扑内主机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('timeout.json') as f:
        jstr = f.read()
    resp = http_put(url,jstr)
    print (resp.content)

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

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)

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

(1)实现上述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('timeout1.json') as f:
        jstr = f.read()
    resp = http_post(url,jstr)
    print (resp.content)

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

import json

import requests

if __name__ == "__main__":
    url = 'http://127.0.0.1:8080/stats/flowentry/add'
    headers = {'Content-Type': 'application/json'}
    flow1 = {
        "dpid": 1,
        "priority": 1,
        "match":{
            "in_port": 1
        },
        "actions":[
            {
                "type": "PUSH_VLAN",    
                "ethertype": 33024      
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid",    
                "value": 4096           
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow2 = {
        "dpid": 1,
        "priority": 1,
        "match":{
            "in_port": 2
        },
        "actions":[
            {
                "type": "PUSH_VLAN",     
                "ethertype": 33024      
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid",     
                "value": 4097           
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow3 = {
        "dpid": 1,
        "priority": 1,
        "match":{
            "vlan_vid": 0
        },
        "actions":[
            {
                "type": "POP_VLAN",    
                "ethertype": 33024     
            },
            {
                "type": "OUTPUT",
                "port": 1
            }
        ]
    }
    flow4 = {
        "dpid": 1,
        "priority": 1,
        "match": {
            "vlan_vid": 1
        },
        "actions": [
            {
                "type": "POP_VLAN", 
                "ethertype": 33024  
            },
            {
                "type": "OUTPUT",
                "port": 2
            }
        ]
    }
    flow5 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "in_port": 1
        },
        "actions": [
            {
                "type": "PUSH_VLAN", 
                "ethertype": 33024 
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid", 
                "value": 4096  
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow6 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "in_port": 2
        },
        "actions": [
            {
                "type": "PUSH_VLAN",  
                "ethertype": 33024  
            },
            {
                "type": "SET_FIELD",
                "field": "vlan_vid",  
                "value": 4097 
            },
            {
                "type": "OUTPUT",
                "port": 3
            }
        ]
    }
    flow7 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "vlan_vid": 0
        },
        "actions": [
            {
                "type": "POP_VLAN", 
                "ethertype": 33024  
            },
            {
                "type": "OUTPUT",
                "port": 1
            }
        ]
    }
    flow8 = {
        "dpid": 2,
        "priority": 1,
        "match": {
            "vlan_vid": 1
        },
        "actions": [
            {
                "type": "POP_VLAN", 
                "ethertype": 33024  
            },
            {
                "type": "OUTPUT",
                "port": 2
            }
        ]
    }
    res1 = requests.post(url, json.dumps(flow1), headers=headers)
    res2 = requests.post(url, json.dumps(flow2), headers=headers)
    res3 = requests.post(url, json.dumps(flow3), headers=headers)
    res4 = requests.post(url, json.dumps(flow4), headers=headers)
    res5 = requests.post(url, json.dumps(flow5), headers=headers)
    res6 = requests.post(url, json.dumps(flow6), headers=headers)
    res7 = requests.post(url, json.dumps(flow7), headers=headers)
    res8 = requests.post(url, json.dumps(flow8), headers=headers)

进阶要求

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

import requests
import time
import re


class GetInformation:
    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 get_flow_table(self):
        url = 'http://' + self.ip + '/stats/flow/%d'
        list_switch = self.get_switch_id()
        all_flow = []
        for switch in list_switch:
            new_url = format(url % int(switch, 16))
            re_switch_flow = requests.get(url=new_url).json()
            all_flow.append(re_switch_flow)

        return all_flow

    def show_flow(self):
        list_flow = self.get_flow_table()
        for flow in list_flow:
            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)
    def show_name(self):
    	
        list_flow = self.get_flow_table()
        for flow in list_flow:
            for dpid in flow.keys():
                dp_id = dpid
                switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id))        
                print('s'+switchnum)
                switchnum = int(switchnum)
            for list_table in flow.values():
                count = 0
                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')
                       if num == '1' and switchnum == 1:
                          print('h2')
                       if num == '0' and switchnum == 2:
                          print('h3')
                       if num == '1' and switchnum == 2:
                          print('h4')

S1 = GetInformation("127.0.0.1:8080");
S1.show_name();
S1.show_flow();

个人总结

首先,这次实验好难,其次,好难这次实验,最后,实验好难这次。
实验过程中遇到的问题主要有OpenFlow用了10但是要求13报错问题,不知道怎么用ryu查看流表和节点,还有cucl命令出错问题。
实验过程中用cucl删除流表时由于虚拟机未安装cucl,根据报错提示安装即可,还有查看节点的问题,我用了正则去匹配流表信息中的dl_vlan项,再根据所属节点判断是哪一个主机
因此查看的主机节点方法也只使用于这一个拓扑,局限性很大。由于对代码的不太了解,实验过程中也是一直报错一直百度,因此花了很多时间,总的来说这次实验对我个人而言还是很难的。

标签:__,url,flow,REST,headers,API,SDN,type,requests
From: https://www.cnblogs.com/jiege1575902/p/16819361.html

相关文章

  • Restful 风格的接口简介
    本文对Restful风格的接口做一个简单的陈述,纯属个人理解。 最直观的印象是,在Restful风格中,尽管请求的url一致,但是请求方式不一致会调用不同的接口。四种请求方......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • js原生转码API
    encodeURIComponent(编码)letencodeBase=encodeURIComponent();decodeURIComponent(解码)letdecodeBase=decodeURIComponent();......
  • .NET 6学习笔记(4)——如何在.NET 6的Desktop App中使用Windows Runtime API
    WindowsRuntimeAPI是当初某软为了区别Win32API,力挺UWP而创建的另一套Windows10专用的API集合。后来因为一些原因,UWP没火。为了不埋没很有价值的WindowsRuntimeAPI,某......
  • 使用 Windows Core Audio APIs 进行 Loopback Recording 并生成 WAV 文件
    参考文档COMCodingPracticesAudioFileFormatSpecificationsCoreAudioAPIsLoopbackRecording#include<iostream>#include<fstream>#include<vector>#......
  • EBS:导入弹性域关键字的值(FND_FLEX_LOADER_APIS.up_value_set_value)
     EBSR12.1导入弹性域关键字的值第一步:创建一个临时表 CUX.CUX_FND_FLEX_VALUE_TEMP,其表结构同FND_FLEX_LOADER_APIS.up_value_set_value()过程的参数一致。导......
  • GenericAPIView
    https://www.bilibili.com/video/BV1z5411D7BQ?p=14&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea views.pyfromsers.modelsimportBookfromrest_frameworkimpor......
  • go mux实现rest api
    :30·字数:675·阅读:10012一个使用github.com/gorilla/mux实现RESTAPIService的例子,主要内容包括:GET/POST方法,如何接收path参数,query参数,以及POSTbody参数如何设置返回......
  • 使用eolink优雅地进行API接口管理
    为什么使用eolink?我们都知道在一个项目团队中是由很多角色组成的,例如:业务>产品>设计>前端>后端>测试等。每个角色各司其职,一起合作完成项目的生命周期。而前端与后端的沟通......
  • golang postman api(environments)
    获取所有environmentspackagemainimport("fmt""net/http""io/ioutil")funcmain(){url:="https://api.getpostman.com/environments"method:="GET"clien......