diff mbox

[ovs-dev,40/55] ovs-vtep: Fix Python 3 compatibility.

Message ID 1450730875-18083-41-git-send-email-russell@ovn.org
State Deferred
Headers show

Commit Message

Russell Bryant Dec. 21, 2015, 8:47 p.m. UTC
Also fix style issues found by the flake8 tool and add ovs-vtep to the
list of files checked in the pep8 tox environment.

Signed-off-by: Russell Bryant <russell@ovn.org>
---
 python/tox.ini |  2 +-
 vtep/ovs-vtep  | 96 +++++++++++++++++++++++++++++++++-------------------------
 2 files changed, 56 insertions(+), 42 deletions(-)
diff mbox

Patch

diff --git a/python/tox.ini b/python/tox.ini
index c55c3db..104aeac 100644
--- a/python/tox.ini
+++ b/python/tox.ini
@@ -14,7 +14,7 @@  deps = -r{toxinidir}/requirements.txt
        -r{toxinidir}/test-requirements.txt
 
 [testenv:pep8]
-commands = flake8 --exclude="*testsuite.dir*" ovs/ ovstest/ {toxinidir}/../tests/ {toxinidir}/../debian/ovs-monitor-ipsec {toxinidir}/../utilities/ovs-pcap.in
+commands = flake8 --exclude="*testsuite.dir*" ovs/ ovstest/ {toxinidir}/../tests/ {toxinidir}/../debian/ovs-monitor-ipsec {toxinidir}/../utilities/ovs-pcap.in {toxinidir}/../vtep/ovs-vtep
 
 [flake8]
 ignore=E111,E113,E126,E127,E128,E129,E131
diff --git a/vtep/ovs-vtep b/vtep/ovs-vtep
index 46a5692..0a25e18 100755
--- a/vtep/ovs-vtep
+++ b/vtep/ovs-vtep
@@ -22,13 +22,14 @@  import shlex
 import subprocess
 import sys
 import time
-import types
 
 import ovs.dirs
 import ovs.util
 import ovs.daemon
 import ovs.unixctl.server
 import ovs.vlog
+import six
+from six.moves import range
 
 
 VERSION = "0.99"
@@ -49,21 +50,25 @@  tun_id = 0
 bfd_bridge = "vtep_bfd"
 bfd_ref = {}
 
+
 def call_prog(prog, args_list):
     cmd = [prog, "-vconsole:off"] + args_list
     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
-    if len(output) == 0 or output[0] == None:
+    if len(output) == 0 or output[0] is None:
         output = ""
     else:
         output = output[0].strip()
     return output
 
+
 def ovs_vsctl(args):
     return call_prog("ovs-vsctl", shlex.split(args))
 
+
 def ovs_ofctl(args):
     return call_prog("ovs-ofctl", shlex.split(args))
 
+
 def vtep_ctl(args):
     return call_prog("vtep-ctl", shlex.split(args))
 
@@ -96,7 +101,7 @@  class Logical_Switch(object):
         column = vtep_ctl("--columns=tunnel_key find logical_switch "
                               "name=%s" % self.name)
         tunnel_key = column.partition(":")[2].strip()
-        if (tunnel_key and type(eval(tunnel_key)) == types.IntType):
+        if tunnel_key and isinstance(eval(tunnel_key), six.integer_types):
             self.tunnel_key = tunnel_key
             vlog.info("using tunnel key %s in %s"
                       % (self.tunnel_key, self.name))
@@ -122,11 +127,11 @@  class Logical_Switch(object):
         ovs_ofctl("add-flow %s priority=0,action=drop" % self.short_name)
 
     def cleanup_ls(self):
-        for port_no, tun_name, remote_ip in self.tunnels.itervalues():
+        for port_no, tun_name, remote_ip in six.itervalues(self.tunnels):
             del_bfd(remote_ip)
 
     def update_flood(self):
-        flood_ports = self.ports.values()
+        flood_ports = list(self.ports.values())
 
         # Traffic flowing from one 'unknown-dst' should not be flooded to
         # port belonging to another 'unknown-dst'.
@@ -161,7 +166,7 @@  class Logical_Switch(object):
     def del_lbinding(self, lbinding):
         vlog.info("removing %s binding from %s" % (lbinding, self.name))
         port_no = self.ports[lbinding]
-        ovs_ofctl("del-flows %s in_port=%s" % (self.short_name, port_no));
+        ovs_ofctl("del-flows %s in_port=%s" % (self.short_name, port_no))
         del self.ports[lbinding]
         self.update_flood()
 
@@ -233,7 +238,7 @@  class Logical_Switch(object):
         self.local_macs = macs
 
     def add_remote_mac(self, mac, tunnel):
-        port_no = self.tunnels.get(tunnel, (0,""))[0]
+        port_no = self.tunnels.get(tunnel, (0, ""))[0]
         if not port_no:
             return
 
@@ -277,12 +282,12 @@  class Logical_Switch(object):
         for tunnel in old_tunnels.difference(tunnels):
             self.del_tunnel(tunnel)
 
