Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
| 4 | # Author: Dave Pigott <dave.pigot@linaro.org> |
| 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 |
| 23 | |
| 24 | class VlanDB: |
| 25 | def __init__(self, db_name="vland", username="vland"): |
| 26 | try: |
| 27 | self.connection = psycopg2.connect(database=db_name, user=username, |
| 28 | cursor_factory=psycopg2.extras.RealDictCursor) |
| 29 | self.cursor = self.connection.cursor() |
| 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 | |
| 37 | def create_switch(self, name, admin_password, cli_password): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame^] | 38 | try: |
| 39 | self.cursor("INSERT INTO switch (name, admin_pw, cli_pw) VALUES (%s, %s, %s) RETURNING switch_id" % |
| 40 | (name, admin_password, cli_password)) |
| 41 | switch_id = self.cursor.fetchone()[0] |
| 42 | self.connection.commit() |
| 43 | except: |
| 44 | self.connection.rollback() |
| 45 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 46 | return switch_id |
| 47 | |
| 48 | def create_port(self, name, switch_id, is_locked, is_trunk, default_vlan_id, base_vlan_id): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame^] | 49 | try: |
| 50 | self.cursor("INSERT INTO port (name, switch_id, is_locked, is_trunk, default_vlan_id, base_vlan_id)" |
| 51 | "VALUES (%s, %s, %s %s %s %s) RETURNING port_id" % |
| 52 | (name, switch_id, is_locked, is_trunk, default_vlan_id, base_vlan_id)) |
| 53 | port_id = self.cursor.fetchone()[0] |
| 54 | self.connection.commit() |
| 55 | except: |
| 56 | self.connection.rollback() |
| 57 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 58 | return port_id |
| 59 | |
| 60 | def create_vlan(self, name, tag, is_default_vlan): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame^] | 61 | try: |
| 62 | self.cursor("INSERT INTO vlan (name, tag, is_default_vlan)" |
| 63 | "VALUES (%s, %s, %s) RETURNING vlan_id" % |
| 64 | (name, tag, is_default_vlan)) |
| 65 | vlan_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 vlan_id |
| 71 | |
| 72 | def _delete_row(self, table, field, value): |
Dave Pigott | 2649a1a | 2014-09-18 00:04:49 +0100 | [diff] [blame^] | 73 | try: |
| 74 | self.cursor.execute("DELETE FROM %s WHERE %s = %s" % (table, field, value)) |
| 75 | self.connection.commit() |
| 76 | except: |
| 77 | self.connection.rollback() |
| 78 | raise |
Dave Pigott | 281203e | 2014-09-17 23:45:02 +0100 | [diff] [blame] | 79 | |
| 80 | def delete_switch(self, switch_id): |
| 81 | self._delete_row("switch", "switch_id", switch_id) |
| 82 | |
| 83 | def delete_port(self, port_id): |
| 84 | self._delete_row("port", "port_id", port_id) |
| 85 | |
| 86 | def delete_vlan(self, vlan_id): |
| 87 | self._delete_row("vlan", "vlan_id", vlan_id) |
| 88 | |
| 89 | def _get_row(self, select_field, table, compare_field, value): |
| 90 | self.cursor.execute("SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, value)) |
| 91 | return self.cursor.fetchone()[0] |
| 92 | |
| 93 | def get_switch_id(self, name): |
| 94 | return self._get_row("switch_id", "switch", "name", name) |
| 95 | |
| 96 | def get_port_id(self, name): |
| 97 | return self._get_row("port_id", "port", "name", name) |
| 98 | |
| 99 | def get_vlan_id(self, name): |
| 100 | return self._get_row("vlan_id", "vlan", "name", name) |
| 101 | |
| 102 | def get_switch_name(self, switch_id): |
| 103 | return self._get_row("name", "switch", "switch_id", switch_id) |
| 104 | |
| 105 | def get_port_name(self, port_id): |
| 106 | return self._get_row("port_name", "port", "port_id", port_id) |
| 107 | |
| 108 | def get_vlan_name(self, vlan_id): |
| 109 | return self._get_row("vlan_name", "vlan", "vlan_id", vlan_id) |
| 110 | |
| 111 | def _dump_table(self, table): |
| 112 | result = [] |
| 113 | self.cursor.execute("SELECT * FROM %s" % table) |
| 114 | self.cursor.execute("SELECT * FROM switch") |
| 115 | record = self.cursor.fetchone() |
| 116 | while record != None: |
| 117 | result += record |
| 118 | record = self.cursor.fetchone() |
| 119 | return result |
| 120 | |
| 121 | def all_switches(self): |
| 122 | return self._dump_table("switch") |
| 123 | |
| 124 | def all_ports(self): |
| 125 | return self._dump_table("port") |
| 126 | |
| 127 | def all_vlans(self): |
| 128 | return self._dump_table("vlan") |