blob: 1bb0534e44cd0bde004cf1ae8f470d18503a20c5 [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):
Steve McIntyre5fa22652015-04-01 18:01:45 +01009 logging.debug("Trying to find a driver for %s", switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000010 driver = config.switches[switch_name].driver
Steve McIntyre5fa22652015-04-01 18:01:45 +010011 logging.debug("Driver: %s", driver)
Steve McIntyref1c04f92014-12-16 18:23:15 +000012 module = __import__("drivers.%s" % driver, fromlist=[driver])
13 class_ = getattr(module, driver)
Steve McIntyre0f38f0e2015-07-14 15:26:05 +010014 return class_(switch_name, debug = config.switches[switch_name].debug)
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):
Steve McIntyre5fa22652015-04-01 18:01:45 +010020 logging.debug("Found switch %s:", switch_name)
Steve McIntyre7cf80982015-02-12 07:03:40 +000021 logging.debug(" 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()
Steve McIntyre2ed90f52015-07-21 17:59:52 +010031 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 McIntyre7cf80982015-02-12 07:03:40 +000036 logging.debug('perform_db_query')
37 logging.debug(command)
38 logging.debug(data)
Steve McIntyref1c04f92014-12-16 18:23:15 +000039 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'])
Steve McIntyre65533d72015-01-23 18:01:17 +000070 elif command == 'db.get_vlan_tag_by_id':
71 ret = db.get_vlan_tag_by_id(data['vlan_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000072 elif command == 'db.get_vlan_id_by_name':
73 ret = db.get_vlan_id_by_name(data['name'])
74 elif command == 'db.get_vlan_id_by_tag':
Steve McIntyre07946c22014-12-17 13:14:15 +000075 ret = db.get_vlan_id_by_tag(data['tag'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000076 elif command == 'db.get_vlan_name_by_id':
77 ret = db.get_vlan_name_by_id(data['vlan_id'])
78 else:
Steve McIntyree749fef2014-12-17 16:35:45 +000079 raise InputError("Unknown db_query command \"%s\"" % command)
Steve McIntyref1c04f92014-12-16 18:23:15 +000080
Steve McIntyre5da37fa2014-12-17 13:14:44 +000081 except InputError:
82 raise
83
Steve McIntyre798af842014-12-23 22:29:46 +000084# except:
85# raise InputError("Invalid input in query")
Steve McIntyref1c04f92014-12-16 18:23:15 +000086
87 return ret
88
Steve McIntyre53c7ad92014-12-16 19:21:13 +000089 # Simple helper wrapper for all the read-only daemon state queries
90 def perform_daemon_query(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +000091 logging.debug('perform_daemon_query')
92 logging.debug(command)
93 logging.debug(data)
Steve McIntyre53c7ad92014-12-16 19:21:13 +000094 ret = {}
95 try:
96 if command == 'daemon.status':
97 # data ignored
98 ret['running'] = 'ok'
99 elif command == 'daemon.version':
100 # data ignored
101 ret['version'] = state.version
102 elif command == 'daemon.statistics':
103 ret['uptime'] = time.time() - state.starttime
Steve McIntyre88b79df2014-12-23 13:45:08 +0000104 elif command == 'daemon.probe_switches':
105 ret = self.probe_switches(state)
Steve McIntyre06fe6422015-01-23 17:55:43 +0000106 elif command == 'daemon.shutdown':
107 # data ignored
108 ret['shutdown'] = 'Shutting down'
109 state.running = False
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000110 else:
Steve McIntyree749fef2014-12-17 16:35:45 +0000111 raise InputError("Unknown daemon_query command \"%s\"" % command)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000112
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000113 except InputError:
114 raise
115
Steve McIntyre798af842014-12-23 22:29:46 +0000116# except:
117# raise InputError("Invalid input in query")
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000118
119 return ret
120
Steve McIntyree749fef2014-12-17 16:35:45 +0000121 # Helper wrapper for API functions modifying database state only
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000122 def perform_db_update(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000123 logging.debug('perform_db_update')
124 logging.debug(command)
125 logging.debug(data)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000126 ret = {}
127 db = state.db
128 try:
129 if command == 'db.create_switch':
130 ret = db.create_switch(data['name'])
131 elif command == 'db.create_port':
132 ret = db.create_port(data['switch_id'], data['name'],
Steve McIntyrefefdbb42014-12-22 16:14:28 +0000133 state.default_vlan_id,
134 state.default_vlan_id)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000135 elif command == 'db.delete_switch':
136 ret = db.delete_switch(data['switch_id'])
137 elif command == 'db.delete_port':
138 ret = db.delete_port(data['port_id'])
139 elif command == 'db.set_port_is_locked':
140 ret = db.set_port_is_locked(data['port_id'], data['is_locked'])
141 elif command == 'db.set_base_vlan':
142 ret = db.set_base_vlan(data['port_id'], data['base_vlan_id'])
143 else:
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000144 raise InputError("Unknown db_update command \"%s\"" % command)
145
146 except InputError:
147 raise
148
Steve McIntyre798af842014-12-23 22:29:46 +0000149# except:
150# raise InputError("Invalid input in query")
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000151
152 return ret
153
154 # Helper wrapper for API functions that modify both database state
155 # and on-switch VLAN state
156 def perform_vlan_update(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000157 logging.debug('perform_vlan_update')
158 logging.debug(command)
159 logging.debug(data)
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000160 ret = {}
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000161
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000162 try:
163 # All of these are complex commands, so call helpers
164 # rather than inline the code here
165 if command == 'api.create_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000166 ret = self.create_vlan(state, data['name'], int(data['tag']), data['is_base_vlan'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000167 elif command == 'api.delete_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000168 ret = self.delete_vlan(state, int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000169 elif command == 'api.set_port_mode':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000170 ret = self.set_port_mode(state, int(data['port_id']), data['mode'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000171 elif command == 'api.set_current_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000172 ret = self.set_current_vlan(state, int(data['port_id']), int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000173 elif command == 'api.restore_base_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000174 ret = self.restore_base_vlan(state, int(data['port_id']))
175 elif command == 'api.auto_import_switch':
176 ret = self.auto_import_switch(state, data['switch'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000177 else:
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000178 raise InputError("Unknown query command \"%s\"" % command)
179
Steve McIntyre3256b182014-12-19 15:38:15 +0000180 except InputError as e:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100181 logging.debug('got error %s', e)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000182 raise
183
Steve McIntyre798af842014-12-23 22:29:46 +0000184# except:
185# raise InputError("Invalid input in query")
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000186
187 return ret
188
189
Steve McIntyre3256b182014-12-19 15:38:15 +0000190 # Complex call
191 # 1. create the VLAN in the DB
192 # 2. Iterate through all switches:
Steve McIntyre1ab8b872014-12-19 18:37:00 +0000193 # a. Create the VLAN
194 # b. Add the VLAN to all trunk ports (if needed)
195 # 3. If all went OK, save config on all the switches
Steve McIntyre3256b182014-12-19 15:38:15 +0000196 #
197 # The VLAN may already exist on some of the switches, that's
Steve McIntyre153157d2014-12-19 18:05:20 +0000198 # fine. If things fail, we attempt to roll back by rebooting
199 # switches then removing the VLAN in the DB.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000200 def create_vlan(self, state, name, tag, is_base_vlan):
Steve McIntyre3256b182014-12-19 15:38:15 +0000201
Steve McIntyre7cf80982015-02-12 07:03:40 +0000202 logging.debug('create_vlan')
Steve McIntyre3256b182014-12-19 15:38:15 +0000203 db = state.db
204 config = state.config
205
Steve McIntyre3256b182014-12-19 15:38:15 +0000206 # 1. Database record first
207 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100208 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 +0000209 vlan_id = db.create_vlan(name, tag, is_base_vlan)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100210 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 +0000211 except InputError:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000212 logging.debug('DB creation failed')
Steve McIntyre3256b182014-12-19 15:38:15 +0000213 raise
214
Steve McIntyre153157d2014-12-19 18:05:20 +0000215 # Keep track of which switches we've configured, for later use
216 switches_done = []
217
Steve McIntyre3256b182014-12-19 15:38:15 +0000218 # 2. Now the switches
219 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000220 for switch in db.all_switches():
Steve McIntyre3256b182014-12-19 15:38:15 +0000221 trunk_ports = []
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000222 switch_name = switch.name
Steve McIntyre3256b182014-12-19 15:38:15 +0000223 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100224 logging.debug('Adding new VLAN to switch %s', switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000225 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000226 s = self.get_switch_driver(switch_name, config)
227 s.switch_connect(config.switches[switch_name].username,
228 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000229 config.switches[switch_name].enable_password)
Steve McIntyre3256b182014-12-19 15:38:15 +0000230
Steve McIntyre153157d2014-12-19 18:05:20 +0000231 # Mark this switch as one we've touched, for
232 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000233 switches_done.append(switch_name)
Steve McIntyre153157d2014-12-19 18:05:20 +0000234
Steve McIntyre3256b182014-12-19 15:38:15 +0000235 # 2a. Create the VLAN on the switch
236 s.vlan_create(tag)
237 s.vlan_set_name(tag, name)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100238 logging.debug('Added VLAN tag %d, name %s to switch %s', tag, name, switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000239
240 # 2b. Do we need to worry about trunk ports on this switch?
241 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000242 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre3256b182014-12-19 15:38:15 +0000243 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000244 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000245 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyre3256b182014-12-19 15:38:15 +0000246 if trunk_ports is None:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000247 logging.debug("But it has no trunk ports defined")
Steve McIntyre3256b182014-12-19 15:38:15 +0000248 trunk_ports = []
249 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100250 logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports))
Steve McIntyre3256b182014-12-19 15:38:15 +0000251
252 # Modify any trunk ports as needed
253 for port in trunk_ports:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100254 logging.debug('Added VLAN tag %d, name %s to switch %s', tag, name, switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000255 s.port_add_trunk_to_vlan(port, tag)
256
Steve McIntyre3256b182014-12-19 15:38:15 +0000257 # And now we're done with this switch
258 s.switch_disconnect()
259 del s
260
261 except IOError:
262 raise
263
264 except IOError:
Steve McIntyre153157d2014-12-19 18:05:20 +0000265 # Bugger. Looks like one of the switch calls above
266 # failed. To undo the changes safely, we'll need to reset
267 # all the switches we managed to configure. This could
268 # take some time!
Steve McIntyre38a24e52015-07-21 17:54:58 +0100269 logging.error('create_vlan failed, resetting all switches to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000270 for switch_name in switches_done:
271 s = self.get_switch_driver(switch_name, config)
272 s.switch_connect(config.switches[switch_name].username,
273 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000274 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000275 s.switch_restart() # Will implicitly also close the connection
276 del s
277
Steve McIntyre3256b182014-12-19 15:38:15 +0000278 # Undo the database change
Steve McIntyre7cf80982015-02-12 07:03:40 +0000279 logging.debug('Switch access failed. Deleting the new VLAN entry in the database')
Steve McIntyre3256b182014-12-19 15:38:15 +0000280 db.delete_vlan(vlan_id)
281 raise
282
Steve McIntyre153157d2014-12-19 18:05:20 +0000283 # If we've got this far, things were successful. Save config
284 # on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000285 for switch_name in switches_done:
286 s = self.get_switch_driver(switch_name, config)
287 s.switch_connect(config.switches[switch_name].username,
288 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000289 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000290 s.switch_save_running_config()
291 s.switch_disconnect()
292 del s
293
Steve McIntyre3256b182014-12-19 15:38:15 +0000294 return vlan_id # If we're successful
295
Steve McIntyrefeb64522014-12-19 18:53:02 +0000296 # Complex call
297 # 1. Check in the DB if there are any ports on the VLAN. Bail if so
298 # 2. Iterate through all switches:
299 # a. Remove the VLAN from all trunk ports (if needed)
300 # b. Remove the VLAN
301 # 3. If all went OK, save config on the switches
302 # 4. Remove the VLAN in the DB
303 #
304 # If things fail, we attempt to roll back by rebooting switches.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000305 def delete_vlan(self, state, vlan_id):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000306
Steve McIntyre7cf80982015-02-12 07:03:40 +0000307 logging.debug('delete_vlan')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000308 db = state.db
309 config = state.config
310
Steve McIntyrefeb64522014-12-19 18:53:02 +0000311 # 1. Check for database records first
Steve McIntyre5fa22652015-04-01 18:01:45 +0100312 logging.debug('Checking for ports using VLAN ID %d', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000313 vlan = db.get_vlan_by_id(vlan_id)
314 if vlan is None:
315 raise InputError("VLAN ID %d does not exist" % vlan_id)
316 vlan_tag = vlan.tag
317 ports = db.get_ports_by_current_vlan(vlan_id)
318 if ports is not None:
319 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
320 (vlan_id, len(ports)))
321 ports = db.get_ports_by_base_vlan(vlan_id)
322 if ports is not None:
323 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
324 (vlan_id, len(ports)))
325
326 # Keep track of which switches we've configured, for later use
327 switches_done = []
328
329 # 2. Now the switches
330 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000331 for switch in db.all_switches():
332 switch_name = switch.name
Steve McIntyrefeb64522014-12-19 18:53:02 +0000333 trunk_ports = []
334 try:
335 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000336 s = self.get_switch_driver(switch_name, config)
337 s.switch_connect(config.switches[switch_name].username,
338 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000339 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000340
341 # Mark this switch as one we've touched, for
342 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000343 switches_done.append(switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000344
345 # 2a. Do we need to worry about trunk ports on this switch?
346 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000347 logging.debug('This switch does not need special trunk port handling')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000348 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000349 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000350 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000351 if trunk_ports is None:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000352 logging.debug("But it has no trunk ports defined")
Steve McIntyrefeb64522014-12-19 18:53:02 +0000353 trunk_ports = []
354 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100355 logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports))
Steve McIntyrefeb64522014-12-19 18:53:02 +0000356
357 # Modify any trunk ports as needed
358 for port in trunk_ports:
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000359 s.port_remove_trunk_from_vlan(port, vlan_tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100360 logging.debug('Removed VLAN tag %d from switch %s port %s', vlan_tag, switch_name, port)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000361
362 # 2b. Remove the VLAN from the switch
Steve McIntyre5fa22652015-04-01 18:01:45 +0100363 logging.debug('Removing VLAN tag %d from switch %s', vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000364 s.vlan_destroy(vlan_tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100365 logging.debug('Removed VLAN tag %d from switch %s', vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000366
367 # And now we're done with this switch
368 s.switch_disconnect()
369 del s
370
371 except IOError:
372 raise
373
374 except IOError:
375 # Bugger. Looks like one of the switch calls above
376 # failed. To undo the changes safely, we'll need to reset
377 # all the switches we managed to configure. This could
378 # take some time!
Steve McIntyre38a24e52015-07-21 17:54:58 +0100379 logging.error('delete_vlan failed, resetting all switches to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000380 for switch_name in switches_done:
381 s = self.get_switch_driver(switch_name, config)
382 s.switch_connect(config.switches[switch_name].username,
383 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000384 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000385 s.switch_restart() # Will implicitly also close the connection
386 del s
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000387 raise
Steve McIntyrefeb64522014-12-19 18:53:02 +0000388
389 # 3. If we've got this far, things were successful. Save
390 # config on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000391 for switch_name in switches_done:
392 s = self.get_switch_driver(switch_name, config)
393 s.switch_connect(config.switches[switch_name].username,
394 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000395 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000396 s.switch_save_running_config()
397 s.switch_disconnect()
398 del s
399
400 # 4. Finally, remove the VLAN in the DB
401 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100402 logging.debug('Removing DB record: VLAN ID %d', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000403 vlan_id = db.delete_vlan(vlan_id)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100404 logging.debug('Removed VLAN ID %d from the database OK', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000405 except InputError:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000406 logging.debug('DB deletion failed')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000407 raise
408
409 return vlan_id # If we're successful
410
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000411 # Complex call, depends on existing state a lot
412 # 1. Check validity of inputs
413 # 2. Switch mode and other config on the port.
414 # a. If switching trunk->access, remove all trunk VLANs from it
415 # (if needed) and switch back to the base VLAN for the
416 # port. Next, switch to access mode.
417 # b. If switching access->trunk, switch back to the base VLAN
418 # for the port. Next, switch mode. Then add all trunk VLANs
419 # to it (if needed)
420 # 3. If all went OK, save config on the switch
421 # 4. Change details of the port in the DB
422 #
423 # If things fail, we attempt to roll back by rebooting the switch
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000424 def set_port_mode(self, state, port_id, mode):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000425
Steve McIntyre7cf80982015-02-12 07:03:40 +0000426 logging.debug('set_port_mode')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000427 db = state.db
428 config = state.config
429
430 # 1. Sanity-check inputs
Steve McIntyre5b0de002015-01-23 18:05:13 +0000431 if mode != 'access' and mode != 'trunk':
432 raise InputError("Port mode '%s' is not a valid option: try 'access' or 'trunk'" % mode)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000433 port = db.get_port_by_id(port_id)
434 if port is None:
435 raise InputError("Port ID %d does not exist" % port_id)
Steve McIntyre28114092015-02-13 03:04:40 +0000436 if port.is_locked:
437 raise InputError("Port ID %d is locked" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000438 if mode == 'trunk' and port.is_trunk:
Steve McIntyre3aff7da2015-02-13 04:00:11 +0000439 raise InputError("Port ID %d is already in trunk mode" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000440 if mode == 'access' and not port.is_trunk:
Steve McIntyre3aff7da2015-02-13 04:00:11 +0000441 raise InputError("Port ID %d is already in access mode" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000442 base_vlan_tag = db.get_vlan_tag_by_id(port.base_vlan_id)
443
444 # Get the right driver
445 switch_name = db.get_switch_name_by_id(port.switch_id)
446 s = self.get_switch_driver(switch_name, config)
447
448 # 2. Now start configuring the switch
449 try:
450 s.switch_connect(config.switches[switch_name].username,
451 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000452 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000453 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100454 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000455 raise
456
457 try:
458 if port.is_trunk:
459 # 2a. We're going from a trunk port to an access port
460 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000461 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000462 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000463 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000464 vlans = s.port_get_trunk_vlan_list(port.name)
465 if vlans is None:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100466 logging.debug("But it has no VLANs defined on port %s", port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000467 vlans = []
468 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100469 logging.debug('Found %d vlans that may need dropping on port %s', len(vlans), port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000470
471 for vlan in vlans:
Steve McIntyre97f5e872015-01-23 18:07:05 +0000472 if vlan != state.config.vland.default_vlan_tag:
473 s.port_remove_trunk_from_vlan(port.name, vlan)
Steve McIntyref903b692015-07-09 18:29:05 +0100474
475 s.port_set_mode(port.name, "access")
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000476
477 else:
478 # 2b. We're going from an access port to a trunk port
479 s.port_set_access_vlan(port.name, base_vlan_tag)
480 s.port_set_mode(port.name, "trunk")
481 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000482 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000483 else:
484 vlans = db.all_vlans()
485 for vlan in vlans:
486 s.port_add_trunk_to_vlan(port.name, vlan.tag)
487
488 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100489 logging.error('set_port_mode failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000490 # Bugger. Looks like one of the switch calls above
491 # failed. To undo the changes safely, we'll need to reset
492 # all the config on this switch
493 s.switch_restart() # Will implicitly also close the connection
494 del s
495 raise
496
497 # 3. All seems to have worked so far!
498 s.switch_save_running_config()
499 s.switch_disconnect()
500 del s
501
502 # 4. And update the DB
503 db.set_port_mode(port_id, mode)
504
505 return port_id # If we're successful
506
507 # Complex call, updating both DB and switch state
508 # 1. Check validity of inputs
509 # 2. Update the port config on the switch
510 # 3. If all went OK, save config on the switch
511 # 4. Change details of the port in the DB
512 #
513 # If things fail, we attempt to roll back by rebooting the switch
514 def set_current_vlan(self, state, port_id, vlan_id):
515
Steve McIntyre7cf80982015-02-12 07:03:40 +0000516 logging.debug('set_current_vlan')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000517 db = state.db
518 config = state.config
519
520 # 1. Sanity checks!
521 port = db.get_port_by_id(port_id)
522 if port is None:
523 raise InputError("Port ID %d does not exist" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000524 if port.is_locked:
525 raise InputError("Port ID %d is locked" % port_id)
Steve McIntyre3bb7bbd2015-02-13 03:06:01 +0000526 if port.is_trunk:
527 raise InputError("Port ID %d is not an access port" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000528
529 vlan = db.get_vlan_by_id(vlan_id)
530 if vlan is None:
531 raise InputError("VLAN ID %d does not exist" % vlan_id)
532
533 # Get the right driver
534 switch_name = db.get_switch_name_by_id(port.switch_id)
535 s = self.get_switch_driver(switch_name, config)
536
537 # 2. Now start configuring the switch
538 try:
539 s.switch_connect(config.switches[switch_name].username,
540 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000541 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000542 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100543 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000544 raise
545
546 try:
547 s.port_set_access_vlan(port.name, vlan.tag)
548 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100549 logging.error('set_current_vlan failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000550 # Bugger. Looks like one of the switch calls above
551 # failed. To undo the changes safely, we'll need to reset
552 # all the config on this switch
553 s.switch_restart() # Will implicitly also close the connection
554 del s
555 raise
556
557 # 3. All seems to have worked so far!
558 s.switch_save_running_config()
559 s.switch_disconnect()
560 del s
561
562 # 4. And update the DB
563 db.set_current_vlan(port_id, vlan_id)
564
565 return port_id # If we're successful
566
567 # Complex call, updating both DB and switch state
568 # 1. Check validity of input
569 # 2. Update the port config on the switch
570 # 3. If all went OK, save config on the switch
571 # 4. Change details of the port in the DB
572 #
573 # If things fail, we attempt to roll back by rebooting the switch
574 def restore_base_vlan(self, state, port_id):
575
Steve McIntyre7cf80982015-02-12 07:03:40 +0000576 logging.debug('restore_base_vlan')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000577 db = state.db
578 config = state.config
579
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000580 # 1. Sanity checks!
581 port = db.get_port_by_id(port_id)
582 if port is None:
583 raise InputError("Port ID %d does not exist" % port_id)
584 if port.is_trunk:
585 raise InputError("Port ID %d is not an access port" % port_id)
586 if port.is_locked:
587 raise InputError("Port ID %d is locked" % port_id)
588
589 # Bail out early if we're *already* on the base VLAN. This is
590 # not an error
591 if port.current_vlan_id == port.base_vlan_id:
592 return port_id
593
594 vlan = db.get_vlan_by_id(port.base_vlan_id)
595
596 # Get the right driver
597 switch_name = db.get_switch_name_by_id(port.switch_id)
598 s = self.get_switch_driver(switch_name, config)
599
600 # 2. Now start configuring the switch
601 try:
602 s.switch_connect(config.switches[switch_name].username,
603 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000604 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000605 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100606 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000607 raise
608
609 try:
610 s.port_set_access_vlan(port.name, vlan.tag)
611 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100612 logging.error('restore_base_vlan failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000613 # Bugger. Looks like one of the switch calls above
614 # failed. To undo the changes safely, we'll need to reset
615 # all the config on this switch
616 s.switch_restart() # Will implicitly also close the connection
617 del s
618 raise
619
620 # 3. All seems to have worked so far!
621 s.switch_save_running_config()
622 s.switch_disconnect()
623 del s
624
625 # 4. And update the DB
626 db.set_current_vlan(port_id, port.base_vlan_id)
627
628 return port_id # If we're successful
629
630 # Complex call, updating both DB and switch state
631 # * Check validity of input
632 # * Read all the config from the switch (switch, ports, VLANs)
633 # * Create initial DB entries to match each of those
634 # * Merge VLANs across all switches
635 # * Set up ports appropriately
636 #
637 def auto_import_switch(self, state, switch_name):
638
Steve McIntyre7cf80982015-02-12 07:03:40 +0000639 logging.debug('auto_import_switch')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000640 db = state.db
641 config = state.config
642
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000643 port_vlans = {}
644
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000645 # 1. Sanity checks!
646 switch_id = db.get_switch_id_by_name(switch_name)
647 if switch_id is not None:
648 raise InputError("Switch name %s already exists in the DB (ID %d)" % (switch_name, switch_id))
649
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000650 if not switch_name in config.switches:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000651 raise InputError("Switch name %s not defined in config" % switch_name)
652
653 # 2. Now start reading config from the switch
654 try:
655 s = self.get_switch_driver(switch_name, config)
656 s.switch_connect(config.switches[switch_name].username,
657 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000658 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000659 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100660 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000661 raise
662
663 # DON'T create the switch record in the DB first - we'll want
Steve McIntyrefc511242014-12-23 22:28:30 +0000664 # to create VLANs on *other* switches, and it's easier to do
665 # that before we've added our new switch
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000666
667 new_vlan_tags = []
668
669 # Grab the VLANs defined on this switch
670 vlan_tags = s.vlan_get_list()
Steve McIntyrefc511242014-12-23 22:28:30 +0000671
Steve McIntyre5fa22652015-04-01 18:01:45 +0100672 logging.debug(' found %d vlans on the switch', len(vlan_tags))
Steve McIntyrefc511242014-12-23 22:28:30 +0000673
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000674 for vlan_tag in vlan_tags:
675 vlan_name = s.vlan_get_name(vlan_tag)
676
677 # If a VLAN is already in the database, then that's easy -
678 # we can just ignore it. However, we have to check that
679 # there is not a different name for the existing VLAN tag
Steve McIntyreb1529072014-12-23 17:17:22 +0000680 # - bail out if so... UNLESS we're looking at the default
681 # VLAN
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000682 #
683 # If this VLAN tag is not already in the DB, we'll need to
684 # add it there and to all the other switches (and their
685 # trunk ports!) too.
Steve McIntyrefc511242014-12-23 22:28:30 +0000686 vlan_id = db.get_vlan_id_by_tag(vlan_tag)
Steve McIntyre6c09c0c2015-07-17 17:20:01 +0100687 if vlan_id != state.default_vlan_id:
Steve McIntyreb1529072014-12-23 17:17:22 +0000688 if vlan_id is not None:
689 vlan_db_name = db.get_vlan_name_by_id(vlan_id)
690 if vlan_name != vlan_db_name:
691 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 +0000692
Steve McIntyreb1529072014-12-23 17:17:22 +0000693 else:
694 # OK, we'll need to set up the new VLAN now. It can't
695 # be a base VLAN - switches don't have such a concept!
696 # Rather than create individually here, add to a
697 # list. *Only* once we've worked through all the
698 # switch's VLANs successfully (checking for existing
699 # records and possible clashes!) should we start
700 # committing changes
701 new_vlan_tags.append(vlan_tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000702
703 # Now create the VLAN DB entries
704 for vlan_tag in new_vlan_tags:
705 vlan_name = s.vlan_get_name(vlan_tag)
706 vlan_id = self.create_vlan(state, vlan_name, vlan_tag, False)
707
708 # *Now* add this switch itself to the database, after we've
709 # worked on all the other switches
710 switch_id = db.create_switch(switch_name)
711
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000712 # And now the ports
713 trunk_ports = []
714 ports = s.switch_get_port_names()
Steve McIntyre5fa22652015-04-01 18:01:45 +0100715 logging.debug(' found %d ports on the switch', len(ports))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000716 for port_name in ports:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100717 logging.debug(' trying to import port %s', port_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000718 port_id = None
719 port_mode = s.port_get_mode(port_name)
720 if port_mode == 'access':
721 # Access ports are easy - just create the port, and
722 # set both the current and base VLANs to the current
723 # VLAN on the switch. We'll end up changing this after
724 # import if needed.
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000725 port_vlans[port_name] = (s.port_get_access_vlan(port_name),)
726 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyre6f17b102014-12-24 02:18:08 +0000727 port_id = db.create_port(switch_id, port_name,
728 port_vlan_id, port_vlan_id)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100729 logging.debug(' access port, VLAN %d', int(port_vlans[port_name][0]))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000730 # Nothing further needed
731 elif port_mode == 'trunk':
Steve McIntyre7cf80982015-02-12 07:03:40 +0000732 logging.debug(' trunk port, VLANs:')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000733 # Trunk ports are a little more involved. First,
734 # create the port in the DB, setting the VLANs to the
735 # first VLAN found on the trunk port. This will *also*
736 # be in access mode by default, and unlocked.
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000737 port_vlans[port_name] = s.port_get_trunk_vlan_list(port_name)
Steve McIntyre7cf80982015-02-12 07:03:40 +0000738 logging.debug(port_vlans[port_name])
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000739 if port_vlans[port_name] == [] or port_vlans[port_name] is None or 'ALL' in port_vlans[port_name]:
740 port_vlans[port_name] = (state.config.vland.default_vlan_tag,)
741 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000742 port_id = db.create_port(switch_id, port_name,
Steve McIntyre6f17b102014-12-24 02:18:08 +0000743 port_vlan_id, port_vlan_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000744 # Append to a list of trunk ports that we will need to
745 # modify once we're done
746 trunk_ports.append(port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000747 else:
748 raise CriticalError("Unrecognised port port mode %s???" % port_mode)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000749
Steve McIntyre5fa22652015-04-01 18:01:45 +0100750 logging.debug(" Added port %s, got port ID %d", port_name, port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000751
Steve McIntyre574e3342015-01-23 18:08:33 +0000752 db.set_port_mode(port_id, port_mode)
753
Steve McIntyre6feea572015-02-09 15:49:20 +0000754 # Make sure this switch has all the VLANs we need
755 for vlan in db.all_vlans():
756 if vlan.tag is not state.config.vland.default_vlan_tag:
Steve McIntyred7fb4072015-02-11 17:13:50 +0000757 if not vlan.tag in vlan_tags:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100758 logging.debug("Adding VLAN tag %d to this switch", vlan.tag)
Steve McIntyre6feea572015-02-09 15:49:20 +0000759 s.vlan_create(vlan.tag)
760 s.vlan_set_name(vlan.tag, vlan.name)
761
Steve McIntyrefc511242014-12-23 22:28:30 +0000762 # Now, on each trunk port on the switch, we need to add all
763 # the VLANs already configured across our system
764 if not 'TrunkWildCardVlans' in s.switch_get_capabilities():
765 for port_id in trunk_ports:
766 port = db.get_port_by_id(port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000767
768 for vlan in db.all_vlans():
769 if vlan.vlan_id is not state.default_vlan_id:
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000770 if not vlan.tag in port_vlans[port.name]:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100771 logging.debug("Adding allowed VLAN tag %d to trunk port %s", vlan.tag, port.name)
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000772 s.port_add_trunk_to_vlan(port.name, vlan.tag)
Steve McIntyrefc511242014-12-23 22:28:30 +0000773
Steve McIntyrefc511242014-12-23 22:28:30 +0000774 # Done with this switch \o/
775 s.switch_save_running_config()
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000776 s.switch_disconnect()
777 del s
778
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000779 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000780 ret['switch_id'] = switch_id
781 ret['num_ports_added'] = len(ports)
782 ret['num_vlans_added'] = len(new_vlan_tags)
783 return ret # If we're successful