blob: 1b8da3f1df1cca8b21314a23ca63bf69e75c801d [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
Dave Pigott9b73f3a2014-09-18 22:55:42 +010023from errors import CriticalError
Dave Pigott281203e2014-09-17 23:45:02 +010024
25class VlanDB:
26 def __init__(self, db_name="vland", username="vland"):
27 try:
Steve McIntyree38f6222014-11-27 15:09:49 +000028 self.connection = psycopg2.connect(database=db_name, user=username)
29 self.cursor = self.connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
Dave Pigott281203e2014-09-17 23:45:02 +010030 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 McIntyredbd7fe52014-11-27 16:54:29 +000037 def create_switch(self, name):
Dave Pigott2649a1a2014-09-18 00:04:49 +010038 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000039 sql = "INSERT INTO switch (name) VALUES (%s) RETURNING switch_id"
40 data = name
41 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010042 switch_id = self.cursor.fetchone()[0]
43 self.connection.commit()
44 except:
45 self.connection.rollback()
46 raise
Dave Pigott281203e2014-09-17 23:45:02 +010047 return switch_id
48
Dave Pigott9b73f3a2014-09-18 22:55:42 +010049 def create_port(self, name, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id):
Dave Pigott2649a1a2014-09-18 00:04:49 +010050 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000051 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 Pigott2649a1a2014-09-18 00:04:49 +010057 port_id = self.cursor.fetchone()[0]
58 self.connection.commit()
59 except:
60 self.connection.rollback()
61 raise
Dave Pigott281203e2014-09-17 23:45:02 +010062 return port_id
63
Steve McIntyredbd7fe52014-11-27 16:54:29 +000064 def create_vlan(self, name, tag, is_base_vlan):
Dave Pigott2649a1a2014-09-18 00:04:49 +010065 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000066 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 Pigott2649a1a2014-09-18 00:04:49 +010070 vlan_id = self.cursor.fetchone()[0]
71 self.connection.commit()
72 except:
73 self.connection.rollback()
74 raise
Dave Pigott281203e2014-09-17 23:45:02 +010075 return vlan_id
76
77 def _delete_row(self, table, field, value):
Dave Pigott2649a1a2014-09-18 00:04:49 +010078 try:
Steve McIntyredbd7fe52014-11-27 16:54:29 +000079 sql = "DELETE FROM %s WHERE %s = %s"
80 data = (table, field, value)
81 self.cursor.execute(sql, data)
Dave Pigott2649a1a2014-09-18 00:04:49 +010082 self.connection.commit()
83 except:
84 self.connection.rollback()
85 raise
Dave Pigott281203e2014-09-17 23:45:02 +010086
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 Pigott9b73f3a2014-09-18 22:55:42 +010096 def _get_element(self, select_field, table, compare_field, value):
Steve McIntyredbd7fe52014-11-27 16:54:29 +000097 sql = "SELECT %s FROM %s WHERE %s = %s"
98 data = (select_field, table, compare_field, value)
99 self.cursor.execute(sql, data)
Dave Pigott281203e2014-09-17 23:45:02 +0100100 return self.cursor.fetchone()[0]
101
102 def get_switch_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100103 return self._get_element("switch_id", "switch", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100104
105 def get_port_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100106 return self._get_element("port_id", "port", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100107
108 def get_vlan_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100109 return self._get_element("vlan_id", "vlan", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100110
111 def get_switch_name(self, switch_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100112 return self._get_element("name", "switch", "switch_id", switch_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100113
114 def get_port_name(self, port_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100115 return self._get_element("port_name", "port", "port_id", port_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100116
117 def get_vlan_name(self, vlan_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100118 return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id)
119
120 def _get_row(self, table, field, value):
Steve McIntyredbd7fe52014-11-27 16:54:29 +0000121 sql = "SELECT * FROM %s WHERE %s = %s"
122 data = (table, field, value)
123 self.cursor.execute(sql, data)
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100124 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 McIntyredbd7fe52014-11-27 16:54:29 +0000148 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 Pigott9b73f3a2014-09-18 22:55:42 +0100151 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 McIntyredbd7fe52014-11-27 16:54:29 +0000164 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 Pigott9b73f3a2014-09-18 22:55:42 +0100167 except:
168 self.connection.rollback()
169 raise
170
Dave Pigott281203e2014-09-17 23:45:02 +0100171 def _dump_table(self, table):
172 result = []
173 self.cursor.execute("SELECT * FROM %s" % table)
Dave Pigott281203e2014-09-17 23:45:02 +0100174 record = self.cursor.fetchone()
175 while record != None:
Steve McIntyree73eb122014-11-27 15:18:47 +0000176 result.append(record)
Dave Pigott281203e2014-09-17 23:45:02 +0100177 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 Pigott9b73f3a2014-09-18 22:55:42 +0100188