blob: a35d24c2264023725afceaff12a3ee0ad4bacc13 [file] [log] [blame]
Steve McIntyref1c04f92014-12-16 18:23:15 +00001import logging
Steve McIntyre53c7ad92014-12-16 19:21:13 +00002import time
Steve McIntyref1c04f92014-12-16 18:23:15 +00003from errors import CriticalError, InputError, ConfigError, SocketError
4
5class VlanUtil:
6 """VLANd utility functions"""
7
Steve McIntyre5f6f85e2014-12-22 16:42:28 +00008 def get_switch_driver(self, switch_name, config):
9 logging.debug("Trying to find a driver for %s" % switch_name)
10 driver = config.switches[switch_name].driver
Steve McIntyref1c04f92014-12-16 18:23:15 +000011 logging.debug("Driver: %s" % driver)
12 module = __import__("drivers.%s" % driver, fromlist=[driver])
13 class_ = getattr(module, driver)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000014 return class_(switch_name)
Steve McIntyref1c04f92014-12-16 18:23:15 +000015
Steve McIntyre519158e2014-12-23 13:44:44 +000016 def probe_switches(self, state):
17 config = state.config
Steve McIntyree8d80582014-12-23 16:53:39 +000018 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000019 for switch_name in sorted(config.switches):
20 print "Found switch %s:" % (switch_name)
Steve McIntyree8d80582014-12-23 16:53:39 +000021 print " Probing..."
Steve McIntyre519158e2014-12-23 13:44:44 +000022
Steve McIntyre4b4ab652014-12-22 17:19:09 +000023 s = self.get_switch_driver(switch_name, config)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000024 s.switch_connect(config.switches[switch_name].username,
25 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +000026 config.switches[switch_name].enable_password)
Steve McIntyre27d4b582014-12-23 22:51:00 +000027 ret[switch_name] = 'Found %d ports: ' % len(s.switch_get_port_names())
28 for name in s.switch_get_port_names():
29 ret[switch_name] += '%s ' % name
Steve McIntyrea2a8f792014-12-17 17:34:32 +000030 s.switch_disconnect()
31 del(s)
Steve McIntyrea2020cb2014-12-23 16:56:40 +000032 return ret
Steve McIntyrec68a18e2014-12-17 16:29:28 +000033
Steve McIntyre091e2ac2014-12-16 19:20:07 +000034 # Simple helper wrapper for all the read-only database queries
Steve McIntyre2150bc22014-12-17 13:13:56 +000035 def perform_db_query(self, state, command, data):
Steve McIntyre091e2ac2014-12-16 19:20:07 +000036 print 'perform_db_query'
Steve McIntyref1c04f92014-12-16 18:23:15 +000037 print command
38 print data
39 ret = {}
Steve McIntyre2150bc22014-12-17 13:13:56 +000040 db = state.db
Steve McIntyref1c04f92014-12-16 18:23:15 +000041 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +000042 if command == 'db.all_switches':
Steve McIntyref1c04f92014-12-16 18:23:15 +000043 ret = db.all_switches()
44 elif command == 'db.all_ports':
45 ret = db.all_ports()
46 elif command == 'db.all_vlans':
47 ret = db.all_vlans()
48 elif command == 'db.get_switch_by_id':
49 ret = db.get_switch_by_id(data['switch_id'])
50 elif command == 'db.get_switch_id_by_name':
51 ret = db.get_switch_id_by_name(data['name'])
52 elif command == 'db.get_switch_name_by_id':
53 ret = db.get_switch_name_by_id(data['switch_id'])
54 elif command == 'db.get_port_by_id':
55 ret = db.get_port_by_id(data['port_id'])
56 elif command == 'db.get_ports_by_switch':
57 ret = db.get_ports_by_switch(data['switch_id'])
58 elif command == 'db.get_port_by_switch_and_name':
59 ret = db.get_port_by_switch_and_name(data['switch_id'], data['name'])
60 elif command == 'db.get_current_vlan_id_by_port':
61 ret = db.get_current_vlan_id_by_port(data['port_id'])
62 elif command == 'db.get_base_vlan_id_by_port':
63 ret = db.get_base_vlan_id_by_port(data['port_id'])
64 elif command == 'db.get_ports_by_current_vlan':
65 ret = db.get_ports_by_current_vlan(data['vlan_id'])
66 elif command == 'db.get_ports_by_base_vlan':
67 ret = db.get_ports_by_base_vlan(data['vlan_id'])
68 elif command == 'db.get_vlan_by_id':
69 ret = db.get_vlan_by_id(data['vlan_id'])
70 elif command == 'db.get_vlan_id_by_name':
71 ret = db.get_vlan_id_by_name(data['name'])
72 elif command == 'db.get_vlan_id_by_tag':
Steve McIntyre07946c22014-12-17 13:14:15 +000073 ret = db.get_vlan_id_by_tag(data['tag'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000074 elif command == 'db.get_vlan_name_by_id':
75 ret = db.get_vlan_name_by_id(data['vlan_id'])
76 else:
Steve McIntyree749fef2014-12-17 16:35:45 +000077 raise InputError("Unknown db_query command \"%s\"" % command)
Steve McIntyref1c04f92014-12-16 18:23:15 +000078
Steve McIntyre5da37fa2014-12-17 13:14:44 +000079 except InputError:
80 raise
81
Steve McIntyre798af842014-12-23 22:29:46 +000082# except:
83# raise InputError("Invalid input in query")
Steve McIntyref1c04f92014-12-16 18:23:15 +000084
85 return ret
86
Steve McIntyre53c7ad92014-12-16 19:21:13 +000087 # Simple helper wrapper for all the read-only daemon state queries
88 def perform_daemon_query(self, state, command, data):
89 print 'perform_daemon_query'
90 print command
91 print data
92 ret = {}
93 try:
94 if command == 'daemon.status':
95 # data ignored
96 ret['running'] = 'ok'
97 elif command == 'daemon.version':
98 # data ignored
99 ret['version'] = state.version
100 elif command == 'daemon.statistics':
101 ret['uptime'] = time.time() - state.starttime
Steve McIntyre88b79df2014-12-23 13:45:08 +0000102 elif command == 'daemon.probe_switches':
103 ret = self.probe_switches(state)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000104 else:
Steve McIntyree749fef2014-12-17 16:35:45 +0000105 raise InputError("Unknown daemon_query command \"%s\"" % command)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000106
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000107 except InputError:
108 raise
109
Steve McIntyre798af842014-12-23 22:29:46 +0000110# except:
111# raise InputError("Invalid input in query")
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000112
113 return ret
114
Steve McIntyree749fef2014-12-17 16:35:45 +0000115 # Helper wrapper for API functions modifying database state only
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000116 def perform_db_update(self, state, command, data):
117 print 'perform_db_update'
118 print command
119 print data
120 ret = {}
121 db = state.db
122 try:
123 if command == 'db.create_switch':
124 ret = db.create_switch(data['name'])
125 elif command == 'db.create_port':
126 ret = db.create_port(data['switch_id'], data['name'],
Steve McIntyrefefdbb42014-12-22 16:14:28 +0000127 state.default_vlan_id,
128 state.default_vlan_id)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000129 elif command == 'db.delete_switch':
130 ret = db.delete_switch(data['switch_id'])
131 elif command == 'db.delete_port':
132 ret = db.delete_port(data['port_id'])
133 elif command == 'db.set_port_is_locked':
134 ret = db.set_port_is_locked(data['port_id'], data['is_locked'])
135 elif command == 'db.set_base_vlan':
136 ret = db.set_base_vlan(data['port_id'], data['base_vlan_id'])
137 else:
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000138 raise InputError("Unknown db_update command \"%s\"" % command)
139
140 except InputError:
141 raise
142
Steve McIntyre798af842014-12-23 22:29:46 +0000143# except:
144# raise InputError("Invalid input in query")
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000145
146 return ret
147
148 # Helper wrapper for API functions that modify both database state
149 # and on-switch VLAN state
150 def perform_vlan_update(self, state, command, data):
151 print 'perform_vlan_update'
152 print command
153 print data
154 ret = {}
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000155
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000156 try:
157 # All of these are complex commands, so call helpers
158 # rather than inline the code here
159 if command == 'api.create_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000160 ret = self.create_vlan(state, data['name'], int(data['tag']), data['is_base_vlan'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000161 elif command == 'api.delete_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000162 ret = self.delete_vlan(state, int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000163 elif command == 'api.set_port_mode':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000164 ret = self.set_port_mode(state, int(data['port_id']), data['mode'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000165 elif command == 'api.set_current_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000166 ret = self.set_current_vlan(state, int(data['port_id']), int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000167 elif command == 'api.restore_base_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000168 ret = self.restore_base_vlan(state, int(data['port_id']))
169 elif command == 'api.auto_import_switch':
170 ret = self.auto_import_switch(state, data['switch'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000171 else:
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000172 raise InputError("Unknown query command \"%s\"" % command)
173
Steve McIntyre3256b182014-12-19 15:38:15 +0000174 except InputError as e:
175 print 'got error %s' % e
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000176 raise
177
Steve McIntyre798af842014-12-23 22:29:46 +0000178# except:
179# raise InputError("Invalid input in query")
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000180
181 return ret
182
183
Steve McIntyre3256b182014-12-19 15:38:15 +0000184 # Complex call
185 # 1. create the VLAN in the DB
186 # 2. Iterate through all switches:
Steve McIntyre1ab8b872014-12-19 18:37:00 +0000187 # a. Create the VLAN
188 # b. Add the VLAN to all trunk ports (if needed)
189 # 3. If all went OK, save config on all the switches
Steve McIntyre3256b182014-12-19 15:38:15 +0000190 #
191 # The VLAN may already exist on some of the switches, that's
Steve McIntyre153157d2014-12-19 18:05:20 +0000192 # fine. If things fail, we attempt to roll back by rebooting
193 # switches then removing the VLAN in the DB.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000194 def create_vlan(self, state, name, tag, is_base_vlan):
Steve McIntyre3256b182014-12-19 15:38:15 +0000195
196 print 'create_vlan'
197 db = state.db
198 config = state.config
199
Steve McIntyre3256b182014-12-19 15:38:15 +0000200 # 1. Database record first
201 try:
202 print 'Adding DB record first: name %s, tag %d, is_base_vlan %d' % (name, tag, is_base_vlan)
203 vlan_id = db.create_vlan(name, tag, is_base_vlan)
204 print 'Added VLAN tag %d, name %s to the database, created VLAN ID %d' % (tag, name, vlan_id)
205 except InputError:
206 print 'DB creation failed'
207 raise
208
Steve McIntyre153157d2014-12-19 18:05:20 +0000209 # Keep track of which switches we've configured, for later use
210 switches_done = []
211
Steve McIntyre3256b182014-12-19 15:38:15 +0000212 # 2. Now the switches
213 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000214 for switch in db.all_switches():
Steve McIntyre3256b182014-12-19 15:38:15 +0000215 trunk_ports = []
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000216 switch_name = switch.name
Steve McIntyre3256b182014-12-19 15:38:15 +0000217 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000218 print 'Adding new VLAN to switch %s' % switch_name
Steve McIntyre3256b182014-12-19 15:38:15 +0000219 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000220 s = self.get_switch_driver(switch_name, config)
221 s.switch_connect(config.switches[switch_name].username,
222 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000223 config.switches[switch_name].enable_password)
Steve McIntyre3256b182014-12-19 15:38:15 +0000224
Steve McIntyre153157d2014-12-19 18:05:20 +0000225 # Mark this switch as one we've touched, for
226 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000227 switches_done.append(switch_name)
Steve McIntyre153157d2014-12-19 18:05:20 +0000228
Steve McIntyre3256b182014-12-19 15:38:15 +0000229 # 2a. Create the VLAN on the switch
230 s.vlan_create(tag)
231 s.vlan_set_name(tag, name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000232 print 'Added VLAN tag %d, name %s to switch %s' % (tag, name, switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000233
234 # 2b. Do we need to worry about trunk ports on this switch?
235 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
236 print 'This switch does not need special trunk port handling'
237 else:
238 print 'This switch needs special trunk port handling'
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000239 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyre3256b182014-12-19 15:38:15 +0000240 if trunk_ports is None:
241 print "But it has no trunk ports defined"
242 trunk_ports = []
243 else:
244 print 'Found %d trunk_ports that need adjusting' % len(trunk_ports)
245
246 # Modify any trunk ports as needed
247 for port in trunk_ports:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000248 print 'Added VLAN tag %d, name %s to switch %s' % (tag, name, switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000249 s.port_add_trunk_to_vlan(port, tag)
250
Steve McIntyre3256b182014-12-19 15:38:15 +0000251 # And now we're done with this switch
252 s.switch_disconnect()
253 del s
254
255 except IOError:
256 raise
257
258 except IOError:
Steve McIntyre153157d2014-12-19 18:05:20 +0000259 # Bugger. Looks like one of the switch calls above
260 # failed. To undo the changes safely, we'll need to reset
261 # all the switches we managed to configure. This could
262 # take some time!
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000263 for switch_name in switches_done:
264 s = self.get_switch_driver(switch_name, config)
265 s.switch_connect(config.switches[switch_name].username,
266 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000267 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000268 s.switch_restart() # Will implicitly also close the connection
269 del s
270
Steve McIntyre3256b182014-12-19 15:38:15 +0000271 # Undo the database change
272 print 'Switch access failed. Deleting the new VLAN entry in the database'
273 db.delete_vlan(vlan_id)
274 raise
275
Steve McIntyre153157d2014-12-19 18:05:20 +0000276 # If we've got this far, things were successful. Save config
277 # on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000278 for switch_name in switches_done:
279 s = self.get_switch_driver(switch_name, config)
280 s.switch_connect(config.switches[switch_name].username,
281 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000282 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000283 s.switch_save_running_config()
284 s.switch_disconnect()
285 del s
286
Steve McIntyre3256b182014-12-19 15:38:15 +0000287 return vlan_id # If we're successful
288
Steve McIntyrefeb64522014-12-19 18:53:02 +0000289 # Complex call
290 # 1. Check in the DB if there are any ports on the VLAN. Bail if so
291 # 2. Iterate through all switches:
292 # a. Remove the VLAN from all trunk ports (if needed)
293 # b. Remove the VLAN
294 # 3. If all went OK, save config on the switches
295 # 4. Remove the VLAN in the DB
296 #
297 # If things fail, we attempt to roll back by rebooting switches.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000298 def delete_vlan(self, state, vlan_id):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000299
300 print 'delete_vlan'
301 db = state.db
302 config = state.config
303
Steve McIntyrefeb64522014-12-19 18:53:02 +0000304 # 1. Check for database records first
Steve McIntyre4a43ab02014-12-22 01:47:03 +0000305 print 'Checking for ports using VLAN ID %d' % vlan_id
Steve McIntyrefeb64522014-12-19 18:53:02 +0000306 vlan = db.get_vlan_by_id(vlan_id)
307 if vlan is None:
308 raise InputError("VLAN ID %d does not exist" % vlan_id)
309 vlan_tag = vlan.tag
310 ports = db.get_ports_by_current_vlan(vlan_id)
311 if ports is not None:
312 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
313 (vlan_id, len(ports)))
314 ports = db.get_ports_by_base_vlan(vlan_id)
315 if ports is not None:
316 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
317 (vlan_id, len(ports)))
318
319 # Keep track of which switches we've configured, for later use
320 switches_done = []
321
322 # 2. Now the switches
323 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000324 for switch in db.all_switches():
325 switch_name = switch.name
Steve McIntyrefeb64522014-12-19 18:53:02 +0000326 trunk_ports = []
327 try:
328 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000329 s = self.get_switch_driver(switch_name, config)
330 s.switch_connect(config.switches[switch_name].username,
331 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000332 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000333
334 # Mark this switch as one we've touched, for
335 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000336 switches_done.append(switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000337
338 # 2a. Do we need to worry about trunk ports on this switch?
339 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
340 print 'This switch does not need special trunk port handling'
341 else:
342 print 'This switch needs special trunk port handling'
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000343 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000344 if trunk_ports is None:
345 print "But it has no trunk ports defined"
346 trunk_ports = []
347 else:
348 print 'Found %d trunk_ports that need adjusting' % len(trunk_ports)
349
350 # Modify any trunk ports as needed
351 for port in trunk_ports:
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000352 s.port_remove_trunk_from_vlan(port, vlan_tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000353 print 'Removed VLAN tag %d from switch %s port %s' % (vlan_tag, switch_name, port)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000354
355 # 2b. Remove the VLAN from the switch
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000356 print 'Removing VLAN tag %d from switch %s' % (vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000357 s.vlan_destroy(vlan_tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000358 print 'Removed VLAN tag %d from switch %s' % (vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000359
360 # And now we're done with this switch
361 s.switch_disconnect()
362 del s
363
364 except IOError:
365 raise
366
367 except IOError:
368 # Bugger. Looks like one of the switch calls above
369 # failed. To undo the changes safely, we'll need to reset
370 # all the switches we managed to configure. This could
371 # take some time!
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000372 for switch_name in switches_done:
373 s = self.get_switch_driver(switch_name, config)
374 s.switch_connect(config.switches[switch_name].username,
375 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000376 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000377 s.switch_restart() # Will implicitly also close the connection
378 del s
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000379 raise
Steve McIntyrefeb64522014-12-19 18:53:02 +0000380
381 # 3. If we've got this far, things were successful. Save
382 # config on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000383 for switch_name in switches_done:
384 s = self.get_switch_driver(switch_name, config)
385 s.switch_connect(config.switches[switch_name].username,
386 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000387 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000388 s.switch_save_running_config()
389 s.switch_disconnect()
390 del s
391
392 # 4. Finally, remove the VLAN in the DB
393 try:
Steve McIntyre4a43ab02014-12-22 01:47:03 +0000394 print 'Removing DB record: VLAN ID %d' % vlan_id
Steve McIntyrefeb64522014-12-19 18:53:02 +0000395 vlan_id = db.delete_vlan(vlan_id)
Steve McIntyre4a43ab02014-12-22 01:47:03 +0000396 print 'Removed VLAN ID %d from the database OK' % vlan_id
Steve McIntyrefeb64522014-12-19 18:53:02 +0000397 except InputError:
398 print 'DB deletion failed'
399 raise
400
401 return vlan_id # If we're successful
402
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000403 # Complex call, depends on existing state a lot
404 # 1. Check validity of inputs
405 # 2. Switch mode and other config on the port.
406 # a. If switching trunk->access, remove all trunk VLANs from it
407 # (if needed) and switch back to the base VLAN for the
408 # port. Next, switch to access mode.
409 # b. If switching access->trunk, switch back to the base VLAN
410 # for the port. Next, switch mode. Then add all trunk VLANs
411 # to it (if needed)
412 # 3. If all went OK, save config on the switch
413 # 4. Change details of the port in the DB
414 #
415 # If things fail, we attempt to roll back by rebooting the switch
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000416 def set_port_mode(self, state, port_id, mode):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000417
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000418 print 'set_port_mode'
419 db = state.db
420 config = state.config
421
422 # 1. Sanity-check inputs
423 if mode is not 'access' and mode is not 'trunk':
424 raise InputError("Port mode %s is not a valid option: try 'access' or 'trunk" % mode)
425 port = db.get_port_by_id(port_id)
426 if port is None:
427 raise InputError("Port ID %d does not exist" % port_id)
428 if mode == 'trunk' and port.is_trunk:
429 raise InputError("Port ID %d is already in trunk mode")
430 if mode == 'access' and not port.is_trunk:
431 raise InputError("Port ID %d is already in access mode")
432 base_vlan_tag = db.get_vlan_tag_by_id(port.base_vlan_id)
433
434 # Get the right driver
435 switch_name = db.get_switch_name_by_id(port.switch_id)
436 s = self.get_switch_driver(switch_name, config)
437
438 # 2. Now start configuring the switch
439 try:
440 s.switch_connect(config.switches[switch_name].username,
441 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000442 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000443 except:
444 print 'Failed to talk to switch %s!' % switch_name
445 raise
446
447 try:
448 if port.is_trunk:
449 # 2a. We're going from a trunk port to an access port
450 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
451 print 'This switch does not need special trunk port handling'
452 else:
453 print 'This switch needs special trunk port handling'
454 vlans = s.port_get_trunk_vlan_list(port.name)
455 if vlans is None:
456 print "But it has no VLANs defined on port %s" % port.name
457 vlans = []
458 else:
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000459 print 'Found %d vlans that need dropping on port %s' % (len(vlans), port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000460
461 for vlan in vlans:
462 s.port_remove_trunk_from_vlan(port.name, vlan)
463 s.port_add_trunk_to_vlan(port.name, base_vlan_tag)
464 s.port_set_mode(port.name, "access")
465
466 else:
467 # 2b. We're going from an access port to a trunk port
468 s.port_set_access_vlan(port.name, base_vlan_tag)
469 s.port_set_mode(port.name, "trunk")
470 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
471 print 'This switch does not need special trunk port handling'
472 else:
473 vlans = db.all_vlans()
474 for vlan in vlans:
475 s.port_add_trunk_to_vlan(port.name, vlan.tag)
476
477 except IOError:
478 # Bugger. Looks like one of the switch calls above
479 # failed. To undo the changes safely, we'll need to reset
480 # all the config on this switch
481 s.switch_restart() # Will implicitly also close the connection
482 del s
483 raise
484
485 # 3. All seems to have worked so far!
486 s.switch_save_running_config()
487 s.switch_disconnect()
488 del s
489
490 # 4. And update the DB
491 db.set_port_mode(port_id, mode)
492
493 return port_id # If we're successful
494
495 # Complex call, updating both DB and switch state
496 # 1. Check validity of inputs
497 # 2. Update the port config on the switch
498 # 3. If all went OK, save config on the switch
499 # 4. Change details of the port in the DB
500 #
501 # If things fail, we attempt to roll back by rebooting the switch
502 def set_current_vlan(self, state, port_id, vlan_id):
503
504 print 'set_current_vlan'
505 db = state.db
506 config = state.config
507
508 # 1. Sanity checks!
509 port = db.get_port_by_id(port_id)
510 if port is None:
511 raise InputError("Port ID %d does not exist" % port_id)
512 if port.is_trunk:
513 raise InputError("Port ID %d is not an access port" % port_id)
514 if port.is_locked:
515 raise InputError("Port ID %d is locked" % port_id)
516
517 vlan = db.get_vlan_by_id(vlan_id)
518 if vlan is None:
519 raise InputError("VLAN ID %d does not exist" % vlan_id)
520
521 # Get the right driver
522 switch_name = db.get_switch_name_by_id(port.switch_id)
523 s = self.get_switch_driver(switch_name, config)
524
525 # 2. Now start configuring the switch
526 try:
527 s.switch_connect(config.switches[switch_name].username,
528 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000529 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000530 except:
531 print 'Failed to talk to switch %s!' % switch_name
532 raise
533
534 try:
535 s.port_set_access_vlan(port.name, vlan.tag)
536 except IOError:
537 # Bugger. Looks like one of the switch calls above
538 # failed. To undo the changes safely, we'll need to reset
539 # all the config on this switch
540 s.switch_restart() # Will implicitly also close the connection
541 del s
542 raise
543
544 # 3. All seems to have worked so far!
545 s.switch_save_running_config()
546 s.switch_disconnect()
547 del s
548
549 # 4. And update the DB
550 db.set_current_vlan(port_id, vlan_id)
551
552 return port_id # If we're successful
553
554 # Complex call, updating both DB and switch state
555 # 1. Check validity of input
556 # 2. Update the port config on the switch
557 # 3. If all went OK, save config on the switch
558 # 4. Change details of the port in the DB
559 #
560 # If things fail, we attempt to roll back by rebooting the switch
561 def restore_base_vlan(self, state, port_id):
562
563 print 'restore_base_vlan'
564 db = state.db
565 config = state.config
566
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000567 # 1. Sanity checks!
568 port = db.get_port_by_id(port_id)
569 if port is None:
570 raise InputError("Port ID %d does not exist" % port_id)
571 if port.is_trunk:
572 raise InputError("Port ID %d is not an access port" % port_id)
573 if port.is_locked:
574 raise InputError("Port ID %d is locked" % port_id)
575
576 # Bail out early if we're *already* on the base VLAN. This is
577 # not an error
578 if port.current_vlan_id == port.base_vlan_id:
579 return port_id
580
581 vlan = db.get_vlan_by_id(port.base_vlan_id)
582
583 # Get the right driver
584 switch_name = db.get_switch_name_by_id(port.switch_id)
585 s = self.get_switch_driver(switch_name, config)
586
587 # 2. Now start configuring the switch
588 try:
589 s.switch_connect(config.switches[switch_name].username,
590 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000591 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000592 except:
593 print 'Failed to talk to switch %s!' % switch_name
594 raise
595
596 try:
597 s.port_set_access_vlan(port.name, vlan.tag)
598 except IOError:
599 # Bugger. Looks like one of the switch calls above
600 # failed. To undo the changes safely, we'll need to reset
601 # all the config on this switch
602 s.switch_restart() # Will implicitly also close the connection
603 del s
604 raise
605
606 # 3. All seems to have worked so far!
607 s.switch_save_running_config()
608 s.switch_disconnect()
609 del s
610
611 # 4. And update the DB
612 db.set_current_vlan(port_id, port.base_vlan_id)
613
614 return port_id # If we're successful
615
616 # Complex call, updating both DB and switch state
617 # * Check validity of input
618 # * Read all the config from the switch (switch, ports, VLANs)
619 # * Create initial DB entries to match each of those
620 # * Merge VLANs across all switches
621 # * Set up ports appropriately
622 #
623 def auto_import_switch(self, state, switch_name):
624
625 print 'auto_import_switch'
626 db = state.db
627 config = state.config
628
629 # 1. Sanity checks!
630 switch_id = db.get_switch_id_by_name(switch_name)
631 if switch_id is not None:
632 raise InputError("Switch name %s already exists in the DB (ID %d)" % (switch_name, switch_id))
633
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000634 if not switch_name in config.switches:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000635 raise InputError("Switch name %s not defined in config" % switch_name)
636
Steve McIntyrefc511242014-12-23 22:28:30 +0000637 print 'args look ok'
638
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000639 # 2. Now start reading config from the switch
640 try:
641 s = self.get_switch_driver(switch_name, config)
642 s.switch_connect(config.switches[switch_name].username,
643 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000644 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000645 except:
646 print 'Failed to talk to switch %s!' % switch_name
647 raise
648
649 # DON'T create the switch record in the DB first - we'll want
Steve McIntyrefc511242014-12-23 22:28:30 +0000650 # to create VLANs on *other* switches, and it's easier to do
651 # that before we've added our new switch
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000652
653 new_vlan_tags = []
654
655 # Grab the VLANs defined on this switch
656 vlan_tags = s.vlan_get_list()
Steve McIntyrefc511242014-12-23 22:28:30 +0000657
658 print ' found %d vlans on the switch' % len(vlan_tags)
659
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000660 for vlan_tag in vlan_tags:
661 vlan_name = s.vlan_get_name(vlan_tag)
662
663 # If a VLAN is already in the database, then that's easy -
664 # we can just ignore it. However, we have to check that
665 # there is not a different name for the existing VLAN tag
Steve McIntyreb1529072014-12-23 17:17:22 +0000666 # - bail out if so... UNLESS we're looking at the default
667 # VLAN
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000668 #
669 # If this VLAN tag is not already in the DB, we'll need to
670 # add it there and to all the other switches (and their
671 # trunk ports!) too.
Steve McIntyrefc511242014-12-23 22:28:30 +0000672 vlan_id = db.get_vlan_id_by_tag(vlan_tag)
Steve McIntyreb1529072014-12-23 17:17:22 +0000673 if vlan_id is not state.default_vlan_id:
Steve McIntyreb1529072014-12-23 17:17:22 +0000674 if vlan_id is not None:
675 vlan_db_name = db.get_vlan_name_by_id(vlan_id)
676 if vlan_name != vlan_db_name:
677 raise InputError("Can't add VLAN tag %d (name %s) for this switch - VLAN tag %d already exists in the database, but with a different name (%s)" % (vlan_tag, vlan_name, vlan_tag, vlan_db_name))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000678
Steve McIntyreb1529072014-12-23 17:17:22 +0000679 else:
680 # OK, we'll need to set up the new VLAN now. It can't
681 # be a base VLAN - switches don't have such a concept!
682 # Rather than create individually here, add to a
683 # list. *Only* once we've worked through all the
684 # switch's VLANs successfully (checking for existing
685 # records and possible clashes!) should we start
686 # committing changes
687 new_vlan_tags.append(vlan_tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000688
689 # Now create the VLAN DB entries
690 for vlan_tag in new_vlan_tags:
691 vlan_name = s.vlan_get_name(vlan_tag)
692 vlan_id = self.create_vlan(state, vlan_name, vlan_tag, False)
693
694 # *Now* add this switch itself to the database, after we've
695 # worked on all the other switches
696 switch_id = db.create_switch(switch_name)
697
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000698 # And now the ports
699 trunk_ports = []
700 ports = s.switch_get_port_names()
Steve McIntyrefc511242014-12-23 22:28:30 +0000701 print ' found %d ports on the switch' % len(ports)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000702 for port_name in ports:
Steve McIntyrefc511242014-12-23 22:28:30 +0000703 print ' trying to import port %s' % port_name
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000704 port_id = None
705 port_mode = s.port_get_mode(port_name)
706 if port_mode == 'access':
707 # Access ports are easy - just create the port, and
708 # set both the current and base VLANs to the current
709 # VLAN on the switch. We'll end up changing this after
710 # import if needed.
711 port_vlan = s.port_get_access_vlan(port_name)
Steve McIntyre6f17b102014-12-24 02:18:08 +0000712 port_vlan_id = db.get_vlan_id_by_tag(port_vlan)
713 port_id = db.create_port(switch_id, port_name,
714 port_vlan_id, port_vlan_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000715 print ' access port, VLAN %d' % int(port_vlan)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000716 # Nothing further needed
717 elif port_mode == 'trunk':
Steve McIntyrefc511242014-12-23 22:28:30 +0000718 print ' trunk port'
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000719 # Trunk ports are a little more involved. First,
720 # create the port in the DB, setting the VLANs to the
721 # first VLAN found on the trunk port. This will *also*
722 # be in access mode by default, and unlocked.
723 port_vlans = s.port_get_trunk_vlan_list(port_name)
Steve McIntyrefc511242014-12-23 22:28:30 +0000724 print ' trunk port has VLANs:'
725 print port_vlans
Steve McIntyre6f17b102014-12-24 02:18:08 +0000726 if port_vlans == [] or port_vlans is None or 'ALL' in port_vlans:
727 port_vlans = (state.config.vland.default_vlan_tag,) # easy for our purposes
728 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[0])
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000729 port_id = db.create_port(switch_id, port_name,
Steve McIntyre6f17b102014-12-24 02:18:08 +0000730 port_vlan_id, port_vlan_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000731 # Append to a list of trunk ports that we will need to
732 # modify once we're done
733 trunk_ports.append(port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000734 else:
735 raise CriticalError("Unrecognised port port mode %s???" % port_mode)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000736
Steve McIntyrefc511242014-12-23 22:28:30 +0000737 print " Added port %s, got port ID %d" % (port_name, port_id)
738
739 # Now, on each trunk port on the switch, we need to add all
740 # the VLANs already configured across our system
741 if not 'TrunkWildCardVlans' in s.switch_get_capabilities():
742 for port_id in trunk_ports:
743 port = db.get_port_by_id(port_id)
744 db.set_port_mode(port_id, "trunk")
745
746 for vlan in db.all_vlans():
747 if vlan.vlan_id is not state.default_vlan_id:
748 print "Adding allowed VLAN tag %d to trunk port %s" % (vlan.tag, port.name)
749 s.port_add_trunk_to_vlan(port.name, vlan.tag)
750
751
752 # Done with this switch \o/
753 s.switch_save_running_config()
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000754 s.switch_disconnect()
755 del s
756
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000757 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000758 ret['switch_id'] = switch_id
759 ret['num_ports_added'] = len(ports)
760 ret['num_vlans_added'] = len(new_vlan_tags)
761 return ret # If we're successful