be66a2ebd07aadccf095ea6e9c6be2062b8682dc
[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 def ovs_vsctl_del_pbridge(bridge, iface):
37     """
38     This function deletes the OVS bridge and assigns the bridge IP address
39     back to the iface.
40     """
41     (ip_addr, mask) = util.interface_get_ip(bridge)
42     util.interface_assign_ip(iface, ip_addr, mask)
43     util.move_routes(bridge, iface)
44     return ovs_vsctl_del_bridge(bridge)
45
46
47 def ovs_vsctl_is_ovs_bridge(bridge):
48     """
49     This function verifies whether given port is an OVS bridge. If it is an
50     OVS bridge then it will return True.
51     """
52     ret, _out, _err = util.start_process(["ovs-vsctl", "br-exists", bridge])
53     return ret == 0
54
55
56 def ovs_vsctl_add_port_to_bridge(bridge, iface):
57     """
58     This function adds given interface to the bridge.
59     """
60     ret, _out, _err = util.start_process(["ovs-vsctl", "add-port", bridge,
61                                           iface])
62     return ret
63
64
65 def ovs_vsctl_del_port_from_bridge(port):
66     """
67     This function removes given port from a OVS bridge.
68     """
69     ret, _out, _err = util.start_process(["ovs-vsctl", "del-port", port])
70     return ret
71
72
73 def ovs_vsctl_set(table, record, column, key, value):
74     """
75     This function allows to alter the OVS database. If column is a map, then
76     caller should also set the key, otherwise the key should be left as an
77     empty string.
78     """
79     if key is None:
80         index = column
81     else:
82         index = "%s:%s" % (column, key)
83     index_value = "%s=%s" % (index, value)
84     ret, _out, _err = util.start_process(["ovs-vsctl", "set", table, record,
85                                           index_value])
86     return ret
87
88
89 def ovs_get_physical_interface(bridge):
90     """
91     This function tries to figure out which is the physical interface that
92     belongs to the bridge. If there are multiple physical interfaces assigned
93     to this bridge then it will return the first match.
94     """
95     ret, out, _err = util.start_process(["ovs-vsctl", "list-ifaces", bridge])
96
97     if ret == 0:
98         ifaces = out.splitlines()
99         for iface in ifaces:
100             ret, out, _err = util.start_process(["ovs-vsctl", "get",
101                                                  "Interface", iface, "type"])
102             if ret == 0:
103                 if ('""' in out) or ('system' in out):
104                     return iface  # this should be the physical interface
105     return None