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