Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
Steve McIntyre | 663dc06 | 2014-10-20 11:11:47 +0100 | [diff] [blame] | 4 | # Author: Dave Pigott <dave.pigott@linaro.org> |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
| 21 | import psycopg2 |
| 22 | import psycopg2.extras |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame^] | 23 | import datetime, os, sys |
| 24 | |
| 25 | if __name__ == '__main__': |
| 26 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 27 | sys.path.insert(0, vlandpath) |
| 28 | sys.path.insert(0, "%s/.." % vlandpath) |
| 29 | |
Steve McIntyre | 6b01365 | 2014-12-02 12:35:18 +0000 | [diff] [blame] | 30 | from errors import CriticalError, InputError |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 31 | |
| 32 | class VlanDB: |
| 33 | def __init__(self, db_name="vland", username="vland"): |
| 34 | try: |
Steve McIntyre | e38f622 | 2014-11-27 15:09:49 +0000 | [diff] [blame] | 35 | self.connection = psycopg2.connect(database=db_name, user=username) |
Steve McIntyre | b09ed28 | 2014-12-02 17:59:35 +0000 | [diff] [blame] | 36 | self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 37 | except Exception as e: |
| 38 | print "Failed to access database: %s" % e |
| 39 | |
| 40 | def __del__(self): |
| 41 | self.cursor.close() |
| 42 | self.connection.close() |
| 43 | |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 44 | # Create a new switch in the database. Switches are really simple |
| 45 | # devices - they're just containers for ports. |
| 46 | # |
| 47 | # Constraints: |
| 48 | # Switches must be uniquely named |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 49 | def create_switch(self, name): |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 50 | |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 51 | switch_id = self.get_switch_id_by_name(name) |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 52 | if switch_id is not None: |
| 53 | raise InputError("Switch name %s already exists" % name) |
| 54 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 55 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 56 | sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id" |
Steve McIntyre | 31d6dfa | 2014-12-02 12:35:56 +0000 | [diff] [blame] | 57 | data = (name, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 58 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 59 | switch_id = self.cursor.fetchone()[0] |
| 60 | self.connection.commit() |
| 61 | except: |
| 62 | self.connection.rollback() |
| 63 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 64 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 65 | return switch_id |
| 66 | |
Steve McIntyre | 90a4a97 | 2014-11-28 16:50:56 +0000 | [diff] [blame] | 67 | # Create a new port in the database. Two of the fields are created |
| 68 | # with default values (is_locked, is_trunk) here, and should be |
| 69 | # updated separately if desired. For the current_vlan_id and |
| 70 | # base_vlan_id fields, *BE CAREFUL* that you have already looked |
| 71 | # up the correct VLAN_ID for each. This is *NOT* the same as the |
| 72 | # VLAN tag (likely to be 1). |
| 73 | # You Have Been Warned! |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 74 | # |
| 75 | # Constraints: |
| 76 | # 1. The switch referred to must already exist |
| 77 | # 2. The VLANs mentioned here must already exist |
Steve McIntyre | 6a7fdb2 | 2014-12-05 15:17:30 +0000 | [diff] [blame] | 78 | # 3. (Switch/name) must be unique |
Steve McIntyre | 1d10dbe | 2014-12-02 18:23:36 +0000 | [diff] [blame] | 79 | def create_port(self, switch_id, name, current_vlan_id, base_vlan_id): |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 80 | |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 81 | switch = self.get_switch_by_id(switch_id) |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 82 | if switch is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 83 | raise InputError("Switch ID %d does not exist" % int(switch_id)) |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 84 | |
| 85 | for vlan_id in (current_vlan_id, base_vlan_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 86 | vlan = self.get_vlan_by_id(vlan_id) |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 87 | if vlan is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 88 | raise InputError("VLAN ID %d does not exist" % int(vlan_id)) |
Steve McIntyre | 6a7fdb2 | 2014-12-05 15:17:30 +0000 | [diff] [blame] | 89 | |
| 90 | port_id = self.get_port_by_switch_and_name(switch_id, name) |
| 91 | if port_id is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 92 | 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] | 93 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 94 | try: |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 95 | 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" |
| 96 | data = (name, switch_id, |
Steve McIntyre | 90a4a97 | 2014-11-28 16:50:56 +0000 | [diff] [blame] | 97 | False, False, |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 98 | current_vlan_id, base_vlan_id) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 99 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 100 | port_id = self.cursor.fetchone()[0] |
| 101 | self.connection.commit() |
| 102 | except: |
| 103 | self.connection.rollback() |
| 104 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 105 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 106 | return port_id |
| 107 | |
Steve McIntyre | b005a2f | 2014-11-28 18:23:05 +0000 | [diff] [blame] | 108 | # Create a new vlan in the database. We locally add a creation |
| 109 | # timestamp, for debug purposes. If vlans seems to be sticking |
| 110 | # around, we'll be able to see when they were created. |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 111 | # |
| 112 | # Constraints: |
| 113 | # Names and tags must be unique |
Steve McIntyre | 57f4591 | 2014-12-08 14:43:00 +0000 | [diff] [blame] | 114 | # Tags must be in the range 1-4095 (802.1q spec) |
Steve McIntyre | 49777e7 | 2014-12-08 16:00:46 +0000 | [diff] [blame] | 115 | # Names can be any free-form text, length 1-32 characters |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 116 | def create_vlan(self, name, tag, is_base_vlan): |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 117 | |
Steve McIntyre | 57f4591 | 2014-12-08 14:43:00 +0000 | [diff] [blame] | 118 | if int(tag) < 1 or int(tag) > 4095: |
Steve McIntyre | 49777e7 | 2014-12-08 16:00:46 +0000 | [diff] [blame] | 119 | raise InputError("VLAN tag %d is outside of the valid range (1-4095)" % int(tag)) |
| 120 | |
| 121 | if (len(name) < 1) or (len(name) > 32): |
| 122 | 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] | 123 | |
Steve McIntyre | a34c181 | 2014-12-05 15:27:55 +0000 | [diff] [blame] | 124 | vlan_id = self.get_vlan_id_by_name(name) |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 125 | if vlan_id is not None: |
| 126 | raise InputError("VLAN name %s is already in use" % name) |
| 127 | |
Steve McIntyre | 50eb060 | 2014-12-05 15:29:04 +0000 | [diff] [blame] | 128 | vlan_id = self.get_vlan_id_by_tag(tag) |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 129 | if vlan_id is not None: |
| 130 | raise InputError("VLAN tag %d is already in use" % int(tag)) |
| 131 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 132 | try: |
Steve McIntyre | d74d97c | 2014-11-28 14:44:39 +0000 | [diff] [blame] | 133 | dt = datetime.datetime.now() |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 134 | sql = "INSERT INTO vlan (name, tag, is_base_vlan, creation_time) VALUES (%s, %s, %s, %s) RETURNING vlan_id" |
| 135 | data = (name, tag, is_base_vlan, dt) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 136 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 137 | vlan_id = self.cursor.fetchone()[0] |
| 138 | self.connection.commit() |
| 139 | except: |
| 140 | self.connection.rollback() |
| 141 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 142 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 143 | return vlan_id |
| 144 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 145 | # Internal helper function |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 146 | def _delete_row(self, table, field, value): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 147 | try: |
Steve McIntyre | e03de00 | 2014-12-02 17:14:14 +0000 | [diff] [blame] | 148 | sql = "DELETE FROM %s WHERE %s = %s" % (table, field, '%s') |
| 149 | data = (value,) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 150 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 151 | self.connection.commit() |
| 152 | except: |
| 153 | self.connection.rollback() |
| 154 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 155 | |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 156 | # Delete the specified switch |
| 157 | # |
| 158 | # Constraints: |
| 159 | # 1. The switch must exist |
| 160 | # 2. The switch may not be referenced by any ports - |
| 161 | # delete them first! |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 162 | def delete_switch(self, switch_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 163 | switch = self.get_switch_by_id(switch_id) |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 164 | if switch is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 165 | raise InputError("Switch ID %d does not exist" % int(switch_id)) |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 166 | ports = self.get_ports_by_switch(switch_id) |
| 167 | if ports is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 168 | raise InputError("Cannot delete switch ID %d when it still has %d ports" % |
| 169 | (int(switch_id), len(ports))) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 170 | self._delete_row("switch", "switch_id", switch_id) |
Steve McIntyre | 388f0e2 | 2014-12-02 17:19:04 +0000 | [diff] [blame] | 171 | return switch_id |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 172 | |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 173 | # Delete the specified port |
| 174 | # |
| 175 | # Constraints: |
| 176 | # 1. The port must exist |
| 177 | # 2. The port must not be locked |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 178 | def delete_port(self, port_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 179 | port = self.get_port_by_id(port_id) |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 180 | if port is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 181 | raise InputError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 182 | if port.is_locked: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 183 | 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] | 184 | self._delete_row("port", "port_id", port_id) |
Steve McIntyre | 6a96862 | 2014-12-02 18:01:41 +0000 | [diff] [blame] | 185 | return port_id |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 186 | |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 187 | # Delete the specified VLAN |
| 188 | # |
| 189 | # Constraints: |
| 190 | # 1. The VLAN |
| 191 | # 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] | 192 | def delete_vlan(self, vlan_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 193 | vlan = self.get_vlan_by_id(vlan_id) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 194 | if vlan is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 195 | raise InputError("VLAN ID %d does not exist" % int(vlan_id)) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 196 | ports = self.get_ports_by_current_vlan(vlan_id) |
| 197 | if ports is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 198 | raise InputError("Cannot delete VLAN ID %d when it still has %d ports" % |
| 199 | (int(vlan_id), len(ports))) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 200 | ports = self.get_ports_by_base_vlan(vlan_id) |
| 201 | if ports is not None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 202 | raise InputError("Cannot delete VLAN ID %d when it still has %d ports" % |
| 203 | (int(vlan_id), len(ports))) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 204 | self._delete_row("vlan", "vlan_id", vlan_id) |
Steve McIntyre | 14552ac | 2014-12-05 15:23:57 +0000 | [diff] [blame] | 205 | return vlan_id |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 206 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 207 | # Grab one column from one row of a query on one column; useful as |
| 208 | # a quick wrapper |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 209 | def _get_element(self, select_field, table, compare_field, value): |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 210 | |
| 211 | # We really want to use psycopg's type handling deal with the |
| 212 | # (potentially) user-supplied data in the value field, so we |
| 213 | # have to pass (sql,data) through to cursor.execute. However, |
| 214 | # we can't have psycopg do all the argument substitution here |
| 215 | # as it will quote all the params like the table name. That |
| 216 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 217 | # keep it after python's own string substitution. |
| 218 | sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s") |
| 219 | |
| 220 | # Now, the next icky thing: we need to make sure that we're |
| 221 | # passing a dict so that psycopg2 can pick it apart properly |
| 222 | # for its own substitution code. We force this with the |
| 223 | # trailing comma here |
| 224 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 225 | self.cursor.execute(sql, data) |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 226 | |
Steve McIntyre | 58b57a4 | 2014-12-02 13:09:21 +0000 | [diff] [blame] | 227 | if self.cursor.rowcount > 0: |
| 228 | return self.cursor.fetchone()[0] |
| 229 | else: |
Steve McIntyre | c831f9c | 2014-12-02 12:38:54 +0000 | [diff] [blame] | 230 | return None |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 231 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 232 | # Grab one column from one row of a query on 2 columns; useful as |
| 233 | # a quick wrapper |
Steve McIntyre | a74c7fe | 2014-12-02 18:49:38 +0000 | [diff] [blame] | 234 | def _get_element2(self, select_field, table, compare_field1, value1, compare_field2, value2): |
| 235 | |
| 236 | # We really want to use psycopg's type handling deal with the |
| 237 | # (potentially) user-supplied data in the value field, so we |
| 238 | # have to pass (sql,data) through to cursor.execute. However, |
| 239 | # we can't have psycopg do all the argument substitution here |
| 240 | # as it will quote all the params like the table name. That |
| 241 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 242 | # keep it after python's own string substitution. |
| 243 | sql = "SELECT %s FROM %s WHERE %s = %s AND %s = %s" % (select_field, table, compare_field1, "%s", compare_field2, "%s") |
| 244 | |
Steve McIntyre | a74c7fe | 2014-12-02 18:49:38 +0000 | [diff] [blame] | 245 | data = (value1, value2) |
| 246 | self.cursor.execute(sql, data) |
| 247 | |
| 248 | if self.cursor.rowcount > 0: |
| 249 | return self.cursor.fetchone()[0] |
| 250 | else: |
| 251 | return None |
| 252 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 253 | # Grab one column from multiple rows of a query; useful as a quick |
| 254 | # wrapper |
Steve McIntyre | e9da15e | 2014-12-05 15:22:41 +0000 | [diff] [blame] | 255 | def _get_multi_elements(self, select_field, table, compare_field, value): |
| 256 | |
| 257 | # We really want to use psycopg's type handling deal with the |
| 258 | # (potentially) user-supplied data in the value field, so we |
| 259 | # have to pass (sql,data) through to cursor.execute. However, |
| 260 | # we can't have psycopg do all the argument substitution here |
| 261 | # as it will quote all the params like the table name. That |
| 262 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 263 | # keep it after python's own string substitution. |
| 264 | sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s") |
| 265 | |
| 266 | # Now, the next icky thing: we need to make sure that we're |
| 267 | # passing a dict so that psycopg2 can pick it apart properly |
| 268 | # for its own substitution code. We force this with the |
| 269 | # trailing comma here |
| 270 | data = (value, ) |
| 271 | self.cursor.execute(sql, data) |
| 272 | |
| 273 | if self.cursor.rowcount > 0: |
| 274 | results = [] |
| 275 | for record in self.cursor: |
Steve McIntyre | 5250962 | 2014-12-02 17:13:15 +0000 | [diff] [blame] | 276 | results.append(record[0]) |
Steve McIntyre | e9da15e | 2014-12-05 15:22:41 +0000 | [diff] [blame] | 277 | return results |
Steve McIntyre | 5250962 | 2014-12-02 17:13:15 +0000 | [diff] [blame] | 278 | else: |
| 279 | return None |
| 280 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 281 | # Simple lookup: look up a switch by ID, and return all the |
| 282 | # details of that switch. |
| 283 | # |
| 284 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 285 | def get_switch_by_id(self, switch_id): |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 286 | return self._get_row("switch", "switch_id", switch_id) |
| 287 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 288 | # Simple lookup: look up a switch by name, and return the ID of |
| 289 | # that switch. |
| 290 | # |
| 291 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 292 | def get_switch_id_by_name(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 293 | return self._get_element("switch_id", "switch", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 294 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 295 | # Simple lookup: look up a switch by ID, and return the name of |
| 296 | # that switch. |
| 297 | # |
| 298 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 299 | def get_switch_name_by_id(self, switch_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 300 | return self._get_element("name", "switch", "switch_id", switch_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 301 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 302 | # Simple lookup: look up a port by ID, and return all the details |
| 303 | # of that port. |
| 304 | # |
| 305 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 306 | def get_port_by_id(self, port_id): |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 307 | return self._get_row("port", "port_id", port_id) |
| 308 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 309 | # Simple lookup: look up a switch by ID, and return the IDs of all |
| 310 | # the ports on that switch. |
| 311 | # |
| 312 | # Returns None on failure. |
Steve McIntyre | b67f391 | 2014-12-02 17:14:36 +0000 | [diff] [blame] | 313 | def get_ports_by_switch(self, switch_id): |
| 314 | return self._get_multi_elements("port_id", "port", "switch_id", switch_id) |
| 315 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 316 | # Simple lookup: look up a port by its name and its parent switch |
| 317 | # by ID, and return the ID of the port. |
| 318 | # |
| 319 | # Returns None on failure. |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 320 | def get_port_by_switch_and_name(self, switch_id, name): |
| 321 | return self._get_element2("port_id", "port", "switch_id", switch_id, "name", name) |
| 322 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 323 | # Simple lookup: look up a port by ID, and return the current VLAN |
| 324 | # id of that port. |
| 325 | # |
| 326 | # Returns None on failure. |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 327 | def get_current_vlan_id_by_port(self, port_id): |
| 328 | return self._get_element("current_vlan_id", "port", "port_id", port_id) |
| 329 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 330 | # Simple lookup: look up a port by ID, and return the base VLAN |
| 331 | # id of that port. |
| 332 | # |
| 333 | # Returns None on failure. |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 334 | def get_base_vlan_id_by_port(self, port_id): |
| 335 | return self._get_element("base_vlan_id", "port", "port_id", port_id) |
| 336 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 337 | # Simple lookup: look up a current VLAN by ID, and return the IDs |
| 338 | # of all the ports on that VLAN. |
| 339 | # |
| 340 | # Returns None on failure. |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 341 | def get_ports_by_current_vlan(self, vlan_id): |
| 342 | return self._get_multi_elements("port_id", "port", "current_vlan_id", vlan_id) |
| 343 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 344 | # Simple lookup: look up a base VLAN by ID, and return the IDs |
| 345 | # of all the ports on that VLAN. |
| 346 | # |
| 347 | # Returns None on failure. |
Steve McIntyre | 53a7bc8 | 2014-12-05 15:23:34 +0000 | [diff] [blame] | 348 | def get_ports_by_base_vlan(self, vlan_id): |
| 349 | return self._get_multi_elements("port_id", "port", "base_vlan_id", vlan_id) |
| 350 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 351 | # Simple lookup: look up a VLAN by ID, and return all the details |
| 352 | # of that VLAN. |
| 353 | # |
| 354 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 355 | def get_vlan_by_id(self, vlan_id): |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 356 | return self._get_row("vlan", "vlan_id", vlan_id) |
| 357 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 358 | # Simple lookup: look up a VLAN by name, and return the ID of that |
| 359 | # VLAN. |
| 360 | # |
| 361 | # Returns None on failure. |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 362 | def get_vlan_id_by_name(self, name): |
| 363 | return self._get_element("vlan_id", "vlan", "name", name) |
| 364 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 365 | # Simple lookup: look up a VLAN by tag, and return the ID of that |
| 366 | # VLAN. |
| 367 | # |
| 368 | # Returns None on failure. |
Steve McIntyre | f365506 | 2014-12-05 15:34:39 +0000 | [diff] [blame] | 369 | def get_vlan_id_by_tag(self, tag): |
| 370 | return self._get_element("vlan_id", "vlan", "tag", tag) |
| 371 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 372 | # Simple lookup: look up a VLAN by ID, and return the name of that |
| 373 | # VLAN. |
| 374 | # |
| 375 | # Returns None on failure. |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 376 | def get_vlan_name_by_id(self, vlan_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 377 | return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id) |
| 378 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 379 | # 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] | 380 | def _get_row(self, table, field, value): |
Steve McIntyre | e0b842a | 2014-11-28 18:23:47 +0000 | [diff] [blame] | 381 | |
| 382 | # We really want to use psycopg's type handling deal with the |
| 383 | # (potentially) user-supplied data in the value field, so we |
| 384 | # have to pass (sql,data) through to cursor.execute. However, |
| 385 | # we can't have psycopg do all the argument substitution here |
| 386 | # as it will quote all the params like the table name. That |
| 387 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 388 | # keep it after python's own string substitution. |
| 389 | sql = "SELECT * FROM %s WHERE %s = %s" % (table, field, "%s") |
| 390 | |
| 391 | # Now, the next icky thing: we need to make sure that we're |
| 392 | # passing a dict so that psycopg2 can pick it apart properly |
| 393 | # for its own substitution code. We force this with the |
| 394 | # trailing comma here |
| 395 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 396 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 397 | return self.cursor.fetchone() |
| 398 | |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 399 | # (Un)Lock a port in the database. This can only be done through |
| 400 | # the admin interface, and will stop API users from modifying |
| 401 | # settings on the port. Use this to lock down ports that are used |
| 402 | # for PDUs and other core infrastructure |
| 403 | def set_port_is_locked(self, port_id, is_locked): |
Steve McIntyre | 8c64d95 | 2014-12-05 16:22:44 +0000 | [diff] [blame] | 404 | port = self.get_port_by_id(port_id) |
| 405 | if port is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 406 | raise InputError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 407 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 408 | 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] | 409 | data = (is_locked, port_id) |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 410 | self.cursor.execute(sql, data) |
| 411 | port_id = self.cursor.fetchone()[0] |
| 412 | self.connection.commit() |
| 413 | except: |
| 414 | self.connection.rollback() |
| 415 | raise |
| 416 | return port_id |
| 417 | |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 418 | # Set the mode of a port in the database. Valid values for mode |
| 419 | # are "trunk" and "access" |
| 420 | def set_port_mode(self, port_id, mode): |
| 421 | port = self.get_port_by_id(port_id) |
| 422 | if port is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 423 | raise InputError("Port ID %d does not exist" % int(port_id)) |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 424 | if mode == "access": |
| 425 | is_trunk = False |
| 426 | elif mode == "trunk": |
| 427 | is_trunk = True |
| 428 | else: |
| 429 | raise InputError("Port mode %s is not valid" % mode) |
| 430 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 431 | 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] | 432 | data = (is_trunk, port_id) |
Steve McIntyre | 4204d0d | 2014-12-05 16:24:10 +0000 | [diff] [blame] | 433 | self.cursor.execute(sql, data) |
| 434 | port_id = self.cursor.fetchone()[0] |
| 435 | self.connection.commit() |
| 436 | except: |
| 437 | self.connection.rollback() |
| 438 | raise |
| 439 | return port_id |
| 440 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 441 | # Set the current vlan of a port in the database. The VLAN is |
| 442 | # passed by ID. |
| 443 | # |
| 444 | # Constraints: |
| 445 | # 1. The port must already exist |
| 446 | # 2. The port must not be a trunk port |
| 447 | # 3. The port must not be locked |
| 448 | # 1. The VLAN must already exist in the database |
Steve McIntyre | 9eb7865 | 2014-12-05 17:51:53 +0000 | [diff] [blame] | 449 | def set_current_vlan(self, port_id, vlan_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 450 | port = self.get_port_by_id(port_id) |
Steve McIntyre | 028b3cc | 2014-12-05 16:24:46 +0000 | [diff] [blame] | 451 | if port is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 452 | raise InputError("Port ID %d does not exist" % int(port_id)) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 453 | |
Steve McIntyre | 6dd00be | 2014-12-05 17:29:35 +0000 | [diff] [blame] | 454 | if port.is_trunk or port.is_locked: |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 455 | raise CriticalError("The port is locked") |
| 456 | |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 457 | vlan = self.get_vlan_by_id(vlan_id) |
Steve McIntyre | 028b3cc | 2014-12-05 16:24:46 +0000 | [diff] [blame] | 458 | if vlan is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 459 | raise InputError("VLAN ID %d does not exist" % int(vlan_id)) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 460 | |
| 461 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 462 | 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] | 463 | data = (vlan_id, port_id) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 464 | self.cursor.execute(sql, data) |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 465 | port_id = self.cursor.fetchone()[0] |
| 466 | self.connection.commit() |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 467 | except: |
| 468 | self.connection.rollback() |
| 469 | raise |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 470 | return port_id |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 471 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 472 | # Set the base vlan of a port in the database. The VLAN is |
| 473 | # passed by ID. |
| 474 | # |
| 475 | # Constraints: |
| 476 | # 1. The port must already exist |
| 477 | # 2. The port must not be a trunk port |
| 478 | # 3. The port must not be locked |
| 479 | # 1. The VLAN must already exist in the database |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 480 | def set_base_vlan(self, port_id, vlan_id): |
| 481 | port = self.get_port_by_id(port_id) |
| 482 | if port is None: |
| 483 | raise InputError("Port ID %d does not exist" % int(port_id)) |
| 484 | |
| 485 | if port.is_trunk or port.is_locked: |
| 486 | raise CriticalError("The port is locked") |
| 487 | |
| 488 | vlan = self.get_vlan_by_id(vlan_id) |
| 489 | if vlan is None: |
| 490 | raise InputError("VLAN ID %d does not exist" % int(vlan_id)) |
| 491 | if not vlan.is_base_vlan: |
| 492 | raise InputError("VLAN ID %d is not a base VLAN" % int(vlan_id)) |
| 493 | |
| 494 | try: |
| 495 | sql = "UPDATE port SET base_vlan_id=%s WHERE port_id=%s RETURNING port_id" |
| 496 | data = (vlan_id, port_id) |
| 497 | self.cursor.execute(sql, data) |
| 498 | port_id = self.cursor.fetchone()[0] |
| 499 | self.connection.commit() |
| 500 | except: |
| 501 | self.connection.rollback() |
| 502 | raise |
| 503 | return port_id |
| 504 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 505 | # Return a port back to its base VLAN |
| 506 | # |
| 507 | # Constraints: |
| 508 | # 1. The port must already exist |
| 509 | # 2. The port must not be a trunk port |
| 510 | # 3. The port must not be locked |
Steve McIntyre | daae550 | 2014-12-05 17:55:18 +0000 | [diff] [blame] | 511 | def restore_base_vlan(self, port_id): |
Steve McIntyre | 549435f | 2014-12-05 15:42:46 +0000 | [diff] [blame] | 512 | port = self.get_port_by_id(port_id) |
Steve McIntyre | 028b3cc | 2014-12-05 16:24:46 +0000 | [diff] [blame] | 513 | if port is None: |
Steve McIntyre | a1c7522 | 2014-12-05 16:57:13 +0000 | [diff] [blame] | 514 | raise InputError("Port ID %d does not exist" % int(port_id)) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 515 | |
Steve McIntyre | 6dd00be | 2014-12-05 17:29:35 +0000 | [diff] [blame] | 516 | if port.is_trunk or port.is_locked: |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 517 | raise CriticalError("The port is locked") |
| 518 | |
| 519 | try: |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 520 | sql = "UPDATE port SET current_vlan_id=base_vlan_id WHERE port_id=%s RETURNING port_id" |
Steve McIntyre | 4b91813 | 2014-12-05 17:04:46 +0000 | [diff] [blame] | 521 | data = (port_id,) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 522 | self.cursor.execute(sql, data) |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 523 | port_id = self.cursor.fetchone()[0] |
| 524 | self.connection.commit() |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 525 | except: |
| 526 | self.connection.rollback() |
| 527 | raise |
Steve McIntyre | e137110 | 2014-12-05 17:17:09 +0000 | [diff] [blame] | 528 | return port_id |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 529 | |
Steve McIntyre | 2d685c7 | 2014-12-08 15:24:12 +0000 | [diff] [blame] | 530 | # Trivial helper function to return all the rows in a given table |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 531 | def _dump_table(self, table): |
| 532 | result = [] |
| 533 | self.cursor.execute("SELECT * FROM %s" % table) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 534 | record = self.cursor.fetchone() |
| 535 | while record != None: |
Steve McIntyre | e73eb12 | 2014-11-27 15:18:47 +0000 | [diff] [blame] | 536 | result.append(record) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 537 | record = self.cursor.fetchone() |
| 538 | return result |
| 539 | |
| 540 | def all_switches(self): |
| 541 | return self._dump_table("switch") |
| 542 | |
| 543 | def all_ports(self): |
| 544 | return self._dump_table("port") |
| 545 | |
| 546 | def all_vlans(self): |
| 547 | return self._dump_table("vlan") |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 548 | |
Steve McIntyre | 6a61816 | 2014-12-10 16:47:07 +0000 | [diff] [blame^] | 549 | if __name__ == '__main__': |
| 550 | db = VlanDB() |
| 551 | switches = db.all_switches() |
| 552 | print 'The DB knows about %d switch(es)' % len(switches) |
| 553 | print switches |
| 554 | ports = db.all_ports() |
| 555 | print 'The DB knows about %d port(s)' % len(ports) |
| 556 | print ports |
| 557 | vlans = db.all_vlans() |
| 558 | print 'The DB knows about %d vlan(s)' % len(vlans) |
| 559 | print vlans |
| 560 | |
| 561 | |