python: Resolve pep8 blank line errors.
[cascardo/ovs.git] / python / ovstest / vswitch.py
1 # Copyright (c) 2012 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 vswitch module allows its callers to interact with OVS DB.
17 """
18 import util
19
20
21 def ovs_vsctl_add_bridge(bridge):
22     """
23     This function creates an OVS bridge.
24     """
25     ret, _out, _err = util.start_process(["ovs-vsctl", "add-br", bridge])
26     return ret
27
28
29 def ovs_vsctl_del_bridge(bridge):
30     """
31     This function deletes the OVS bridge.
32     """
33     ret, _out, _err = util.start_process(["ovs-vsctl", "del-br", bridge])
34     return ret
35
36
37 def ovs_vsctl_del_pbridge(bridge, iface):
38     """
39     This function deletes the OVS bridge and assigns the bridge IP address
40     back to the iface.
41     """
42     (ip_addr, mask) = util.interface_get_ip(bridge)
43     util.interface_assign_ip(iface, ip_addr, mask)
44     util.move_routes(bridge, iface)
45     return ovs_vsctl_del_bridge(bridge)
46
47
48 def ovs_vsctl_is_ovs_bridge(bridge):
49     """
50     This function verifies whether given port is an OVS bridge. If it is an
51     OVS bridge then it will return True.
52     """
53     ret, _out, _err = util.start_process(["ovs-vsctl", "br-exists", bridge])
54     return ret == 0
55
56
57 def ovs_vsctl_add_port_to_bridge(bridge, iface):
58     """
59     This function adds given interface to the bridge.
60     """
61     ret, _out, _err = util.start_process(["ovs-vsctl", "add-port", bridge,
62                                           iface])
63     return ret
64
65
66 def ovs_vsctl_del_port_from_bridge(port):
67     """
68     This function removes given port from a OVS bridge.
69     """
70     ret, _out, _err = util.start_process(["ovs-vsctl", "del-port", port])
71     return ret
72
73
74 def ovs_vsctl_set(table, record, column, key, value):
75     """
76     This function allows to alter the OVS database. If column is a map, then
77     caller should also set the key, otherwise the key should be left as an
78     empty string.
79     """
80     if key is None:
81         index = column
82     else:
83         index = "%s:%s" % (column, key)
84     index_value = "%s=%s" % (index, value)
85     ret, _out, _err = util.start_process(["ovs-vsctl", "set", table, record,
86                                           index_value])
87     return ret
88
89
90 def ovs_get_physical_interface(bridge):
91     """
92     This function tries to figure out which is the physical interface that
93     belongs to the bridge. If there are multiple physical interfaces assigned
94     to this bridge then it will return the first match.
95     """
96     ret, out, _err = util.start_process(["ovs-vsctl", "list-ifaces", bridge])
97
98     if ret == 0:
99         ifaces = out.splitlines()
100         for iface in ifaces:
101             ret, out, _err = util.start_process(["ovs-vsctl", "get",
102                                                  "Interface", iface, "type"])
103             if ret == 0:
104                 if ('""' in out) or ('system' in out):
105                     return iface  # this should be the physical interface
106     return None