Add the new number field in the ports table

Added in several places:
 * database creation code
 * port addition code

Change-Id: Id4e2bd2704d5b9660df8baad34abda0f613ba4f2
diff --git a/admin.py b/admin.py
index cbed93d..2e95ae3 100644
--- a/admin.py
+++ b/admin.py
@@ -184,8 +184,8 @@
                   action = "store",
                   type = "string",
                   help = "Add a new port to the system",
-                  nargs = 2,
-                  metavar = "<switch_id> <name>")
+                  nargs = 3,
+                  metavar = "<switch_id> <name> <number>")
 port_group.add_option("--delete_port",
                   dest = "delete_port",
                   action = "store",
@@ -344,7 +344,8 @@
                              {'command':'db.create_port',
                               'data':
                               {'switch_id': opts.create_port[0],
-                               'name': opts.create_port[1]}})
+                                'name': opts.create_port[1],
+                                'number': opts.create_port[2]}})
         print 'Created port_id %d' % port_id
     except InputError as inst:
         print 'Failed: %s' % inst
diff --git a/db/db.py b/db/db.py
index b5bd631..cc79c07 100644
--- a/db/db.py
+++ b/db/db.py
@@ -78,7 +78,8 @@
     # 1. The switch referred to must already exist
     # 2. The VLANs mentioned here must already exist
     # 3. (Switch/name) must be unique
-    def create_port(self, switch_id, name, current_vlan_id, base_vlan_id):
+    # 4. (Switch/number) must be unique
+    def create_port(self, switch_id, name, number, current_vlan_id, base_vlan_id):
 
         switch = self.get_switch_by_id(switch_id)
         if switch is None:
@@ -93,9 +94,13 @@
         if port_id is not None:
             raise InputError("Already have a port %s on switch ID %d" % (name, int(switch_id)))
 
+        port_id = self.get_port_by_switch_and_number(switch_id, int(number))
+        if port_id is not None:
+            raise InputError("Already have a port %d on switch ID %d" % (int(number), int(switch_id)))
+
         try:
-            sql = "INSERT INTO port (name, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id) VALUES (%s, %s, %s, %s, %s, %s) RETURNING port_id"
-            data = (name, switch_id,
+            sql = "INSERT INTO port (name, number, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING port_id"
+            data = (name, number, switch_id,
                     False, False,
                     current_vlan_id, base_vlan_id)
             self.cursor.execute(sql, data)
diff --git a/db/setup_db.py b/db/setup_db.py
index 82ced99..a821abd 100644
--- a/db/setup_db.py
+++ b/db/setup_db.py
@@ -36,10 +36,10 @@
 cur = conn.cursor()
 
 cur.execute("CREATE TABLE switch (switch_id SERIAL, name VARCHAR(64))")
-cur.execute("CREATE TABLE port (port_id SERIAL, name VARCHAR(64), "
+cur.execute("CREATE TABLE port (port_id SERIAL, name VARCHAR(64),"
             "switch_id INTEGER, is_locked BOOLEAN,"
             "is_trunk BOOLEAN, base_vlan_id INTEGER,"
-            "current_vlan_id INTEGER)")
+            "current_vlan_id INTEGER, number INTEGER)")
 cur.execute("CREATE TABLE vlan (vlan_id SERIAL, name VARCHAR(32),"
             "tag INTEGER, is_base_vlan BOOLEAN, creation_time TIMESTAMP)")
 
diff --git a/util.py b/util.py
index d1b2a51..45f82a3 100644
--- a/util.py
+++ b/util.py
@@ -130,6 +130,7 @@
                 ret = db.create_switch(data['name'])
             elif command == 'db.create_port':
                 ret = db.create_port(data['switch_id'], data['name'],
+                                     int(data['number']),
                                      state.default_vlan_id,
                                      state.default_vlan_id)
             elif command == 'db.delete_switch':
@@ -722,6 +723,7 @@
             logging.debug('  trying to import port %s', port_name)
             port_id = None
             port_mode = s.port_get_mode(port_name)
+            port_number = s.port_map_name_to_number(port_name)
             if port_mode == 'access':
                 # Access ports are easy - just create the port, and
                 # set both the current and base VLANs to the current
@@ -729,7 +731,7 @@
                 # import if needed.
                 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_id = db.create_port(switch_id, port_name, port_number,
                                          port_vlan_id, port_vlan_id)
                 logging.debug('    access port, VLAN %d', int(port_vlans[port_name][0]))
                 # Nothing further needed
@@ -744,7 +746,7 @@
                 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_id = db.create_port(switch_id, port_name, port_number,
                                          port_vlan_id, port_vlan_id)
                 # Append to a list of trunk ports that we will need to
                 # modify once we're done
@@ -757,7 +759,7 @@
                 s.port_set_mode(port_name, 'access')
                 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_id = db.create_port(switch_id, port_name, port_number,
                                          port_vlan_id, port_vlan_id)
                 logging.debug('    Found port in %s mode', port_mode)
                 logging.debug('    Forcing to access mode, VLAN %d', int(port_vlans[port_name][0]))