blob: 9b3032554250c82f16349fb78527a2883120cc90 [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):
Steve McIntyre26de4d92015-09-23 00:04:55 +01009 loglevel = logging.CRITICAL
10 if level == "ERROR":
11 loglevel = logging.ERROR
12 elif level == "WARNING":
13 loglevel = logging.WARNING
14 elif level == "INFO":
15 loglevel = logging.INFO
16 elif level == "DEBUG":
17 loglevel = logging.DEBUG
18 return loglevel
19
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000020 def get_switch_driver(self, switch_name, config):
Steve McIntyre5fa22652015-04-01 18:01:45 +010021 logging.debug("Trying to find a driver for %s", switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000022 driver = config.switches[switch_name].driver
Steve McIntyre5fa22652015-04-01 18:01:45 +010023 logging.debug("Driver: %s", driver)
Steve McIntyref1c04f92014-12-16 18:23:15 +000024 module = __import__("drivers.%s" % driver, fromlist=[driver])
25 class_ = getattr(module, driver)
Steve McIntyre0f38f0e2015-07-14 15:26:05 +010026 return class_(switch_name, debug = config.switches[switch_name].debug)
Steve McIntyref1c04f92014-12-16 18:23:15 +000027
Steve McIntyre519158e2014-12-23 13:44:44 +000028 def probe_switches(self, state):
29 config = state.config
Steve McIntyree8d80582014-12-23 16:53:39 +000030 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000031 for switch_name in sorted(config.switches):
Steve McIntyre5fa22652015-04-01 18:01:45 +010032 logging.debug("Found switch %s:", switch_name)
Steve McIntyre7cf80982015-02-12 07:03:40 +000033 logging.debug(" Probing...")
Steve McIntyre519158e2014-12-23 13:44:44 +000034
Steve McIntyre4b4ab652014-12-22 17:19:09 +000035 s = self.get_switch_driver(switch_name, config)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +000036 s.switch_connect(config.switches[switch_name].username,
37 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +000038 config.switches[switch_name].enable_password)
Steve McIntyre27d4b582014-12-23 22:51:00 +000039 ret[switch_name] = 'Found %d ports: ' % len(s.switch_get_port_names())
40 for name in s.switch_get_port_names():
41 ret[switch_name] += '%s ' % name
Steve McIntyrea2a8f792014-12-17 17:34:32 +000042 s.switch_disconnect()
Steve McIntyre2ed90f52015-07-21 17:59:52 +010043 del s
Steve McIntyrea2020cb2014-12-23 16:56:40 +000044 return ret
Steve McIntyrec68a18e2014-12-17 16:29:28 +000045
Steve McIntyre091e2ac2014-12-16 19:20:07 +000046 # Simple helper wrapper for all the read-only database queries
Steve McIntyre2150bc22014-12-17 13:13:56 +000047 def perform_db_query(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +000048 logging.debug('perform_db_query')
49 logging.debug(command)
50 logging.debug(data)
Steve McIntyref1c04f92014-12-16 18:23:15 +000051 ret = {}
Steve McIntyre2150bc22014-12-17 13:13:56 +000052 db = state.db
Steve McIntyref1c04f92014-12-16 18:23:15 +000053 try:
Steve McIntyre091e2ac2014-12-16 19:20:07 +000054 if command == 'db.all_switches':
Steve McIntyref1c04f92014-12-16 18:23:15 +000055 ret = db.all_switches()
56 elif command == 'db.all_ports':
57 ret = db.all_ports()
58 elif command == 'db.all_vlans':
59 ret = db.all_vlans()
Steve McIntyrec4890132015-08-07 15:19:11 +010060 elif command == 'db.all_trunks':
61 ret = db.all_trunks()
Steve McIntyref1c04f92014-12-16 18:23:15 +000062 elif command == 'db.get_switch_by_id':
63 ret = db.get_switch_by_id(data['switch_id'])
64 elif command == 'db.get_switch_id_by_name':
65 ret = db.get_switch_id_by_name(data['name'])
66 elif command == 'db.get_switch_name_by_id':
67 ret = db.get_switch_name_by_id(data['switch_id'])
68 elif command == 'db.get_port_by_id':
69 ret = db.get_port_by_id(data['port_id'])
70 elif command == 'db.get_ports_by_switch':
71 ret = db.get_ports_by_switch(data['switch_id'])
72 elif command == 'db.get_port_by_switch_and_name':
73 ret = db.get_port_by_switch_and_name(data['switch_id'], data['name'])
Steve McIntyre45f55012015-08-05 13:55:15 +010074 elif command == 'db.get_port_by_switch_and_number':
75 ret = db.get_port_by_switch_and_number(data['switch_id'], int(data['number']))
Steve McIntyref1c04f92014-12-16 18:23:15 +000076 elif command == 'db.get_current_vlan_id_by_port':
77 ret = db.get_current_vlan_id_by_port(data['port_id'])
78 elif command == 'db.get_base_vlan_id_by_port':
79 ret = db.get_base_vlan_id_by_port(data['port_id'])
80 elif command == 'db.get_ports_by_current_vlan':
81 ret = db.get_ports_by_current_vlan(data['vlan_id'])
82 elif command == 'db.get_ports_by_base_vlan':
83 ret = db.get_ports_by_base_vlan(data['vlan_id'])
Steve McIntyrec4890132015-08-07 15:19:11 +010084 elif command == 'db.get_ports_by_trunk':
85 ret = db.get_ports_by_trunk(data['trunk_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000086 elif command == 'db.get_vlan_by_id':
87 ret = db.get_vlan_by_id(data['vlan_id'])
Steve McIntyre65533d72015-01-23 18:01:17 +000088 elif command == 'db.get_vlan_tag_by_id':
89 ret = db.get_vlan_tag_by_id(data['vlan_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000090 elif command == 'db.get_vlan_id_by_name':
91 ret = db.get_vlan_id_by_name(data['name'])
92 elif command == 'db.get_vlan_id_by_tag':
Steve McIntyre07946c22014-12-17 13:14:15 +000093 ret = db.get_vlan_id_by_tag(data['tag'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000094 elif command == 'db.get_vlan_name_by_id':
95 ret = db.get_vlan_name_by_id(data['vlan_id'])
Steve McIntyrec4890132015-08-07 15:19:11 +010096 elif command == 'db.get_trunk_by_id':
97 ret = db.get_trunk_by_id(data['trunk_id'])
Steve McIntyref1c04f92014-12-16 18:23:15 +000098 else:
Steve McIntyree749fef2014-12-17 16:35:45 +000099 raise InputError("Unknown db_query command \"%s\"" % command)
Steve McIntyref1c04f92014-12-16 18:23:15 +0000100
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100101 except InputError as e:
102 logging.error('perform_db_query(%s) got error %s', command, e)
Steve McIntyre5da37fa2014-12-17 13:14:44 +0000103 raise
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100104 except ValueError as e:
105 logging.error('perform_db_query(%s) got error %s', command, e)
106 raise InputError("Invalid value in API call argument: %s" % e)
Steve McIntyref1c04f92014-12-16 18:23:15 +0000107
108 return ret
109
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000110 # Simple helper wrapper for all the read-only daemon state queries
111 def perform_daemon_query(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000112 logging.debug('perform_daemon_query')
113 logging.debug(command)
114 logging.debug(data)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000115 ret = {}
116 try:
117 if command == 'daemon.status':
118 # data ignored
119 ret['running'] = 'ok'
120 elif command == 'daemon.version':
121 # data ignored
122 ret['version'] = state.version
123 elif command == 'daemon.statistics':
124 ret['uptime'] = time.time() - state.starttime
Steve McIntyre88b79df2014-12-23 13:45:08 +0000125 elif command == 'daemon.probe_switches':
126 ret = self.probe_switches(state)
Steve McIntyre06fe6422015-01-23 17:55:43 +0000127 elif command == 'daemon.shutdown':
128 # data ignored
129 ret['shutdown'] = 'Shutting down'
130 state.running = False
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000131 else:
Steve McIntyree749fef2014-12-17 16:35:45 +0000132 raise InputError("Unknown daemon_query command \"%s\"" % command)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000133
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100134 except InputError as e:
135 logging.error('perform_daemon_query(%s) got error %s', command, e)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000136 raise
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100137 except ValueError as e:
138 logging.error('perform_daemon_query(%s) got error %s', command, e)
139 raise InputError("Invalid value in API call: %s" % e)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000140
141 return ret
142
Steve McIntyree749fef2014-12-17 16:35:45 +0000143 # Helper wrapper for API functions modifying database state only
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000144 def perform_db_update(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000145 logging.debug('perform_db_update')
146 logging.debug(command)
147 logging.debug(data)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000148 ret = {}
149 db = state.db
150 try:
151 if command == 'db.create_switch':
152 ret = db.create_switch(data['name'])
153 elif command == 'db.create_port':
Steve McIntyreca6adfc2015-08-06 15:08:58 +0100154 try:
155 number = int(data['number'])
156 except ValueError:
157 raise InputError("Invalid value for port number (%s) - must be numeric only!" % data['number'])
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000158 ret = db.create_port(data['switch_id'], data['name'],
Steve McIntyreca6adfc2015-08-06 15:08:58 +0100159 number,
Steve McIntyrefefdbb42014-12-22 16:14:28 +0000160 state.default_vlan_id,
161 state.default_vlan_id)
Steve McIntyrec4890132015-08-07 15:19:11 +0100162 if command == 'db.create_trunk':
163 ret = db.create_trunk(data['port_id1'], data['port_id2'])
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000164 elif command == 'db.delete_switch':
165 ret = db.delete_switch(data['switch_id'])
166 elif command == 'db.delete_port':
167 ret = db.delete_port(data['port_id'])
168 elif command == 'db.set_port_is_locked':
169 ret = db.set_port_is_locked(data['port_id'], data['is_locked'])
170 elif command == 'db.set_base_vlan':
171 ret = db.set_base_vlan(data['port_id'], data['base_vlan_id'])
Steve McIntyrec4890132015-08-07 15:19:11 +0100172 elif command == 'db.delete_trunk':
173 ret = db.delete_trunk(data['trunk_id'])
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000174 else:
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000175 raise InputError("Unknown db_update command \"%s\"" % command)
176
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100177 except InputError as e:
178 logging.error('perform_db_update(%s) got error %s', command, e)
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000179 raise
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100180 except ValueError as e:
181 logging.error('perform_db_update(%s) got error %s', command, e)
182 raise InputError("Invalid value in API call: %s" % e)
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000183
184 return ret
185
186 # Helper wrapper for API functions that modify both database state
187 # and on-switch VLAN state
188 def perform_vlan_update(self, state, command, data):
Steve McIntyre7cf80982015-02-12 07:03:40 +0000189 logging.debug('perform_vlan_update')
190 logging.debug(command)
191 logging.debug(data)
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000192 ret = {}
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000193
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000194 try:
195 # All of these are complex commands, so call helpers
196 # rather than inline the code here
197 if command == 'api.create_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000198 ret = self.create_vlan(state, data['name'], int(data['tag']), data['is_base_vlan'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000199 elif command == 'api.delete_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000200 ret = self.delete_vlan(state, int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000201 elif command == 'api.set_port_mode':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000202 ret = self.set_port_mode(state, int(data['port_id']), data['mode'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000203 elif command == 'api.set_current_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000204 ret = self.set_current_vlan(state, int(data['port_id']), int(data['vlan_id']))
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000205 elif command == 'api.restore_base_vlan':
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000206 ret = self.restore_base_vlan(state, int(data['port_id']))
207 elif command == 'api.auto_import_switch':
208 ret = self.auto_import_switch(state, data['switch'])
Steve McIntyre0f9dea62014-12-17 16:37:01 +0000209 else:
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000210 raise InputError("Unknown query command \"%s\"" % command)
211
Steve McIntyre3256b182014-12-19 15:38:15 +0000212 except InputError as e:
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100213 logging.error('perform_vlan_update(%s) got error %s', command, e)
Steve McIntyrea590b5b2014-12-17 13:15:14 +0000214 raise
Steve McIntyre978cb9f2015-10-09 16:51:28 +0100215 except ValueError as e:
216 logging.error('perform_vlan_update(%s) got error %s', command, e)
217 raise InputError("Invalid value in API call: %s" % e)
Steve McIntyre53c7ad92014-12-16 19:21:13 +0000218
219 return ret
220
221
Steve McIntyre3256b182014-12-19 15:38:15 +0000222 # Complex call
223 # 1. create the VLAN in the DB
224 # 2. Iterate through all switches:
Steve McIntyre1ab8b872014-12-19 18:37:00 +0000225 # a. Create the VLAN
226 # b. Add the VLAN to all trunk ports (if needed)
227 # 3. If all went OK, save config on all the switches
Steve McIntyre3256b182014-12-19 15:38:15 +0000228 #
229 # The VLAN may already exist on some of the switches, that's
Steve McIntyre153157d2014-12-19 18:05:20 +0000230 # fine. If things fail, we attempt to roll back by rebooting
231 # switches then removing the VLAN in the DB.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000232 def create_vlan(self, state, name, tag, is_base_vlan):
Steve McIntyre3256b182014-12-19 15:38:15 +0000233
Steve McIntyre7cf80982015-02-12 07:03:40 +0000234 logging.debug('create_vlan')
Steve McIntyre3256b182014-12-19 15:38:15 +0000235 db = state.db
236 config = state.config
237
Steve McIntyre6b8d3862015-08-03 19:28:25 +0100238 # Check for tag == -1, i.e. use the next available tag
239 if tag == -1:
240 tag = db.find_lowest_unused_vlan_tag()
241 logging.debug('create_vlan called with a tag of -1, found first unused tag %d', tag)
242
Steve McIntyre3256b182014-12-19 15:38:15 +0000243 # 1. Database record first
244 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100245 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 +0000246 vlan_id = db.create_vlan(name, tag, is_base_vlan)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100247 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 +0000248 except InputError:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000249 logging.debug('DB creation failed')
Steve McIntyre3256b182014-12-19 15:38:15 +0000250 raise
251
Steve McIntyre153157d2014-12-19 18:05:20 +0000252 # Keep track of which switches we've configured, for later use
253 switches_done = []
254
Steve McIntyre3256b182014-12-19 15:38:15 +0000255 # 2. Now the switches
256 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000257 for switch in db.all_switches():
Steve McIntyre3256b182014-12-19 15:38:15 +0000258 trunk_ports = []
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000259 switch_name = switch.name
Steve McIntyre3256b182014-12-19 15:38:15 +0000260 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100261 logging.debug('Adding new VLAN to switch %s', switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000262 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000263 s = self.get_switch_driver(switch_name, config)
264 s.switch_connect(config.switches[switch_name].username,
265 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000266 config.switches[switch_name].enable_password)
Steve McIntyre3256b182014-12-19 15:38:15 +0000267
Steve McIntyre153157d2014-12-19 18:05:20 +0000268 # Mark this switch as one we've touched, for
269 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000270 switches_done.append(switch_name)
Steve McIntyre153157d2014-12-19 18:05:20 +0000271
Steve McIntyre3256b182014-12-19 15:38:15 +0000272 # 2a. Create the VLAN on the switch
273 s.vlan_create(tag)
274 s.vlan_set_name(tag, name)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100275 logging.debug('Added VLAN tag %d, name %s to switch %s', tag, name, switch_name)
Steve McIntyre3256b182014-12-19 15:38:15 +0000276
277 # 2b. Do we need to worry about trunk ports on this switch?
278 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000279 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre3256b182014-12-19 15:38:15 +0000280 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000281 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000282 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyre3256b182014-12-19 15:38:15 +0000283 if trunk_ports is None:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000284 logging.debug("But it has no trunk ports defined")
Steve McIntyre3256b182014-12-19 15:38:15 +0000285 trunk_ports = []
286 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100287 logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports))
Steve McIntyre3256b182014-12-19 15:38:15 +0000288
289 # Modify any trunk ports as needed
290 for port in trunk_ports:
Steve McIntyre1ce09f92015-09-04 14:35:13 +0100291 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 +0000292 s.port_add_trunk_to_vlan(port, tag)
293
Steve McIntyre3256b182014-12-19 15:38:15 +0000294 # And now we're done with this switch
295 s.switch_disconnect()
296 del s
297
Steve McIntyre8f27ae12015-09-04 14:35:38 +0100298 except IOError as e:
Steve McIntyre9f7bac02015-09-18 14:10:34 +0100299 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 +0000300 raise
Steve McIntyreb826fc72015-07-27 17:57:40 +0100301
Steve McIntyre3256b182014-12-19 15:38:15 +0000302 except IOError:
Steve McIntyre153157d2014-12-19 18:05:20 +0000303 # Bugger. Looks like one of the switch calls above
304 # failed. To undo the changes safely, we'll need to reset
305 # all the switches we managed to configure. This could
306 # take some time!
Steve McIntyre38a24e52015-07-21 17:54:58 +0100307 logging.error('create_vlan failed, resetting all switches to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000308 for switch_name in switches_done:
309 s = self.get_switch_driver(switch_name, config)
310 s.switch_connect(config.switches[switch_name].username,
311 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000312 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000313 s.switch_restart() # Will implicitly also close the connection
314 del s
315
Steve McIntyre3256b182014-12-19 15:38:15 +0000316 # Undo the database change
Steve McIntyre7cf80982015-02-12 07:03:40 +0000317 logging.debug('Switch access failed. Deleting the new VLAN entry in the database')
Steve McIntyre3256b182014-12-19 15:38:15 +0000318 db.delete_vlan(vlan_id)
319 raise
320
Steve McIntyre153157d2014-12-19 18:05:20 +0000321 # If we've got this far, things were successful. Save config
322 # on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000323 for switch_name in switches_done:
324 s = self.get_switch_driver(switch_name, config)
325 s.switch_connect(config.switches[switch_name].username,
326 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000327 config.switches[switch_name].enable_password)
Steve McIntyre153157d2014-12-19 18:05:20 +0000328 s.switch_save_running_config()
329 s.switch_disconnect()
330 del s
331
Steve McIntyre6b8d3862015-08-03 19:28:25 +0100332 return (vlan_id, tag) # If we're successful
Steve McIntyre3256b182014-12-19 15:38:15 +0000333
Steve McIntyrefeb64522014-12-19 18:53:02 +0000334 # Complex call
335 # 1. Check in the DB if there are any ports on the VLAN. Bail if so
336 # 2. Iterate through all switches:
337 # a. Remove the VLAN from all trunk ports (if needed)
338 # b. Remove the VLAN
339 # 3. If all went OK, save config on the switches
340 # 4. Remove the VLAN in the DB
341 #
342 # If things fail, we attempt to roll back by rebooting switches.
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000343 def delete_vlan(self, state, vlan_id):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000344
Steve McIntyre7cf80982015-02-12 07:03:40 +0000345 logging.debug('delete_vlan')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000346 db = state.db
347 config = state.config
348
Steve McIntyrefeb64522014-12-19 18:53:02 +0000349 # 1. Check for database records first
Steve McIntyre5fa22652015-04-01 18:01:45 +0100350 logging.debug('Checking for ports using VLAN ID %d', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000351 vlan = db.get_vlan_by_id(vlan_id)
352 if vlan is None:
353 raise InputError("VLAN ID %d does not exist" % vlan_id)
354 vlan_tag = vlan.tag
355 ports = db.get_ports_by_current_vlan(vlan_id)
356 if ports is not None:
357 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
358 (vlan_id, len(ports)))
359 ports = db.get_ports_by_base_vlan(vlan_id)
360 if ports is not None:
361 raise InputError("Cannot delete VLAN ID %d when it still has %d ports" %
362 (vlan_id, len(ports)))
363
364 # Keep track of which switches we've configured, for later use
365 switches_done = []
366
367 # 2. Now the switches
368 try:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000369 for switch in db.all_switches():
370 switch_name = switch.name
Steve McIntyrefeb64522014-12-19 18:53:02 +0000371 trunk_ports = []
372 try:
373 # Get the right driver
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000374 s = self.get_switch_driver(switch_name, config)
375 s.switch_connect(config.switches[switch_name].username,
376 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000377 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000378
379 # Mark this switch as one we've touched, for
380 # either config saving or rollback below
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000381 switches_done.append(switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000382
383 # 2a. Do we need to worry about trunk ports on this switch?
384 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000385 logging.debug('This switch does not need special trunk port handling')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000386 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000387 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000388 trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000389 if trunk_ports is None:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000390 logging.debug("But it has no trunk ports defined")
Steve McIntyrefeb64522014-12-19 18:53:02 +0000391 trunk_ports = []
392 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100393 logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports))
Steve McIntyrefeb64522014-12-19 18:53:02 +0000394
395 # Modify any trunk ports as needed
396 for port in trunk_ports:
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000397 s.port_remove_trunk_from_vlan(port, vlan_tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100398 logging.debug('Removed VLAN tag %d from switch %s port %s', vlan_tag, switch_name, port)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000399
400 # 2b. Remove the VLAN from the switch
Steve McIntyre5fa22652015-04-01 18:01:45 +0100401 logging.debug('Removing VLAN tag %d from switch %s', vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000402 s.vlan_destroy(vlan_tag)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100403 logging.debug('Removed VLAN tag %d from switch %s', vlan_tag, switch_name)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000404
405 # And now we're done with this switch
406 s.switch_disconnect()
407 del s
408
409 except IOError:
410 raise
411
412 except IOError:
413 # Bugger. Looks like one of the switch calls above
414 # failed. To undo the changes safely, we'll need to reset
415 # all the switches we managed to configure. This could
416 # take some time!
Steve McIntyre38a24e52015-07-21 17:54:58 +0100417 logging.error('delete_vlan failed, resetting all switches to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000418 for switch_name in switches_done:
419 s = self.get_switch_driver(switch_name, config)
420 s.switch_connect(config.switches[switch_name].username,
421 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000422 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000423 s.switch_restart() # Will implicitly also close the connection
424 del s
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000425 raise
Steve McIntyrefeb64522014-12-19 18:53:02 +0000426
427 # 3. If we've got this far, things were successful. Save
428 # config on all the switches so it will persist across reboots
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000429 for switch_name in switches_done:
430 s = self.get_switch_driver(switch_name, config)
431 s.switch_connect(config.switches[switch_name].username,
432 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000433 config.switches[switch_name].enable_password)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000434 s.switch_save_running_config()
435 s.switch_disconnect()
436 del s
437
438 # 4. Finally, remove the VLAN in the DB
439 try:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100440 logging.debug('Removing DB record: VLAN ID %d', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000441 vlan_id = db.delete_vlan(vlan_id)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100442 logging.debug('Removed VLAN ID %d from the database OK', vlan_id)
Steve McIntyrefeb64522014-12-19 18:53:02 +0000443 except InputError:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000444 logging.debug('DB deletion failed')
Steve McIntyrefeb64522014-12-19 18:53:02 +0000445 raise
446
447 return vlan_id # If we're successful
448
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000449 # Complex call, depends on existing state a lot
450 # 1. Check validity of inputs
451 # 2. Switch mode and other config on the port.
452 # a. If switching trunk->access, remove all trunk VLANs from it
453 # (if needed) and switch back to the base VLAN for the
454 # port. Next, switch to access mode.
455 # b. If switching access->trunk, switch back to the base VLAN
456 # for the port. Next, switch mode. Then add all trunk VLANs
457 # to it (if needed)
458 # 3. If all went OK, save config on the switch
459 # 4. Change details of the port in the DB
460 #
461 # If things fail, we attempt to roll back by rebooting the switch
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000462 def set_port_mode(self, state, port_id, mode):
Steve McIntyrefeb64522014-12-19 18:53:02 +0000463
Steve McIntyre7cf80982015-02-12 07:03:40 +0000464 logging.debug('set_port_mode')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000465 db = state.db
466 config = state.config
467
468 # 1. Sanity-check inputs
Steve McIntyre5b0de002015-01-23 18:05:13 +0000469 if mode != 'access' and mode != 'trunk':
470 raise InputError("Port mode '%s' is not a valid option: try 'access' or 'trunk'" % mode)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000471 port = db.get_port_by_id(port_id)
472 if port is None:
473 raise InputError("Port ID %d does not exist" % port_id)
Steve McIntyre28114092015-02-13 03:04:40 +0000474 if port.is_locked:
475 raise InputError("Port ID %d is locked" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000476 if mode == 'trunk' and port.is_trunk:
Steve McIntyre3aff7da2015-02-13 04:00:11 +0000477 raise InputError("Port ID %d is already in trunk mode" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000478 if mode == 'access' and not port.is_trunk:
Steve McIntyre3aff7da2015-02-13 04:00:11 +0000479 raise InputError("Port ID %d is already in access mode" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000480 base_vlan_tag = db.get_vlan_tag_by_id(port.base_vlan_id)
481
482 # Get the right driver
483 switch_name = db.get_switch_name_by_id(port.switch_id)
484 s = self.get_switch_driver(switch_name, config)
485
486 # 2. Now start configuring the switch
487 try:
488 s.switch_connect(config.switches[switch_name].username,
489 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000490 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000491 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100492 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000493 raise
494
495 try:
496 if port.is_trunk:
497 # 2a. We're going from a trunk port to an access port
498 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000499 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000500 else:
Steve McIntyre7cf80982015-02-12 07:03:40 +0000501 logging.debug('This switch needs special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000502 vlans = s.port_get_trunk_vlan_list(port.name)
503 if vlans is None:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100504 logging.debug("But it has no VLANs defined on port %s", port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000505 vlans = []
506 else:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100507 logging.debug('Found %d vlans that may need dropping on port %s', len(vlans), port.name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000508
509 for vlan in vlans:
Steve McIntyre97f5e872015-01-23 18:07:05 +0000510 if vlan != state.config.vland.default_vlan_tag:
511 s.port_remove_trunk_from_vlan(port.name, vlan)
Steve McIntyref903b692015-07-09 18:29:05 +0100512
513 s.port_set_mode(port.name, "access")
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000514
515 else:
516 # 2b. We're going from an access port to a trunk port
517 s.port_set_access_vlan(port.name, base_vlan_tag)
518 s.port_set_mode(port.name, "trunk")
519 if 'TrunkWildCardVlans' in s.switch_get_capabilities():
Steve McIntyre7cf80982015-02-12 07:03:40 +0000520 logging.debug('This switch does not need special trunk port handling')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000521 else:
522 vlans = db.all_vlans()
523 for vlan in vlans:
Steve McIntyreee742d92015-09-03 18:16:09 +0100524 if vlan.tag != state.config.vland.default_vlan_tag:
Steve McIntyre36ae6ac2015-09-03 18:10:33 +0100525 s.port_add_trunk_to_vlan(port.name, vlan.tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000526
527 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100528 logging.error('set_port_mode failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000529 # Bugger. Looks like one of the switch calls above
530 # failed. To undo the changes safely, we'll need to reset
531 # all the config on this switch
532 s.switch_restart() # Will implicitly also close the connection
533 del s
534 raise
535
536 # 3. All seems to have worked so far!
537 s.switch_save_running_config()
538 s.switch_disconnect()
539 del s
540
541 # 4. And update the DB
542 db.set_port_mode(port_id, mode)
543
544 return port_id # If we're successful
545
546 # Complex call, updating both DB and switch state
547 # 1. Check validity of inputs
548 # 2. Update the port config on the switch
549 # 3. If all went OK, save config on the switch
550 # 4. Change details of the port in the DB
551 #
552 # If things fail, we attempt to roll back by rebooting the switch
553 def set_current_vlan(self, state, port_id, vlan_id):
554
Steve McIntyre7cf80982015-02-12 07:03:40 +0000555 logging.debug('set_current_vlan')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000556 db = state.db
557 config = state.config
558
559 # 1. Sanity checks!
560 port = db.get_port_by_id(port_id)
561 if port is None:
562 raise InputError("Port ID %d does not exist" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000563 if port.is_locked:
564 raise InputError("Port ID %d is locked" % port_id)
Steve McIntyre3bb7bbd2015-02-13 03:06:01 +0000565 if port.is_trunk:
566 raise InputError("Port ID %d is not an access port" % port_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000567
568 vlan = db.get_vlan_by_id(vlan_id)
569 if vlan is None:
570 raise InputError("VLAN ID %d does not exist" % vlan_id)
571
572 # Get the right driver
573 switch_name = db.get_switch_name_by_id(port.switch_id)
574 s = self.get_switch_driver(switch_name, config)
575
576 # 2. Now start configuring the switch
577 try:
578 s.switch_connect(config.switches[switch_name].username,
579 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000580 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000581 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100582 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000583 raise
584
585 try:
586 s.port_set_access_vlan(port.name, vlan.tag)
587 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100588 logging.error('set_current_vlan failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000589 # Bugger. Looks like one of the switch calls above
590 # failed. To undo the changes safely, we'll need to reset
591 # all the config on this switch
592 s.switch_restart() # Will implicitly also close the connection
593 del s
594 raise
595
596 # 3. All seems to have worked so far!
597 s.switch_save_running_config()
598 s.switch_disconnect()
599 del s
600
601 # 4. And update the DB
602 db.set_current_vlan(port_id, vlan_id)
603
604 return port_id # If we're successful
605
606 # Complex call, updating both DB and switch state
607 # 1. Check validity of input
608 # 2. Update the port config on the switch
609 # 3. If all went OK, save config on the switch
610 # 4. Change details of the port in the DB
611 #
612 # If things fail, we attempt to roll back by rebooting the switch
613 def restore_base_vlan(self, state, port_id):
614
Steve McIntyre7cf80982015-02-12 07:03:40 +0000615 logging.debug('restore_base_vlan')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000616 db = state.db
617 config = state.config
618
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000619 # 1. Sanity checks!
620 port = db.get_port_by_id(port_id)
621 if port is None:
622 raise InputError("Port ID %d does not exist" % port_id)
623 if port.is_trunk:
624 raise InputError("Port ID %d is not an access port" % port_id)
625 if port.is_locked:
626 raise InputError("Port ID %d is locked" % port_id)
627
628 # Bail out early if we're *already* on the base VLAN. This is
629 # not an error
630 if port.current_vlan_id == port.base_vlan_id:
631 return port_id
632
633 vlan = db.get_vlan_by_id(port.base_vlan_id)
634
635 # Get the right driver
636 switch_name = db.get_switch_name_by_id(port.switch_id)
637 s = self.get_switch_driver(switch_name, config)
638
639 # 2. Now start configuring the switch
640 try:
641 s.switch_connect(config.switches[switch_name].username,
642 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000643 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000644 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100645 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000646 raise
647
648 try:
649 s.port_set_access_vlan(port.name, vlan.tag)
650 except IOError:
Steve McIntyre38a24e52015-07-21 17:54:58 +0100651 logging.error('restore_base_vlan failed, resetting switch to recover')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000652 # Bugger. Looks like one of the switch calls above
653 # failed. To undo the changes safely, we'll need to reset
654 # all the config on this switch
655 s.switch_restart() # Will implicitly also close the connection
656 del s
657 raise
658
659 # 3. All seems to have worked so far!
660 s.switch_save_running_config()
661 s.switch_disconnect()
662 del s
663
664 # 4. And update the DB
665 db.set_current_vlan(port_id, port.base_vlan_id)
666
667 return port_id # If we're successful
668
669 # Complex call, updating both DB and switch state
670 # * Check validity of input
671 # * Read all the config from the switch (switch, ports, VLANs)
672 # * Create initial DB entries to match each of those
673 # * Merge VLANs across all switches
674 # * Set up ports appropriately
675 #
676 def auto_import_switch(self, state, switch_name):
677
Steve McIntyre7cf80982015-02-12 07:03:40 +0000678 logging.debug('auto_import_switch')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000679 db = state.db
680 config = state.config
681
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000682 port_vlans = {}
683
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000684 # 1. Sanity checks!
685 switch_id = db.get_switch_id_by_name(switch_name)
686 if switch_id is not None:
687 raise InputError("Switch name %s already exists in the DB (ID %d)" % (switch_name, switch_id))
688
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000689 if not switch_name in config.switches:
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000690 raise InputError("Switch name %s not defined in config" % switch_name)
691
692 # 2. Now start reading config from the switch
693 try:
694 s = self.get_switch_driver(switch_name, config)
695 s.switch_connect(config.switches[switch_name].username,
696 config.switches[switch_name].password,
Steve McIntyre3b655af2014-12-23 13:43:19 +0000697 config.switches[switch_name].enable_password)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000698 except:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100699 logging.debug('Failed to talk to switch %s!', switch_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000700 raise
701
702 # DON'T create the switch record in the DB first - we'll want
Steve McIntyrefc511242014-12-23 22:28:30 +0000703 # to create VLANs on *other* switches, and it's easier to do
704 # that before we've added our new switch
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000705
706 new_vlan_tags = []
707
708 # Grab the VLANs defined on this switch
709 vlan_tags = s.vlan_get_list()
Steve McIntyrefc511242014-12-23 22:28:30 +0000710
Steve McIntyre5fa22652015-04-01 18:01:45 +0100711 logging.debug(' found %d vlans on the switch', len(vlan_tags))
Steve McIntyrefc511242014-12-23 22:28:30 +0000712
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000713 for vlan_tag in vlan_tags:
714 vlan_name = s.vlan_get_name(vlan_tag)
715
716 # If a VLAN is already in the database, then that's easy -
717 # we can just ignore it. However, we have to check that
718 # there is not a different name for the existing VLAN tag
Steve McIntyreb1529072014-12-23 17:17:22 +0000719 # - bail out if so... UNLESS we're looking at the default
720 # VLAN
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000721 #
722 # If this VLAN tag is not already in the DB, we'll need to
723 # add it there and to all the other switches (and their
724 # trunk ports!) too.
Steve McIntyrefc511242014-12-23 22:28:30 +0000725 vlan_id = db.get_vlan_id_by_tag(vlan_tag)
Steve McIntyre6c09c0c2015-07-17 17:20:01 +0100726 if vlan_id != state.default_vlan_id:
Steve McIntyreb1529072014-12-23 17:17:22 +0000727 if vlan_id is not None:
728 vlan_db_name = db.get_vlan_name_by_id(vlan_id)
729 if vlan_name != vlan_db_name:
730 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 +0000731
Steve McIntyreb1529072014-12-23 17:17:22 +0000732 else:
733 # OK, we'll need to set up the new VLAN now. It can't
734 # be a base VLAN - switches don't have such a concept!
735 # Rather than create individually here, add to a
736 # list. *Only* once we've worked through all the
737 # switch's VLANs successfully (checking for existing
738 # records and possible clashes!) should we start
739 # committing changes
740 new_vlan_tags.append(vlan_tag)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000741
742 # Now create the VLAN DB entries
743 for vlan_tag in new_vlan_tags:
744 vlan_name = s.vlan_get_name(vlan_tag)
745 vlan_id = self.create_vlan(state, vlan_name, vlan_tag, False)
746
747 # *Now* add this switch itself to the database, after we've
748 # worked on all the other switches
749 switch_id = db.create_switch(switch_name)
750
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000751 # And now the ports
752 trunk_ports = []
753 ports = s.switch_get_port_names()
Steve McIntyre5fa22652015-04-01 18:01:45 +0100754 logging.debug(' found %d ports on the switch', len(ports))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000755 for port_name in ports:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100756 logging.debug(' trying to import port %s', port_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000757 port_id = None
758 port_mode = s.port_get_mode(port_name)
Steve McIntyreea753972015-08-05 13:52:48 +0100759 port_number = s.port_map_name_to_number(port_name)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000760 if port_mode == 'access':
761 # Access ports are easy - just create the port, and
762 # set both the current and base VLANs to the current
763 # VLAN on the switch. We'll end up changing this after
764 # import if needed.
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000765 port_vlans[port_name] = (s.port_get_access_vlan(port_name),)
766 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyreea753972015-08-05 13:52:48 +0100767 port_id = db.create_port(switch_id, port_name, port_number,
Steve McIntyre6f17b102014-12-24 02:18:08 +0000768 port_vlan_id, port_vlan_id)
Steve McIntyre5fa22652015-04-01 18:01:45 +0100769 logging.debug(' access port, VLAN %d', int(port_vlans[port_name][0]))
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000770 # Nothing further needed
771 elif port_mode == 'trunk':
Steve McIntyre7cf80982015-02-12 07:03:40 +0000772 logging.debug(' trunk port, VLANs:')
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000773 # Trunk ports are a little more involved. First,
774 # create the port in the DB, setting the VLANs to the
775 # first VLAN found on the trunk port. This will *also*
776 # be in access mode by default, and unlocked.
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000777 port_vlans[port_name] = s.port_get_trunk_vlan_list(port_name)
Steve McIntyre7cf80982015-02-12 07:03:40 +0000778 logging.debug(port_vlans[port_name])
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000779 if port_vlans[port_name] == [] or port_vlans[port_name] is None or 'ALL' in port_vlans[port_name]:
780 port_vlans[port_name] = (state.config.vland.default_vlan_tag,)
781 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyreea753972015-08-05 13:52:48 +0100782 port_id = db.create_port(switch_id, port_name, port_number,
Steve McIntyre6f17b102014-12-24 02:18:08 +0000783 port_vlan_id, port_vlan_id)
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000784 # Append to a list of trunk ports that we will need to
785 # modify once we're done
786 trunk_ports.append(port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000787 else:
Steve McIntyre857d89f2015-07-24 16:22:37 +0100788 # We've found a port mode we don't want, e.g. the
789 # "dynamic auto" on a Cisco Catalyst. Handle that here
790 # - tell the switch to set that port to access and
791 # handle accordingly.
792 s.port_set_mode(port_name, 'access')
793 port_vlans[port_name] = (s.port_get_access_vlan(port_name),)
794 port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0])
Steve McIntyreea753972015-08-05 13:52:48 +0100795 port_id = db.create_port(switch_id, port_name, port_number,
Steve McIntyre857d89f2015-07-24 16:22:37 +0100796 port_vlan_id, port_vlan_id)
797 logging.debug(' Found port in %s mode', port_mode)
798 logging.debug(' Forcing to access mode, VLAN %d', int(port_vlans[port_name][0]))
799 port_mode = "access"
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000800
Steve McIntyre5fa22652015-04-01 18:01:45 +0100801 logging.debug(" Added port %s, got port ID %d", port_name, port_id)
Steve McIntyrefc511242014-12-23 22:28:30 +0000802
Steve McIntyre574e3342015-01-23 18:08:33 +0000803 db.set_port_mode(port_id, port_mode)
804
Steve McIntyre6feea572015-02-09 15:49:20 +0000805 # Make sure this switch has all the VLANs we need
806 for vlan in db.all_vlans():
Steve McIntyre49d3c082015-07-24 16:17:23 +0100807 if vlan.tag != state.config.vland.default_vlan_tag:
Steve McIntyred7fb4072015-02-11 17:13:50 +0000808 if not vlan.tag in vlan_tags:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100809 logging.debug("Adding VLAN tag %d to this switch", vlan.tag)
Steve McIntyre6feea572015-02-09 15:49:20 +0000810 s.vlan_create(vlan.tag)
811 s.vlan_set_name(vlan.tag, vlan.name)
812
Steve McIntyrefc511242014-12-23 22:28:30 +0000813 # Now, on each trunk port on the switch, we need to add all
814 # the VLANs already configured across our system
815 if not 'TrunkWildCardVlans' in s.switch_get_capabilities():
816 for port_id in trunk_ports:
817 port = db.get_port_by_id(port_id)
Steve McIntyreb826fc72015-07-27 17:57:40 +0100818
Steve McIntyrefc511242014-12-23 22:28:30 +0000819 for vlan in db.all_vlans():
Steve McIntyre49d3c082015-07-24 16:17:23 +0100820 if vlan.vlan_id != state.default_vlan_id:
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000821 if not vlan.tag in port_vlans[port.name]:
Steve McIntyre5fa22652015-04-01 18:01:45 +0100822 logging.debug("Adding allowed VLAN tag %d to trunk port %s", vlan.tag, port.name)
Steve McIntyrea8fe1de2015-02-11 17:13:12 +0000823 s.port_add_trunk_to_vlan(port.name, vlan.tag)
Steve McIntyrefc511242014-12-23 22:28:30 +0000824
Steve McIntyrefc511242014-12-23 22:28:30 +0000825 # Done with this switch \o/
826 s.switch_save_running_config()
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000827 s.switch_disconnect()
828 del s
829
Steve McIntyre4b4ab652014-12-22 17:19:09 +0000830 ret = {}
Steve McIntyre5f6f85e2014-12-22 16:42:28 +0000831 ret['switch_id'] = switch_id
832 ret['num_ports_added'] = len(ports)
833 ret['num_vlans_added'] = len(new_vlan_tags)
834 return ret # If we're successful