Don't re-add VLANs to trunks

Save time and effort - keep track of the VLANs already set on a
switch's trunk ports and don't try to add them again later.

Change-Id: Ibeb76f568dac0b1a7eb67c1648cbd15e5680bf06
diff --git a/util.py b/util.py
index a72b584..f3bb99c 100644
--- a/util.py
+++ b/util.py
@@ -632,6 +632,8 @@
         db = state.db
         config = state.config
 
+        port_vlans = {}
+
         # 1. Sanity checks!
         switch_id = db.get_switch_id_by_name(switch_name)
         if switch_id is not None:
@@ -712,11 +714,11 @@
                 # set both the current and base VLANs to the current
                 # VLAN on the switch. We'll end up changing this after
                 # import if needed.
-                port_vlan = s.port_get_access_vlan(port_name)
-                port_vlan_id = db.get_vlan_id_by_tag(port_vlan)
+                port_vlans[port_name] = (s.port_get_access_vlan(port_name),)
+                port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
                 port_id = db.create_port(switch_id, port_name,
                                          port_vlan_id, port_vlan_id)
-                print '    access port, VLAN %d' % int(port_vlan)
+                print '    access port, VLAN %d' % int(port_vlans[port_name][0])
                 # Nothing further needed
             elif port_mode == 'trunk':
                 print '    trunk port'
@@ -724,12 +726,12 @@
                 # create the port in the DB, setting the VLANs to the
                 # first VLAN found on the trunk port. This will *also*
                 # be in access mode by default, and unlocked.
-                port_vlans = s.port_get_trunk_vlan_list(port_name)
+                port_vlans[port_name] = s.port_get_trunk_vlan_list(port_name)
                 print '    trunk port has VLANs:'
-                print port_vlans
-                if port_vlans == [] or port_vlans is None or 'ALL' in port_vlans:
-                    port_vlans = (state.config.vland.default_vlan_tag,) # easy for our purposes
-                port_vlan_id = db.get_vlan_id_by_tag(port_vlans[0])
+                print port_vlans[port_name]
+                if port_vlans[port_name] == [] or port_vlans[port_name] is None or 'ALL' in port_vlans[port_name]:
+                    port_vlans[port_name] = (state.config.vland.default_vlan_tag,)
+                port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
                 port_id = db.create_port(switch_id, port_name,
                                          port_vlan_id, port_vlan_id)
                 # Append to a list of trunk ports that we will need to
@@ -758,8 +760,9 @@
                 
                 for vlan in db.all_vlans():
                     if vlan.vlan_id is not state.default_vlan_id:
-                        print "Adding allowed VLAN tag %d to trunk port %s" % (vlan.tag, port.name)
-                        s.port_add_trunk_to_vlan(port.name, vlan.tag)
+                        if not vlan.tag in port_vlans[port.name]:
+                            print "Adding allowed VLAN tag %d to trunk port %s" % (vlan.tag, port.name)
+                            s.port_add_trunk_to_vlan(port.name, vlan.tag)
 
         # Done with this switch \o/
         s.switch_save_running_config()