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