blob: e1de89dcf6600822bcf3a4d598f7b0116daf6235 [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 McIntyre26de4d92015-09-23 00:04:55 +01008 def set_logging_level(self, level):
9
10 # Set up logging and maybe go quiet...
11 if level is None:
12 level = "CRITICAL"
13
14 loglevel = logging.CRITICAL
15 if level == "ERROR":
16 loglevel = logging.ERROR
17 elif level == "WARNING":
18 loglevel = logging.WARNING
19 elif level == "INFO":
20 loglevel = logging.INFO
21 elif level == "DEBUG":
22 loglevel = logging.DEBUG
23 return loglevel
24
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000025 def get_switch_driver(self, switch_name, config):
Steve McIntyre5fa22652015-04-01 18:01:45 +010026 logging.debug("Trying to find a driver for %s", switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000027 driver = config.switches[switch_name].driver
Steve McIntyre5fa22652015-04-01 18:01:45 +010028 logging.debug("Driver: %s", driver)
Steve McIntyref1c04f92014-12-16 18:23:15 +000029 module = __import__("drivers.%s" % driver, fromlist=[driver])
30 class_ = getattr(module, driver)
Steve McIntyre0f38f0e2015-07-14 15:26:05 +010031 return class_(switch_name, debug = config.switches[switch_name].debug)
Steve McIntyref1c04f92014-12-16 18:23:15 +000032
Steve McIntyre519158e2014-12-23 13:44:44 +000033 def probe_switches(self, state):
34 config = state.config
Steve McIntyree8d80582014-12-23 16:53:39 +000035 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000036 for switch_name in sorted(config.switches):
Steve McIntyre5fa22652015-04-01 18:01:45 +010037 logging.debug("Found switch %s:", switch_name)
Steve McIntyre7cf80982015-02-12 07:03:40 +000038 logging.debug(" Probing...")
Steve McIntyre519158e2014-12-23 13:44:44 +000039
Steve McIntyre4b4ab652014-12-22 17:19:09 +000040 s = self.get_switch_driver(switch_name, config)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000041 s.switch_connect(config.switches[switch_name].username,
42 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +000043 config.switches[switch_name].enable_password)
Steve McIntyre27d4b582014-12-23 22:51:00 +000044 ret[switch_name] = 'Found %d ports: ' % len(s.switch_get_port_names())
45 for name in s.switch_get_port_names():
46 ret[switch_name] += '%s ' % name
Steve McIntyrea2a8f792014-12-17 17:34:32 +000047 s.switch_disconnect()
Steve McIntyre2ed90f52015-07-21 17:59:52 +010048 del s
Steve McIntyrea2020cb2014-12-23 16:56:40 +000049 return ret
Steve McIntyrec68a18e2014-12-17 16:29:28 +000050
Steve McIntyre091e2ac2014-12-16 19:20:07 +000051 # Simple helper wrapper for all the read-only database queries
Steve McIntyre2150bc22014-12-17 13:13:56 +000052 def perform_db_query(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +000053 logging.debug('perform_db_query')
54 logging.debug(command)
55 logging.debug(data)
Steve McIntyref1c04f92014-12-16 18:23:15 +000056 ret = {}
Steve McIntyre2150bc22014-12-17 13:13:56 +000057 db = state.db
Steve McIntyref1c04f92014-12-16 18:23:15 +000058 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +000059 if command == 'db.all_switches':
Steve McIntyref1c04f92014-12-16 18:23:15 +000060 ret = db.all_switches()
61 elif command == 'db.all_ports':
62 ret = db.all_ports()
63 elif command == 'db.all_vlans':
64 ret = db.all_vlans()
Steve McIntyrec4890132015-08-07 15:19:11 +010065 elif command == 'db.all_trunks':
66 ret = db.all_trunks()
Steve McIntyref1c04f92014-12-16 18:23:15 +000067 elif command == 'db.get_switch_by_id':
68 ret = db.get_switch_by_id(data['switch_id'])
69 elif command == 'db.get_switch_id_by_name':
70 ret = db.get_switch_id_by_name(data['name'])
71 elif command == 'db.get_switch_name_by_id':
72 ret = db.get_switch_name_by_id(data['switch_id'])
73 elif command == 'db.get_port_by_id':
74 ret = db.get_port_by_id(data['port_id'])
75 elif command == 'db.get_ports_by_switch':
76 ret = db.get_ports_by_switch(data['switch_id'])
77 elif command == 'db.get_port_by_switch_and_name':
78 ret = db.get_port_by_switch_and_name(data['switch_id'], data['name'])
Steve McIntyre45f55012015-08-05 13:55:15 +010079 elif command == 'db.get_port_by_switch_and_number':
80 ret = db.get_port_by_switch_and_number(data['switch_id'], int(data['number']))
Steve McIntyref1c04f92014-12-16 18:23:15 +000081 elif command == 'db.get_current_vlan_id_by_port':
82 ret = db.get_current_vlan_id_by_port(data['port_id'])
83 elif command == 'db.get_base_vlan_id_by_port':
84 ret = db.get_base_vlan_id_by_port(data['port_id'])
85 elif command == 'db.get_ports_by_current_vlan':
86 ret = db.get_ports_by_current_vlan(data['vlan_id'])
87 elif command == 'db.get_ports_by_base_vlan':
88 ret = db.get_ports_by_base_vlan(data['vlan_id'])
Steve McIntyrec4890132015-08-07 15:19:11 +010089 elif command == 'db.get_ports_by_trunk':
90 ret = db.get_ports_by_trunk(data['trunk_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000091 elif command == 'db.get_vlan_by_id':
92 ret = db.get_vlan_by_id(data['vlan_id'])
Steve McIntyre65533d72015-01-23 18:01:17 +000093 elif command == 'db.get_vlan_tag_by_id':
94 ret = db.get_vlan_tag_by_id(data['vlan_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000095 elif command == 'db.get_vlan_id_by_name':
96 ret = db.get_vlan_id_by_name(data['name'])
97 elif command == 'db.get_vlan_id_by_tag':
Steve McIntyre07946c22014-12-17 13:14:15 +000098 ret = db.get_vlan_id_by_tag(data['tag'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000099 elif command == 'db.get_vlan_name_by_id':
100 ret = db.get_vlan_name_by_id(data['vlan_id'])
Steve McIntyrec4890132015-08-07 15:19:11 +0100101 elif command == 'db.get_trunk_by_id':
102 ret = db.get_trunk_by_id(data['trunk_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +0000103 else:
Steve McIntyree749fef2014-12-17 16:35:45 +0000104 raise InputError("Unknown db_query command \"%s\"" % command)
Steve McIntyref1c04f92014-12-16 18:23:15 +0000105
Steve McIntyre5da37fa2014-12-17 13:14:44 +0000106 except InputError:
107 raise
108
Steve McIntyre798af842014-12-23 22:29:46 +0000109# except:
110# raise InputError("Invalid input in query")
Steve McIntyref1c04f92014-12-16 18:23:15 +0000111
112 return ret
113
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000114 # Simple helper wrapper for all the read-only daemon state queries
115 def perform_daemon_query(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000116 logging.debug('perform_daemon_query')
117 logging.debug(command)
118 logging.debug(data)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000119 ret = {}
120 try:
121 if command == 'daemon.status':
122 # data ignored
123 ret['running'] = 'ok'
124 elif command == 'daemon.version':
125 # data ignored
126 ret['version'] = state.version
127 elif command == 'daemon.statistics':
128 ret['uptime'] = time.time() - state.starttime
Steve McIntyre88b79df2014-12-23 13:45:08 +0000129 elif command == 'daemon.probe_switches':
130 ret = self.probe_switches(state)
Steve McIntyre06fe6422015-01-23 17:55:43 +0000131 elif command == 'daemon.shutdown':
132 # data ignored
133 ret['shutdown'] = 'Shutting down'
134 state.running = False
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000135 else:
Steve McIntyree749fef2014-12-17 16:35:45 +0000136 raise InputError("Unknown daemon_query command \"%s\"" % command)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000137
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000138 except InputError:
139 raise
140
Steve McIntyre798af842014-12-23 22:29:46 +0000141# except:
142# raise InputError("Invalid input in query")
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000143
144 return ret
145
Steve McIntyree749fef2014-12-17 16:35:45 +0000146 # Helper wrapper for API functions modifying database state only
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000147 def perform_db_update(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000148 logging.debug('perform_db_update')
149 logging.debug(command)
150 logging.debug(data)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000151 ret = {}
152 db = state.db
153 try:
154 if command == 'db.create_switch':
155 ret = db.create_switch(data['name'])
156 elif command == 'db.create_port':
Steve McIntyreca6adfc2015-08-06 15:08:58 +0100157 try:
158 number = int(data['number'])
159 except ValueError:
160 raise InputError("Invalid value for port number (%s) - must be numeric only!" % data['number'])
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000161 ret = db.create_port(data['switch_id'], data['name'],
Steve McIntyreca6adfc2015-08-06 15:08:58 +0100162 number,
Steve McIntyrefefdbb42014-12-22 16:14:28 +0000163 state.default_vlan_id,
164 state.default_vlan_id)
Steve McIntyrec4890132015-08-07 15:19:11 +0100165 if command == 'db.create_trunk':
166 ret = db.create_trunk(data['port_id1'], data['port_id2'])
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000167 elif command == 'db.delete_switch':
168 ret = db.delete_switch(data['switch_id'])
169 elif command == 'db.delete_port':
170 ret = db.delete_port(data['port_id'])
171 elif command == 'db.set_port_is_locked':
172 ret = db.set_port_is_locked(data['port_id'], data['is_locked'])
173 elif command == 'db.set_base_vlan':
174 ret = db.set_base_vlan(data['port_id'], data['base_vlan_id'])
Steve McIntyrec4890132015-08-07 15:19:11 +0100175 elif command == 'db.delete_trunk':
176 ret = db.delete_trunk(data['trunk_id'])
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000177 else:
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000178 raise InputError("Unknown db_update command \"%s\"" % command)
179
180 except InputError:
181 raise
182
Steve McIntyre798af842014-12-23 22:29:46 +0000183# except:
184# raise InputError("Invalid input in query")
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000185
186 return ret
187
188 # Helper wrapper for API functions that modify both database state
189 # and on-switch VLAN state
190 def perform_vlan_update(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000191 logging.debug('perform_vlan_update')
192 logging.debug(command)
193 logging.debug(data)
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000194 ret = {}
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000195
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000196 try:
197 # All of these are complex commands, so call helpers
198 # rather than inline the code here
199 if command == 'api.create_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000200 ret = self.create_vlan(state, data['name'], int(data['tag']), data['is_base_vlan'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000201 elif command == 'api.delete_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000202 ret = self.delete_vlan(state, int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000203 elif command == 'api.set_port_mode':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000204 ret = self.set_port_mode(state, int(data['port_id']), data['mode'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000205 elif command == 'api.set_current_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000206 ret = self.set_current_vlan(state, int(data['port_id']), int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000207 elif command == 'api.restore_base_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000208 ret = self.restore_base_vlan(state, int(data['port_id']))
209 elif command == 'api.auto_import_switch':
210 ret = self.auto_import_switch(state, data['switch'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000211 else:
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000212 raise InputError("Unknown query command \"%s\"" % command)
213
Steve McIntyre3256b182014-12-19 15:38:15 +0000214 except InputError as e:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100215 logging.debug('got error %s', e)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000216 raise
217
Steve McIntyre798af842014-12-23 22:29:46 +0000218# except:
219# raise InputError("Invalid input in query")
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000220
221 return ret
222
223
Steve McIntyre3256b182014-12-19 15:38:15 +0000224 # Complex call
225 # 1. create the VLAN in the DB
226 # 2. Iterate through all switches:
Steve McIntyre1ab8b872014-12-19 18:37:00 +0000227 # a. Create the VLAN
228 # b. Add the VLAN to all trunk ports (if needed)
229 # 3. If all went OK, save config on all the switches
Steve McIntyre3256b182014-12-19 15:38:15 +0000230 #
231 # The VLAN may already exist on some of the switches, that's
Steve McIntyre153157d2014-12-19 18:05:20 +0000232 # fine. If things fail, we attempt to roll back by rebooting
233 # switches then removing the VLAN in the DB.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000234 def create_vlan(self, state, name, tag, is_base_vlan):
Steve McIntyre3256b182014-12-19 15:38:15 +0000235
Steve McIntyre7cf80982015-02-12 07:03:40 +0000236 logging.debug('create_vlan')
Steve McIntyre3256b182014-12-19 15:38:15 +0000237 db = state.db
238 config = state.config
239
Steve McIntyre6b8d3862015-08-03 19:28:25 +0100240 # Check for tag == -1, i.e. use the next available tag
241 if tag == -1:
242 tag = db.find_lowest_unused_vlan_tag()
243 logging.debug('create_vlan called with a tag of -1, found first unused tag %d', tag)
244
Steve McIntyre3256b182014-12-19 15:38:15 +0000245 # 1. Database record first
246 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100247 logging.debug('Adding DB record first: name %s, tag %d, is_base_vlan %d', name, tag, is_base_vlan)
Steve McIntyre3256b182014-12-19 15:38:15 +0000248 vlan_id = db.create_vlan(name, tag, is_base_vlan)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100249 logging.debug('Added VLAN tag %d, name %s to the database, created VLAN ID %d', tag, name, vlan_id)
Steve McIntyre3256b182014-12-19 15:38:15 +0000250 except InputError:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000251 logging.debug('DB creation failed')
Steve McIntyre3256b182014-12-19 15:38:15 +0000252 raise
253
Steve McIntyre153157d2014-12-19 18:05:20 +0000254 # Keep track of which switches we've configured, for later use
255 switches_done = []
256
Steve McIntyre3256b182014-12-19 15:38:15 +0000257 # 2. Now the switches
258 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000259 for switch in db.all_switches():
Steve McIntyre3256b182014-12-19 15:38:15 +0000260 trunk_ports = []
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000261 switch_name = switch.name
Steve McIntyre3256b182014-12-19 15:38:15 +0000262 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100263 logging.debug('Adding new VLAN to switch %s', switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000264 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000265 s = self.get_switch_driver(switch_name, config)
266 s.switch_connect(config.switches[switch_name].username,
267 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000268 config.switches[switch_name].enable_password)
Steve McIntyre3256b182014-12-19 15:38:15 +0000269
Steve McIntyre153157d2014-12-19 18:05:20 +0000270 # Mark this switch as one we've touched, for
271 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000272 switches_done.append(switch_name)
Steve McIntyre153157d2014-12-19 18:05:20 +0000273
Steve McIntyre3256b182014-12-19 15:38:15 +0000274 # 2a. Create the VLAN on the switch
275 s.vlan_create(tag)
276 s.vlan_set_name(tag, name)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100277 logging.debug('Added VLAN tag %d, name %s to switch %s', tag, name, switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000278
279 # 2b. Do we need to worry about trunk ports on this switch?
280 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000281 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre3256b182014-12-19 15:38:15 +0000282 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000283 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000284 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyre3256b182014-12-19 15:38:15 +0000285 if trunk_ports is None:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000286 logging.debug("But it has no trunk ports defined")
Steve McIntyre3256b182014-12-19 15:38:15 +0000287 trunk_ports = []
288 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100289 logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports))
Steve McIntyre3256b182014-12-19 15:38:15 +0000290
291 # Modify any trunk ports as needed
292 for port in trunk_ports:
Steve McIntyre1ce09f92015-09-04 14:35:13 +0100293 logging.debug('Adding VLAN tag %d, name %s to switch %s port %s', tag, name, switch_name, port)
Steve McIntyre3256b182014-12-19 15:38:15 +0000294 s.port_add_trunk_to_vlan(port, tag)
295
Steve McIntyre3256b182014-12-19 15:38:15 +0000296 # And now we're done with this switch
297 s.switch_disconnect()
298 del s
299
Steve McIntyre8f27ae12015-09-04 14:35:38 +0100300 except IOError as e:
Steve McIntyre9f7bac02015-09-18 14:10:34 +0100301 logging.error('Failed to add VLAN %d to switch ID %d (%s): %s', tag, switch.switch_id, switch.name, e)
Steve McIntyre3256b182014-12-19 15:38:15 +0000302 raise
Steve McIntyreb826fc72015-07-27 17:57:40 +0100303
Steve McIntyre3256b182014-12-19 15:38:15 +0000304 except IOError:
Steve McIntyre153157d2014-12-19 18:05:20 +0000305 # Bugger. Looks like one of the switch calls above
306 # failed. To undo the changes safely, we'll need to reset
307 # all the switches we managed to configure. This could
308 # take some time!
Steve McIntyre38a24e52015-07-21 17:54:58 +0100309 logging.error('create_vlan failed, resetting all switches to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000310 for switch_name in switches_done:
311 s = self.get_switch_driver(switch_name, config)
312 s.switch_connect(config.switches[switch_name].username,
313 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000314 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000315 s.switch_restart() # Will implicitly also close the connection
316 del s
317
Steve McIntyre3256b182014-12-19 15:38:15 +0000318 # Undo the database change
Steve McIntyre7cf80982015-02-12 07:03:40 +0000319 logging.debug('Switch access failed. Deleting the new VLAN entry in the database')
Steve McIntyre3256b182014-12-19 15:38:15 +0000320 db.delete_vlan(vlan_id)
321 raise
322
Steve McIntyre153157d2014-12-19 18:05:20 +0000323 # If we've got this far, things were successful. Save config
324 # on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000325 for switch_name in switches_done:
326 s = self.get_switch_driver(switch_name, config)
327 s.switch_connect(config.switches[switch_name].username,
328 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000329 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000330 s.switch_save_running_config()
331 s.switch_disconnect()
332 del s
333
Steve McIntyre6b8d3862015-08-03 19:28:25 +0100334 return (vlan_id, tag) # If we're successful
Steve McIntyre3256b182014-12-19 15:38:15 +0000335
Steve McIntyrefeb64522014-12-19 18:53:02 +0000336 # Complex call
337 # 1. Check in the DB if there are any ports on the VLAN. Bail if so
338 # 2. Iterate through all switches:
339 # a. Remove the VLAN from all trunk ports (if needed)
340 # b. Remove the VLAN
341 # 3. If all went OK, save config on the switches
342 # 4. Remove the VLAN in the DB
343 #
344 # If things fail, we attempt to roll back by rebooting switches.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000345 def delete_vlan(self, state, vlan_id):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000346
Steve McIntyre7cf80982015-02-12 07:03:40 +0000347 logging.debug('delete_vlan')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000348 db = state.db
349 config = state.config
350
Steve McIntyrefeb64522014-12-19 18:53:02 +0000351 # 1. Check for database records first
Steve McIntyre5fa22652015-04-01 18:01:45 +0100352 logging.debug('Checking for ports using VLAN ID %d', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000353 vlan = db.get_vlan_by_id(vlan_id)
354 if vlan is None:
355 raise InputError("VLAN ID %d does not exist" % vlan_id)
356 vlan_tag = vlan.tag
357 ports = db.get_ports_by_current_vlan(vlan_id)
358 if ports is not None:
359 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
360 (vlan_id, len(ports)))
361 ports = db.get_ports_by_base_vlan(vlan_id)
362 if ports is not None:
363 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
364 (vlan_id, len(ports)))
365
366 # Keep track of which switches we've configured, for later use
367 switches_done = []
368
369 # 2. Now the switches
370 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000371 for switch in db.all_switches():
372 switch_name = switch.name
Steve McIntyrefeb64522014-12-19 18:53:02 +0000373 trunk_ports = []
374 try:
375 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000376 s = self.get_switch_driver(switch_name, config)
377 s.switch_connect(config.switches[switch_name].username,
378 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000379 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000380
381 # Mark this switch as one we've touched, for
382 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000383 switches_done.append(switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000384
385 # 2a. Do we need to worry about trunk ports on this switch?
386 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000387 logging.debug('This switch does not need special trunk port handling')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000388 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000389 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000390 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000391 if trunk_ports is None:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000392 logging.debug("But it has no trunk ports defined")
Steve McIntyrefeb64522014-12-19 18:53:02 +0000393 trunk_ports = []
394 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100395 logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports))
Steve McIntyrefeb64522014-12-19 18:53:02 +0000396
397 # Modify any trunk ports as needed
398 for port in trunk_ports:
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000399 s.port_remove_trunk_from_vlan(port, vlan_tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100400 logging.debug('Removed VLAN tag %d from switch %s port %s', vlan_tag, switch_name, port)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000401
402 # 2b. Remove the VLAN from the switch
Steve McIntyre5fa22652015-04-01 18:01:45 +0100403 logging.debug('Removing VLAN tag %d from switch %s', vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000404 s.vlan_destroy(vlan_tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100405 logging.debug('Removed VLAN tag %d from switch %s', vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000406
407 # And now we're done with this switch
408 s.switch_disconnect()
409 del s
410
411 except IOError:
412 raise
413
414 except IOError:
415 # Bugger. Looks like one of the switch calls above
416 # failed. To undo the changes safely, we'll need to reset
417 # all the switches we managed to configure. This could
418 # take some time!
Steve McIntyre38a24e52015-07-21 17:54:58 +0100419 logging.error('delete_vlan failed, resetting all switches to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000420 for switch_name in switches_done:
421 s = self.get_switch_driver(switch_name, config)
422 s.switch_connect(config.switches[switch_name].username,
423 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000424 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000425 s.switch_restart() # Will implicitly also close the connection
426 del s
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000427 raise
Steve McIntyrefeb64522014-12-19 18:53:02 +0000428
429 # 3. If we've got this far, things were successful. Save
430 # config on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000431 for switch_name in switches_done:
432 s = self.get_switch_driver(switch_name, config)
433 s.switch_connect(config.switches[switch_name].username,
434 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000435 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000436 s.switch_save_running_config()
437 s.switch_disconnect()
438 del s
439
440 # 4. Finally, remove the VLAN in the DB
441 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100442 logging.debug('Removing DB record: VLAN ID %d', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000443 vlan_id = db.delete_vlan(vlan_id)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100444 logging.debug('Removed VLAN ID %d from the database OK', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000445 except InputError:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000446 logging.debug('DB deletion failed')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000447 raise
448
449 return vlan_id # If we're successful
450
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000451 # Complex call, depends on existing state a lot
452 # 1. Check validity of inputs
453 # 2. Switch mode and other config on the port.
454 # a. If switching trunk->access, remove all trunk VLANs from it
455 # (if needed) and switch back to the base VLAN for the
456 # port. Next, switch to access mode.
457 # b. If switching access->trunk, switch back to the base VLAN
458 # for the port. Next, switch mode. Then add all trunk VLANs
459 # to it (if needed)
460 # 3. If all went OK, save config on the switch
461 # 4. Change details of the port in the DB
462 #
463 # If things fail, we attempt to roll back by rebooting the switch
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000464 def set_port_mode(self, state, port_id, mode):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000465
Steve McIntyre7cf80982015-02-12 07:03:40 +0000466 logging.debug('set_port_mode')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000467 db = state.db
468 config = state.config
469
470 # 1. Sanity-check inputs
Steve McIntyre5b0de002015-01-23 18:05:13 +0000471 if mode != 'access' and mode != 'trunk':
472 raise InputError("Port mode '%s' is not a valid option: try 'access' or 'trunk'" % mode)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000473 port = db.get_port_by_id(port_id)
474 if port is None:
475 raise InputError("Port ID %d does not exist" % port_id)
Steve McIntyre28114092015-02-13 03:04:40 +0000476 if port.is_locked:
477 raise InputError("Port ID %d is locked" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000478 if mode == 'trunk' and port.is_trunk:
Steve McIntyre3aff7da2015-02-13 04:00:11 +0000479 raise InputError("Port ID %d is already in trunk mode" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000480 if mode == 'access' and not port.is_trunk:
Steve McIntyre3aff7da2015-02-13 04:00:11 +0000481 raise InputError("Port ID %d is already in access mode" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000482 base_vlan_tag = db.get_vlan_tag_by_id(port.base_vlan_id)
483
484 # Get the right driver
485 switch_name = db.get_switch_name_by_id(port.switch_id)
486 s = self.get_switch_driver(switch_name, config)
487
488 # 2. Now start configuring the switch
489 try:
490 s.switch_connect(config.switches[switch_name].username,
491 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000492 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000493 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100494 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000495 raise
496
497 try:
498 if port.is_trunk:
499 # 2a. We're going from a trunk port to an access port
500 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000501 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000502 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000503 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000504 vlans = s.port_get_trunk_vlan_list(port.name)
505 if vlans is None:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100506 logging.debug("But it has no VLANs defined on port %s", port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000507 vlans = []
508 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100509 logging.debug('Found %d vlans that may need dropping on port %s', len(vlans), port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000510
511 for vlan in vlans:
Steve McIntyre97f5e872015-01-23 18:07:05 +0000512 if vlan != state.config.vland.default_vlan_tag:
513 s.port_remove_trunk_from_vlan(port.name, vlan)
Steve McIntyref903b692015-07-09 18:29:05 +0100514
515 s.port_set_mode(port.name, "access")
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000516
517 else:
518 # 2b. We're going from an access port to a trunk port
519 s.port_set_access_vlan(port.name, base_vlan_tag)
520 s.port_set_mode(port.name, "trunk")
521 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000522 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000523 else:
524 vlans = db.all_vlans()
525 for vlan in vlans:
Steve McIntyreee742d92015-09-03 18:16:09 +0100526 if vlan.tag != state.config.vland.default_vlan_tag:
Steve McIntyre36ae6ac2015-09-03 18:10:33 +0100527 s.port_add_trunk_to_vlan(port.name, vlan.tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000528
529 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100530 logging.error('set_port_mode failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000531 # Bugger. Looks like one of the switch calls above
532 # failed. To undo the changes safely, we'll need to reset
533 # all the config on this switch
534 s.switch_restart() # Will implicitly also close the connection
535 del s
536 raise
537
538 # 3. All seems to have worked so far!
539 s.switch_save_running_config()
540 s.switch_disconnect()
541 del s
542
543 # 4. And update the DB
544 db.set_port_mode(port_id, mode)
545
546 return port_id # If we're successful
547
548 # Complex call, updating both DB and switch state
549 # 1. Check validity of inputs
550 # 2. Update the port config on the switch
551 # 3. If all went OK, save config on the switch
552 # 4. Change details of the port in the DB
553 #
554 # If things fail, we attempt to roll back by rebooting the switch
555 def set_current_vlan(self, state, port_id, vlan_id):
556
Steve McIntyre7cf80982015-02-12 07:03:40 +0000557 logging.debug('set_current_vlan')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000558 db = state.db
559 config = state.config
560
561 # 1. Sanity checks!
562 port = db.get_port_by_id(port_id)
563 if port is None:
564 raise InputError("Port ID %d does not exist" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000565 if port.is_locked:
566 raise InputError("Port ID %d is locked" % port_id)
Steve McIntyre3bb7bbd2015-02-13 03:06:01 +0000567 if port.is_trunk:
568 raise InputError("Port ID %d is not an access port" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000569
570 vlan = db.get_vlan_by_id(vlan_id)
571 if vlan is None:
572 raise InputError("VLAN ID %d does not exist" % vlan_id)
573
574 # Get the right driver
575 switch_name = db.get_switch_name_by_id(port.switch_id)
576 s = self.get_switch_driver(switch_name, config)
577
578 # 2. Now start configuring the switch
579 try:
580 s.switch_connect(config.switches[switch_name].username,
581 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000582 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000583 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100584 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000585 raise
586
587 try:
588 s.port_set_access_vlan(port.name, vlan.tag)
589 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100590 logging.error('set_current_vlan failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000591 # Bugger. Looks like one of the switch calls above
592 # failed. To undo the changes safely, we'll need to reset
593 # all the config on this switch
594 s.switch_restart() # Will implicitly also close the connection
595 del s
596 raise
597
598 # 3. All seems to have worked so far!
599 s.switch_save_running_config()
600 s.switch_disconnect()
601 del s
602
603 # 4. And update the DB
604 db.set_current_vlan(port_id, vlan_id)
605
606 return port_id # If we're successful
607
608 # Complex call, updating both DB and switch state
609 # 1. Check validity of input
610 # 2. Update the port config on the switch
611 # 3. If all went OK, save config on the switch
612 # 4. Change details of the port in the DB
613 #
614 # If things fail, we attempt to roll back by rebooting the switch
615 def restore_base_vlan(self, state, port_id):
616
Steve McIntyre7cf80982015-02-12 07:03:40 +0000617 logging.debug('restore_base_vlan')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000618 db = state.db
619 config = state.config
620
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000621 # 1. Sanity checks!
622 port = db.get_port_by_id(port_id)
623 if port is None:
624 raise InputError("Port ID %d does not exist" % port_id)
625 if port.is_trunk:
626 raise InputError("Port ID %d is not an access port" % port_id)
627 if port.is_locked:
628 raise InputError("Port ID %d is locked" % port_id)
629
630 # Bail out early if we're *already* on the base VLAN. This is
631 # not an error
632 if port.current_vlan_id == port.base_vlan_id:
633 return port_id
634
635 vlan = db.get_vlan_by_id(port.base_vlan_id)
636
637 # Get the right driver
638 switch_name = db.get_switch_name_by_id(port.switch_id)
639 s = self.get_switch_driver(switch_name, config)
640
641 # 2. Now start configuring the switch
642 try:
643 s.switch_connect(config.switches[switch_name].username,
644 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000645 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000646 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100647 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000648 raise
649
650 try:
651 s.port_set_access_vlan(port.name, vlan.tag)
652 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100653 logging.error('restore_base_vlan failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000654 # Bugger. Looks like one of the switch calls above
655 # failed. To undo the changes safely, we'll need to reset
656 # all the config on this switch
657 s.switch_restart() # Will implicitly also close the connection
658 del s
659 raise
660
661 # 3. All seems to have worked so far!
662 s.switch_save_running_config()
663 s.switch_disconnect()
664 del s
665
666 # 4. And update the DB
667 db.set_current_vlan(port_id, port.base_vlan_id)
668
669 return port_id # If we're successful
670
671 # Complex call, updating both DB and switch state
672 # * Check validity of input
673 # * Read all the config from the switch (switch, ports, VLANs)
674 # * Create initial DB entries to match each of those
675 # * Merge VLANs across all switches
676 # * Set up ports appropriately
677 #
678 def auto_import_switch(self, state, switch_name):
679
Steve McIntyre7cf80982015-02-12 07:03:40 +0000680 logging.debug('auto_import_switch')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000681 db = state.db
682 config = state.config
683
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000684 port_vlans = {}
685
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000686 # 1. Sanity checks!
687 switch_id = db.get_switch_id_by_name(switch_name)
688 if switch_id is not None:
689 raise InputError("Switch name %s already exists in the DB (ID %d)" % (switch_name, switch_id))
690
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000691 if not switch_name in config.switches:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000692 raise InputError("Switch name %s not defined in config" % switch_name)
693
694 # 2. Now start reading config from the switch
695 try:
696 s = self.get_switch_driver(switch_name, config)
697 s.switch_connect(config.switches[switch_name].username,
698 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000699 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000700 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100701 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000702 raise
703
704 # DON'T create the switch record in the DB first - we'll want
Steve McIntyrefc511242014-12-23 22:28:30 +0000705 # to create VLANs on *other* switches, and it's easier to do
706 # that before we've added our new switch
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000707
708 new_vlan_tags = []
709
710 # Grab the VLANs defined on this switch
711 vlan_tags = s.vlan_get_list()
Steve McIntyrefc511242014-12-23 22:28:30 +0000712
Steve McIntyre5fa22652015-04-01 18:01:45 +0100713 logging.debug(' found %d vlans on the switch', len(vlan_tags))
Steve McIntyrefc511242014-12-23 22:28:30 +0000714
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000715 for vlan_tag in vlan_tags:
716 vlan_name = s.vlan_get_name(vlan_tag)
717
718 # If a VLAN is already in the database, then that's easy -
719 # we can just ignore it. However, we have to check that
720 # there is not a different name for the existing VLAN tag
Steve McIntyreb1529072014-12-23 17:17:22 +0000721 # - bail out if so... UNLESS we're looking at the default
722 # VLAN
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000723 #
724 # If this VLAN tag is not already in the DB, we'll need to
725 # add it there and to all the other switches (and their
726 # trunk ports!) too.
Steve McIntyrefc511242014-12-23 22:28:30 +0000727 vlan_id = db.get_vlan_id_by_tag(vlan_tag)
Steve McIntyre6c09c0c2015-07-17 17:20:01 +0100728 if vlan_id != state.default_vlan_id:
Steve McIntyreb1529072014-12-23 17:17:22 +0000729 if vlan_id is not None:
730 vlan_db_name = db.get_vlan_name_by_id(vlan_id)
731 if vlan_name != vlan_db_name:
732 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 +0000733
Steve McIntyreb1529072014-12-23 17:17:22 +0000734 else:
735 # OK, we'll need to set up the new VLAN now. It can't
736 # be a base VLAN - switches don't have such a concept!
737 # Rather than create individually here, add to a
738 # list. *Only* once we've worked through all the
739 # switch's VLANs successfully (checking for existing
740 # records and possible clashes!) should we start
741 # committing changes
742 new_vlan_tags.append(vlan_tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000743
744 # Now create the VLAN DB entries
745 for vlan_tag in new_vlan_tags:
746 vlan_name = s.vlan_get_name(vlan_tag)
747 vlan_id = self.create_vlan(state, vlan_name, vlan_tag, False)
748
749 # *Now* add this switch itself to the database, after we've
750 # worked on all the other switches
751 switch_id = db.create_switch(switch_name)
752
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000753 # And now the ports
754 trunk_ports = []
755 ports = s.switch_get_port_names()
Steve McIntyre5fa22652015-04-01 18:01:45 +0100756 logging.debug(' found %d ports on the switch', len(ports))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000757 for port_name in ports:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100758 logging.debug(' trying to import port %s', port_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000759 port_id = None
760 port_mode = s.port_get_mode(port_name)
Steve McIntyreea753972015-08-05 13:52:48 +0100761 port_number = s.port_map_name_to_number(port_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000762 if port_mode == 'access':
763 # Access ports are easy - just create the port, and
764 # set both the current and base VLANs to the current
765 # VLAN on the switch. We'll end up changing this after
766 # import if needed.
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000767 port_vlans[port_name] = (s.port_get_access_vlan(port_name),)
768 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyreea753972015-08-05 13:52:48 +0100769 port_id = db.create_port(switch_id, port_name, port_number,
Steve McIntyre6f17b102014-12-24 02:18:08 +0000770 port_vlan_id, port_vlan_id)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100771 logging.debug(' access port, VLAN %d', int(port_vlans[port_name][0]))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000772 # Nothing further needed
773 elif port_mode == 'trunk':
Steve McIntyre7cf80982015-02-12 07:03:40 +0000774 logging.debug(' trunk port, VLANs:')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000775 # Trunk ports are a little more involved. First,
776 # create the port in the DB, setting the VLANs to the
777 # first VLAN found on the trunk port. This will *also*
778 # be in access mode by default, and unlocked.
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000779 port_vlans[port_name] = s.port_get_trunk_vlan_list(port_name)
Steve McIntyre7cf80982015-02-12 07:03:40 +0000780 logging.debug(port_vlans[port_name])
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000781 if port_vlans[port_name] == [] or port_vlans[port_name] is None or 'ALL' in port_vlans[port_name]:
782 port_vlans[port_name] = (state.config.vland.default_vlan_tag,)
783 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyreea753972015-08-05 13:52:48 +0100784 port_id = db.create_port(switch_id, port_name, port_number,
Steve McIntyre6f17b102014-12-24 02:18:08 +0000785 port_vlan_id, port_vlan_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000786 # Append to a list of trunk ports that we will need to
787 # modify once we're done
788 trunk_ports.append(port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000789 else:
Steve McIntyre857d89f2015-07-24 16:22:37 +0100790 # We've found a port mode we don't want, e.g. the
791 # "dynamic auto" on a Cisco Catalyst. Handle that here
792 # - tell the switch to set that port to access and
793 # handle accordingly.
794 s.port_set_mode(port_name, 'access')
795 port_vlans[port_name] = (s.port_get_access_vlan(port_name),)
796 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyreea753972015-08-05 13:52:48 +0100797 port_id = db.create_port(switch_id, port_name, port_number,
Steve McIntyre857d89f2015-07-24 16:22:37 +0100798 port_vlan_id, port_vlan_id)
799 logging.debug(' Found port in %s mode', port_mode)
800 logging.debug(' Forcing to access mode, VLAN %d', int(port_vlans[port_name][0]))
801 port_mode = "access"
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000802
Steve McIntyre5fa22652015-04-01 18:01:45 +0100803 logging.debug(" Added port %s, got port ID %d", port_name, port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000804
Steve McIntyre574e3342015-01-23 18:08:33 +0000805 db.set_port_mode(port_id, port_mode)
806
Steve McIntyre6feea572015-02-09 15:49:20 +0000807 # Make sure this switch has all the VLANs we need
808 for vlan in db.all_vlans():
Steve McIntyre49d3c082015-07-24 16:17:23 +0100809 if vlan.tag != state.config.vland.default_vlan_tag:
Steve McIntyred7fb4072015-02-11 17:13:50 +0000810 if not vlan.tag in vlan_tags:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100811 logging.debug("Adding VLAN tag %d to this switch", vlan.tag)
Steve McIntyre6feea572015-02-09 15:49:20 +0000812 s.vlan_create(vlan.tag)
813 s.vlan_set_name(vlan.tag, vlan.name)
814
Steve McIntyrefc511242014-12-23 22:28:30 +0000815 # Now, on each trunk port on the switch, we need to add all
816 # the VLANs already configured across our system
817 if not 'TrunkWildCardVlans' in s.switch_get_capabilities():
818 for port_id in trunk_ports:
819 port = db.get_port_by_id(port_id)
Steve McIntyreb826fc72015-07-27 17:57:40 +0100820
Steve McIntyrefc511242014-12-23 22:28:30 +0000821 for vlan in db.all_vlans():
Steve McIntyre49d3c082015-07-24 16:17:23 +0100822 if vlan.vlan_id != state.default_vlan_id:
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000823 if not vlan.tag in port_vlans[port.name]:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100824 logging.debug("Adding allowed VLAN tag %d to trunk port %s", vlan.tag, port.name)
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000825 s.port_add_trunk_to_vlan(port.name, vlan.tag)
Steve McIntyrefc511242014-12-23 22:28:30 +0000826
Steve McIntyrefc511242014-12-23 22:28:30 +0000827 # Done with this switch \o/
828 s.switch_save_running_config()
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000829 s.switch_disconnect()
830 del s
831
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000832 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000833 ret['switch_id'] = switch_id
834 ret['num_ports_added'] = len(ports)
835 ret['num_vlans_added'] = len(new_vlan_tags)
836 return ret # If we're successful