summaryrefslogtreecommitdiffhomepage
path: root/doc/source/library_ovsdb_manager.rst
blob: b36a93353b26a2b0cb136e078439667489e357b0 (plain)
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
*********************
OVSDB Manager library
*********************

Introduction
============

Ryu OVSDB Manager library allows your code to interact with devices
speaking the OVSDB protocol. This enables your code to perform remote
management of the devices and react to topology changes on them.

Example
=======

The following logs all new OVSDB connections and allows creating a port
on a bridge.

.. code-block:: python

    import uuid

    from ryu.base import app_manager
    from ryu.controller.handler import set_ev_cls
    from ryu.services.protocols.ovsdb import api as ovsdb
    from ryu.services.protocols.ovsdb import event as ovsdb_event


    class MyApp(app_manager.RyuApp):
        @set_ev_cls(ovsdb_event.EventNewOVSDBConnection)
        def handle_new_ovsdb_connection(self, ev):
            system_id = ev.system_id
            addr = ev.client.address
            self.logger.info('New OVSDB connection from system id %s',
                             system_id)
            self.logger.info('The connection address id %s',
                             addr)

        def create_port(self, system_id, bridge_name, name):
            new_iface_uuid = uuid.uuid4()
            new_port_uuid = uuid.uuid4()

            bridge = ovsdb.row_by_name(self, system_id, bridge_name)

            def _create_port(tables, insert):

                iface = insert(tables['Interface'], new_iface_uuid)
                iface.name = name
                iface.type = 'internal'

                port = insert(tables['Port'], new_port_uuid)
                port.name = name
                port.interfaces = [iface]

                bridge.ports = bridge.ports + [port]

                return (new_port_uuid, new_iface_uuid)

            req = ovsdb_event.EventModifyRequest(system_id, _create_port)
            rep = self.send_request(req)

            if rep.status != 'success':
                self.logger.error('Error creating port %s on bridge %s: %s',
                                  name, bridge, rep.status)
                return None

            return rep.insert_uuids[new_port_uuid]