File: api.py

package info (click to toggle)
python-os-ken 3.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,320 kB
  • sloc: python: 100,703; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (92 lines) | stat: -rw-r--r-- 3,035 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# client for os_ken.app.ofctl.service

from os_ken.base import app_manager
from . import event


def get_datapath(app, dpid=None):
    """
    Get datapath object by dpid.

    :param app: Client OSKenApp instance
    :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 os_ken.app.ofctl.api as ofctl_api


        class MyApp(app_manager.OSKenApp):

            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)
    """
    return app.send_request(event.GetDatapathRequest(dpid=dpid))()


def send_msg(app, msg, reply_cls=None, reply_multi=False):
    """
    Send an OpenFlow message and wait for reply messages.

    :param app: Client OSKenApp instance
    :param msg: An OpenFlow controller-to-switch message to send
    :param reply_cls: OpenFlow message class for expected replies.
        None means no replies are expected.  The default is None.
    :param reply_multi: True if multipart replies are expected.
        The default is False.

    If no replies, returns None.
    If reply_multi=False, returns OpenFlow switch-to-controller message.
    If reply_multi=True, returns a list of OpenFlow switch-to-controller
    messages.

    Raise an exception on error.

    Example::

        # ...(snip)...
        import os_ken.app.ofctl.api as ofctl_api


        class MyApp(app_manager.OSKenApp):

            def _my_handler(self, ev):
                # ...(snip)...
                msg = parser.OFPPortDescStatsRequest(datapath=datapath)
                result = ofctl_api.send_msg(
                    self, msg,
                    reply_cls=parser.OFPPortDescStatsReply,
                    reply_multi=True)
    """
    return app.send_request(event.SendMsgRequest(msg=msg,
                                                 reply_cls=reply_cls,
                                                 reply_multi=reply_multi))()


app_manager.require_app('os_ken.app.ofctl.service', api_style=True)