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 |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 24 | from errors import CriticalError |
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 | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 38 | def create_switch(self, name): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 39 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 40 | sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id" |
| 41 | data = name |
| 42 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 43 | switch_id = self.cursor.fetchone()[0] |
| 44 | self.connection.commit() |
| 45 | except: |
| 46 | self.connection.rollback() |
| 47 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 48 | return switch_id |
| 49 | |
Steve McIntyre | 90a4a97 | 2014-11-28 16:50:56 +0000 | [diff] [blame] | 50 | # Create a new port in the database. Two of the fields are created |
| 51 | # with default values (is_locked, is_trunk) here, and should be |
| 52 | # updated separately if desired. For the current_vlan_id and |
| 53 | # base_vlan_id fields, *BE CAREFUL* that you have already looked |
| 54 | # up the correct VLAN_ID for each. This is *NOT* the same as the |
| 55 | # VLAN tag (likely to be 1). |
| 56 | # You Have Been Warned! |
| 57 | def create_port(self, name, switch_id, current_vlan_id, base_vlan_id): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 58 | try: |
Steve McIntyre | d74d97c | 2014-11-28 14:44:39 +0000 | [diff] [blame] | 59 | 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] | 60 | data = (name, switch_id, |
| 61 | False, False, |
| 62 | current_vlan_id, base_vlan_id) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 63 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 64 | port_id = self.cursor.fetchone()[0] |
| 65 | self.connection.commit() |
| 66 | except: |
| 67 | self.connection.rollback() |
| 68 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 69 | return port_id |
| 70 | |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 71 | def create_vlan(self, name, tag, is_base_vlan): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 72 | try: |
Steve McIntyre | d74d97c | 2014-11-28 14:44:39 +0000 | [diff] [blame] | 73 | dt = datetime.datetime.now() |
| 74 | sql = "INSERT INTO vlan (name, tag, is_base_vlan, creation_time) VALUES (%s, %s, %s, %s) RETURNING vlan_id" |
| 75 | data = (name, tag, is_base_vlan, dt) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 76 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 77 | vlan_id = self.cursor.fetchone()[0] |
| 78 | self.connection.commit() |
| 79 | except: |
| 80 | self.connection.rollback() |
| 81 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 82 | return vlan_id |
| 83 | |
| 84 | def _delete_row(self, table, field, value): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 85 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 86 | sql = "DELETE FROM %s WHERE %s = %s" |
| 87 | data = (table, field, value) |
| 88 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 89 | self.connection.commit() |
| 90 | except: |
| 91 | self.connection.rollback() |
| 92 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 93 | |
| 94 | def delete_switch(self, switch_id): |
| 95 | self._delete_row("switch", "switch_id", switch_id) |
| 96 | |
| 97 | def delete_port(self, port_id): |
| 98 | self._delete_row("port", "port_id", port_id) |
| 99 | |
| 100 | def delete_vlan(self, vlan_id): |
| 101 | self._delete_row("vlan", "vlan_id", vlan_id) |
| 102 | |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 103 | def _get_element(self, select_field, table, compare_field, value): |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame^] | 104 | |
| 105 | # We really want to use psycopg's type handling deal with the |
| 106 | # (potentially) user-supplied data in the value field, so we |
| 107 | # have to pass (sql,data) through to cursor.execute. However, |
| 108 | # we can't have psycopg do all the argument substitution here |
| 109 | # as it will quote all the params like the table name. That |
| 110 | # doesn't work. So, we substitute a "%s" for "%s" here so we |
| 111 | # keep it after python's own string substitution. |
| 112 | sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s") |
| 113 | |
| 114 | # Now, the next icky thing: we need to make sure that we're |
| 115 | # passing a dict so that psycopg2 can pick it apart properly |
| 116 | # for its own substitution code. We force this with the |
| 117 | # trailing comma here |
| 118 | data = (value, ) |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 119 | self.cursor.execute(sql, data) |
Steve McIntyre | 95614c2 | 2014-11-28 17:02:44 +0000 | [diff] [blame^] | 120 | |
| 121 | # Will raise an exception here if there are no rows that |
| 122 | # match. That's OK - the caller needs to deal with that. |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 123 | return self.cursor.fetchone()[0] |
| 124 | |
| 125 | def get_switch_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 126 | return self._get_element("switch_id", "switch", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 127 | |
| 128 | def get_port_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 129 | return self._get_element("port_id", "port", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 130 | |
| 131 | def get_vlan_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 132 | return self._get_element("vlan_id", "vlan", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 133 | |
| 134 | def get_switch_name(self, switch_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 135 | return self._get_element("name", "switch", "switch_id", switch_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 136 | |
| 137 | def get_port_name(self, port_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 138 | return self._get_element("port_name", "port", "port_id", port_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 139 | |
| 140 | def get_vlan_name(self, vlan_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 141 | return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id) |
| 142 | |
| 143 | def _get_row(self, table, field, value): |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 144 | sql = "SELECT * FROM %s WHERE %s = %s" |
| 145 | data = (table, field, value) |
| 146 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 147 | return self.cursor.fetchone() |
| 148 | |
| 149 | def get_switch(self, switch_id): |
| 150 | return self._get_row("switch", "switch_id", switch_id) |
| 151 | |
| 152 | def get_port(self, port_id): |
| 153 | return self._get_row("port", "port_id", port_id) |
| 154 | |
| 155 | def get_vlan(self, vlan_id): |
| 156 | return self._get_row("vlan", "vlan_id", vlan_id) |
| 157 | |
| 158 | def set_vlan(self, port_id, vlan_id): |
| 159 | port = self.get_port(port_id) |
| 160 | if port == None: |
| 161 | raise("Port %s does not exist" % port_id) |
| 162 | |
| 163 | if port["is_trunk"] or port["is_locked"]: |
| 164 | raise CriticalError("The port is locked") |
| 165 | |
| 166 | vlan = self.get_vlan(vlan_id) |
| 167 | if vlan == None: |
| 168 | raise CriticalError("VLAN %s does not exist" % vlan_id) |
| 169 | |
| 170 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 171 | sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s" |
| 172 | data = (vlan_id, port_id) |
| 173 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 174 | except: |
| 175 | self.connection.rollback() |
| 176 | raise |
| 177 | |
| 178 | def restore_default_vlan(self, port_id): |
| 179 | port = self.get_port(port_id) |
| 180 | if port == None: |
| 181 | raise CriticalError("Port %s does not exist") |
| 182 | |
| 183 | if port["is_trunk"] or port["is_locked"]: |
| 184 | raise CriticalError("The port is locked") |
| 185 | |
| 186 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame] | 187 | sql = "UPDATE port SET current_vlan_id=base_vlan_id WHERE port_id=%d" |
| 188 | data = port_id |
| 189 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 190 | except: |
| 191 | self.connection.rollback() |
| 192 | raise |
| 193 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 194 | def _dump_table(self, table): |
| 195 | result = [] |
| 196 | self.cursor.execute("SELECT * FROM %s" % table) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 197 | record = self.cursor.fetchone() |
| 198 | while record != None: |
Steve McIntyre | e73eb12 | 2014-11-27 15:18:47 +0000 | [diff] [blame] | 199 | result.append(record) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 200 | record = self.cursor.fetchone() |
| 201 | return result |
| 202 | |
| 203 | def all_switches(self): |
| 204 | return self._dump_table("switch") |
| 205 | |
| 206 | def all_ports(self): |
| 207 | return self._dump_table("port") |
| 208 | |
| 209 | def all_vlans(self): |
| 210 | return self._dump_table("vlan") |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 211 | |