ofctl: Enable to get all datapath objects

Also, updates the API document of ryu.app.ofctl.api.

Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWASE Yusuke 2018-01-15 12:56:09 +09:00 committed by FUJITA Tomonori
parent 37c73db7b1
commit e81ec3fb01
3 changed files with 39 additions and 19 deletions

View File

@ -16,22 +16,37 @@
# client for ryu.app.ofctl.service
import numbers
from ryu.base import app_manager
from . import event
def get_datapath(app, dpid):
def get_datapath(app, dpid=None):
"""
Get datapath object by dpid.
:param app: Client RyuApp instance
:param dpid: Datapath-id (in integer)
:param dpid: Datapath ID (int type) or None to get all datapath objects
Returns None on error.
Returns a object of datapath, a list of datapath objects when no dpid
given or None when error.
Raises an exception if any of the given values is invalid.
Example::
# ...(snip)...
import ryu.app.ofctl.api as ofctl_api
class MyApp(app_manager.RyuApp):
def _my_handler(self, ev):
# Get all datapath objects
result = ofctl_api.get_datapath(self)
# Get the datapath object which has the given dpid
result = ofctl_api.get_datapath(self, dpid=1)
"""
assert isinstance(dpid, numbers.Integral)
return app.send_request(event.GetDatapathRequest(dpid=dpid))()
@ -55,10 +70,17 @@ def send_msg(app, msg, reply_cls=None, reply_multi=False):
Example::
import ryu.app.ofctl.api as api
# ...(snip)...
import ryu.app.ofctl.api as ofctl_api
class MyApp(app_manager.RyuApp):
def _my_handler(self, ev):
# ...(snip)...
msg = parser.OFPPortDescStatsRequest(datapath=datapath)
result = api.send_msg(self, msg,
result = ofctl_api.send_msg(
self, msg,
reply_cls=parser.OFPPortDescStatsReply,
reply_multi=True)
"""

View File

@ -33,8 +33,8 @@ class _ReplyBase(event.EventReplyBase):
# get datapath
class GetDatapathRequest(_RequestBase):
def __init__(self, dpid):
assert isinstance(dpid, numbers.Integral)
def __init__(self, dpid=None):
assert dpid is None or isinstance(dpid, numbers.Integral)
super(GetDatapathRequest, self).__init__()
self.dpid = dpid

View File

@ -99,15 +99,13 @@ class OfctlService(app_manager.RyuApp):
@set_ev_cls(event.GetDatapathRequest, MAIN_DISPATCHER)
def _handle_get_datapath(self, req):
id = req.dpid
assert isinstance(id, numbers.Integral)
try:
datapath = self._switches[id].datapath
except KeyError:
datapath = None
self.logger.debug('dpid %s -> datapath %s', id, datapath)
rep = event.Reply(result=datapath)
self.reply_to_request(req, rep)
result = None
if req.dpid is None:
result = [v.datapath for v in self._switches.values()]
else:
if req.dpid in self._switches:
result = self._switches[req.dpid].datapath
self.reply_to_request(req, event.Reply(result=result))
@set_ev_cls(event.SendMsgRequest, MAIN_DISPATCHER)
def _handle_send_msg(self, req):