blob: 58536275fc9297f71c7d597eb847bc32354da735 [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 McIntyredbd7fe52014-11-27 16:54:29 +0000108 def create_vlan(self, name, tag, is_base_vlan):
Steve McIntyre31b2df52014-12-02 12:37:54 +0000109
Steve McIntyrea34c1812014-12-05 15:27:55 +0000110 vlan_id = self.get_vlan_id_by_name(name)
Steve McIntyre31b2df52014-12-02 12:37:54 +0000111 if vlan_id is not None:
112 raise InputError("VLAN name %s is already in use" % name)
113
Steve McIntyre50eb0602014-12-05 15:29:04 +0000114 vlan_id = self.get_vlan_id_by_tag(tag)
Steve McIntyre31b2df52014-12-02 12:37:54 +0000115 if vlan_id is not None:
116 raise InputError("VLAN tag %d is already in use" % int(tag))
117
Dave Pigott2649a1a2014-09-18 00:04:49 +0100118 try:
Steve McIntyred74d97c2014-11-28 14:44:39 +0000119 dt = datetime.datetime.now()
Steve McIntyre4b918132014-12-05 17:04:46 +0000120 sql = "INSERT INTO vlan (name, tag, is_base_vlan, creation_time) VALUES (%s, %s, %s, %s) RETURNING vlan_id"
121 data = (name, tag, is_base_vlan, dt)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000122 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +0100123 vlan_id = self.cursor.fetchone()[0]
124 self.connection.commit()
125 except:
126 self.connection.rollback()
127 raise
Steve McIntyree1febdb2014-12-02 12:39:14 +0000128
Dave Pigott281203e2014-09-17 23:45:02 +0100129 return vlan_id
130
131 def _delete_row(self, table, field, value):
Dave Pigott2649a1a2014-09-18 00:04:49 +0100132 try:
Steve McIntyree03de002014-12-02 17:14:14 +0000133 sql = "DELETE FROM %s WHERE %s = %s" % (table, field, '%s')
134 data = (value,)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000135 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +0100136 self.connection.commit()
137 except:
138 self.connection.rollback()
139 raise
Dave Pigott281203e2014-09-17 23:45:02 +0100140
Steve McIntyre388f0e22014-12-02 17:19:04 +0000141 # Delete the specified switch
142 #
143 # Constraints:
144 # 1. The switch must exist
145 # 2. The switch may not be referenced by any ports -
146 # delete them first!
Dave Pigott281203e2014-09-17 23:45:02 +0100147 def delete_switch(self, switch_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000148 switch = self.get_switch_by_id(switch_id)
Steve McIntyre388f0e22014-12-02 17:19:04 +0000149 if switch is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000150 raise InputError("Switch ID %d does not exist" % int(switch_id))
Steve McIntyre388f0e22014-12-02 17:19:04 +0000151 ports = self.get_ports_by_switch(switch_id)
152 if ports is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000153 raise InputError("Cannot delete switch ID %d when it still has %d ports" %
154 (int(switch_id), len(ports)))
Dave Pigott281203e2014-09-17 23:45:02 +0100155 self._delete_row("switch", "switch_id", switch_id)
Steve McIntyre388f0e22014-12-02 17:19:04 +0000156 return switch_id
Dave Pigott281203e2014-09-17 23:45:02 +0100157
Steve McIntyre6a968622014-12-02 18:01:41 +0000158 # Delete the specified port
159 #
160 # Constraints:
161 # 1. The port must exist
162 # 2. The port must not be locked
Dave Pigott281203e2014-09-17 23:45:02 +0100163 def delete_port(self, port_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000164 port = self.get_port_by_id(port_id)
Steve McIntyre6a968622014-12-02 18:01:41 +0000165 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000166 raise InputError("Port ID %d does not exist" % int(port_id))
Steve McIntyre6a968622014-12-02 18:01:41 +0000167 if port.is_locked:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000168 raise InputError("Cannot delete port ID %d as it is locked" % int(port_id))
Dave Pigott281203e2014-09-17 23:45:02 +0100169 self._delete_row("port", "port_id", port_id)
Steve McIntyre6a968622014-12-02 18:01:41 +0000170 return port_id
Dave Pigott281203e2014-09-17 23:45:02 +0100171
Steve McIntyre14552ac2014-12-05 15:23:57 +0000172 # Delete the specified VLAN
173 #
174 # Constraints:
175 # 1. The VLAN
176 # 2. The VLAN may not contain any ports - move or delete them first!
Dave Pigott281203e2014-09-17 23:45:02 +0100177 def delete_vlan(self, vlan_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000178 vlan = self.get_vlan_by_id(vlan_id)
Steve McIntyre14552ac2014-12-05 15:23:57 +0000179 if vlan is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000180 raise InputError("VLAN ID %d does not exist" % int(vlan_id))
Steve McIntyre14552ac2014-12-05 15:23:57 +0000181 ports = self.get_ports_by_current_vlan(vlan_id)
182 if ports is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000183 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
184 (int(vlan_id), len(ports)))
Steve McIntyre14552ac2014-12-05 15:23:57 +0000185 ports = self.get_ports_by_base_vlan(vlan_id)
186 if ports is not None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000187 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
188 (int(vlan_id), len(ports)))
Dave Pigott281203e2014-09-17 23:45:02 +0100189 self._delete_row("vlan", "vlan_id", vlan_id)
Steve McIntyre14552ac2014-12-05 15:23:57 +0000190 return vlan_id
Dave Pigott281203e2014-09-17 23:45:02 +0100191
Steve McIntyre0884f932014-12-05 15:18:14 +0000192 # Grab one column from one row of a query on one column; useful as a quick wrapper
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100193 def _get_element(self, select_field, table, compare_field, value):
Steve McIntyre95614c22014-11-28 17:02:44 +0000194
195 # We really want to use psycopg's type handling deal with the
196 # (potentially) user-supplied data in the value field, so we
197 # have to pass (sql,data) through to cursor.execute. However,
198 # we can't have psycopg do all the argument substitution here
199 # as it will quote all the params like the table name. That
200 # doesn't work. So, we substitute a "%s" for "%s" here so we
201 # keep it after python's own string substitution.
202 sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s")
203
204 # Now, the next icky thing: we need to make sure that we're
205 # passing a dict so that psycopg2 can pick it apart properly
206 # for its own substitution code. We force this with the
207 # trailing comma here
208 data = (value, )
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000209 self.cursor.execute(sql, data)
Steve McIntyre95614c22014-11-28 17:02:44 +0000210
Steve McIntyre58b57a42014-12-02 13:09:21 +0000211 if self.cursor.rowcount > 0:
212 return self.cursor.fetchone()[0]
213 else:
Steve McIntyrec831f9c2014-12-02 12:38:54 +0000214 return None
Dave Pigott281203e2014-09-17 23:45:02 +0100215
Steve McIntyrea74c7fe2014-12-02 18:49:38 +0000216 # Grab one column from one row of a query on 2 columns; useful as a quick wrapper
217 def _get_element2(self, select_field, table, compare_field1, value1, compare_field2, value2):
218
219 # We really want to use psycopg's type handling deal with the
220 # (potentially) user-supplied data in the value field, so we
221 # have to pass (sql,data) through to cursor.execute. However,
222 # we can't have psycopg do all the argument substitution here
223 # as it will quote all the params like the table name. That
224 # doesn't work. So, we substitute a "%s" for "%s" here so we
225 # keep it after python's own string substitution.
226 sql = "SELECT %s FROM %s WHERE %s = %s AND %s = %s" % (select_field, table, compare_field1, "%s", compare_field2, "%s")
227
228 # Now, the next icky thing: we need to make sure that we're
229 # passing a dict so that psycopg2 can pick it apart properly
230 # for its own substitution code. We force this with the
231 # trailing comma here
232 data = (value1, value2)
233 self.cursor.execute(sql, data)
234
235 if self.cursor.rowcount > 0:
236 return self.cursor.fetchone()[0]
237 else:
238 return None
239
Steve McIntyree9da15e2014-12-05 15:22:41 +0000240 # Grab one column from multiple rows of a query; useful as a quick wrapper
241 def _get_multi_elements(self, select_field, table, compare_field, value):
242
243 # We really want to use psycopg's type handling deal with the
244 # (potentially) user-supplied data in the value field, so we
245 # have to pass (sql,data) through to cursor.execute. However,
246 # we can't have psycopg do all the argument substitution here
247 # as it will quote all the params like the table name. That
248 # doesn't work. So, we substitute a "%s" for "%s" here so we
249 # keep it after python's own string substitution.
250 sql = "SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, "%s")
251
252 # Now, the next icky thing: we need to make sure that we're
253 # passing a dict so that psycopg2 can pick it apart properly
254 # for its own substitution code. We force this with the
255 # trailing comma here
256 data = (value, )
257 self.cursor.execute(sql, data)
258
259 if self.cursor.rowcount > 0:
260 results = []
261 for record in self.cursor:
Steve McIntyre52509622014-12-02 17:13:15 +0000262 results.append(record[0])
Steve McIntyree9da15e2014-12-05 15:22:41 +0000263 return results
Steve McIntyre52509622014-12-02 17:13:15 +0000264 else:
265 return None
266
Steve McIntyre549435f2014-12-05 15:42:46 +0000267 def get_switch_by_id(self, switch_id):
Steve McIntyref3655062014-12-05 15:34:39 +0000268 return self._get_row("switch", "switch_id", switch_id)
269
Steve McIntyre549435f2014-12-05 15:42:46 +0000270 def get_switch_id_by_name(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100271 return self._get_element("switch_id", "switch", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100272
Steve McIntyre549435f2014-12-05 15:42:46 +0000273 def get_switch_name_by_id(self, switch_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100274 return self._get_element("name", "switch", "switch_id", switch_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100275
Steve McIntyre549435f2014-12-05 15:42:46 +0000276 def get_port_by_id(self, port_id):
Steve McIntyref3655062014-12-05 15:34:39 +0000277 return self._get_row("port", "port_id", port_id)
278
Steve McIntyre549435f2014-12-05 15:42:46 +0000279 def get_port_id_by_name(self, name):
Steve McIntyref3655062014-12-05 15:34:39 +0000280 return self._get_element("port_id", "port", "name", name)
281
Steve McIntyre549435f2014-12-05 15:42:46 +0000282 def get_port_name_by_id(self, port_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100283 return self._get_element("port_name", "port", "port_id", port_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100284
Steve McIntyreb67f3912014-12-02 17:14:36 +0000285 def get_ports_by_switch(self, switch_id):
286 return self._get_multi_elements("port_id", "port", "switch_id", switch_id)
287
Steve McIntyre53a7bc82014-12-05 15:23:34 +0000288 def get_port_by_switch_and_name(self, switch_id, name):
289 return self._get_element2("port_id", "port", "switch_id", switch_id, "name", name)
290
291 def get_ports_by_current_vlan(self, vlan_id):
292 return self._get_multi_elements("port_id", "port", "current_vlan_id", vlan_id)
293
294 def get_ports_by_base_vlan(self, vlan_id):
295 return self._get_multi_elements("port_id", "port", "base_vlan_id", vlan_id)
296
Steve McIntyre549435f2014-12-05 15:42:46 +0000297 def get_vlan_by_id(self, vlan_id):
Steve McIntyref3655062014-12-05 15:34:39 +0000298 return self._get_row("vlan", "vlan_id", vlan_id)
299
300 def get_vlan_id_by_name(self, name):
301 return self._get_element("vlan_id", "vlan", "name", name)
302
303 def get_vlan_id_by_tag(self, tag):
304 return self._get_element("vlan_id", "vlan", "tag", tag)
305
Steve McIntyre549435f2014-12-05 15:42:46 +0000306 def get_vlan_name_by_id(self, vlan_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100307 return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id)
308
309 def _get_row(self, table, field, value):
Steve McIntyree0b842a2014-11-28 18:23:47 +0000310
311 # We really want to use psycopg's type handling deal with the
312 # (potentially) user-supplied data in the value field, so we
313 # have to pass (sql,data) through to cursor.execute. However,
314 # we can't have psycopg do all the argument substitution here
315 # as it will quote all the params like the table name. That
316 # doesn't work. So, we substitute a "%s" for "%s" here so we
317 # keep it after python's own string substitution.
318 sql = "SELECT * FROM %s WHERE %s = %s" % (table, field, "%s")
319
320 # Now, the next icky thing: we need to make sure that we're
321 # passing a dict so that psycopg2 can pick it apart properly
322 # for its own substitution code. We force this with the
323 # trailing comma here
324 data = (value, )
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000325 self.cursor.execute(sql, data)
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100326 return self.cursor.fetchone()
327
Steve McIntyre3330f4b2014-11-28 18:11:02 +0000328 # (Un)Lock a port in the database. This can only be done through
329 # the admin interface, and will stop API users from modifying
330 # settings on the port. Use this to lock down ports that are used
331 # for PDUs and other core infrastructure
332 def set_port_is_locked(self, port_id, is_locked):
Steve McIntyre8c64d952014-12-05 16:22:44 +0000333 port = self.get_port_by_id(port_id)
334 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000335 raise InputError("Port ID %d does not exist" % int(port_id))
Steve McIntyre3330f4b2014-11-28 18:11:02 +0000336 try:
Steve McIntyre4b918132014-12-05 17:04:46 +0000337 sql = "UPDATE port SET is_locked=%s WHERE port_id=%s"
338 data = (is_locked, port_id)
Steve McIntyre3330f4b2014-11-28 18:11:02 +0000339 self.cursor.execute(sql, data)
340 port_id = self.cursor.fetchone()[0]
341 self.connection.commit()
342 except:
343 self.connection.rollback()
344 raise
345 return port_id
346
Steve McIntyre4204d0d2014-12-05 16:24:10 +0000347 # Set the mode of a port in the database. Valid values for mode
348 # are "trunk" and "access"
349 def set_port_mode(self, port_id, mode):
350 port = self.get_port_by_id(port_id)
351 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000352 raise InputError("Port ID %d does not exist" % int(port_id))
Steve McIntyre4204d0d2014-12-05 16:24:10 +0000353 if mode == "access":
354 is_trunk = False
355 elif mode == "trunk":
356 is_trunk = True
357 else:
358 raise InputError("Port mode %s is not valid" % mode)
359 try:
Steve McIntyre4b918132014-12-05 17:04:46 +0000360 sql = "UPDATE port SET is_trunk=%s WHERE port_id=%s"
361 data = (is_trunk, port_id)
Steve McIntyre4204d0d2014-12-05 16:24:10 +0000362 self.cursor.execute(sql, data)
363 port_id = self.cursor.fetchone()[0]
364 self.connection.commit()
365 except:
366 self.connection.rollback()
367 raise
368 return port_id
369
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100370 def set_vlan(self, port_id, vlan_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000371 port = self.get_port_by_id(port_id)
Steve McIntyre028b3cc2014-12-05 16:24:46 +0000372 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000373 raise InputError("Port ID %d does not exist" % int(port_id))
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100374
375 if port["is_trunk"] or port["is_locked"]:
376 raise CriticalError("The port is locked")
377
Steve McIntyre549435f2014-12-05 15:42:46 +0000378 vlan = self.get_vlan_by_id(vlan_id)
Steve McIntyre028b3cc2014-12-05 16:24:46 +0000379 if vlan is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000380 raise InputError("VLAN ID %d does not exist" % int(vlan_id))
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100381
382 try:
Steve McIntyre4b918132014-12-05 17:04:46 +0000383 sql = "UPDATE port SET current_vlan_id=%s WHERE port_id=%s"
384 data = (vlan_id, port_id)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000385 self.cursor.execute(sql, data)
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100386 except:
387 self.connection.rollback()
388 raise
389
390 def restore_default_vlan(self, port_id):
Steve McIntyre549435f2014-12-05 15:42:46 +0000391 port = self.get_port_by_id(port_id)
Steve McIntyre028b3cc2014-12-05 16:24:46 +0000392 if port is None:
Steve McIntyrea1c75222014-12-05 16:57:13 +0000393 raise InputError("Port ID %d does not exist" % int(port_id))
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100394
395 if port["is_trunk"] or port["is_locked"]:
396 raise CriticalError("The port is locked")
397
398 try:
Steve McIntyre4b918132014-12-05 17:04:46 +0000399 sql = "UPDATE port SET current_vlan_id=base_vlan_id WHERE port_id=%s"
400 data = (port_id,)
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000401 self.cursor.execute(sql, data)
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100402 except:
403 self.connection.rollback()
404 raise
405
Dave Pigott281203e2014-09-17 23:45:02 +0100406 def _dump_table(self, table):
407 result = []
408 self.cursor.execute("SELECT * FROM %s" % table)
Dave Pigott281203e2014-09-17 23:45:02 +0100409 record = self.cursor.fetchone()
410 while record != None:
Steve McIntyree73eb122014-11-27 15:18:47 +0000411 result.append(record)
Dave Pigott281203e2014-09-17 23:45:02 +0100412 record = self.cursor.fetchone()
413 return result
414
415 def all_switches(self):
416 return self._dump_table("switch")
417
418 def all_ports(self):
419 return self._dump_table("port")
420
421 def all_vlans(self):
422 return self._dump_table("vlan")
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100423