summaryrefslogtreecommitdiff
path: root/python/ovstest/vswitch.py
blob: 9d5b5cffd00237afcf758b152a2e25c0af46cc25 (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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright (c) 2012 Nicira, Inc.
#
# 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.

"""
vswitch module allows its callers to interact with OVS DB.
"""
import util


def ovs_vsctl_add_bridge(bridge):
    """
    This function creates an OVS bridge.
    """
    ret, _out, _err = util.start_process(["ovs-vsctl", "add-br", bridge])
    return ret


def ovs_vsctl_del_bridge(bridge):
    """
    This function deletes the OVS bridge.
    """
    ret, _out, _err = util.start_process(["ovs-vsctl", "del-br", bridge])
    return ret


def ovs_vsctl_del_pbridge(bridge, iface):
    """
    This function deletes the OVS bridge and assigns the bridge IP address
    back to the iface.
    """
    (ip_addr, mask) = util.interface_get_ip(bridge)
    util.interface_assign_ip(iface, ip_addr, mask)
    util.interface_up(iface)
    util.move_routes(bridge, iface)
    return ovs_vsctl_del_bridge(bridge)


def ovs_vsctl_is_ovs_bridge(bridge):
    """
    This function verifies whether given port is an OVS bridge. If it is an
    OVS bridge then it will return True.
    """
    ret, _out, _err = util.start_process(["ovs-vsctl", "br-exists", bridge])
    return ret == 0


def ovs_vsctl_add_port_to_bridge(bridge, iface):
    """
    This function adds given interface to the bridge.
    """
    ret, _out, _err = util.start_process(["ovs-vsctl", "add-port", bridge,
                                          iface])
    return ret


def ovs_vsctl_del_port_from_bridge(port):
    """
    This function removes given port from a OVS bridge.
    """
    ret, _out, _err = util.start_process(["ovs-vsctl", "del-port", port])
    return ret


def ovs_vsctl_set(table, record, column, key, value):
    """
    This function allows to alter the OVS database. If column is a map, then
    caller should also set the key, otherwise the key should be left as an
    empty string.
    """
    if key is None:
        index = column
    else:
        index = "%s:%s" % (column, key)
    index_value = "%s=%s" % (index, value)
    ret, _out, _err = util.start_process(["ovs-vsctl", "set", table, record,
                                          index_value])
    return ret


def ovs_get_physical_interface(bridge):
    """
    This function tries to figure out which is the physical interface that
    belongs to the bridge. If there are multiple physical interfaces assigned
    to this bridge then it will return the first match.
    """
    ret, out, _err = util.start_process(["ovs-vsctl", "list-ifaces", bridge])

    if ret == 0:
        ifaces = out.splitlines()
        for iface in ifaces:
            ret, out, _err = util.start_process(["ovs-vsctl", "get",
                                                 "Interface", iface, "type"])
            if ret == 0:
                if ('""' in out) or ('system' in out):
                    return iface  # this should be the physical interface
    return None