使用Python-Requests实现ODL对OVS的流表下发

时间:2022-05-06
本文章向大家介绍使用Python-Requests实现ODL对OVS的流表下发,主要内容包括目标、准备工作、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

目标

  • 通过ODL,在OVS上添加如下的一个group

# ovs-vsctl add-br br0# ovs-vsctl set bridge br0 protocols=OpenFlow13# ovs-ofctl dump-groups br0 -O openflow13OFPST_GROUP_DESC reply (OF1.3) (xid=0x2): group_id=1,type=all,bucket=weight:0,set_field:00:00:00:00:00:05->eth_dst,set_field:192.168.100.105->ip_dst,output:5,bucket=weight:0,set_field:00:00:00:00:00:06->eth_dst,set_field:192.168.100.106->ip_dst,output:6,bucket=weight:0,set_field:00:00:00:00:00:07->eth_dst,set_field:192.168.100.107->ip_dst,output:7

准备工作

  • 运行ODL的Beryllium-SR3版本
  • Python 2.7
  • Requests :requests==2.2.1
  • 通过YangUI填写group所需信息,做PUT操作,抓取PUT的报文
  • 获取对应的Json字段
将JSON字段存入文件,供后面调用




$ cat odl.json
{
 "group": [
 {
 "group-type": "group-all",
 "group-id": "1",
 "buckets": {
 "bucket": [
 {
 "bucket-id": "0",
 "action": [
 {
 "order": "0",
 "set-field": {
 "ethernet-match": {
 "ethernet-destination": {
 "address": "00:00:00:00:00:05"
 }
 }
 }
 },
 {
 "order": "1',
                             "set-field": {
                                 "ipv4-destination": "192.168.100.105/32"
                             }
                         },
                         {
                             "order": "2",
                             "output-action": {
                                 "output-node-connector": "5"
                             }
                         }
                     ]
                 },
                 {
                     "bucket-id": "1",
                     "action": [
                         {
                             "order": "0",
                             "set-field": {
                                 "ethernet-match": {
                                     "ethernet-destination": {
                                         "address": "00:00:00:00:00:06"
                                     }
                                 }
                             }
                         },
                         {
                             "order": "1",
                             "set-field": {
                                 "ipv4-destination": "192.168.100.106/32"
                             }
                         },
                         {
                             "order": "2",
                             "output-action": {
                                 "output-node-connector": "6"
                             }
                         }
                     ]
                 },
                 {
                     "bucket-id": "2",
                     "action": [
                         {
                             "order": "0",
                             "set-field": {
                                 "ethernet-match": {
                                     "ethernet-destination": {
                                         "address": "00:00:00:00:00:07"
                                     }
                                 }
                             }
                         },
                         {
                             "order": "1",
                             "set-field": {
                                 "ipv4-destination": "192.168.100.107/32"
                             }
                         },
                         {
                             "order": "2",
                             "output-action": {
                                 "output-node-connector": "7"
 }
 }
 ]
 }
 ]
 }
 }
 ]
}

代码实现
  • 关键点有两个
    • ‘Content-Type’:’application/json’
    • Basic Authentication: admin/admin
  • 下面是参考代码

Java

$ cat odl_http.py 
#!/usr/bin/env python
import requests
from requests.auth import HTTPBasicAuth
def http_post(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://10.10.11.80:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:128983912192/flow-node-inventory:group/1'
 with open('odl.json') as f:
 jstr = f.read()
 resp = http_post(url,jstr)
 print resp.content
  • 在OVS查看流表,期望的group已经被添加成功。