blob: ff5f2e1c50e44d2ad3407129ec3563c9960de97f [file] [log] [blame]
Dave Pigott281203e2014-09-17 23:45:02 +01001#! /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
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:
28 self.connection = psycopg2.connect(database=db_name, user=username,
29 cursor_factory=psycopg2.extras.RealDictCursor)
30 self.cursor = self.connection.cursor()
31 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
38 def create_switch(self, name, admin_password, cli_password):
Dave Pigott2649a1a2014-09-18 00:04:49 +010039 try:
40 self.cursor("INSERT INTO switch (name, admin_pw, cli_pw) VALUES (%s, %s, %s) RETURNING switch_id" %
41 (name, admin_password, cli_password))
42 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:
Dave Pigott9b73f3a2014-09-18 22:55:42 +010051 self.cursor("INSERT INTO port (name, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id)"
Dave Pigott2649a1a2014-09-18 00:04:49 +010052 "VALUES (%s, %s, %s %s %s %s) RETURNING port_id" %
Dave Pigott9b73f3a2014-09-18 22:55:42 +010053 (name, switch_id, is_locked, is_trunk, current_vlan_id, base_vlan_id))
Dave Pigott2649a1a2014-09-18 00:04:49 +010054 port_id = self.cursor.fetchone()[0]
55 self.connection.commit()
56 except:
57 self.connection.rollback()
58 raise
Dave Pigott281203e2014-09-17 23:45:02 +010059 return port_id
60
61 def create_vlan(self, name, tag, is_default_vlan):
Dave Pigott2649a1a2014-09-18 00:04:49 +010062 try:
63 self.cursor("INSERT INTO vlan (name, tag, is_default_vlan)"
64 "VALUES (%s, %s, %s) RETURNING vlan_id" %
65 (name, tag, is_default_vlan))
66 vlan_id = self.cursor.fetchone()[0]
67 self.connection.commit()
68 except:
69 self.connection.rollback()
70 raise
Dave Pigott281203e2014-09-17 23:45:02 +010071 return vlan_id
72
73 def _delete_row(self, table, field, value):
Dave Pigott2649a1a2014-09-18 00:04:49 +010074 try:
75 self.cursor.execute("DELETE FROM %s WHERE %s = %s" % (table, field, value))
76 self.connection.commit()
77 except:
78 self.connection.rollback()
79 raise
Dave Pigott281203e2014-09-17 23:45:02 +010080
81 def delete_switch(self, switch_id):
82 self._delete_row("switch", "switch_id", switch_id)
83
84 def delete_port(self, port_id):
85 self._delete_row("port", "port_id", port_id)
86
87 def delete_vlan(self, vlan_id):
88 self._delete_row("vlan", "vlan_id", vlan_id)
89
Dave Pigott9b73f3a2014-09-18 22:55:42 +010090 def _get_element(self, select_field, table, compare_field, value):
Dave Pigott281203e2014-09-17 23:45:02 +010091 self.cursor.execute("SELECT %s FROM %s WHERE %s = %s" % (select_field, table, compare_field, value))
92 return self.cursor.fetchone()[0]
93
94 def get_switch_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +010095 return self._get_element("switch_id", "switch", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +010096
97 def get_port_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +010098 return self._get_element("port_id", "port", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +010099
100 def get_vlan_id(self, name):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100101 return self._get_element("vlan_id", "vlan", "name", name)
Dave Pigott281203e2014-09-17 23:45:02 +0100102
103 def get_switch_name(self, switch_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100104 return self._get_element("name", "switch", "switch_id", switch_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100105
106 def get_port_name(self, port_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100107 return self._get_element("port_name", "port", "port_id", port_id)
Dave Pigott281203e2014-09-17 23:45:02 +0100108
109 def get_vlan_name(self, vlan_id):
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100110 return self._get_element("vlan_name", "vlan", "vlan_id", vlan_id)
111
112 def _get_row(self, table, field, value):
113 self.cursor.execute("SELECT * FROM %s WHERE %s = %s" % (table, field, value))
114 return self.cursor.fetchone()
115
116 def get_switch(self, switch_id):
117 return self._get_row("switch", "switch_id", switch_id)
118
119 def get_port(self, port_id):
120 return self._get_row("port", "port_id", port_id)
121
122 def get_vlan(self, vlan_id):
123 return self._get_row("vlan", "vlan_id", vlan_id)
124
125 def set_vlan(self, port_id, vlan_id):
126 port = self.get_port(port_id)
127 if port == None:
128 raise("Port %s does not exist" % port_id)
129
130 if port["is_trunk"] or port["is_locked"]:
131 raise CriticalError("The port is locked")
132
133 vlan = self.get_vlan(vlan_id)
134 if vlan == None:
135 raise CriticalError("VLAN %s does not exist" % vlan_id)
136
137 try:
138 self.cursor.execute("UPDATE port SET current_vlan_id=%s WHERE port_id=%s" %(vlan_id, port_id))
139 except:
140 self.connection.rollback()
141 raise
142
143 def restore_default_vlan(self, port_id):
144 port = self.get_port(port_id)
145 if port == None:
146 raise CriticalError("Port %s does not exist")
147
148 if port["is_trunk"] or port["is_locked"]:
149 raise CriticalError("The port is locked")
150
151 try:
152 self.cursor.execute("UPDATE port SET current_vlan_id=base_vlan_id WHERE port=%s" % port_id)
153 except:
154 self.connection.rollback()
155 raise
156
157
Dave Pigott281203e2014-09-17 23:45:02 +0100158
159 def _dump_table(self, table):
160 result = []
161 self.cursor.execute("SELECT * FROM %s" % table)
162 self.cursor.execute("SELECT * FROM switch")
163 record = self.cursor.fetchone()
164 while record != None:
165 result += record
166 record = self.cursor.fetchone()
167 return result
168
169 def all_switches(self):
170 return self._dump_table("switch")
171
172 def all_ports(self):
173 return self._dump_table("port")
174
175 def all_vlans(self):
176 return self._dump_table("vlan")
Dave Pigott9b73f3a2014-09-18 22:55:42 +0100177