blob: f302c603db96765c4c91d02dbdefdcae89ba731d [file] [log] [blame]
Dave Pigott281203e2014-09-17 23:45:02 +01001#! /usr/bin/python
2
3# Copyright 2014 Linaro Limited
Steve McIntyre663dc062014-10-20 11:11:47 +01004# Author: Dave Pigott <dave.pigott@linaro.org>
Dave Pigott281203e2014-09-17 23:45:02 +01005#
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
21import psycopg2
22import psycopg2.extras
Steve McIntyred74d97c2014-11-28 14:44:39 +000023import datetime
Dave Pigott9b73f3a2014-09-18 22:55:42 +010024from errors import CriticalError
Dave Pigott281203e2014-09-17 23:45:02 +010025
26class VlanDB:
27 def __init__(self, db_name="vland", username="vland"):
28 try:
Steve McIntyree38f6222014-11-27 15:09:49 +000029 self.connection = psycopg2.connect(database=db_name, user=username)
30 self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
Dave Pigott281203e2014-09-17 23:45:02 +010031 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 McIntyre8d39c792014-11-28 18:09:31 +000038 # Create a new switch in the database
Steve McIntyredbd7fe52014-11-27 16:54:29 +000039 def create_switch(self, name):
Dave Pigott2649a1a2014-09-18 00:04:49 +010040 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000041 sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id"
42 data = name
43 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010044 switch_id = self.cursor.fetchone()[0]
45 self.connection.commit()
46 except:
47 self.connection.rollback()
48 raise
Dave Pigott281203e2014-09-17 23:45:02 +010049 return switch_id
50
Steve McIntyre90a4a972014-11-28 16:50:56 +000051 # 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 Pigott2649a1a2014-09-18 00:04:49 +010059 try:
Steve McIntyred74d97c2014-11-28 14:44:39 +000060 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 McIntyre90a4a972014-11-28 16:50:56 +000061 data = (name, switch_id,
62 False, False,
63 current_vlan_id, base_vlan_id)
Steve McIntyredbd7fe52014-11-27 16:54:29 +000064 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010065 port_id = self.cursor.fetchone()[0]
66 self.connection.commit()
67 except:
68 self.connection.rollback()
69 raise
Dave Pigott281203e2014-09-17 23:45:02 +010070 return port_id
71
Steve McIntyredbd7fe52014-11-27 16:54:29 +000072 def create_vlan(self, name, tag, is_base_vlan):
Dave Pigott2649a1a2014-09-18 00:04:49 +010073 try:
Steve McIntyred74d97c2014-11-28 14:44:39 +000074 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 McIntyredbd7fe52014-11-27 16:54:29 +000077 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010078 vlan_id = self.cursor.fetchone()[0]
79 self.connection.commit()
80 except:
81 self.connection.rollback()
82 raise
Dave Pigott281203e2014-09-17 23:45:02 +010083 return vlan_id
84
85 def _delete_row(self, table, field, value):
Dave Pigott2649a1a2014-09-18 00:04:49 +010086 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000087 sql = "DELETE FROM %s WHERE %s = %s"
88 data = (table, field, value)
89 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010090 self.connection.commit()
91 except:
92 self.connection.rollback()
93 raise
Dave Pigott281203e2014-09-17 23:45:02 +010094
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 Pigott9b73f3a2014-09-18 22:55:42 +0100104 def _get_element(self, select_field, table, compare_field, value):
Steve McIntyre95614c22014-11-28 17:02:44 +0000105
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 McIntyredbd7fe52014-11-27 16:54:29 +0000120 self.cursor.execute(sql, data)
Steve McIntyre95614c22014-11-28 17:02:44 +0000121
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 Pigott281203e2014-09-17 23:45:02 +0100124 return self.cursor.fetchone()[0]
125
126 def get_switch_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100127 return self._get_element("switch_id", "switch", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100128
129 def get_port_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100130 return self._get_element("port_id", "port", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100131
Steve McIntyre9f403e82014-11-28 18:10:09 +0000132 def get_vlan_id_from_name(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100133 return self._get_element("vlan_id", "vlan", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100134
Steve McIntyre9f403e82014-11-28 18:10:09 +0000135 def get_vlan_id_from_tag(self, tag):
136 return self._get_element("vlan_id", "vlan", "tag", tag)
137
Dave Pigott281203e2014-09-17 23:45:02 +0100138 def get_switch_name(self, switch_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100139 return self._get_element("name", "switch", "switch_id", switch_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100140
141 def get_port_name(self, port_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100142 return self._get_element("port_name", "port", "port_id", port_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100143
144 def get_vlan_name(self, vlan_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100145 return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id)
146
147 def _get_row(self, table, field, value):
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000148 sql = "SELECT * FROM %s WHERE %s = %s"
149 data = (table, field, value)
150 self.cursor.execute(sql, data)
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100151 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 McIntyredbd7fe52014-11-27 16:54:29 +0000175 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 Pigott9b73f3a2014-09-18 22:55:42 +0100178 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 McIntyredbd7fe52014-11-27 16:54:29 +0000191 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 Pigott9b73f3a2014-09-18 22:55:42 +0100194 except:
195 self.connection.rollback()
196 raise
197
Dave Pigott281203e2014-09-17 23:45:02 +0100198 def _dump_table(self, table):
199 result = []
200 self.cursor.execute("SELECT * FROM %s" % table)
Dave Pigott281203e2014-09-17 23:45:02 +0100201 record = self.cursor.fetchone()
202 while record != None:
Steve McIntyree73eb122014-11-27 15:18:47 +0000203 result.append(record)
Dave Pigott281203e2014-09-17 23:45:02 +0100204 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 Pigott9b73f3a2014-09-18 22:55:42 +0100215