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 |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 23 | from errors import CriticalError |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 24 | |
| 25 | class VlanDB: |
| 26 | def __init__(self, db_name="vland", username="vland"): |
| 27 | try: |
Steve McIntyre | e38f622 | 2014-11-27 15:09:49 +0000 | [diff] [blame] | 28 | self.connection = psycopg2.connect(database=db_name, user=username) |
| 29 | self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.DictCursor) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 30 | except Exception as e: |
| 31 | print "Failed to access database: %s" % e |
| 32 | |
| 33 | def __del__(self): |
| 34 | self.cursor.close() |
| 35 | self.connection.close() |
| 36 | |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 37 | def create_switch(self, name): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 38 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 39 | sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id" |
| 40 | data = name |
| 41 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 42 | switch_id = self.cursor.fetchone()[0] |
| 43 | self.connection.commit() |
| 44 | except: |
| 45 | self.connection.rollback() |
| 46 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 47 | return switch_id |
| 48 | |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 49 | def create_port(self, name, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 50 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 51 | sql = "INSERT INTO port (name, switch_id, is_locked, " |
| 52 | + "is_trunk, current_vlan_id, base_vlan_id) " |
| 53 | + "VALUES (%s, %s, %s, %s, %s, %s) RETURNING port_id" |
| 54 | data = (name, switch_id, is_locked, |
| 55 | is_trunk, current_vlan_id, base_vlan_id) |
| 56 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 57 | port_id = self.cursor.fetchone()[0] |
| 58 | self.connection.commit() |
| 59 | except: |
| 60 | self.connection.rollback() |
| 61 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 62 | return port_id |
| 63 | |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 64 | def create_vlan(self, name, tag, is_base_vlan): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 65 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 66 | sql = "INSERT INTO vlan (name, tag, is_base_vlan) " |
| 67 | + "VALUES (%s, %s, %s) RETURNING vlan_id" |
| 68 | data = (name, tag, is_base_vlan) |
| 69 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 70 | vlan_id = self.cursor.fetchone()[0] |
| 71 | self.connection.commit() |
| 72 | except: |
| 73 | self.connection.rollback() |
| 74 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 75 | return vlan_id |
| 76 | |
| 77 | def _delete_row(self, table, field, value): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 78 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 79 | sql = "DELETE FROM %s WHERE %s = %s" |
| 80 | data = (table, field, value) |
| 81 | self.cursor.execute(sql, data) |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame] | 82 | self.connection.commit() |
| 83 | except: |
| 84 | self.connection.rollback() |
| 85 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 86 | |
| 87 | def delete_switch(self, switch_id): |
| 88 | self._delete_row("switch", "switch_id", switch_id) |
| 89 | |
| 90 | def delete_port(self, port_id): |
| 91 | self._delete_row("port", "port_id", port_id) |
| 92 | |
| 93 | def delete_vlan(self, vlan_id): |
| 94 | self._delete_row("vlan", "vlan_id", vlan_id) |
| 95 | |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 96 | def _get_element(self, select_field, table, compare_field, value): |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 97 | sql = "SELECT %s FROM %s WHERE %s = %s" |
| 98 | data = (select_field, table, compare_field, value) |
| 99 | self.cursor.execute(sql, data) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 100 | return self.cursor.fetchone()[0] |
| 101 | |
| 102 | def get_switch_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 103 | return self._get_element("switch_id", "switch", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 104 | |
| 105 | def get_port_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 106 | return self._get_element("port_id", "port", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 107 | |
| 108 | def get_vlan_id(self, name): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 109 | return self._get_element("vlan_id", "vlan", "name", name) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 110 | |
| 111 | def get_switch_name(self, switch_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 112 | return self._get_element("name", "switch", "switch_id", switch_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 113 | |
| 114 | def get_port_name(self, port_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 115 | return self._get_element("port_name", "port", "port_id", port_id) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 116 | |
| 117 | def get_vlan_name(self, vlan_id): |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 118 | return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id) |
| 119 | |
| 120 | def _get_row(self, table, field, value): |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 121 | sql = "SELECT * FROM %s WHERE %s = %s" |
| 122 | data = (table, field, value) |
| 123 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 124 | return self.cursor.fetchone() |
| 125 | |
| 126 | def get_switch(self, switch_id): |
| 127 | return self._get_row("switch", "switch_id", switch_id) |
| 128 | |
| 129 | def get_port(self, port_id): |
| 130 | return self._get_row("port", "port_id", port_id) |
| 131 | |
| 132 | def get_vlan(self, vlan_id): |
| 133 | return self._get_row("vlan", "vlan_id", vlan_id) |
| 134 | |
| 135 | def set_vlan(self, port_id, vlan_id): |
| 136 | port = self.get_port(port_id) |
| 137 | if port == None: |
| 138 | raise("Port %s does not exist" % port_id) |
| 139 | |
| 140 | if port["is_trunk"] or port["is_locked"]: |
| 141 | raise CriticalError("The port is locked") |
| 142 | |
| 143 | vlan = self.get_vlan(vlan_id) |
| 144 | if vlan == None: |
| 145 | raise CriticalError("VLAN %s does not exist" % vlan_id) |
| 146 | |
| 147 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 148 | sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s" |
| 149 | data = (vlan_id, port_id) |
| 150 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 151 | except: |
| 152 | self.connection.rollback() |
| 153 | raise |
| 154 | |
| 155 | def restore_default_vlan(self, port_id): |
| 156 | port = self.get_port(port_id) |
| 157 | if port == None: |
| 158 | raise CriticalError("Port %s does not exist") |
| 159 | |
| 160 | if port["is_trunk"] or port["is_locked"]: |
| 161 | raise CriticalError("The port is locked") |
| 162 | |
| 163 | try: |
Steve McIntyre | dbd7fe5 | 2014-11-27 16:54:29 +0000 | [diff] [blame^] | 164 | sql = "UPDATE port SET current_vlan_id=base_vlan_id WHERE port_id=%d" |
| 165 | data = port_id |
| 166 | self.cursor.execute(sql, data) |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 167 | except: |
| 168 | self.connection.rollback() |
| 169 | raise |
| 170 | |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 171 | def _dump_table(self, table): |
| 172 | result = [] |
| 173 | self.cursor.execute("SELECT * FROM %s" % table) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 174 | record = self.cursor.fetchone() |
| 175 | while record != None: |
Steve McIntyre | e73eb12 | 2014-11-27 15:18:47 +0000 | [diff] [blame] | 176 | result.append(record) |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 177 | record = self.cursor.fetchone() |
| 178 | return result |
| 179 | |
| 180 | def all_switches(self): |
| 181 | return self._dump_table("switch") |
| 182 | |
| 183 | def all_ports(self): |
| 184 | return self._dump_table("port") |
| 185 | |
| 186 | def all_vlans(self): |
| 187 | return self._dump_table("vlan") |
Dave Pigott | 9b73f3a | 2014-09-18 22:55:42 +0100 | [diff] [blame] | 188 | |