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) |
| 30 | self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.DictCursor) |
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 | |
| 45 | switch_id = self.get_switch_id(name) |
| 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 | 90a4a97 | 2014-11-28 16:50:56 +0000 | [diff] [blame] | 72 | def create_port(self, name, switch_id, current_vlan_id, base_vlan_id): |
Steve McIntyre | cb42ebf | 2014-12-02 12:36:45 +0000 | [diff] [blame] | 73 | |
| 74 | switch = self.get_switch(switch_id) |
| 75 | if switch is None: |
| 76 | raise InputError("Switch id %s does not exist" % switch_id) |
| 77 | |
| 78 | for vlan_id in (current_vlan_id, base_vlan_id): |
| 79 | vlan = self.get_vlan(vlan_id) |
| 80 | if vlan is None: |
| 81 | raise InputError("VLAN id %s does not exist" % vlan_id) |
| 82 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 83 | try: |
Steve McIntyre | d74d97c | 2014-11-28 14:44:39 +0000 | [diff] [blame] | 84 | 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" |
Steve McIntyre | 90a4a97 | 2014-11-28 16:50:56 +0000 | [diff] [blame] | 85 | data = (name, switch_id, |
| 86 | False, False, |
| 87 | current_vlan_id, base_vlan_id) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 88 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 89 | port_id = self.cursor.fetchone()[0] |
| 90 | self.connection.commit() |
| 91 | except: |
| 92 | self.connection.rollback() |
| 93 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 94 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 95 | return port_id |
| 96 | |
Steve McIntyre | b005a2f | 2014-11-28 18:23:05 +0000 | [diff] [blame] | 97 | # Create a new vlan in the database. We locally add a creation |
| 98 | # timestamp, for debug purposes. If vlans seems to be sticking |
| 99 | # around, we'll be able to see when they were created. |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 100 | # |
| 101 | # Constraints: |
| 102 | # Names and tags must be unique |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 103 | def create_vlan(self, name, tag, is_base_vlan): |
Steve McIntyre | 31b2df5 | 2014-12-02 12:37:54 +0000 | [diff] [blame] | 104 | |
| 105 | vlan_id = self.get_vlan_id_from_name(name) |
| 106 | if vlan_id is not None: |
| 107 | raise InputError("VLAN name %s is already in use" % name) |
| 108 | |
| 109 | vlan_id = self.get_vlan_id_from_tag(tag) |
| 110 | if vlan_id is not None: |
| 111 | raise InputError("VLAN tag %d is already in use" % int(tag)) |
| 112 | |
| 113 | for vlan_id in (current_vlan_id, base_vlan_id): |
| 114 | vlan = self.get_vlan(vlan_id) |
| 115 | if vlan is None: |
| 116 | raise InputError("VLAN id %s does not exist" % vlan_id) |
| 117 | |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 118 | try: |
Steve McIntyre | d74d97c | 2014-11-28 14:44:39 +0000 | [diff] [blame] | 119 | dt = datetime.datetime.now() |
| 120 | sql = "INSERT INTO vlan (name, tag, is_base_vlan, creation_time) VALUES (%s, %s, %s, %s) RETURNING vlan_id" |
| 121 | data = (name, tag, is_base_vlan, dt) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 122 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 123 | vlan_id = self.cursor.fetchone()[0] |
| 124 | self.connection.commit() |
| 125 | except: |
| 126 | self.connection.rollback() |
| 127 | raise |
Steve McIntyre | e1febdb | 2014-12-02 12:39:14 +0000 | [diff] [blame] | 128 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 129 | return vlan_id |
| 130 | |
| 131 | def _delete_row(self, table, field, value): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 132 | try: |
Steve McIntyre | e03de00 | 2014-12-02 17:14:14 +0000 | [diff] [blame] | 133 | sql = "DELETE FROM %s WHERE %s = %s" % (table, field, '%s') |
| 134 | data = (value,) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 135 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 136 | self.connection.commit() |
| 137 | except: |
| 138 | self.connection.rollback() |
| 139 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 140 | |
| 141 | def delete_switch(self, switch_id): |
| 142 | self._delete_row("switch", "switch_id", switch_id) |
| 143 | |
| 144 | def delete_port(self, port_id): |
| 145 | self._delete_row("port", "port_id", port_id) |
| 146 | |
| 147 | def delete_vlan(self, vlan_id): |
| 148 | self._delete_row("vlan", "vlan_id", vlan_id) |
| 149 | |
Steve McIntyre | c3b2ae4 | 2014-12-02 17:13:36 +0000 | [diff] [blame] | 150 | # Grab one column from one row of a query; useful as a quick wrapper |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 151 | def _get_element(self, select_field, table, compare_field, value): |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 152 | |
| 153 | # We really want to use psycopg's type handling deal with the |
| 154 | # (potentially) user-supplied data in the value field, so we |
| 155 | # have to pass (sql,data) through to cursor.execute. However, |
| 156 | # we can't have psycopg do all the argument substitution here |
| 157 | # as it will quote all the params like the table name. That |
| 158 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 159 | # keep it after python's own string substitution. |
| 160 | sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s") |
| 161 | |
| 162 | # Now, the next icky thing: we need to make sure that we're |
| 163 | # passing a dict so that psycopg2 can pick it apart properly |
| 164 | # for its own substitution code. We force this with the |
| 165 | # trailing comma here |
| 166 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 167 | self.cursor.execute(sql, data) |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame] | 168 | |
Steve McIntyre | 58b57a4 | 2014-12-02 13:09:21 +0000 | [diff] [blame] | 169 | if self.cursor.rowcount > 0: |
| 170 | return self.cursor.fetchone()[0] |
| 171 | else: |
Steve McIntyre | c831f9c | 2014-12-02 12:38:54 +0000 | [diff] [blame] | 172 | return None |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 173 | |
Steve McIntyre | 5250962 | 2014-12-02 17:13:15 +0000 | [diff] [blame] | 174 | # Grab one column from multiple rows of a query; useful as a quick wrapper |
| 175 | def _get_multi_elements(self, select_field, table, compare_field, value): |
| 176 | |
| 177 | # We really want to use psycopg's type handling deal with the |
| 178 | # (potentially) user-supplied data in the value field, so we |
| 179 | # have to pass (sql,data) through to cursor.execute. However, |
| 180 | # we can't have psycopg do all the argument substitution here |
| 181 | # as it will quote all the params like the table name. That |
| 182 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 183 | # keep it after python's own string substitution. |
| 184 | sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s") |
| 185 | |
| 186 | # Now, the next icky thing: we need to make sure that we're |
| 187 | # passing a dict so that psycopg2 can pick it apart properly |
| 188 | # for its own substitution code. We force this with the |
| 189 | # trailing comma here |
| 190 | data = (value, ) |
| 191 | self.cursor.execute(sql, data) |
| 192 | |
| 193 | if self.cursor.rowcount > 0: |
| 194 | results = [] |
| 195 | for record in self.cursor: |
| 196 | results.append(record[0]) |
| 197 | else: |
| 198 | return None |
| 199 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 200 | def get_switch_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 201 | return self._get_element("switch_id", "switch", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 202 | |
| 203 | def get_port_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 204 | return self._get_element("port_id", "port", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 205 | |
Steve McIntyre | 9f403e8 | 2014-11-28 18:10:09 +0000 | [diff] [blame] | 206 | def get_vlan_id_from_name(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 207 | return self._get_element("vlan_id", "vlan", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 208 | |
Steve McIntyre | 9f403e8 | 2014-11-28 18:10:09 +0000 | [diff] [blame] | 209 | def get_vlan_id_from_tag(self, tag): |
| 210 | return self._get_element("vlan_id", "vlan", "tag", tag) |
| 211 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 212 | def get_switch_name(self, switch_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 213 | return self._get_element("name", "switch", "switch_id", switch_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 214 | |
| 215 | def get_port_name(self, port_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 216 | return self._get_element("port_name", "port", "port_id", port_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 217 | |
Steve McIntyre | b67f391 | 2014-12-02 17:14:36 +0000 | [diff] [blame^] | 218 | def get_ports_by_switch(self, switch_id): |
| 219 | return self._get_multi_elements("port_id", "port", "switch_id", switch_id) |
| 220 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 221 | def get_vlan_name(self, vlan_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 222 | return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id) |
| 223 | |
| 224 | def _get_row(self, table, field, value): |
Steve McIntyre | e0b842a | 2014-11-28 18:23:47 +0000 | [diff] [blame] | 225 | |
| 226 | # We really want to use psycopg's type handling deal with the |
| 227 | # (potentially) user-supplied data in the value field, so we |
| 228 | # have to pass (sql,data) through to cursor.execute. However, |
| 229 | # we can't have psycopg do all the argument substitution here |
| 230 | # as it will quote all the params like the table name. That |
| 231 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 232 | # keep it after python's own string substitution. |
| 233 | sql = "SELECT * FROM %s WHERE %s = %s" % (table, field, "%s") |
| 234 | |
| 235 | # Now, the next icky thing: we need to make sure that we're |
| 236 | # passing a dict so that psycopg2 can pick it apart properly |
| 237 | # for its own substitution code. We force this with the |
| 238 | # trailing comma here |
| 239 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 240 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 241 | return self.cursor.fetchone() |
| 242 | |
| 243 | def get_switch(self, switch_id): |
| 244 | return self._get_row("switch", "switch_id", switch_id) |
| 245 | |
| 246 | def get_port(self, port_id): |
| 247 | return self._get_row("port", "port_id", port_id) |
| 248 | |
| 249 | def get_vlan(self, vlan_id): |
| 250 | return self._get_row("vlan", "vlan_id", vlan_id) |
| 251 | |
Steve McIntyre | 3330f4b | 2014-11-28 18:11:02 +0000 | [diff] [blame] | 252 | # (Un)Lock a port in the database. This can only be done through |
| 253 | # the admin interface, and will stop API users from modifying |
| 254 | # settings on the port. Use this to lock down ports that are used |
| 255 | # for PDUs and other core infrastructure |
| 256 | def set_port_is_locked(self, port_id, is_locked): |
| 257 | try: |
| 258 | sql = "UPDATE port SET is_locked=%s WHERE port_id=%s" |
| 259 | data = (is_locked, port_id) |
| 260 | self.cursor.execute(sql, data) |
| 261 | port_id = self.cursor.fetchone()[0] |
| 262 | self.connection.commit() |
| 263 | except: |
| 264 | self.connection.rollback() |
| 265 | raise |
| 266 | return port_id |
| 267 | |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 268 | def set_vlan(self, port_id, vlan_id): |
| 269 | port = self.get_port(port_id) |
| 270 | if port == None: |
| 271 | raise("Port %s does not exist" % port_id) |
| 272 | |
| 273 | if port["is_trunk"] or port["is_locked"]: |
| 274 | raise CriticalError("The port is locked") |
| 275 | |
| 276 | vlan = self.get_vlan(vlan_id) |
| 277 | if vlan == None: |
| 278 | raise CriticalError("VLAN %s does not exist" % vlan_id) |
| 279 | |
| 280 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 281 | sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s" |
| 282 | data = (vlan_id, port_id) |
| 283 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 284 | except: |
| 285 | self.connection.rollback() |
| 286 | raise |
| 287 | |
| 288 | def restore_default_vlan(self, port_id): |
| 289 | port = self.get_port(port_id) |
| 290 | if port == None: |
| 291 | raise CriticalError("Port %s does not exist") |
| 292 | |
| 293 | if port["is_trunk"] or port["is_locked"]: |
| 294 | raise CriticalError("The port is locked") |
| 295 | |
| 296 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 297 | sql = "UPDATE port SET current_vlan_id=base_vlan_id WHERE port_id=%d" |
| 298 | data = port_id |
| 299 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 300 | except: |
| 301 | self.connection.rollback() |
| 302 | raise |
| 303 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 304 | def _dump_table(self, table): |
| 305 | result = [] |
| 306 | self.cursor.execute("SELECT * FROM %s" % table) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 307 | record = self.cursor.fetchone() |
| 308 | while record != None: |
Steve McIntyre | e73eb12 | 2014-11-27 15:18:47 +0000 | [diff] [blame] | 309 | result.append(record) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 310 | record = self.cursor.fetchone() |
| 311 | return result |
| 312 | |
| 313 | def all_switches(self): |
| 314 | return self._dump_table("switch") |
| 315 | |
| 316 | def all_ports(self): |
| 317 | return self._dump_table("port") |
| 318 | |
| 319 | def all_vlans(self): |
| 320 | return self._dump_table("vlan") |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 321 | |