-        for mac in remote_macs.keys():
+        for mac in six.iterkeys(remote_macs):
             if (self.remote_macs.get(mac) != remote_macs[mac]):
                 self.add_remote_mac(mac, remote_macs[mac])
 
-        for mac in self.remote_macs.keys():
-            if not remote_macs.has_key(mac):
+        for mac in six.iterkeys(self.remote_macs):
+            if mac not in remote_macs:
                 self.del_remote_mac(mac)
 
         self.remote_macs = remote_macs
@@ -296,14 +301,14 @@  class Logical_Switch(object):
         # vtep's logical_binding_stats. Since we are using the 'interface' from
         # the logical switch to collect stats, packets transmitted from it
         # is received in the physical switch and vice versa.
-        stats_map = {'tx_packets':'packets_to_local',
-                    'tx_bytes':'bytes_to_local',
-                    'rx_packets':'packets_from_local',
-                     'rx_bytes':'bytes_from_local'}
+        stats_map = {'tx_packets': 'packets_to_local',
+                     'tx_bytes': 'bytes_to_local',
+                     'rx_packets': 'packets_from_local',
+                     'rx_bytes': 'bytes_from_local'}
 
         # Go through all the logical switch's interfaces that end with "-l"
         # and copy the statistics to logical_binding_stats.
-        for interface in self.ports.iterkeys():
+        for interface in six.iterkeys(self.ports):
             if not interface.endswith("-l"):
                 continue
             # Physical ports can have a '-' as part of its name.
@@ -314,7 +319,7 @@  class Logical_Switch(object):
             if not uuid:
                 continue
 
-            for (mapfrom, mapto) in stats_map.iteritems():
+            for (mapfrom, mapto) in six.iteritems(stats_map):
                 value = ovs_vsctl("get interface %s statistics:%s"
                                 % (interface, mapfrom)).strip('"')
                 vtep_ctl("set logical_binding_stats %s %s=%s"
@@ -325,6 +330,7 @@  class Logical_Switch(object):
         self.update_remote_macs()
         self.update_stats()
 
+
 def get_vtep_tunnel(remote_ip):
     # Get the physical_locator record for the local tunnel end point.
     column = vtep_ctl("--columns=_uuid find physical_locator "
@@ -346,6 +352,7 @@  def get_vtep_tunnel(remote_ip):
 
     return (local, remote, tunnel)
 
+
 def create_vtep_tunnel(remote_ip):
     local, remote, tunnel = get_vtep_tunnel(remote_ip)
     if not local or not remote:
@@ -356,9 +363,10 @@  def create_vtep_tunnel(remote_ip):
                   % remote_ip)
         tunnel = vtep_ctl("add physical_switch %s tunnels @tun -- "
                           "--id=@tun create Tunnel local=%s remote=%s"
-                          %(ps_name, local, remote))
+                          % (ps_name, local, remote))
     return tunnel
 
+
 def destroy_vtep_tunnel(remote_ip):
     local, remote, tunnel = get_vtep_tunnel(remote_ip)
     if tunnel:
@@ -368,6 +376,7 @@  def destroy_vtep_tunnel(remote_ip):
                  "-- --if-exists destroy tunnel %s"
                  % (ps_name, tunnel, tunnel))
 
+
 def add_bfd(remote_ip):
     # The VTEP emulator creates one OVS bridge for every logical switch.
     # Multiple logical switches can have multiple OVS tunnels to the
@@ -397,6 +406,7 @@  def add_bfd(remote_ip):
     # conditions, pass the responsibility of creating a 'tunnel' record
     # to run_bfd() which runs more often.
 
+
 def del_bfd(remote_ip):
     if remote_ip in bfd_ref:
         if bfd_ref[remote_ip] == 1:
@@ -408,6 +418,7 @@  def del_bfd(remote_ip):
         else:
             bfd_ref[remote_ip] -= 1
 
+
 def run_bfd():
     bfd_ports = ovs_vsctl("list-ports %s" % bfd_bridge).split()
     for port in bfd_ports:
@@ -416,15 +427,15 @@  def run_bfd():
         if not tunnel:
             continue
 
-        bfd_params_default = {'bfd_params:enable' : 'false',
-                              'bfd_params:min_rx' : 1000,
-                              'bfd_params:min_tx' : 100,
-                              'bfd_params:decay_min_rx' : 0,
-                              'bfd_params:cpath_down' : 'false',
-                              'bfd_params:check_tnl_key' : 'false'}
+        bfd_params_default = {'bfd_params:enable': 'false',
+                              'bfd_params:min_rx': 1000,
+                              'bfd_params:min_tx': 100,
+                              'bfd_params:decay_min_rx': 0,
+                              'bfd_params:cpath_down': 'false',
+                              'bfd_params:check_tnl_key': 'false'}
         bfd_params_values = {}
 
-        for key, default in bfd_params_default.iteritems():
+        for key, default in six.iteritems(bfd_params_default):
             column = vtep_ctl("--if-exists get tunnel %s %s"
                                % (tunnel, key))
             if not column:
@@ -432,8 +443,8 @@  def run_bfd():
             else:
                 bfd_params_values[key] = column
 
