Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 3 | # Copyright 2014-2018 Linaro Limited |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 4 | # Authors: Dave Pigott <dave.pigott@linaro.org>, |
| 5 | # Steve McIntyre <steve.mcintyre@linaro.org> |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program; if not, write to the Free Software |
| 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 20 | # MA 02110-1301, USA. |
| 21 | |
| 22 | import psycopg2 |
| 23 | import psycopg2.extras |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame] | 24 | import datetime, os, sys |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 25 | import logging |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame] | 26 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 27 | TRUNK_ID_NONE = -1 |
| 28 | |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 29 | # The schema version that this code expects. If it finds an older version (or |
| 30 | # no version!) at startup, it will auto-migrate to the latest version |
| 31 | # |
| 32 | # Version 0: Base, no version found |
| 33 | # Version 1: No changes, except adding the version and coping with upgrade |
| 34 | DATABASE_SCHEMA_VERSION = 1 |
| 35 | |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame] | 36 | if __name__ == '__main__': |
| 37 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 38 | sys.path.insert(0, vlandpath) |
| 39 | sys.path.insert(0, "%s/.." % vlandpath) |
| 40 | |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 41 | from errors import CriticalError, InputError, NotFoundError |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 42 | |
| 43 | class VlanDB: |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 44 | def __init__(self, db_name="vland", username="vland", readonly=True): |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 45 | try: |
Steve McIntyre | e38f622 | 2014-11-27 15:09:49 +0000 | [diff] [blame] | 46 | self.connection = psycopg2.connect(database=db_name, user=username) |
Steve McIntyre | b09ed28 | 2014-12-02 17:59:35 +0000 | [diff] [blame] | 47 | self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 48 | if not readonly: |
| 49 | self._init_state() |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 50 | except Exception as e: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 51 | logging.error("Failed to access database: %s", e) |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 52 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 53 | |
| 54 | def __del__(self): |
| 55 | self.cursor.close() |
| 56 | self.connection.close() |
| 57 | |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 58 | # Create the state table (if needed) and add its only record |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 59 | # |
| 60 | # Use the stored record of the expected database schema to track what |
| 61 | # version the on-disk database is, and upgrade it to match the current code |
| 62 | # if necessary. |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 63 | def _init_state(self): |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 64 | found_db = False |
| 65 | current_db_version = 0 |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 66 | try: |
| 67 | sql = "SELECT * FROM state" |
| 68 | self.cursor.execute(sql) |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 69 | found_db = True |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 70 | except psycopg2.ProgrammingError: |
| 71 | self.connection.commit() # state doesn't exist; clear error |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 72 | sql = "CREATE TABLE state (last_modified TIMESTAMP, schema_version INTEGER)" |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 73 | self.cursor.execute(sql) |
Steve McIntyre | 8fed491 | 2018-01-31 17:21:31 +0000 | [diff] [blame^] | 74 | # We've just created a version 1 database |
| 75 | current_db_version = 1 |
| 76 | |
| 77 | if found_db: |
| 78 | # Grab the version of the database we have |
| 79 | try: |
| 80 | sql = "SELECT schema_version FROM state" |
| 81 | self.cursor.execute(sql) |
| 82 | current_db_version = self.cursor.fetchone()[0] |
| 83 | # No version found ==> we have "version 0" |
| 84 | except psycopg2.ProgrammingError: |
| 85 | self.connection.commit() # state doesn't exist; clear error |
| 86 | current_db_version = 0 |
| 87 | |
| 88 | # Now delete the existing state record, we'll write a new one in a |
| 89 | # moment |
| 90 | self.cursor.execute('DELETE FROM state') |
| 91 | logging.info("Found a database, version %d", current_db_version) |
| 92 | |
| 93 | # Apply upgrades here! |
| 94 | if current_db_version < 1: |
| 95 | logging.info("Upgrading database to match schema version 1") |
| 96 | sql = "ALTER TABLE state ADD schema_version INTEGER" |
| 97 | self.cursor.execute(sql) |
| 98 | logging.info("Sschema version 1 upgrade successful") |
| 99 | |
| 100 | sql = "INSERT INTO state (last_modified, schema_version) VALUES (%s, %s)" |
| 101 | data = (datetime.datetime.now(), DATABASE_SCHEMA_VERSION) |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 102 | self.cursor.execute(sql, data) |
| 103 | self.connection.commit() |
| 104 | |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 105 | # Create a new switch in the database. Switches are really simple |
| 106 | # devices - they're just containers for ports. |
| 107 | # |
| 108 | # Constraints: |
| 109 | # Switches must be uniquely named |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 110 | def create_switch(self, name): |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 111 | |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 112 | switch_id = self.get_switch_id_by_name(name) |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 113 | if switch_id is not None: |
| 114 | raise InputError("Switch name %s already exists" % name) |
| 115 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 116 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 117 | sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id" |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 118 | data = (name, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 119 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 120 | switch_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 121 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 122 | self.connection.commit() |
| 123 | except: |
| 124 | self.connection.rollback() |
| 125 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 126 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 127 | return switch_id |
| 128 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 129 | # Create a new port in the database. Three of the fields are |
| 130 | # created with default values (is_locked, is_trunk, trunk_id) |
| 131 | # here, and should be updated separately if desired. For the |
| 132 | # current_vlan_id and base_vlan_id fields, *BE CAREFUL* that you |
| 133 | # have already looked up the correct VLAN_ID for each. This is |
| 134 | # *NOT* the same as the VLAN tag (likely to be 1). You Have Been |
| 135 | # Warned! |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 136 | # |
| 137 | # Constraints: |
| 138 | # 1. The switch referred to must already exist |
| 139 | # 2. The VLANs mentioned here must already exist |
Steve McIntyre | 6a7fdb2 | 2014-12-05 15:17:30 +0000 | [diff] [blame] | 140 | # 3. (Switch/name) must be unique |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 141 | # 4. (Switch/number) must be unique |
| 142 | def create_port(self, switch_id, name, number, current_vlan_id, base_vlan_id): |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 143 | |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 144 | switch = self.get_switch_by_id(switch_id) |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 145 | if switch is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 146 | raise NotFoundError("Switch ID %d does not exist" % int(switch_id)) |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 147 | |
| 148 | for vlan_id in (current_vlan_id, base_vlan_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 149 | vlan = self.get_vlan_by_id(vlan_id) |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 150 | if vlan is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 151 | raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id)) |
Steve McIntyre | 6a7fdb2 | 2014-12-05 15:17:30 +0000 | [diff] [blame] | 152 | |
| 153 | port_id = self.get_port_by_switch_and_name(switch_id, name) |
| 154 | if port_id is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 155 | raise InputError("Already have a port %s on switch ID %d" % (name, int(switch_id))) |
Steve McIntyre | 6a7fdb2 | 2014-12-05 15:17:30 +0000 | [diff] [blame] | 156 | |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 157 | port_id = self.get_port_by_switch_and_number(switch_id, int(number)) |
| 158 | if port_id is not None: |
| 159 | raise InputError("Already have a port %d on switch ID %d" % (int(number), int(switch_id))) |
| 160 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 161 | try: |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 162 | sql = "INSERT INTO port (name, number, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id, trunk_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING port_id" |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 163 | data = (name, number, switch_id, |
Steve McIntyre | 90a4a97 | 2014-11-28 16:50:56 +0000 | [diff] [blame] | 164 | False, False, |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 165 | current_vlan_id, base_vlan_id, TRUNK_ID_NONE) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 166 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 167 | port_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 168 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 169 | self.connection.commit() |
| 170 | except: |
| 171 | self.connection.rollback() |
| 172 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 173 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 174 | return port_id |
| 175 | |
Steve McIntyre | b005a2f | 2014-11-28 18:23:05 +0000 | [diff] [blame] | 176 | # Create a new vlan in the database. We locally add a creation |
| 177 | # timestamp, for debug purposes. If vlans seems to be sticking |
| 178 | # around, we'll be able to see when they were created. |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 179 | # |
| 180 | # Constraints: |
| 181 | # Names and tags must be unique |
Steve McIntyre | 57f4591 | 2014-12-08 14:43:00 +0000 | [diff] [blame] | 182 | # Tags must be in the range 1-4095 (802.1q spec) |
Steve McIntyre | 49777e7 | 2014-12-08 16:00:46 +0000 | [diff] [blame] | 183 | # Names can be any free-form text, length 1-32 characters |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 184 | def create_vlan(self, name, tag, is_base_vlan): |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 185 | |
Steve McIntyre | 57f4591 | 2014-12-08 14:43:00 +0000 | [diff] [blame] | 186 | if int(tag) < 1 or int(tag) > 4095: |
Steve McIntyre | 49777e7 | 2014-12-08 16:00:46 +0000 | [diff] [blame] | 187 | raise InputError("VLAN tag %d is outside of the valid range (1-4095)" % int(tag)) |
| 188 | |
| 189 | if (len(name) < 1) or (len(name) > 32): |
| 190 | raise InputError("VLAN name %s is invalid (must be 1-32 chars)" % name) |
Steve McIntyre | 57f4591 | 2014-12-08 14:43:00 +0000 | [diff] [blame] | 191 | |
Steve McIntyre | a34c181 | 2014-12-05 15:27:55 +0000 | [diff] [blame] | 192 | vlan_id = self.get_vlan_id_by_name(name) |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 193 | if vlan_id is not None: |
| 194 | raise InputError("VLAN name %s is already in use" % name) |
| 195 | |
Steve McIntyre | 50eb060 | 2014-12-05 15:29:04 +0000 | [diff] [blame] | 196 | vlan_id = self.get_vlan_id_by_tag(tag) |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 197 | if vlan_id is not None: |
| 198 | raise InputError("VLAN tag %d is already in use" % int(tag)) |
| 199 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 200 | try: |
Steve McIntyre | d74d97c | 2014-11-28 14:44:39 +0000 | [diff] [blame] | 201 | dt = datetime.datetime.now() |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 202 | sql = "INSERT INTO vlan (name, tag, is_base_vlan, creation_time) VALUES (%s, %s, %s, %s) RETURNING vlan_id" |
| 203 | data = (name, tag, is_base_vlan, dt) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 204 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 205 | vlan_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 206 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 207 | self.connection.commit() |
| 208 | except: |
| 209 | self.connection.rollback() |
| 210 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 211 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 212 | return vlan_id |
| 213 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 214 | # Create a new trunk in the database, linking two ports. Trunks |
| 215 | # are really simple objects for our use - they're just containers |
| 216 | # for 2 ports. |
| 217 | # |
| 218 | # Constraints: |
| 219 | # 1. Both ports listed must already exist. |
| 220 | # 2. Both ports must be in trunk mode. |
| 221 | # 3. Both must not be locked. |
| 222 | # 4. Both must not already be in a trunk. |
| 223 | def create_trunk(self, port_id1, port_id2): |
| 224 | |
| 225 | for port_id in (port_id1, port_id2): |
| 226 | port = self.get_port_by_id(int(port_id)) |
| 227 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 228 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 229 | if not port.is_trunk: |
| 230 | raise InputError("Port ID %d is not in trunk mode" % int(port_id)) |
| 231 | if port.is_locked: |
| 232 | raise InputError("Port ID %d is locked" % int(port_id)) |
| 233 | if port.trunk_id != TRUNK_ID_NONE: |
| 234 | raise InputError("Port ID %d is already on trunk ID %d" % (int(port_id), int(port.trunk_id))) |
| 235 | |
| 236 | try: |
| 237 | # Add the trunk itself |
| 238 | dt = datetime.datetime.now() |
| 239 | sql = "INSERT INTO trunk (creation_time) VALUES (%s) RETURNING trunk_id" |
| 240 | data = (dt, ) |
| 241 | self.cursor.execute(sql, data) |
| 242 | trunk_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 243 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 244 | self.connection.commit() |
| 245 | # And update the ports |
| 246 | for port_id in (port_id1, port_id2): |
| 247 | self._set_port_trunk(port_id, trunk_id) |
| 248 | except: |
| 249 | self.delete_trunk(trunk_id) |
| 250 | raise |
| 251 | |
| 252 | return trunk_id |
| 253 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 254 | # Internal helper function |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 255 | def _delete_row(self, table, field, value): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 256 | try: |
Steve McIntyre | e03de00 | 2014-12-02 17:14:14 +0000 | [diff] [blame] | 257 | sql = "DELETE FROM %s WHERE %s = %s" % (table, field, '%s') |
| 258 | data = (value,) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 259 | self.cursor.execute(sql, data) |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 260 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 261 | self.connection.commit() |
| 262 | except: |
| 263 | self.connection.rollback() |
| 264 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 265 | |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 266 | # Delete the specified switch |
| 267 | # |
| 268 | # Constraints: |
| 269 | # 1. The switch must exist |
| 270 | # 2. The switch may not be referenced by any ports - |
| 271 | # delete them first! |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 272 | def delete_switch(self, switch_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 273 | switch = self.get_switch_by_id(switch_id) |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 274 | if switch is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 275 | raise NotFoundError("Switch ID %d does not exist" % int(switch_id)) |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 276 | ports = self.get_ports_by_switch(switch_id) |
| 277 | if ports is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 278 | raise InputError("Cannot delete switch ID %d when it still has %d ports" % |
| 279 | (int(switch_id), len(ports))) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 280 | self._delete_row("switch", "switch_id", switch_id) |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 281 | return switch_id |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 282 | |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 283 | # Delete the specified port |
| 284 | # |
| 285 | # Constraints: |
| 286 | # 1. The port must exist |
| 287 | # 2. The port must not be locked |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 288 | def delete_port(self, port_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 289 | port = self.get_port_by_id(port_id) |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 290 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 291 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 292 | if port.is_locked: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 293 | raise InputError("Cannot delete port ID %d as it is locked" % int(port_id)) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 294 | self._delete_row("port", "port_id", port_id) |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 295 | return port_id |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 296 | |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 297 | # Delete the specified VLAN |
| 298 | # |
| 299 | # Constraints: |
Steve McIntyre | 2a5df97 | 2015-08-07 15:19:40 +0100 | [diff] [blame] | 300 | # 1. The VLAN must exist |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 301 | # 2. The VLAN may not contain any ports - move or delete them first! |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 302 | def delete_vlan(self, vlan_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 303 | vlan = self.get_vlan_by_id(vlan_id) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 304 | if vlan is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 305 | raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id)) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 306 | ports = self.get_ports_by_current_vlan(vlan_id) |
| 307 | if ports is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 308 | raise InputError("Cannot delete VLAN ID %d when it still has %d ports" % |
| 309 | (int(vlan_id), len(ports))) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 310 | ports = self.get_ports_by_base_vlan(vlan_id) |
| 311 | if ports is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 312 | raise InputError("Cannot delete VLAN ID %d when it still has %d ports" % |
| 313 | (int(vlan_id), len(ports))) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 314 | self._delete_row("vlan", "vlan_id", vlan_id) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 315 | return vlan_id |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 316 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 317 | # Delete the specified trunk |
| 318 | # |
| 319 | # Constraints: |
| 320 | # 1. The trunk must exist |
| 321 | # |
| 322 | # Any ports attached will be detached (i.e. moved to trunk TRUNK_ID_NONE) |
| 323 | def delete_trunk(self, trunk_id): |
| 324 | trunk = self.get_trunk_by_id(trunk_id) |
| 325 | if trunk is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 326 | raise NotFoundError("Trunk ID %d does not exist" % int(trunk_id)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 327 | ports = self.get_ports_by_trunk(trunk_id) |
| 328 | for port_id in ports: |
| 329 | self._set_port_trunk(port_id, TRUNK_ID_NONE) |
| 330 | self._delete_row("trunk", "trunk_id", trunk_id) |
| 331 | return trunk_id |
| 332 | |
Steve McIntyre | 6c4f33f | 2015-08-03 19:25:07 +0100 | [diff] [blame] | 333 | # Find the lowest unused VLAN tag and return it |
| 334 | # |
| 335 | # Constraints: |
| 336 | # None |
| 337 | def find_lowest_unused_vlan_tag(self): |
| 338 | sql = "SELECT tag FROM vlan ORDER BY tag ASC" |
| 339 | self.cursor.execute(sql,) |
| 340 | |
| 341 | # Walk through the list, looking for gaps |
| 342 | last = 1 |
| 343 | result = None |
| 344 | |
| 345 | for record in self.cursor: |
| 346 | if (record[0] - last) > 1: |
| 347 | result = last + 1 |
| 348 | break |
| 349 | last = record[0] |
| 350 | |
| 351 | if result is None: |
| 352 | result = last + 1 |
| 353 | |
| 354 | if result > 4093: |
| 355 | raise CriticalError("Can't find any VLAN tags remaining for allocation!") |
| 356 | |
| 357 | return result |
| 358 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 359 | # Grab one column from one row of a query on one column; useful as |
| 360 | # a quick wrapper |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 361 | def _get_element(self, select_field, table, compare_field, value): |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 362 | |
Steve McIntyre | dc17307 | 2016-08-25 18:29:10 +0100 | [diff] [blame] | 363 | if value is None: |
| 364 | raise ValueError("Asked to look up using None as a key in %s" % compare_field) |
| 365 | |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 366 | # We really want to use psycopg's type handling deal with the |
| 367 | # (potentially) user-supplied data in the value field, so we |
| 368 | # have to pass (sql,data) through to cursor.execute. However, |
| 369 | # we can't have psycopg do all the argument substitution here |
| 370 | # as it will quote all the params like the table name. That |
| 371 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 372 | # keep it after python's own string substitution. |
| 373 | sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s") |
| 374 | |
| 375 | # Now, the next icky thing: we need to make sure that we're |
| 376 | # passing a dict so that psycopg2 can pick it apart properly |
| 377 | # for its own substitution code. We force this with the |
| 378 | # trailing comma here |
| 379 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 380 | self.cursor.execute(sql, data) |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 381 | |
Steve McIntyre | 58b57a4 | 2014-12-02 13:09:21 +0000 | [diff] [blame] | 382 | if self.cursor.rowcount > 0: |
| 383 | return self.cursor.fetchone()[0] |
| 384 | else: |
Steve McIntyre | c831f9c | 2014-12-02 12:38:54 +0000 | [diff] [blame] | 385 | return None |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 386 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 387 | # Grab one column from one row of a query on 2 columns; useful as |
| 388 | # a quick wrapper |
Steve McIntyre | a74c7fe | 2014-12-02 18:49:38 +0000 | [diff] [blame] | 389 | def _get_element2(self, select_field, table, compare_field1, value1, compare_field2, value2): |
| 390 | |
Steve McIntyre | dc17307 | 2016-08-25 18:29:10 +0100 | [diff] [blame] | 391 | if value1 is None: |
| 392 | raise ValueError("Asked to look up using None as a key in %s" % compare_field1) |
| 393 | if value2 is None: |
| 394 | raise ValueError("Asked to look up using None as a key in %s" % compare_field2) |
| 395 | |
Steve McIntyre | a74c7fe | 2014-12-02 18:49:38 +0000 | [diff] [blame] | 396 | # We really want to use psycopg's type handling deal with the |
| 397 | # (potentially) user-supplied data in the value field, so we |
| 398 | # have to pass (sql,data) through to cursor.execute. However, |
| 399 | # we can't have psycopg do all the argument substitution here |
| 400 | # as it will quote all the params like the table name. That |
| 401 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 402 | # keep it after python's own string substitution. |
| 403 | sql = "SELECT %s FROM %s WHERE %s = %s AND %s = %s" % (select_field, table, compare_field1, "%s", compare_field2, "%s") |
| 404 | |
Steve McIntyre | a74c7fe | 2014-12-02 18:49:38 +0000 | [diff] [blame] | 405 | data = (value1, value2) |
| 406 | self.cursor.execute(sql, data) |
| 407 | |
| 408 | if self.cursor.rowcount > 0: |
| 409 | return self.cursor.fetchone()[0] |
| 410 | else: |
| 411 | return None |
| 412 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 413 | # Grab one column from multiple rows of a query; useful as a quick |
| 414 | # wrapper |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 415 | def _get_multi_elements(self, select_field, table, compare_field, value, sort_field): |
Steve McIntyre | e9da15e | 2014-12-05 15:22:41 +0000 | [diff] [blame] | 416 | |
Steve McIntyre | dc17307 | 2016-08-25 18:29:10 +0100 | [diff] [blame] | 417 | if value is None: |
| 418 | raise ValueError("Asked to look up using None as a key in %s" % compare_field) |
| 419 | |
Steve McIntyre | e9da15e | 2014-12-05 15:22:41 +0000 | [diff] [blame] | 420 | # We really want to use psycopg's type handling deal with the |
| 421 | # (potentially) user-supplied data in the value field, so we |
| 422 | # have to pass (sql,data) through to cursor.execute. However, |
| 423 | # we can't have psycopg do all the argument substitution here |
| 424 | # as it will quote all the params like the table name. That |
| 425 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 426 | # keep it after python's own string substitution. |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 427 | sql = "SELECT %s FROM %s WHERE %s = %s ORDER BY %s ASC" % (select_field, table, compare_field, "%s", sort_field) |
Steve McIntyre | e9da15e | 2014-12-05 15:22:41 +0000 | [diff] [blame] | 428 | |
| 429 | # Now, the next icky thing: we need to make sure that we're |
| 430 | # passing a dict so that psycopg2 can pick it apart properly |
| 431 | # for its own substitution code. We force this with the |
| 432 | # trailing comma here |
| 433 | data = (value, ) |
| 434 | self.cursor.execute(sql, data) |
| 435 | |
| 436 | if self.cursor.rowcount > 0: |
| 437 | results = [] |
| 438 | for record in self.cursor: |
Steve McIntyre | 5250962 | 2014-12-02 17:13:15 +0000 | [diff] [blame] | 439 | results.append(record[0]) |
Steve McIntyre | e9da15e | 2014-12-05 15:22:41 +0000 | [diff] [blame] | 440 | return results |
Steve McIntyre | 5250962 | 2014-12-02 17:13:15 +0000 | [diff] [blame] | 441 | else: |
| 442 | return None |
| 443 | |
Steve McIntyre | 7201c9b | 2014-12-17 17:33:51 +0000 | [diff] [blame] | 444 | # Grab one column from multiple rows of a 2-part query; useful as |
| 445 | # a wrapper |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 446 | def _get_multi_elements2(self, select_field, table, compare_field1, value1, compare_field2, value2, sort_field): |
Steve McIntyre | 7201c9b | 2014-12-17 17:33:51 +0000 | [diff] [blame] | 447 | |
Steve McIntyre | dc17307 | 2016-08-25 18:29:10 +0100 | [diff] [blame] | 448 | if value1 is None: |
| 449 | raise ValueError("Asked to look up using None as a key in %s" % compare_field1) |
| 450 | if value2 is None: |
| 451 | raise ValueError("Asked to look up using None as a key in %s" % compare_field2) |
| 452 | |
Steve McIntyre | 7201c9b | 2014-12-17 17:33:51 +0000 | [diff] [blame] | 453 | # We really want to use psycopg's type handling deal with the |
| 454 | # (potentially) user-supplied data in the value field, so we |
| 455 | # have to pass (sql,data) through to cursor.execute. However, |
| 456 | # we can't have psycopg do all the argument substitution here |
| 457 | # as it will quote all the params like the table name. That |
| 458 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 459 | # keep it after python's own string substitution. |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 460 | sql = "SELECT %s FROM %s WHERE %s = %s AND %s = %s ORDER by %s ASC" % (select_field, table, compare_field1, "%s", compare_field2, "%s", sort_field) |
Steve McIntyre | 7201c9b | 2014-12-17 17:33:51 +0000 | [diff] [blame] | 461 | |
| 462 | data = (value1, value2) |
| 463 | self.cursor.execute(sql, data) |
| 464 | |
| 465 | if self.cursor.rowcount > 0: |
| 466 | results = [] |
| 467 | for record in self.cursor: |
| 468 | results.append(record[0]) |
| 469 | return results |
| 470 | else: |
| 471 | return None |
| 472 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 473 | # Simple lookup: look up a switch by ID, and return all the |
| 474 | # details of that switch. |
| 475 | # |
| 476 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 477 | def get_switch_by_id(self, switch_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 478 | return self._get_row("switch", "switch_id", int(switch_id)) |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 479 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 480 | # Simple lookup: look up a switch by name, and return the ID of |
| 481 | # that switch. |
| 482 | # |
| 483 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 484 | def get_switch_id_by_name(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 485 | return self._get_element("switch_id", "switch", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 486 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 487 | # Simple lookup: look up a switch by ID, and return the name of |
| 488 | # that switch. |
| 489 | # |
| 490 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 491 | def get_switch_name_by_id(self, switch_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 492 | return self._get_element("name", "switch", "switch_id", int(switch_id)) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 493 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 494 | # Simple lookup: look up a port by ID, and return all the details |
| 495 | # of that port. |
| 496 | # |
| 497 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 498 | def get_port_by_id(self, port_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 499 | return self._get_row("port", "port_id", int(port_id)) |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 500 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 501 | # Simple lookup: look up a switch by ID, and return the IDs of all |
| 502 | # the ports on that switch. |
| 503 | # |
| 504 | # Returns None on failure. |
Steve McIntyre | b67f391 | 2014-12-02 17:14:36 +0000 | [diff] [blame] | 505 | def get_ports_by_switch(self, switch_id): |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 506 | return self._get_multi_elements("port_id", "port", "switch_id", int(switch_id), "port_id") |
Steve McIntyre | b67f391 | 2014-12-02 17:14:36 +0000 | [diff] [blame] | 507 | |
Steve McIntyre | 7201c9b | 2014-12-17 17:33:51 +0000 | [diff] [blame] | 508 | # More complex lookup: look up all the trunk ports on a switch by |
| 509 | # ID |
| 510 | # |
| 511 | # Returns None on failure. |
| 512 | def get_trunk_port_names_by_switch(self, switch_id): |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 513 | return self._get_multi_elements2("name", "port", "switch_id", int(switch_id), "is_trunk", True, "port_id") |
Steve McIntyre | 7201c9b | 2014-12-17 17:33:51 +0000 | [diff] [blame] | 514 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 515 | # Simple lookup: look up a port by its name and its parent switch |
| 516 | # by ID, and return the ID of the port. |
| 517 | # |
| 518 | # Returns None on failure. |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 519 | def get_port_by_switch_and_name(self, switch_id, name): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 520 | return self._get_element2("port_id", "port", "switch_id", int(switch_id), "name", name) |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 521 | |
Steve McIntyre | 45f5501 | 2015-08-05 13:55:15 +0100 | [diff] [blame] | 522 | # Simple lookup: look up a port by its external name and its |
| 523 | # parent switch by ID, and return the ID of the port. |
| 524 | # |
| 525 | # Returns None on failure. |
| 526 | def get_port_by_switch_and_number(self, switch_id, number): |
| 527 | return self._get_element2("port_id", "port", "switch_id", int(switch_id), "number", int(number)) |
| 528 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 529 | # Simple lookup: look up a port by ID, and return the current VLAN |
| 530 | # id of that port. |
| 531 | # |
| 532 | # Returns None on failure. |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 533 | def get_current_vlan_id_by_port(self, port_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 534 | return self._get_element("current_vlan_id", "port", "port_id", int(port_id)) |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 535 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 536 | # Simple lookup: look up a port by ID, and return the base VLAN |
| 537 | # id of that port. |
| 538 | # |
| 539 | # Returns None on failure. |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 540 | def get_base_vlan_id_by_port(self, port_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 541 | return self._get_element("base_vlan_id", "port", "port_id", int(port_id)) |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 542 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 543 | # Simple lookup: look up a current VLAN by ID, and return the IDs |
| 544 | # of all the ports on that VLAN. |
| 545 | # |
| 546 | # Returns None on failure. |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 547 | def get_ports_by_current_vlan(self, vlan_id): |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 548 | return self._get_multi_elements("port_id", "port", "current_vlan_id", int(vlan_id), "port_id") |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 549 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 550 | # Simple lookup: look up a base VLAN by ID, and return the IDs |
| 551 | # of all the ports on that VLAN. |
| 552 | # |
| 553 | # Returns None on failure. |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 554 | def get_ports_by_base_vlan(self, vlan_id): |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 555 | return self._get_multi_elements("port_id", "port", "base_vlan_id", int(vlan_id), "port_id") |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 556 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 557 | # Simple lookup: look up a trunk by ID, and return the IDs of the |
| 558 | # ports on both ends of that trunk. |
| 559 | # |
| 560 | # Returns None on failure. |
| 561 | def get_ports_by_trunk(self, trunk_id): |
Steve McIntyre | 05e3e62 | 2015-09-25 01:29:18 +0100 | [diff] [blame] | 562 | return self._get_multi_elements("port_id", "port", "trunk_id", int(trunk_id), "port_id") |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 563 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 564 | # Simple lookup: look up a VLAN by ID, and return all the details |
| 565 | # of that VLAN. |
| 566 | # |
| 567 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 568 | def get_vlan_by_id(self, vlan_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 569 | return self._get_row("vlan", "vlan_id", int(vlan_id)) |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 570 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 571 | # Simple lookup: look up a VLAN by name, and return the ID of that |
| 572 | # VLAN. |
| 573 | # |
| 574 | # Returns None on failure. |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 575 | def get_vlan_id_by_name(self, name): |
| 576 | return self._get_element("vlan_id", "vlan", "name", name) |
| 577 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 578 | # Simple lookup: look up a VLAN by tag, and return the ID of that |
| 579 | # VLAN. |
| 580 | # |
| 581 | # Returns None on failure. |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 582 | def get_vlan_id_by_tag(self, tag): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 583 | return self._get_element("vlan_id", "vlan", "tag", int(tag)) |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 584 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 585 | # Simple lookup: look up a VLAN by ID, and return the name of that |
| 586 | # VLAN. |
| 587 | # |
| 588 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 589 | def get_vlan_name_by_id(self, vlan_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 590 | return self._get_element("name", "vlan", "vlan_id", int(vlan_id)) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 591 | |
Steve McIntyre | b9b0aa5 | 2014-12-21 23:31:12 +0000 | [diff] [blame] | 592 | # Simple lookup: look up a VLAN by ID, and return the tag of that |
| 593 | # VLAN. |
| 594 | # |
| 595 | # Returns None on failure. |
| 596 | def get_vlan_tag_by_id(self, vlan_id): |
Steve McIntyre | 32e3a89 | 2015-01-23 17:47:46 +0000 | [diff] [blame] | 597 | return self._get_element("tag", "vlan", "vlan_id", int(vlan_id)) |
Steve McIntyre | b9b0aa5 | 2014-12-21 23:31:12 +0000 | [diff] [blame] | 598 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 599 | # Simple lookup: look up a trunk by ID, and return all the details |
| 600 | # of that trunk. |
| 601 | # |
| 602 | # Returns None on failure. |
| 603 | def get_trunk_by_id(self, trunk_id): |
| 604 | return self._get_row("trunk", "trunk_id", int(trunk_id)) |
| 605 | |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 606 | # Get the last-modified time for the database |
| 607 | def get_last_modified_time(self): |
| 608 | sql = "SELECT last_modified FROM state" |
| 609 | self.cursor.execute(sql) |
Steve McIntyre | af24aaa | 2015-10-23 17:59:04 +0100 | [diff] [blame] | 610 | return self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 611 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 612 | # Grab one row of a query on one column; useful as a quick wrapper |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 613 | def _get_row(self, table, field, value): |
Steve McIntyre | e0b842a | 2014-11-28 18:23:47 +0000 | [diff] [blame] | 614 | |
| 615 | # We really want to use psycopg's type handling deal with the |
| 616 | # (potentially) user-supplied data in the value field, so we |
| 617 | # have to pass (sql,data) through to cursor.execute. However, |
| 618 | # we can't have psycopg do all the argument substitution here |
| 619 | # as it will quote all the params like the table name. That |
| 620 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 621 | # keep it after python's own string substitution. |
| 622 | sql = "SELECT * FROM %s WHERE %s = %s" % (table, field, "%s") |
| 623 | |
| 624 | # Now, the next icky thing: we need to make sure that we're |
| 625 | # passing a dict so that psycopg2 can pick it apart properly |
| 626 | # for its own substitution code. We force this with the |
| 627 | # trailing comma here |
| 628 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 629 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 630 | return self.cursor.fetchone() |
| 631 | |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 632 | # (Un)Lock a port in the database. This can only be done through |
| 633 | # the admin interface, and will stop API users from modifying |
| 634 | # settings on the port. Use this to lock down ports that are used |
| 635 | # for PDUs and other core infrastructure |
| 636 | def set_port_is_locked(self, port_id, is_locked): |
Steve McIntyre | 8c64d95 | 2014-12-05 16:22:44 +0000 | [diff] [blame] | 637 | port = self.get_port_by_id(port_id) |
| 638 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 639 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 640 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 641 | sql = "UPDATE port SET is_locked=%s WHERE port_id=%s RETURNING port_id" |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 642 | data = (is_locked, port_id) |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 643 | self.cursor.execute(sql, data) |
| 644 | port_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 645 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 646 | self.connection.commit() |
| 647 | except: |
| 648 | self.connection.rollback() |
| 649 | raise |
Steve McIntyre | 1c8a321 | 2015-07-14 17:07:31 +0100 | [diff] [blame] | 650 | return port_id |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 651 | |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 652 | # Set the mode of a port in the database. Valid values for mode |
| 653 | # are "trunk" and "access" |
| 654 | def set_port_mode(self, port_id, mode): |
| 655 | port = self.get_port_by_id(port_id) |
| 656 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 657 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 658 | if mode == "access": |
| 659 | is_trunk = False |
| 660 | elif mode == "trunk": |
| 661 | is_trunk = True |
| 662 | else: |
| 663 | raise InputError("Port mode %s is not valid" % mode) |
| 664 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 665 | sql = "UPDATE port SET is_trunk=%s WHERE port_id=%s RETURNING port_id" |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 666 | data = (is_trunk, port_id) |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 667 | self.cursor.execute(sql, data) |
| 668 | port_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 669 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 670 | self.connection.commit() |
| 671 | except: |
| 672 | self.connection.rollback() |
| 673 | raise |
| 674 | return port_id |
| 675 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 676 | # Set the current vlan of a port in the database. The VLAN is |
| 677 | # passed by ID. |
| 678 | # |
| 679 | # Constraints: |
| 680 | # 1. The port must already exist |
| 681 | # 2. The port must not be a trunk port |
| 682 | # 3. The port must not be locked |
| 683 | # 1. The VLAN must already exist in the database |
Steve McIntyre | 9eb7865 | 2014-12-05 17:51:53 +0000 | [diff] [blame] | 684 | def set_current_vlan(self, port_id, vlan_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 685 | port = self.get_port_by_id(port_id) |
Steve McIntyre | 028b3cc | 2014-12-05 16:24:46 +0000 | [diff] [blame] | 686 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 687 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 688 | |
Steve McIntyre | 6dd00be | 2014-12-05 17:29:35 +0000 | [diff] [blame] | 689 | if port.is_trunk or port.is_locked: |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 690 | raise CriticalError("The port is locked") |
| 691 | |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 692 | vlan = self.get_vlan_by_id(vlan_id) |
Steve McIntyre | 028b3cc | 2014-12-05 16:24:46 +0000 | [diff] [blame] | 693 | if vlan is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 694 | raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id)) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 695 | |
| 696 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 697 | sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s RETURNING port_id" |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 698 | data = (vlan_id, port_id) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 699 | self.cursor.execute(sql, data) |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 700 | port_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 701 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 702 | self.connection.commit() |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 703 | except: |
| 704 | self.connection.rollback() |
| 705 | raise |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 706 | return port_id |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 707 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 708 | # Set the base vlan of a port in the database. The VLAN is |
| 709 | # passed by ID. |
| 710 | # |
| 711 | # Constraints: |
| 712 | # 1. The port must already exist |
| 713 | # 2. The port must not be a trunk port |
| 714 | # 3. The port must not be locked |
Steve McIntyre | e653d17 | 2015-08-06 16:51:18 +0100 | [diff] [blame] | 715 | # 4. The VLAN must already exist in the database |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 716 | def set_base_vlan(self, port_id, vlan_id): |
| 717 | port = self.get_port_by_id(port_id) |
| 718 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 719 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 720 | |
| 721 | if port.is_trunk or port.is_locked: |
| 722 | raise CriticalError("The port is locked") |
| 723 | |
| 724 | vlan = self.get_vlan_by_id(vlan_id) |
| 725 | if vlan is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 726 | raise NotFoundError("VLAN ID %d does not exist" % int(vlan_id)) |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 727 | if not vlan.is_base_vlan: |
| 728 | raise InputError("VLAN ID %d is not a base VLAN" % int(vlan_id)) |
| 729 | |
| 730 | try: |
| 731 | sql = "UPDATE port SET base_vlan_id=%s WHERE port_id=%s RETURNING port_id" |
| 732 | data = (vlan_id, port_id) |
| 733 | self.cursor.execute(sql, data) |
| 734 | port_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 735 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 736 | self.connection.commit() |
| 737 | except: |
| 738 | self.connection.rollback() |
| 739 | raise |
| 740 | return port_id |
| 741 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 742 | # Internal function: Attach a port to a trunk in the database. |
| 743 | # |
| 744 | # Constraints: |
| 745 | # 1. The port must already exist |
| 746 | # 2. The port must not be locked |
| 747 | def _set_port_trunk(self, port_id, trunk_id): |
| 748 | port = self.get_port_by_id(port_id) |
| 749 | if port is None: |
Steve McIntyre | b01959f | 2016-03-22 17:02:39 +0000 | [diff] [blame] | 750 | raise NotFoundError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 751 | if port.is_locked: |
| 752 | raise CriticalError("The port is locked") |
| 753 | try: |
| 754 | sql = "UPDATE port SET trunk_id=%s WHERE port_id=%s RETURNING port_id" |
| 755 | data = (int(trunk_id), int(port_id)) |
| 756 | self.cursor.execute(sql, data) |
| 757 | port_id = self.cursor.fetchone()[0] |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 758 | self.cursor.execute("UPDATE state SET last_modified=%s", (datetime.datetime.now(),)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 759 | self.connection.commit() |
| 760 | except: |
| 761 | self.connection.rollback() |
| 762 | raise |
| 763 | return port_id |
| 764 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 765 | # Trivial helper function to return all the rows in a given table |
Steve McIntyre | e3fb49a | 2015-09-23 00:04:12 +0100 | [diff] [blame] | 766 | def _dump_table(self, table, order): |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 767 | result = [] |
Steve McIntyre | e3fb49a | 2015-09-23 00:04:12 +0100 | [diff] [blame] | 768 | self.cursor.execute("SELECT * FROM %s ORDER by %s ASC" % (table, order)) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 769 | record = self.cursor.fetchone() |
| 770 | while record != None: |
Steve McIntyre | e73eb12 | 2014-11-27 15:18:47 +0000 | [diff] [blame] | 771 | result.append(record) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 772 | record = self.cursor.fetchone() |
| 773 | return result |
| 774 | |
| 775 | def all_switches(self): |
Steve McIntyre | e3fb49a | 2015-09-23 00:04:12 +0100 | [diff] [blame] | 776 | return self._dump_table("switch", "switch_id") |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 777 | |
| 778 | def all_ports(self): |
Steve McIntyre | e3fb49a | 2015-09-23 00:04:12 +0100 | [diff] [blame] | 779 | return self._dump_table("port", "port_id") |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 780 | |
| 781 | def all_vlans(self): |
Steve McIntyre | e3fb49a | 2015-09-23 00:04:12 +0100 | [diff] [blame] | 782 | return self._dump_table("vlan", "vlan_id") |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 783 | |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 784 | def all_trunks(self): |
Steve McIntyre | e3fb49a | 2015-09-23 00:04:12 +0100 | [diff] [blame] | 785 | return self._dump_table("trunk", "trunk_id") |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 786 | |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame] | 787 | if __name__ == '__main__': |
| 788 | db = VlanDB() |
Steve McIntyre | 6d84ec1 | 2014-12-18 16:56:56 +0000 | [diff] [blame] | 789 | s = db.all_switches() |
| 790 | print 'The DB knows about %d switch(es)' % len(s) |
| 791 | print s |
| 792 | p = db.all_ports() |
| 793 | print 'The DB knows about %d port(s)' % len(p) |
| 794 | print p |
| 795 | v = db.all_vlans() |
| 796 | print 'The DB knows about %d vlan(s)' % len(v) |
| 797 | print v |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 798 | t = db.all_trunks() |
| 799 | print 'The DB knows about %d trunks(s)' % len(t) |
| 800 | print t |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame] | 801 | |
Steve McIntyre | 6c4f33f | 2015-08-03 19:25:07 +0100 | [diff] [blame] | 802 | print 'First free VLAN tag is %d' % db.find_lowest_unused_vlan_tag() |