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