1 # Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # client for ryu.app.ofctl.service
19 from ryu.base import app_manager
23 def get_datapath(app, dpid=None):
25 Get datapath object by dpid.
27 :param app: Client RyuApp instance
28 :param dpid: Datapath ID (int type) or None to get all datapath objects
30 Returns a object of datapath, a list of datapath objects when no dpid
31 given or None when error.
33 Raises an exception if any of the given values is invalid.
38 import ryu.app.ofctl.api as ofctl_api
41 class MyApp(app_manager.RyuApp):
43 def _my_handler(self, ev):
44 # Get all datapath objects
45 result = ofctl_api.get_datapath(self)
47 # Get the datapath object which has the given dpid
48 result = ofctl_api.get_datapath(self, dpid=1)
50 return app.send_request(event.GetDatapathRequest(dpid=dpid))()
53 def send_msg(app, msg, reply_cls=None, reply_multi=False):
55 Send an OpenFlow message and wait for reply messages.
57 :param app: Client RyuApp instance
58 :param msg: An OpenFlow controller-to-switch message to send
59 :param reply_cls: OpenFlow message class for expected replies.
60 None means no replies are expected. The default is None.
61 :param reply_multi: True if multipart replies are expected.
64 If no replies, returns None.
65 If reply_multi=False, returns OpenFlow switch-to-controller message.
66 If reply_multi=True, returns a list of OpenFlow switch-to-controller
69 Raise an exception on error.
74 import ryu.app.ofctl.api as ofctl_api
77 class MyApp(app_manager.RyuApp):
79 def _my_handler(self, ev):
81 msg = parser.OFPPortDescStatsRequest(datapath=datapath)
82 result = ofctl_api.send_msg(
84 reply_cls=parser.OFPPortDescStatsReply,
87 return app.send_request(event.SendMsgRequest(msg=msg,
89 reply_multi=reply_multi))()
92 app_manager.require_app('ryu.app.ofctl.service', api_style=True)