-        for key, value in bfd_params_values.iteritems():
-            new_key = key.replace('_params','')
+        for key, value in six.iteritems(bfd_params_values):
+            new_key = key.replace('_params', '')
             ovs_vsctl("set interface %s %s=%s" % (port, new_key, value))
 
         bfd_status = ['bfd_status:state', 'bfd_status:forwarding',
@@ -442,7 +453,7 @@  def run_bfd():
         for key in bfd_status:
             value = ovs_vsctl("--if-exists get interface %s %s" % (port, key))
             if value:
-                vtep_ctl("set tunnel %s %s=%s" %(tunnel, key, value))
+                vtep_ctl("set tunnel %s %s=%s" % (tunnel, key, value))
             else:
                 new_key = key.replace('bfd_status:', '')
                 vtep_ctl("remove tunnel %s bfd_status %s" % (tunnel, new_key))
@@ -451,11 +462,11 @@  def run_bfd():
                  % (tunnel, bfd_params_values['bfd_params:enable']))
 
         # Add the defaults as described in VTEP schema to make it explicit.
-        bfd_lconf_default = {'bfd_config_local:bfd_dst_ip' : '169.254.1.0',
-                             'bfd_config_local:bfd_dst_mac' :
-                                    '00:23:20:00:00:01'}
-        for key, value in bfd_lconf_default.iteritems():
-            vtep_ctl("set tunnel %s %s=%s" %(tunnel, key, value))
+        bfd_lconf_default = {'bfd_config_local:bfd_dst_ip': '169.254.1.0',
+                             'bfd_config_local:bfd_dst_mac':
+                             '00:23:20:00:00:01'}
+        for key, value in six.iteritems(bfd_lconf_default):
+            vtep_ctl("set tunnel %s %s=%s" % (tunnel, key, value))
 
         # bfd_config_remote options from VTEP DB should be populated to
         # corresponding OVS DB values.
@@ -475,12 +486,13 @@  def run_bfd():
                   bfd_lconf_default['bfd_config_local:bfd_dst_mac'],
                   bfd_dst_mac))
 
+
 def add_binding(binding, ls):
     vlog.info("adding binding %s" % binding)
 
     vlan, pp_name = binding.split("-", 1)
-    pbinding = binding+"-p"
-    lbinding = binding+"-l"
+    pbinding = binding + "-p"
+    lbinding = binding + "-l"
 
     # Create a patch port that connects the VLAN+port to the lswitch.
     # Do them as two separate calls so if one side already exists, the
@@ -510,18 +522,19 @@  def add_binding(binding, ls):
     if not vlan_:
         vlan_ = "0"
     vtep_ctl("set physical_port %s vlan_stats:%s=@stats --\
-            --id=@stats create logical_binding_stats packets_from_local=0"\
+            --id=@stats create logical_binding_stats packets_from_local=0"
             % (pp_name, vlan_))
 
     ls.add_lbinding(lbinding)
     Bindings[binding] = ls.name
 
+
 def del_binding(binding, ls):
     vlog.info("removing binding %s" % binding)
 
     vlan, pp_name = binding.split("-", 1)
-    pbinding = binding+"-p"
-    lbinding = binding+"-l"
+    pbinding = binding + "-p"
+    lbinding = binding + "-l"
 
     port_no = ovs_vsctl("get Interface %s ofport" % pp_name)
     patch_no = ovs_vsctl("get Interface %s ofport" % pbinding)
@@ -545,6 +558,7 @@  def del_binding(binding, ls):
 
     del Bindings[binding]
 
+
 def handle_physical():
     # Gather physical ports except the patch ports we created
     ovs_ports = ovs_vsctl("list-ports %s" % ps_name).split()
@@ -574,7 +588,7 @@  def handle_physical():
             ls = Lswitches[ls_name]
             new_bindings.add(binding)
 
-            if Bindings.has_key(binding):
+            if binding in Bindings:
                 if Bindings[binding] == ls_name:
                     continue
                 else:
@@ -582,7 +596,6 @@  def handle_physical():
 
             add_binding(binding, ls)
 
-
     dead_bindings = set(Bindings.keys()).difference(new_bindings)
     for binding in dead_bindings:
         ls_name = Bindings[binding]
@@ -596,6 +609,7 @@  def handle_physical():
             vtep_ctl("clear-local-macs %s" % Lswitches[ls_name].name)
             del Lswitches[ls_name]
 
+
 def setup():
     br_list = ovs_vsctl("list-br").split()
     if (ps_name not in br_list):
@@ -641,7 +655,7 @@  def setup():
             for port in bfd_ports:
                 remote_ip = ovs_vsctl("get interface %s options:remote_ip"
                                       % port)
-                tunnel = destroy_vtep_tunnel(remote_ip)
+                destroy_vtep_tunnel(remote_ip)
 
             ovs_vsctl("del-br %s" % br)
 
@@ -692,7 +706,7 @@  def main():
 
         handle_physical()
 
-        for ls_name, ls in Lswitches.items():
+        for ls_name, ls in six.iteritems(Lswitches):
             ls.run()
 
         run_bfd()