opendaylight REST接口 程序编写学习

OpenDayLight控制器的北向API接口整理

网址在这

建立topo

sudo mn --topo=single,3 --mac --controller=remote,ip=控制器ip,port=6633 --switch ovsk,protocols=OpenFlow13

在这里插入图片描述

获取当前topo信息

import requests as rq
from requests.auth import HTTPBasicAuth

if __name__ == '__main__':
    url = 'http://控制器ip:8181/restconf/operational/opendaylight-inventory:nodes'
    headers = {'Content-Type': 'application/json'}
    response = rq.get(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    print(response.content)```

![在这里插入图片描述](https://img-blog.csdnimg.cn/aa6a7f2e1aaf486b93e900c8b164334c.png)

删除s1流表数据

import requests as rq
from requests.auth import HTTPBasicAuth

if __name__ == '__main__':
    url = 'http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/'
    headers = {'Content-Type': 'application/json'}
    response = rq.delete(url=url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
    print(response.content)

但是好像并没有执行成功,输出流表仍有数据
故查找原因
print(response)看看
在这里插入图片描述

返回状态405了
在这里插入图片描述
没有关于405的错误信息
搜索资料后,感觉是版本问题?有待商榷。。。

下发硬超时流,实现网络中断30s

import requests

from requests.auth import HTTPBasicAuth

def http_put(url,jstr):

	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('flowTable.json') as f:

		jstr = f.read()

	resp = http_put(url,jstr)

	print resp

flowTable.json


"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": "flow1",

"priority": "65535",

"hard-timeout": "30",

"cookie": "2",

"table_id": "0"

}

]

}

config和operational区别见:
https://www.sdnlab.com/community/question/181

h1 ping h3,然后去openday运行py文件,成功了
在这里插入图片描述
点开网址:
http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1

也可以看到流表确实下发了

在这里插入图片描述

获取s1上活动的流表数

import requests

from requests.auth import HTTPBasicAuth

def http_get(url):

	headers = {'Content-Type':'application/json'}

	resp = requests.put(url,headers=headers,auth=HTTPBasicAuth('admin', 'admin'))

	return resp

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'

	resp = http_get(url)

	print resp

但是结果返回405
在这里插入图片描述
关于405的问题,暂时不太知道为什么。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.breeze.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值