如何不通过GUI在OpenDaylight 16.02控制器中运行脚本配置流表?
解决方案:通过脚本操作OpenDaylight流表
针对你在ODL 16.02控制器端用脚本增改流表的需求,以下是两种直接可行的方案,无需依赖GUI:
方法1:RESTAPI + 脚本(Python/curl)
ODL原生支持RESTCONF接口,可直接在控制器主机上编写脚本调用本地REST端点,实现流表的增删改查。
示例:Python脚本添加双向转发流表(实现主机互通)
假设拓扑为h1(10.0.0.1) -> s1 -> h2(10.0.0.2),以下脚本可添加双向转发流表:
import requests from requests.auth import HTTPBasicAuth # ODL基础配置 ODL_ADDR = "localhost:8181" AUTH = HTTPBasicAuth("admin", "admin") RESTCONF_FLOW_URL = f"http://{ODL_ADDR}/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow" # h1到h2的流表条目 flow_h1_to_h2 = { "flow": [ { "id": "1", "match": {"ipv4-source": "10.0.0.1/32", "ipv4-destination": "10.0.0.2/32"}, "instructions": { "instruction": [ { "order": "0", "apply-actions": { "action": [{"order": "0", "output-action": {"output-node-connector": "2"}}] } } ] }, "priority": "100", "table_id": "0" } ] } # h2到h1的流表条目 flow_h2_to_h1 = { "flow": [ { "id": "2", "match": {"ipv4-source": "10.0.0.2/32", "ipv4-destination": "10.0.0.1/32"}, "instructions": { "instruction": [ { "order": "0", "apply-actions": { "action": [{"order": "0", "output-action": {"output-node-connector": "1"}}] } } ] }, "priority": "100", "table_id": "0" } ] } # 发送请求添加流表 def add_flow(flow_data): headers = {"Content-Type": "application/json"} resp = requests.post(RESTCONF_FLOW_URL, auth=AUTH, headers=headers, json=flow_data) print(f"添加流表状态码: {resp.status_code}, 响应: {resp.text}") add_flow(flow_h1_to_h2) add_flow(flow_h2_to_h1)
使用说明:将脚本放在ODL控制器主机上,安装requests库(pip install requests)后运行,需根据实际拓扑修改节点ID(openflow:1)、端口号(1/2)、IP地址等参数。
示例:Shell脚本用curl修改流表
如果偏好shell脚本,可直接用curl调用RESTCONF:
# 添加h1到h2的流表 curl -u admin:admin -X POST \ http://localhost:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1 \ -H "Content-Type: application/json" \ -d '{ "flow": { "id": "1", "match": {"ipv4-source": "10.0.0.1/32", "ipv4-destination": "10.0.0.2/32"}, "instructions": { "instruction": [ { "order": "0", "apply-actions": {"action": [{"order": "0", "output-action": {"output-node-connector": "2"}}]} } ] }, "priority": "100", "table_id": "0" } }'
方法2:ODL Karaf命令行脚本
ODL的Karaf控制台支持批量执行命令,可将流表操作写成.karaf脚本,直接在控制器上运行。
示例Karaf脚本(flow_config.karaf)
# 添加双向转发流表 openflow:add-flow openflow:1 0 1 100 ipv4,src=10.0.0.1/32,dst=10.0.0.2/32 output=2 openflow:add-flow openflow:1 0 2 100 ipv4,src=10.0.0.2/32,dst=10.0.0.1/32 output=1 # 验证流表是否添加成功 openflow:show-flows openflow:1
运行方式:进入ODL的bin目录,执行./karaf run flow_config.karaf,或在Karaf控制台内用source flow_config.karaf加载执行。
流量监控脚本思路
通过RESTAPI定期获取流表统计数据,即可实现流量监控:
- 调用
http://localhost:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow接口,提取每个流表条目的packet-count(数据包数)和byte-count(字节数)字段。 - 编写Python脚本定时请求该接口,解析并输出监控数据,或存入日志。
简单监控脚本示例
import requests import time from requests.auth import HTTPBasicAuth ODL_STATS_URL = "http://localhost:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow" AUTH = HTTPBasicAuth("admin", "admin") def get_flow_stats(): headers = {"Accept": "application/json"} resp = requests.get(ODL_STATS_URL, auth=AUTH, headers=headers) if resp.status_code == 200: flows = resp.json()["flow-node-inventory:flow"] for flow in flows: print(f"流表ID {flow['id']}: 数据包数={flow['packet-count']}, 字节数={flow['byte-count']}") else: print(f"获取统计失败,状态码: {resp.status_code}") # 每5秒刷新一次监控数据 while True: get_flow_stats() time.sleep(5)
注意事项
- 确保ODL已安装
openflowplugin和restconf特性,在Karaf控制台执行feature:list | grep openflow和feature:list | grep restconf确认,未安装则用feature:install odl-openflowplugin-all odl-restconf安装。 - 流表的优先级、匹配字段需根据实际拓扑调整,避免流表冲突。
- RESTCONF认证信息需与你的ODL配置一致。
内容的提问来源于stack exchange,提问作者Mohammad Hajyghasem
相关产品推荐
相关产品推荐

