diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2018-01-15 12:56:09 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-01-17 08:46:53 +0900 |
commit | e81ec3fb01d74907d77f9f2f6117f346d72a1a8d (patch) | |
tree | 2a3cf49cdbd73842ceb176abc189a4c3a0794b17 | |
parent | 37c73db7b1b1c98bd000d4f813423702d73a0c8c (diff) |
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>
-rw-r--r-- | ryu/app/ofctl/api.py | 38 | ||||
-rw-r--r-- | ryu/app/ofctl/event.py | 4 | ||||
-rw-r--r-- | ryu/app/ofctl/service.py | 16 |
3 files changed, 39 insertions, 19 deletions
diff --git a/ryu/app/ofctl/api.py b/ryu/app/ofctl/api.py index 466b3314..fe76b807 100644 --- a/ryu/app/ofctl/api.py +++ b/ryu/app/ofctl/api.py @@ -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 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 - Returns None on error. + + 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) """ diff --git a/ryu/app/ofctl/event.py b/ryu/app/ofctl/event.py index 8919452e..dfb0024f 100644 --- a/ryu/app/ofctl/event.py +++ b/ryu/app/ofctl/event.py @@ -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 diff --git a/ryu/app/ofctl/service.py b/ryu/app/ofctl/service.py index 2a2e97d1..ce604958 100644 --- a/ryu/app/ofctl/service.py +++ b/ryu/app/ofctl/service.py @@ -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): |