blob: 7c9fc7567be481bc2078b689b0ec32d83be31c1f [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
Steve McIntyre6b013652014-12-02 12:35:18 +000024from errors import CriticalError, InputError
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)
Steve McIntyreb09ed282014-12-02 17:59:35 +000030 self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
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 McIntyre31d6dfa2014-12-02 12:35:56 +000038 # Create a new switch in the database. Switches are really simple
39 # devices - they're just containers for ports.
40 #
41 # Constraints:
42 # Switches must be uniquely named
Steve McIntyredbd7fe52014-11-27 16:54:29 +000043 def create_switch(self, name):
Steve McIntyre31d6dfa2014-12-02 12:35:56 +000044
Steve McIntyre549435f2014-12-05 15:42:46 +000045 switch_id = self.get_switch_id_by_name(name)
Steve McIntyre31d6dfa2014-12-02 12:35:56 +000046 if switch_id is not None:
47 raise InputError("Switch name %s already exists" % name)
48
Dave Pigott2649a1a2014-09-18 00:04:49 +010049 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000050 sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id"
Steve McIntyre31d6dfa2014-12-02 12:35:56 +000051 data = (name, )
Steve McIntyredbd7fe52014-11-27 16:54:29 +000052 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010053 switch_id = self.cursor.fetchone()[0]
54 self.connection.commit()
55 except:
56 self.connection.rollback()
57 raise
Steve McIntyree1febdb2014-12-02 12:39:14 +000058
Dave Pigott281203e2014-09-17 23:45:02 +010059 return switch_id
60
Steve McIntyre90a4a972014-11-28 16:50:56 +000061 # Create a new port in the database. Two of the fields are created
62 # with default values (is_locked, is_trunk) here, and should be
63 # updated separately if desired. For the current_vlan_id and
64 # base_vlan_id fields, *BE CAREFUL* that you have already looked
65 # up the correct VLAN_ID for each. This is *NOT* the same as the
66 # VLAN tag (likely to be 1).
67 # You Have Been Warned!
Steve McIntyrecb42ebf2014-12-02 12:36:45 +000068 #
69 # Constraints:
70 # 1. The switch referred to must already exist
71 # 2. The VLANs mentioned here must already exist
Steve McIntyre6a7fdb22014-12-05 15:17:30 +000072 # 3. (Switch/name) must be unique
Steve McIntyre1d10dbe2014-12-02 18:23:36 +000073 def create_port(self, switch_id, name, current_vlan_id, base_vlan_id):
Steve McIntyrecb42ebf2014-12-02 12:36:45 +000074
Steve McIntyre549435f2014-12-05 15:42:46 +000075 switch = self.get_switch_by_id(switch_id)
Steve McIntyrecb42ebf2014-12-02 12:36:45 +000076 if switch is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +000077 raise InputError("Switch ID %d does not exist" % int(switch_id))
Steve McIntyrecb42ebf2014-12-02 12:36:45 +000078
79 for vlan_id in (current_vlan_id, base_vlan_id):
Steve McIntyre549435f2014-12-05 15:42:46 +000080 vlan = self.get_vlan_by_id(vlan_id)
Steve McIntyrecb42ebf2014-12-02 12:36:45 +000081 if vlan is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +000082 raise InputError("VLAN ID %d does not exist" % int(vlan_id))
Steve McIntyre6a7fdb22014-12-05 15:17:30 +000083
84 port_id = self.get_port_by_switch_and_name(switch_id, name)
85 if port_id is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +000086 raise InputError("Already have a port %s on switch ID %d" % (name, int(switch_id)))
Steve McIntyre6a7fdb22014-12-05 15:17:30 +000087
Dave Pigott2649a1a2014-09-18 00:04:49 +010088 try:
Steve McIntyre4b918132014-12-05 17:04:46 +000089 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"
90 data = (name, switch_id,
Steve McIntyre90a4a972014-11-28 16:50:56 +000091 False, False,
Steve McIntyre4b918132014-12-05 17:04:46 +000092 current_vlan_id, base_vlan_id)
Steve McIntyredbd7fe52014-11-27 16:54:29 +000093 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010094 port_id = self.cursor.fetchone()[0]
95 self.connection.commit()
96 except:
97 self.connection.rollback()
98 raise
Steve McIntyree1febdb2014-12-02 12:39:14 +000099
Dave Pigott281203e2014-09-17 23:45:02 +0100100 return port_id
101
Steve McIntyreb005a2f2014-11-28 18:23:05 +0000102 # Create a new vlan in the database. We locally add a creation
103 # timestamp, for debug purposes. If vlans seems to be sticking
104 # around, we'll be able to see when they were created.
Steve McIntyre31b2df52014-12-02 12:37:54 +0000105 #
106 # Constraints:
107 # Names and tags must be unique
Steve McIntyre57f45912014-12-08 14:43:00 +0000108 # Tags must be in the range 1-4095 (802.1q spec)
Steve McIntyre2d685c72014-12-08 15:24:12 +0000109 # Names can be any free-form text
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000110 def create_vlan(self, name, tag, is_base_vlan):
Steve McIntyre31b2df52014-12-02 12:37:54 +0000111
Steve McIntyre57f45912014-12-08 14:43:00 +0000112 if int(tag) < 1 or int(tag) > 4095:
113 raise InputError("VLAN tag %d is outside of the valid range (1-4095)" % int(tag)
114
Steve McIntyrea34c1812014-12-05 15:27:55 +0000115 vlan_id = self.get_vlan_id_by_name(name)
Steve McIntyre31b2df52014-12-02 12:37:54 +0000116 if vlan_id is not None:
117 raise InputError("VLAN name %s is already in use" % name)
118
Steve McIntyre50eb0602014-12-05 15:29:04 +0000119 vlan_id = self.get_vlan_id_by_tag(tag)
Steve McIntyre31b2df52014-12-02 12:37:54 +0000120 if vlan_id is not None:
121 raise InputError("VLAN tag %d is already in use" % int(tag))
122
Dave Pigott2649a1a2014-09-18 00:04:49 +0100123 try:
Steve McIntyred74d97c2014-11-28 14:44:39 +0000124 dt = datetime.datetime.now()
Steve McIntyre4b918132014-12-05 17:04:46 +0000125 sql = "INSERT INTO vlan (name, tag, is_base_vlan, creation_time) VALUES (%s, %s, %s, %s) RETURNING vlan_id"
126 data = (name, tag, is_base_vlan, dt)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000127 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +0100128 vlan_id = self.cursor.fetchone()[0]
129 self.connection.commit()
130 except:
131 self.connection.rollback()
132 raise
Steve McIntyree1febdb2014-12-02 12:39:14 +0000133
Dave Pigott281203e2014-09-17 23:45:02 +0100134 return vlan_id
135
Steve McIntyre2d685c72014-12-08 15:24:12 +0000136 # Internal helper function
Dave Pigott281203e2014-09-17 23:45:02 +0100137 def _delete_row(self, table, field, value):
Dave Pigott2649a1a2014-09-18 00:04:49 +0100138 try:
Steve McIntyree03de002014-12-02 17:14:14 +0000139 sql = "DELETE FROM %s WHERE %s = %s" % (table, field, '%s')
140 data = (value,)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000141 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +0100142 self.connection.commit()
143 except:
144 self.connection.rollback()
145 raise
Dave Pigott281203e2014-09-17 23:45:02 +0100146
Steve McIntyre388f0e22014-12-02 17:19:04 +0000147 # Delete the specified switch
148 #
149 # Constraints:
150 # 1. The switch must exist
151 # 2. The switch may not be referenced by any ports -
152 # delete them first!
Dave Pigott281203e2014-09-17 23:45:02 +0100153 def delete_switch(self, switch_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000154 switch = self.get_switch_by_id(switch_id)
Steve McIntyre388f0e22014-12-02 17:19:04 +0000155 if switch is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000156 raise InputError("Switch ID %d does not exist" % int(switch_id))
Steve McIntyre388f0e22014-12-02 17:19:04 +0000157 ports = self.get_ports_by_switch(switch_id)
158 if ports is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000159 raise InputError("Cannot delete switch ID %d when it still has %d ports" %
160 (int(switch_id), len(ports)))
Dave Pigott281203e2014-09-17 23:45:02 +0100161 self._delete_row("switch", "switch_id", switch_id)
Steve McIntyre388f0e22014-12-02 17:19:04 +0000162 return switch_id
Dave Pigott281203e2014-09-17 23:45:02 +0100163
Steve McIntyre6a968622014-12-02 18:01:41 +0000164 # Delete the specified port
165 #
166 # Constraints:
167 # 1. The port must exist
168 # 2. The port must not be locked
Dave Pigott281203e2014-09-17 23:45:02 +0100169 def delete_port(self, port_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000170 port = self.get_port_by_id(port_id)
Steve McIntyre6a968622014-12-02 18:01:41 +0000171 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000172 raise InputError("Port ID %d does not exist" % int(port_id))
Steve McIntyre6a968622014-12-02 18:01:41 +0000173 if port.is_locked:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000174 raise InputError("Cannot delete port ID %d as it is locked" % int(port_id))
Dave Pigott281203e2014-09-17 23:45:02 +0100175 self._delete_row("port", "port_id", port_id)
Steve McIntyre6a968622014-12-02 18:01:41 +0000176 return port_id
Dave Pigott281203e2014-09-17 23:45:02 +0100177
Steve McIntyre14552ac2014-12-05 15:23:57 +0000178 # Delete the specified VLAN
179 #
180 # Constraints:
181 # 1. The VLAN
182 # 2. The VLAN may not contain any ports - move or delete them first!
Dave Pigott281203e2014-09-17 23:45:02 +0100183 def delete_vlan(self, vlan_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000184 vlan = self.get_vlan_by_id(vlan_id)
Steve McIntyre14552ac2014-12-05 15:23:57 +0000185 if vlan is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000186 raise InputError("VLAN ID %d does not exist" % int(vlan_id))
Steve McIntyre14552ac2014-12-05 15:23:57 +0000187 ports = self.get_ports_by_current_vlan(vlan_id)
188 if ports is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000189 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
190 (int(vlan_id), len(ports)))
Steve McIntyre14552ac2014-12-05 15:23:57 +0000191 ports = self.get_ports_by_base_vlan(vlan_id)
192 if ports is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000193 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
194 (int(vlan_id), len(ports)))
Dave Pigott281203e2014-09-17 23:45:02 +0100195 self._delete_row("vlan", "vlan_id", vlan_id)
Steve McIntyre14552ac2014-12-05 15:23:57 +0000196 return vlan_id
Dave Pigott281203e2014-09-17 23:45:02 +0100197
Steve McIntyre2d685c72014-12-08 15:24:12 +0000198 # Grab one column from one row of a query on one column; useful as
199 # a quick wrapper
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100200 def _get_element(self, select_field, table, compare_field, value):
Steve McIntyre95614c22014-11-28 17:02:44 +0000201
202 # We really want to use psycopg's type handling deal with the
203 # (potentially) user-supplied data in the value field, so we
204 # have to pass (sql,data) through to cursor.execute. However,
205 # we can't have psycopg do all the argument substitution here
206 # as it will quote all the params like the table name. That
207 # doesn't work. So, we substitute a "%s" for "%s" here so we
208 # keep it after python's own string substitution.
209 sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s")
210
211 # Now, the next icky thing: we need to make sure that we're
212 # passing a dict so that psycopg2 can pick it apart properly
213 # for its own substitution code. We force this with the
214 # trailing comma here
215 data = (value, )
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000216 self.cursor.execute(sql, data)
Steve McIntyre95614c22014-11-28 17:02:44 +0000217
Steve McIntyre58b57a42014-12-02 13:09:21 +0000218 if self.cursor.rowcount > 0:
219 return self.cursor.fetchone()[0]
220 else:
Steve McIntyrec831f9c2014-12-02 12:38:54 +0000221 return None
Dave Pigott281203e2014-09-17 23:45:02 +0100222
Steve McIntyre2d685c72014-12-08 15:24:12 +0000223 # Grab one column from one row of a query on 2 columns; useful as
224 # a quick wrapper
Steve McIntyrea74c7fe2014-12-02 18:49:38 +0000225 def _get_element2(self, select_field, table, compare_field1, value1, compare_field2, value2):
226
227 # We really want to use psycopg's type handling deal with the
228 # (potentially) user-supplied data in the value field, so we
229 # have to pass (sql,data) through to cursor.execute. However,
230 # we can't have psycopg do all the argument substitution here
231 # as it will quote all the params like the table name. That
232 # doesn't work. So, we substitute a "%s" for "%s" here so we
233 # keep it after python's own string substitution.
234 sql = "SELECT %s FROM %s WHERE %s = %s AND %s = %s" % (select_field, table, compare_field1, "%s", compare_field2, "%s")
235
Steve McIntyrea74c7fe2014-12-02 18:49:38 +0000236 data = (value1, value2)
237 self.cursor.execute(sql, data)
238
239 if self.cursor.rowcount > 0:
240 return self.cursor.fetchone()[0]
241 else:
242 return None
243
Steve McIntyre2d685c72014-12-08 15:24:12 +0000244 # Grab one column from multiple rows of a query; useful as a quick
245 # wrapper
Steve McIntyree9da15e2014-12-05 15:22:41 +0000246 def _get_multi_elements(self, select_field, table, compare_field, value):
247
248 # We really want to use psycopg's type handling deal with the
249 # (potentially) user-supplied data in the value field, so we
250 # have to pass (sql,data) through to cursor.execute. However,
251 # we can't have psycopg do all the argument substitution here
252 # as it will quote all the params like the table name. That
253 # doesn't work. So, we substitute a "%s" for "%s" here so we
254 # keep it after python's own string substitution.
255 sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s")
256
257 # Now, the next icky thing: we need to make sure that we're
258 # passing a dict so that psycopg2 can pick it apart properly
259 # for its own substitution code. We force this with the
260 # trailing comma here
261 data = (value, )
262 self.cursor.execute(sql, data)
263
264 if self.cursor.rowcount > 0:
265 results = []
266 for record in self.cursor:
Steve McIntyre52509622014-12-02 17:13:15 +0000267 results.append(record[0])
Steve McIntyree9da15e2014-12-05 15:22:41 +0000268 return results
Steve McIntyre52509622014-12-02 17:13:15 +0000269 else:
270 return None
271
Steve McIntyre2d685c72014-12-08 15:24:12 +0000272 # Simple lookup: look up a switch by ID, and return all the
273 # details of that switch.
274 #
275 # Returns None on failure.
Steve McIntyre549435f2014-12-05 15:42:46 +0000276 def get_switch_by_id(self, switch_id):
Steve McIntyref3655062014-12-05 15:34:39 +0000277 return self._get_row("switch", "switch_id", switch_id)
278
Steve McIntyre2d685c72014-12-08 15:24:12 +0000279 # Simple lookup: look up a switch by name, and return the ID of
280 # that switch.
281 #
282 # Returns None on failure.
Steve McIntyre549435f2014-12-05 15:42:46 +0000283 def get_switch_id_by_name(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100284 return self._get_element("switch_id", "switch", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100285
Steve McIntyre2d685c72014-12-08 15:24:12 +0000286 # Simple lookup: look up a switch by ID, and return the name of
287 # that switch.
288 #
289 # Returns None on failure.
Steve McIntyre549435f2014-12-05 15:42:46 +0000290 def get_switch_name_by_id(self, switch_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100291 return self._get_element("name", "switch", "switch_id", switch_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100292
Steve McIntyre2d685c72014-12-08 15:24:12 +0000293 # Simple lookup: look up a port by ID, and return all the details
294 # of that port.
295 #
296 # Returns None on failure.
Steve McIntyre549435f2014-12-05 15:42:46 +0000297 def get_port_by_id(self, port_id):
Steve McIntyref3655062014-12-05 15:34:39 +0000298 return self._get_row("port", "port_id", port_id)
299
Steve McIntyre2d685c72014-12-08 15:24:12 +0000300 # Simple lookup: look up a switch by ID, and return the IDs of all
301 # the ports on that switch.
302 #
303 # Returns None on failure.
Steve McIntyreb67f3912014-12-02 17:14:36 +0000304 def get_ports_by_switch(self, switch_id):
305 return self._get_multi_elements("port_id", "port", "switch_id", switch_id)
306
Steve McIntyre2d685c72014-12-08 15:24:12 +0000307 # Simple lookup: look up a port by its name and its parent switch
308 # by ID, and return the ID of the port.
309 #
310 # Returns None on failure.
Steve McIntyre53a7bc82014-12-05 15:23:34 +0000311 def get_port_by_switch_and_name(self, switch_id, name):
312 return self._get_element2("port_id", "port", "switch_id", switch_id, "name", name)
313
Steve McIntyre2d685c72014-12-08 15:24:12 +0000314 # Simple lookup: look up a port by ID, and return the current VLAN
315 # id of that port.
316 #
317 # Returns None on failure.
Steve McIntyredaae5502014-12-05 17:55:18 +0000318 def get_current_vlan_id_by_port(self, port_id):
319 return self._get_element("current_vlan_id", "port", "port_id", port_id)
320
Steve McIntyre2d685c72014-12-08 15:24:12 +0000321 # Simple lookup: look up a port by ID, and return the base VLAN
322 # id of that port.
323 #
324 # Returns None on failure.
Steve McIntyredaae5502014-12-05 17:55:18 +0000325 def get_base_vlan_id_by_port(self, port_id):
326 return self._get_element("base_vlan_id", "port", "port_id", port_id)
327
Steve McIntyre2d685c72014-12-08 15:24:12 +0000328 # Simple lookup: look up a current VLAN by ID, and return the IDs
329 # of all the ports on that VLAN.
330 #
331 # Returns None on failure.
Steve McIntyre53a7bc82014-12-05 15:23:34 +0000332 def get_ports_by_current_vlan(self, vlan_id):
333 return self._get_multi_elements("port_id", "port", "current_vlan_id", vlan_id)
334
Steve McIntyre2d685c72014-12-08 15:24:12 +0000335 # Simple lookup: look up a base VLAN by ID, and return the IDs
336 # of all the ports on that VLAN.
337 #
338 # Returns None on failure.
Steve McIntyre53a7bc82014-12-05 15:23:34 +0000339 def get_ports_by_base_vlan(self, vlan_id):
340 return self._get_multi_elements("port_id", "port", "base_vlan_id", vlan_id)
341
Steve McIntyre2d685c72014-12-08 15:24:12 +0000342 # Simple lookup: look up a VLAN by ID, and return all the details
343 # of that VLAN.
344 #
345 # Returns None on failure.
Steve McIntyre549435f2014-12-05 15:42:46 +0000346 def get_vlan_by_id(self, vlan_id):
Steve McIntyref3655062014-12-05 15:34:39 +0000347 return self._get_row("vlan", "vlan_id", vlan_id)
348
Steve McIntyre2d685c72014-12-08 15:24:12 +0000349 # Simple lookup: look up a VLAN by name, and return the ID of that
350 # VLAN.
351 #
352 # Returns None on failure.
Steve McIntyref3655062014-12-05 15:34:39 +0000353 def get_vlan_id_by_name(self, name):
354 return self._get_element("vlan_id", "vlan", "name", name)
355
Steve McIntyre2d685c72014-12-08 15:24:12 +0000356 # Simple lookup: look up a VLAN by tag, and return the ID of that
357 # VLAN.
358 #
359 # Returns None on failure.
Steve McIntyref3655062014-12-05 15:34:39 +0000360 def get_vlan_id_by_tag(self, tag):
361 return self._get_element("vlan_id", "vlan", "tag", tag)
362
Steve McIntyre2d685c72014-12-08 15:24:12 +0000363 # Simple lookup: look up a VLAN by ID, and return the name of that
364 # VLAN.
365 #
366 # Returns None on failure.
Steve McIntyre549435f2014-12-05 15:42:46 +0000367 def get_vlan_name_by_id(self, vlan_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100368 return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id)
369
Steve McIntyre2d685c72014-12-08 15:24:12 +0000370 # Grab one row of a query on one column; useful as a quick wrapper
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100371 def _get_row(self, table, field, value):
Steve McIntyree0b842a2014-11-28 18:23:47 +0000372
373 # We really want to use psycopg's type handling deal with the
374 # (potentially) user-supplied data in the value field, so we
375 # have to pass (sql,data) through to cursor.execute. However,
376 # we can't have psycopg do all the argument substitution here
377 # as it will quote all the params like the table name. That
378 # doesn't work. So, we substitute a "%s" for "%s" here so we
379 # keep it after python's own string substitution.
380 sql = "SELECT * FROM %s WHERE %s = %s" % (table, field, "%s")
381
382 # Now, the next icky thing: we need to make sure that we're
383 # passing a dict so that psycopg2 can pick it apart properly
384 # for its own substitution code. We force this with the
385 # trailing comma here
386 data = (value, )
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000387 self.cursor.execute(sql, data)
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100388 return self.cursor.fetchone()
389
Steve McIntyre3330f4b2014-11-28 18:11:02 +0000390 # (Un)Lock a port in the database. This can only be done through
391 # the admin interface, and will stop API users from modifying
392 # settings on the port. Use this to lock down ports that are used
393 # for PDUs and other core infrastructure
394 def set_port_is_locked(self, port_id, is_locked):
Steve McIntyre8c64d952014-12-05 16:22:44 +0000395 port = self.get_port_by_id(port_id)
396 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000397 raise InputError("Port ID %d does not exist" % int(port_id))
Steve McIntyre3330f4b2014-11-28 18:11:02 +0000398 try:
Steve McIntyree1371102014-12-05 17:17:09 +0000399 sql = "UPDATE port SET is_locked=%s WHERE port_id=%s RETURNING port_id"
Steve McIntyre4b918132014-12-05 17:04:46 +0000400 data = (is_locked, port_id)
Steve McIntyre3330f4b2014-11-28 18:11:02 +0000401 self.cursor.execute(sql, data)
402 port_id = self.cursor.fetchone()[0]
403 self.connection.commit()
404 except:
405 self.connection.rollback()
406 raise
407 return port_id
408
Steve McIntyre4204d0d2014-12-05 16:24:10 +0000409 # Set the mode of a port in the database. Valid values for mode
410 # are "trunk" and "access"
411 def set_port_mode(self, port_id, mode):
412 port = self.get_port_by_id(port_id)
413 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000414 raise InputError("Port ID %d does not exist" % int(port_id))
Steve McIntyre4204d0d2014-12-05 16:24:10 +0000415 if mode == "access":
416 is_trunk = False
417 elif mode == "trunk":
418 is_trunk = True
419 else:
420 raise InputError("Port mode %s is not valid" % mode)
421 try:
Steve McIntyree1371102014-12-05 17:17:09 +0000422 sql = "UPDATE port SET is_trunk=%s WHERE port_id=%s RETURNING port_id"
Steve McIntyre4b918132014-12-05 17:04:46 +0000423 data = (is_trunk, port_id)
Steve McIntyre4204d0d2014-12-05 16:24:10 +0000424 self.cursor.execute(sql, data)
425 port_id = self.cursor.fetchone()[0]
426 self.connection.commit()
427 except:
428 self.connection.rollback()
429 raise
430 return port_id
431
Steve McIntyre2d685c72014-12-08 15:24:12 +0000432 # Set the current vlan of a port in the database. The VLAN is
433 # passed by ID.
434 #
435 # Constraints:
436 # 1. The port must already exist
437 # 2. The port must not be a trunk port
438 # 3. The port must not be locked
439 # 1. The VLAN must already exist in the database
Steve McIntyre9eb78652014-12-05 17:51:53 +0000440 def set_current_vlan(self, port_id, vlan_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000441 port = self.get_port_by_id(port_id)
Steve McIntyre028b3cc2014-12-05 16:24:46 +0000442 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000443 raise InputError("Port ID %d does not exist" % int(port_id))
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100444
Steve McIntyre6dd00be2014-12-05 17:29:35 +0000445 if port.is_trunk or port.is_locked:
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100446 raise CriticalError("The port is locked")
447
Steve McIntyre549435f2014-12-05 15:42:46 +0000448 vlan = self.get_vlan_by_id(vlan_id)
Steve McIntyre028b3cc2014-12-05 16:24:46 +0000449 if vlan is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000450 raise InputError("VLAN ID %d does not exist" % int(vlan_id))
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100451
452 try:
Steve McIntyree1371102014-12-05 17:17:09 +0000453 sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s RETURNING port_id"
Steve McIntyre4b918132014-12-05 17:04:46 +0000454 data = (vlan_id, port_id)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000455 self.cursor.execute(sql, data)
Steve McIntyree1371102014-12-05 17:17:09 +0000456 port_id = self.cursor.fetchone()[0]
457 self.connection.commit()
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100458 except:
459 self.connection.rollback()
460 raise
Steve McIntyree1371102014-12-05 17:17:09 +0000461 return port_id
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100462
Steve McIntyre2d685c72014-12-08 15:24:12 +0000463 # Set the base vlan of a port in the database. The VLAN is
464 # passed by ID.
465 #
466 # Constraints:
467 # 1. The port must already exist
468 # 2. The port must not be a trunk port
469 # 3. The port must not be locked
470 # 1. The VLAN must already exist in the database
Steve McIntyredaae5502014-12-05 17:55:18 +0000471 def set_base_vlan(self, port_id, vlan_id):
472 port = self.get_port_by_id(port_id)
473 if port is None:
474 raise InputError("Port ID %d does not exist" % int(port_id))
475
476 if port.is_trunk or port.is_locked:
477 raise CriticalError("The port is locked")
478
479 vlan = self.get_vlan_by_id(vlan_id)
480 if vlan is None:
481 raise InputError("VLAN ID %d does not exist" % int(vlan_id))
482 if not vlan.is_base_vlan:
483 raise InputError("VLAN ID %d is not a base VLAN" % int(vlan_id))
484
485 try:
486 sql = "UPDATE port SET base_vlan_id=%s WHERE port_id=%s RETURNING port_id"
487 data = (vlan_id, port_id)
488 self.cursor.execute(sql, data)
489 port_id = self.cursor.fetchone()[0]
490 self.connection.commit()
491 except:
492 self.connection.rollback()
493 raise
494 return port_id
495
Steve McIntyre2d685c72014-12-08 15:24:12 +0000496 # Return a port back to its base VLAN
497 #
498 # Constraints:
499 # 1. The port must already exist
500 # 2. The port must not be a trunk port
501 # 3. The port must not be locked
Steve McIntyredaae5502014-12-05 17:55:18 +0000502 def restore_base_vlan(self, port_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000503 port = self.get_port_by_id(port_id)
Steve McIntyre028b3cc2014-12-05 16:24:46 +0000504 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000505 raise InputError("Port ID %d does not exist" % int(port_id))
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100506
Steve McIntyre6dd00be2014-12-05 17:29:35 +0000507 if port.is_trunk or port.is_locked:
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100508 raise CriticalError("The port is locked")
509
510 try:
Steve McIntyree1371102014-12-05 17:17:09 +0000511 sql = "UPDATE port SET current_vlan_id=base_vlan_id WHERE port_id=%s RETURNING port_id"
Steve McIntyre4b918132014-12-05 17:04:46 +0000512 data = (port_id,)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000513 self.cursor.execute(sql, data)
Steve McIntyree1371102014-12-05 17:17:09 +0000514 port_id = self.cursor.fetchone()[0]
515 self.connection.commit()
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100516 except:
517 self.connection.rollback()
518 raise
Steve McIntyree1371102014-12-05 17:17:09 +0000519 return port_id
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100520
Steve McIntyre2d685c72014-12-08 15:24:12 +0000521 # Trivial helper function to return all the rows in a given table
Dave Pigott281203e2014-09-17 23:45:02 +0100522 def _dump_table(self, table):
523 result = []
524 self.cursor.execute("SELECT * FROM %s" % table)
Dave Pigott281203e2014-09-17 23:45:02 +0100525 record = self.cursor.fetchone()
526 while record != None:
Steve McIntyree73eb122014-11-27 15:18:47 +0000527 result.append(record)
Dave Pigott281203e2014-09-17 23:45:02 +0100528 record = self.cursor.fetchone()
529 return result
530
531 def all_switches(self):
532 return self._dump_table("switch")
533
534 def all_ports(self):
535 return self._dump_table("port")
536
537 def all_vlans(self):
538 return self._dump_table("vlan")
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100539