SDN:Python+RESTAPI+连接ODL控制器

环境:Ubuntu20

一、相关python脚本代码
拓扑代码:命名为topology2.py

from mininet.topo import Topo
from mininet.node import Host, OVSKernelSwitch

class MyTopo(Topo):
    "Simple topology example created by me."

    def __init__(self):
        "Create custom topo."

        # Initialize topology
        Topo.__init__(self)

        # Add hosts
        h1 = self.addHost('h1', ip='10.0.0.1', defaultRoute=None)
        h2 = self.addHost('h2', ip='10.0.0.2', defaultRoute=None)
        h3 = self.addHost('h3', ip='10.0.0.3', defaultRoute=None)
        h4 = self.addHost('h4', ip='10.0.0.4', defaultRoute=None)
        h5 = self.addHost('h5', ip='10.0.0.5', defaultRoute=None)
        h6 = self.addHost('h6', ip='10.0.0.6', defaultRoute=None)

        # Add switches
        s1 = self.addSwitch('s1', cls=OVSKernelSwitch)
        s2 = self.addSwitch('s2', cls=OVSKernelSwitch)
        s3 = self.addSwitch('s3', cls=OVSKernelSwitch)
        s4 = self.addSwitch('s4', cls=OVSKernelSwitch)
        s5 = self.addSwitch('s5', cls=OVSKernelSwitch)
        s6 = self.addSwitch('s6', cls=OVSKernelSwitch)

        # Add links
        self.addLink(h1, s1)
        self.addLink(h2, s2)
        self.addLink(h3, s3)
        self.addLink(h4, s4)
        self.addLink(h5, s5)
        self.addLink(h6, s6)
        self.addLink(s1, s2)
        self.addLink(s2, s3)
        self.addLink(s3, s6)
        self.addLink(s4, s6)
        self.addLink(s4, s3)
        self.addLink(s1, s4)
        self.addLink(s1, s5)
        self.addLink(s2, s5)
        self.addLink(s2, s6)
        self.addLink(s5, s3)

topos = {'mytopo': (lambda: MyTopo())}

拓扑图:

下发流表代码:命名为odlnorth.py

#!/usr/bin/python3
import requests
from requests.auth import HTTPBasicAuth
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

def http_put(url, jstr, session):
    headers = {'Content-Type': 'application/json'}
    resp = session.put(url, data=jstr, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    return resp

if __name__ == "__main__":
    url = 'http://192.168.116.130:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0'
    with open('/home/wangleyi/Desktop/SDN/ODL_firewall_TCP-ver/port.json') as f:
        jstr = f.read()

    retry_strategy = Retry(
        total=3,  # 最大重试次数
        status_forcelist=[429, 500, 502, 503, 504],  # 指定哪些状态码需要重试
        method_whitelist=["HEAD", "GET", "OPTIONS", "PUT"]  # 添加 PUT 方法到白名单
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    http = requests.Session()
    http.mount("http://", adapter)
    http.mount("https://", adapter)

    resp = http_put(url, jstr, http)
    print(resp.content)

流表(flow table)条目:命名为port.json

{"table": [{"id": "0", "flow": [{"match": {"tcp-destination-port": "36688", "ipv4-destination": "10.0.0.6/32", "ethernet-match": {"ethernet-type": {"type": "0x0800"}}, "ipv4-source": "10.0.0.1/32", "tcp-source-port": "56688", "ip-match": {"ip-protocol": "6"}}, "hard-timeout": "10000", "instructions": {"instruction": [{"apply-actions": {"action": [{"drop-action": {}, "order": "0"}]}, "order": "0"}]}, "table_id": "0", "id": "1", "priority": "10"}]}]}

删除流表代码:命名为odlnorth_delete.py

#!/usr/bin/python3
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://192.168.116.130:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/'
 resp = http_delete(url)
 print (resp.content)

二、实验部分

1. 使用sudo ./karaf 命令启动ODL控制器

2. 登录ODL web页面, URL为http://192.168.116.130:8080/index.html, 

4. Mininet连接ODL,使用下面的命令

sudo mn --custom topology2.py --topo mytopo 
--mac --controller=remote,ip=192.168.116.130,port=6633 
--switch ovsk,protocols=OpenFlow13 --switch ovsbr,stp=1

5. 进入Mininet命令行以后,修改OpenFlow交换机的版本为13

6. 执行pingall命令,此时主机之间全部可互相通信(有时可能会有几台主机之间不能通信,可以尝试重启Mininet命令行)

7. 查看交换机流表项

注意要用dpctl dump-flows -O OpenFlow13命令

8. 执行准备好的odlnorth.py脚本,进行流表的下发

sudo python3 odlnorth.py

9. 执行dpctl dump-flows -O OpenFlow13命令,可以看到新增的流表项
10. 运行odlnorth_delete.py脚本,删除之前的drop流表

sudo python3 odlnorth_delete.py

11. dpctl dump-flows -O OpenFlow13命令查看流表情况

发现之前的drop流表项消失了

参考:基于SDN的青少年健康管理系统_哔哩哔哩_bilibili

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值