Steve McIntyre | d2313b2 | 2016-03-12 11:50:10 +0000 | [diff] [blame] | 1 | # Copyright 2014-2016 Linaro Limited |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or modify |
| 4 | # it under the terms of the GNU General Public License as published by |
| 5 | # the Free Software Foundation; either version 2 of the License, or |
| 6 | # (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | # GNU General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public License |
| 14 | # along with this program; if not, write to the Free Software |
| 15 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 16 | # MA 02110-1301, USA. |
| 17 | # |
| 18 | # Utility routines, including handling of API functions |
| 19 | # |
| 20 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 21 | import logging |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 22 | import time |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 23 | from errors import CriticalError, InputError, ConfigError, SocketError |
| 24 | |
| 25 | class VlanUtil: |
| 26 | """VLANd utility functions""" |
| 27 | |
Steve McIntyre | 26de4d9 | 2015-09-23 00:04:55 +0100 | [diff] [blame] | 28 | def set_logging_level(self, level): |
Steve McIntyre | 26de4d9 | 2015-09-23 00:04:55 +0100 | [diff] [blame] | 29 | loglevel = logging.CRITICAL |
| 30 | if level == "ERROR": |
| 31 | loglevel = logging.ERROR |
| 32 | elif level == "WARNING": |
| 33 | loglevel = logging.WARNING |
| 34 | elif level == "INFO": |
| 35 | loglevel = logging.INFO |
| 36 | elif level == "DEBUG": |
| 37 | loglevel = logging.DEBUG |
| 38 | return loglevel |
| 39 | |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 40 | def get_switch_driver(self, switch_name, config): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 41 | logging.debug("Trying to find a driver for %s", switch_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 42 | driver = config.switches[switch_name].driver |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 43 | logging.debug("Driver: %s", driver) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 44 | module = __import__("drivers.%s" % driver, fromlist=[driver]) |
| 45 | class_ = getattr(module, driver) |
Steve McIntyre | 0f38f0e | 2015-07-14 15:26:05 +0100 | [diff] [blame] | 46 | return class_(switch_name, debug = config.switches[switch_name].debug) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 47 | |
Steve McIntyre | 519158e | 2014-12-23 13:44:44 +0000 | [diff] [blame] | 48 | def probe_switches(self, state): |
| 49 | config = state.config |
Steve McIntyre | e8d8058 | 2014-12-23 16:53:39 +0000 | [diff] [blame] | 50 | ret = {} |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 51 | for switch_name in sorted(config.switches): |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 52 | logging.debug("Found switch %s:", switch_name) |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 53 | logging.debug(" Probing...") |
Steve McIntyre | 519158e | 2014-12-23 13:44:44 +0000 | [diff] [blame] | 54 | |
Steve McIntyre | 4b4ab65 | 2014-12-22 17:19:09 +0000 | [diff] [blame] | 55 | s = self.get_switch_driver(switch_name, config) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 56 | s.switch_connect(config.switches[switch_name].username, |
| 57 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 58 | config.switches[switch_name].enable_password) |
Steve McIntyre | 27d4b58 | 2014-12-23 22:51:00 +0000 | [diff] [blame] | 59 | ret[switch_name] = 'Found %d ports: ' % len(s.switch_get_port_names()) |
| 60 | for name in s.switch_get_port_names(): |
| 61 | ret[switch_name] += '%s ' % name |
Steve McIntyre | a2a8f79 | 2014-12-17 17:34:32 +0000 | [diff] [blame] | 62 | s.switch_disconnect() |
Steve McIntyre | 2ed90f5 | 2015-07-21 17:59:52 +0100 | [diff] [blame] | 63 | del s |
Steve McIntyre | a2020cb | 2014-12-23 16:56:40 +0000 | [diff] [blame] | 64 | return ret |
Steve McIntyre | c68a18e | 2014-12-17 16:29:28 +0000 | [diff] [blame] | 65 | |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 66 | # Simple helper wrapper for all the read-only database queries |
Steve McIntyre | 2150bc2 | 2014-12-17 13:13:56 +0000 | [diff] [blame] | 67 | def perform_db_query(self, state, command, data): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 68 | logging.debug('perform_db_query') |
| 69 | logging.debug(command) |
| 70 | logging.debug(data) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 71 | ret = {} |
Steve McIntyre | 2150bc2 | 2014-12-17 13:13:56 +0000 | [diff] [blame] | 72 | db = state.db |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 73 | try: |
Steve McIntyre | 091e2ac | 2014-12-16 19:20:07 +0000 | [diff] [blame] | 74 | if command == 'db.all_switches': |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 75 | ret = db.all_switches() |
| 76 | elif command == 'db.all_ports': |
| 77 | ret = db.all_ports() |
| 78 | elif command == 'db.all_vlans': |
| 79 | ret = db.all_vlans() |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 80 | elif command == 'db.all_trunks': |
| 81 | ret = db.all_trunks() |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 82 | elif command == 'db.get_switch_by_id': |
| 83 | ret = db.get_switch_by_id(data['switch_id']) |
| 84 | elif command == 'db.get_switch_id_by_name': |
| 85 | ret = db.get_switch_id_by_name(data['name']) |
| 86 | elif command == 'db.get_switch_name_by_id': |
| 87 | ret = db.get_switch_name_by_id(data['switch_id']) |
| 88 | elif command == 'db.get_port_by_id': |
| 89 | ret = db.get_port_by_id(data['port_id']) |
| 90 | elif command == 'db.get_ports_by_switch': |
| 91 | ret = db.get_ports_by_switch(data['switch_id']) |
| 92 | elif command == 'db.get_port_by_switch_and_name': |
| 93 | ret = db.get_port_by_switch_and_name(data['switch_id'], data['name']) |
Steve McIntyre | 45f5501 | 2015-08-05 13:55:15 +0100 | [diff] [blame] | 94 | elif command == 'db.get_port_by_switch_and_number': |
| 95 | ret = db.get_port_by_switch_and_number(data['switch_id'], int(data['number'])) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 96 | elif command == 'db.get_current_vlan_id_by_port': |
| 97 | ret = db.get_current_vlan_id_by_port(data['port_id']) |
| 98 | elif command == 'db.get_base_vlan_id_by_port': |
| 99 | ret = db.get_base_vlan_id_by_port(data['port_id']) |
| 100 | elif command == 'db.get_ports_by_current_vlan': |
| 101 | ret = db.get_ports_by_current_vlan(data['vlan_id']) |
| 102 | elif command == 'db.get_ports_by_base_vlan': |
| 103 | ret = db.get_ports_by_base_vlan(data['vlan_id']) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 104 | elif command == 'db.get_ports_by_trunk': |
| 105 | ret = db.get_ports_by_trunk(data['trunk_id']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 106 | elif command == 'db.get_vlan_by_id': |
| 107 | ret = db.get_vlan_by_id(data['vlan_id']) |
Steve McIntyre | 65533d7 | 2015-01-23 18:01:17 +0000 | [diff] [blame] | 108 | elif command == 'db.get_vlan_tag_by_id': |
| 109 | ret = db.get_vlan_tag_by_id(data['vlan_id']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 110 | elif command == 'db.get_vlan_id_by_name': |
| 111 | ret = db.get_vlan_id_by_name(data['name']) |
| 112 | elif command == 'db.get_vlan_id_by_tag': |
Steve McIntyre | 07946c2 | 2014-12-17 13:14:15 +0000 | [diff] [blame] | 113 | ret = db.get_vlan_id_by_tag(data['tag']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 114 | elif command == 'db.get_vlan_name_by_id': |
| 115 | ret = db.get_vlan_name_by_id(data['vlan_id']) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 116 | elif command == 'db.get_trunk_by_id': |
| 117 | ret = db.get_trunk_by_id(data['trunk_id']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 118 | else: |
Steve McIntyre | e749fef | 2014-12-17 16:35:45 +0000 | [diff] [blame] | 119 | raise InputError("Unknown db_query command \"%s\"" % command) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 120 | |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 121 | except InputError as e: |
| 122 | logging.error('perform_db_query(%s) got error %s', command, e) |
Steve McIntyre | 5da37fa | 2014-12-17 13:14:44 +0000 | [diff] [blame] | 123 | raise |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 124 | except ValueError as e: |
| 125 | logging.error('perform_db_query(%s) got error %s', command, e) |
| 126 | raise InputError("Invalid value in API call argument: %s" % e) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 127 | |
| 128 | return ret |
| 129 | |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 130 | # Simple helper wrapper for all the read-only daemon state queries |
| 131 | def perform_daemon_query(self, state, command, data): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 132 | logging.debug('perform_daemon_query') |
| 133 | logging.debug(command) |
| 134 | logging.debug(data) |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 135 | ret = {} |
| 136 | try: |
| 137 | if command == 'daemon.status': |
| 138 | # data ignored |
| 139 | ret['running'] = 'ok' |
Steve McIntyre | ea343aa | 2015-10-23 17:46:17 +0100 | [diff] [blame] | 140 | ret['last_modified'] = state.db.get_last_modified_time() |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 141 | elif command == 'daemon.version': |
| 142 | # data ignored |
| 143 | ret['version'] = state.version |
| 144 | elif command == 'daemon.statistics': |
| 145 | ret['uptime'] = time.time() - state.starttime |
Steve McIntyre | 88b79df | 2014-12-23 13:45:08 +0000 | [diff] [blame] | 146 | elif command == 'daemon.probe_switches': |
| 147 | ret = self.probe_switches(state) |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 148 | elif command == 'daemon.shutdown': |
| 149 | # data ignored |
| 150 | ret['shutdown'] = 'Shutting down' |
| 151 | state.running = False |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 152 | else: |
Steve McIntyre | e749fef | 2014-12-17 16:35:45 +0000 | [diff] [blame] | 153 | raise InputError("Unknown daemon_query command \"%s\"" % command) |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 154 | |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 155 | except InputError as e: |
| 156 | logging.error('perform_daemon_query(%s) got error %s', command, e) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 157 | raise |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 158 | except ValueError as e: |
| 159 | logging.error('perform_daemon_query(%s) got error %s', command, e) |
| 160 | raise InputError("Invalid value in API call: %s" % e) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 161 | |
| 162 | return ret |
| 163 | |
Steve McIntyre | e749fef | 2014-12-17 16:35:45 +0000 | [diff] [blame] | 164 | # Helper wrapper for API functions modifying database state only |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 165 | def perform_db_update(self, state, command, data): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 166 | logging.debug('perform_db_update') |
| 167 | logging.debug(command) |
| 168 | logging.debug(data) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 169 | ret = {} |
| 170 | db = state.db |
| 171 | try: |
| 172 | if command == 'db.create_switch': |
| 173 | ret = db.create_switch(data['name']) |
| 174 | elif command == 'db.create_port': |
Steve McIntyre | ca6adfc | 2015-08-06 15:08:58 +0100 | [diff] [blame] | 175 | try: |
| 176 | number = int(data['number']) |
| 177 | except ValueError: |
| 178 | raise InputError("Invalid value for port number (%s) - must be numeric only!" % data['number']) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 179 | ret = db.create_port(data['switch_id'], data['name'], |
Steve McIntyre | ca6adfc | 2015-08-06 15:08:58 +0100 | [diff] [blame] | 180 | number, |
Steve McIntyre | fefdbb4 | 2014-12-22 16:14:28 +0000 | [diff] [blame] | 181 | state.default_vlan_id, |
| 182 | state.default_vlan_id) |
Steve McIntyre | 0850e9b | 2016-03-12 11:51:47 +0000 | [diff] [blame^] | 183 | elif command == 'db.create_trunk': |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 184 | ret = db.create_trunk(data['port_id1'], data['port_id2']) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 185 | elif command == 'db.delete_switch': |
| 186 | ret = db.delete_switch(data['switch_id']) |
| 187 | elif command == 'db.delete_port': |
| 188 | ret = db.delete_port(data['port_id']) |
| 189 | elif command == 'db.set_port_is_locked': |
| 190 | ret = db.set_port_is_locked(data['port_id'], data['is_locked']) |
| 191 | elif command == 'db.set_base_vlan': |
| 192 | ret = db.set_base_vlan(data['port_id'], data['base_vlan_id']) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 193 | elif command == 'db.delete_trunk': |
| 194 | ret = db.delete_trunk(data['trunk_id']) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 195 | else: |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 196 | raise InputError("Unknown db_update command \"%s\"" % command) |
| 197 | |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 198 | except InputError as e: |
| 199 | logging.error('perform_db_update(%s) got error %s', command, e) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 200 | raise |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 201 | except ValueError as e: |
| 202 | logging.error('perform_db_update(%s) got error %s', command, e) |
| 203 | raise InputError("Invalid value in API call: %s" % e) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 204 | |
| 205 | return ret |
| 206 | |
| 207 | # Helper wrapper for API functions that modify both database state |
| 208 | # and on-switch VLAN state |
| 209 | def perform_vlan_update(self, state, command, data): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 210 | logging.debug('perform_vlan_update') |
| 211 | logging.debug(command) |
| 212 | logging.debug(data) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 213 | ret = {} |
Steve McIntyre | 4b4ab65 | 2014-12-22 17:19:09 +0000 | [diff] [blame] | 214 | |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 215 | try: |
| 216 | # All of these are complex commands, so call helpers |
| 217 | # rather than inline the code here |
| 218 | if command == 'api.create_vlan': |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 219 | ret = self.create_vlan(state, data['name'], int(data['tag']), data['is_base_vlan']) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 220 | elif command == 'api.delete_vlan': |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 221 | ret = self.delete_vlan(state, int(data['vlan_id'])) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 222 | elif command == 'api.set_port_mode': |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 223 | ret = self.set_port_mode(state, int(data['port_id']), data['mode']) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 224 | elif command == 'api.set_current_vlan': |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 225 | ret = self.set_current_vlan(state, int(data['port_id']), int(data['vlan_id'])) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 226 | elif command == 'api.restore_base_vlan': |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 227 | ret = self.restore_base_vlan(state, int(data['port_id'])) |
| 228 | elif command == 'api.auto_import_switch': |
| 229 | ret = self.auto_import_switch(state, data['switch']) |
Steve McIntyre | 0f9dea6 | 2014-12-17 16:37:01 +0000 | [diff] [blame] | 230 | else: |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 231 | raise InputError("Unknown query command \"%s\"" % command) |
| 232 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 233 | except InputError as e: |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 234 | logging.error('perform_vlan_update(%s) got error %s', command, e) |
Steve McIntyre | a590b5b | 2014-12-17 13:15:14 +0000 | [diff] [blame] | 235 | raise |
Steve McIntyre | 978cb9f | 2015-10-09 16:51:28 +0100 | [diff] [blame] | 236 | except ValueError as e: |
| 237 | logging.error('perform_vlan_update(%s) got error %s', command, e) |
| 238 | raise InputError("Invalid value in API call: %s" % e) |
Steve McIntyre | 53c7ad9 | 2014-12-16 19:21:13 +0000 | [diff] [blame] | 239 | |
| 240 | return ret |
| 241 | |
| 242 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 243 | # Complex call |
| 244 | # 1. create the VLAN in the DB |
| 245 | # 2. Iterate through all switches: |
Steve McIntyre | 1ab8b87 | 2014-12-19 18:37:00 +0000 | [diff] [blame] | 246 | # a. Create the VLAN |
| 247 | # b. Add the VLAN to all trunk ports (if needed) |
| 248 | # 3. If all went OK, save config on all the switches |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 249 | # |
| 250 | # The VLAN may already exist on some of the switches, that's |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 251 | # fine. If things fail, we attempt to roll back by rebooting |
| 252 | # switches then removing the VLAN in the DB. |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 253 | def create_vlan(self, state, name, tag, is_base_vlan): |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 254 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 255 | logging.debug('create_vlan') |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 256 | db = state.db |
| 257 | config = state.config |
| 258 | |
Steve McIntyre | 6b8d386 | 2015-08-03 19:28:25 +0100 | [diff] [blame] | 259 | # Check for tag == -1, i.e. use the next available tag |
| 260 | if tag == -1: |
| 261 | tag = db.find_lowest_unused_vlan_tag() |
| 262 | logging.debug('create_vlan called with a tag of -1, found first unused tag %d', tag) |
| 263 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 264 | # 1. Database record first |
| 265 | try: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 266 | logging.debug('Adding DB record first: name %s, tag %d, is_base_vlan %d', name, tag, is_base_vlan) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 267 | vlan_id = db.create_vlan(name, tag, is_base_vlan) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 268 | logging.debug('Added VLAN tag %d, name %s to the database, created VLAN ID %d', tag, name, vlan_id) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 269 | except InputError: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 270 | logging.debug('DB creation failed') |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 271 | raise |
| 272 | |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 273 | # Keep track of which switches we've configured, for later use |
| 274 | switches_done = [] |
| 275 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 276 | # 2. Now the switches |
| 277 | try: |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 278 | for switch in db.all_switches(): |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 279 | trunk_ports = [] |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 280 | switch_name = switch.name |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 281 | try: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 282 | logging.debug('Adding new VLAN to switch %s', switch_name) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 283 | # Get the right driver |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 284 | s = self.get_switch_driver(switch_name, config) |
| 285 | s.switch_connect(config.switches[switch_name].username, |
| 286 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 287 | config.switches[switch_name].enable_password) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 288 | |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 289 | # Mark this switch as one we've touched, for |
| 290 | # either config saving or rollback below |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 291 | switches_done.append(switch_name) |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 292 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 293 | # 2a. Create the VLAN on the switch |
| 294 | s.vlan_create(tag) |
| 295 | s.vlan_set_name(tag, name) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 296 | logging.debug('Added VLAN tag %d, name %s to switch %s', tag, name, switch_name) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 297 | |
| 298 | # 2b. Do we need to worry about trunk ports on this switch? |
| 299 | if 'TrunkWildCardVlans' in s.switch_get_capabilities(): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 300 | logging.debug('This switch does not need special trunk port handling') |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 301 | else: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 302 | logging.debug('This switch needs special trunk port handling') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 303 | trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 304 | if trunk_ports is None: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 305 | logging.debug("But it has no trunk ports defined") |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 306 | trunk_ports = [] |
| 307 | else: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 308 | logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports)) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 309 | |
| 310 | # Modify any trunk ports as needed |
| 311 | for port in trunk_ports: |
Steve McIntyre | 1ce09f9 | 2015-09-04 14:35:13 +0100 | [diff] [blame] | 312 | logging.debug('Adding VLAN tag %d, name %s to switch %s port %s', tag, name, switch_name, port) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 313 | s.port_add_trunk_to_vlan(port, tag) |
| 314 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 315 | # And now we're done with this switch |
| 316 | s.switch_disconnect() |
| 317 | del s |
| 318 | |
Steve McIntyre | 8f27ae1 | 2015-09-04 14:35:38 +0100 | [diff] [blame] | 319 | except IOError as e: |
Steve McIntyre | 9f7bac0 | 2015-09-18 14:10:34 +0100 | [diff] [blame] | 320 | logging.error('Failed to add VLAN %d to switch ID %d (%s): %s', tag, switch.switch_id, switch.name, e) |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 321 | raise |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 322 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 323 | except IOError: |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 324 | # Bugger. Looks like one of the switch calls above |
| 325 | # failed. To undo the changes safely, we'll need to reset |
| 326 | # all the switches we managed to configure. This could |
| 327 | # take some time! |
Steve McIntyre | 38a24e5 | 2015-07-21 17:54:58 +0100 | [diff] [blame] | 328 | logging.error('create_vlan failed, resetting all switches to recover') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 329 | for switch_name in switches_done: |
| 330 | s = self.get_switch_driver(switch_name, config) |
| 331 | s.switch_connect(config.switches[switch_name].username, |
| 332 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 333 | config.switches[switch_name].enable_password) |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 334 | s.switch_restart() # Will implicitly also close the connection |
| 335 | del s |
| 336 | |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 337 | # Undo the database change |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 338 | logging.debug('Switch access failed. Deleting the new VLAN entry in the database') |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 339 | db.delete_vlan(vlan_id) |
| 340 | raise |
| 341 | |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 342 | # If we've got this far, things were successful. Save config |
| 343 | # on all the switches so it will persist across reboots |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 344 | for switch_name in switches_done: |
| 345 | s = self.get_switch_driver(switch_name, config) |
| 346 | s.switch_connect(config.switches[switch_name].username, |
| 347 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 348 | config.switches[switch_name].enable_password) |
Steve McIntyre | 153157d | 2014-12-19 18:05:20 +0000 | [diff] [blame] | 349 | s.switch_save_running_config() |
| 350 | s.switch_disconnect() |
| 351 | del s |
| 352 | |
Steve McIntyre | 6b8d386 | 2015-08-03 19:28:25 +0100 | [diff] [blame] | 353 | return (vlan_id, tag) # If we're successful |
Steve McIntyre | 3256b18 | 2014-12-19 15:38:15 +0000 | [diff] [blame] | 354 | |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 355 | # Complex call |
| 356 | # 1. Check in the DB if there are any ports on the VLAN. Bail if so |
| 357 | # 2. Iterate through all switches: |
| 358 | # a. Remove the VLAN from all trunk ports (if needed) |
| 359 | # b. Remove the VLAN |
| 360 | # 3. If all went OK, save config on the switches |
| 361 | # 4. Remove the VLAN in the DB |
| 362 | # |
| 363 | # If things fail, we attempt to roll back by rebooting switches. |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 364 | def delete_vlan(self, state, vlan_id): |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 365 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 366 | logging.debug('delete_vlan') |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 367 | db = state.db |
| 368 | config = state.config |
| 369 | |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 370 | # 1. Check for database records first |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 371 | logging.debug('Checking for ports using VLAN ID %d', vlan_id) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 372 | vlan = db.get_vlan_by_id(vlan_id) |
| 373 | if vlan is None: |
| 374 | raise InputError("VLAN ID %d does not exist" % vlan_id) |
| 375 | vlan_tag = vlan.tag |
| 376 | ports = db.get_ports_by_current_vlan(vlan_id) |
| 377 | if ports is not None: |
| 378 | raise InputError("Cannot delete VLAN ID %d when it still has %d ports" % |
| 379 | (vlan_id, len(ports))) |
| 380 | ports = db.get_ports_by_base_vlan(vlan_id) |
| 381 | if ports is not None: |
| 382 | raise InputError("Cannot delete VLAN ID %d when it still has %d ports" % |
| 383 | (vlan_id, len(ports))) |
| 384 | |
| 385 | # Keep track of which switches we've configured, for later use |
| 386 | switches_done = [] |
| 387 | |
| 388 | # 2. Now the switches |
| 389 | try: |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 390 | for switch in db.all_switches(): |
| 391 | switch_name = switch.name |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 392 | trunk_ports = [] |
| 393 | try: |
| 394 | # Get the right driver |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 395 | s = self.get_switch_driver(switch_name, config) |
| 396 | s.switch_connect(config.switches[switch_name].username, |
| 397 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 398 | config.switches[switch_name].enable_password) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 399 | |
| 400 | # Mark this switch as one we've touched, for |
| 401 | # either config saving or rollback below |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 402 | switches_done.append(switch_name) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 403 | |
| 404 | # 2a. Do we need to worry about trunk ports on this switch? |
| 405 | if 'TrunkWildCardVlans' in s.switch_get_capabilities(): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 406 | logging.debug('This switch does not need special trunk port handling') |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 407 | else: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 408 | logging.debug('This switch needs special trunk port handling') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 409 | trunk_ports = db.get_trunk_port_names_by_switch(switch.switch_id) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 410 | if trunk_ports is None: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 411 | logging.debug("But it has no trunk ports defined") |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 412 | trunk_ports = [] |
| 413 | else: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 414 | logging.debug('Found %d trunk_ports that need adjusting', len(trunk_ports)) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 415 | |
| 416 | # Modify any trunk ports as needed |
| 417 | for port in trunk_ports: |
Steve McIntyre | 4b4ab65 | 2014-12-22 17:19:09 +0000 | [diff] [blame] | 418 | s.port_remove_trunk_from_vlan(port, vlan_tag) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 419 | logging.debug('Removed VLAN tag %d from switch %s port %s', vlan_tag, switch_name, port) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 420 | |
| 421 | # 2b. Remove the VLAN from the switch |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 422 | logging.debug('Removing VLAN tag %d from switch %s', vlan_tag, switch_name) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 423 | s.vlan_destroy(vlan_tag) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 424 | logging.debug('Removed VLAN tag %d from switch %s', vlan_tag, switch_name) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 425 | |
| 426 | # And now we're done with this switch |
| 427 | s.switch_disconnect() |
| 428 | del s |
| 429 | |
| 430 | except IOError: |
| 431 | raise |
| 432 | |
| 433 | except IOError: |
| 434 | # Bugger. Looks like one of the switch calls above |
| 435 | # failed. To undo the changes safely, we'll need to reset |
| 436 | # all the switches we managed to configure. This could |
| 437 | # take some time! |
Steve McIntyre | 38a24e5 | 2015-07-21 17:54:58 +0100 | [diff] [blame] | 438 | logging.error('delete_vlan failed, resetting all switches to recover') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 439 | for switch_name in switches_done: |
| 440 | s = self.get_switch_driver(switch_name, config) |
| 441 | s.switch_connect(config.switches[switch_name].username, |
| 442 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 443 | config.switches[switch_name].enable_password) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 444 | s.switch_restart() # Will implicitly also close the connection |
| 445 | del s |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 446 | raise |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 447 | |
| 448 | # 3. If we've got this far, things were successful. Save |
| 449 | # config on all the switches so it will persist across reboots |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 450 | for switch_name in switches_done: |
| 451 | s = self.get_switch_driver(switch_name, config) |
| 452 | s.switch_connect(config.switches[switch_name].username, |
| 453 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 454 | config.switches[switch_name].enable_password) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 455 | s.switch_save_running_config() |
| 456 | s.switch_disconnect() |
| 457 | del s |
| 458 | |
| 459 | # 4. Finally, remove the VLAN in the DB |
| 460 | try: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 461 | logging.debug('Removing DB record: VLAN ID %d', vlan_id) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 462 | vlan_id = db.delete_vlan(vlan_id) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 463 | logging.debug('Removed VLAN ID %d from the database OK', vlan_id) |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 464 | except InputError: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 465 | logging.debug('DB deletion failed') |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 466 | raise |
| 467 | |
| 468 | return vlan_id # If we're successful |
| 469 | |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 470 | # Complex call, depends on existing state a lot |
| 471 | # 1. Check validity of inputs |
| 472 | # 2. Switch mode and other config on the port. |
| 473 | # a. If switching trunk->access, remove all trunk VLANs from it |
| 474 | # (if needed) and switch back to the base VLAN for the |
| 475 | # port. Next, switch to access mode. |
| 476 | # b. If switching access->trunk, switch back to the base VLAN |
| 477 | # for the port. Next, switch mode. Then add all trunk VLANs |
| 478 | # to it (if needed) |
| 479 | # 3. If all went OK, save config on the switch |
| 480 | # 4. Change details of the port in the DB |
| 481 | # |
| 482 | # If things fail, we attempt to roll back by rebooting the switch |
Steve McIntyre | 4b4ab65 | 2014-12-22 17:19:09 +0000 | [diff] [blame] | 483 | def set_port_mode(self, state, port_id, mode): |
Steve McIntyre | feb6452 | 2014-12-19 18:53:02 +0000 | [diff] [blame] | 484 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 485 | logging.debug('set_port_mode') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 486 | db = state.db |
| 487 | config = state.config |
| 488 | |
| 489 | # 1. Sanity-check inputs |
Steve McIntyre | 5b0de00 | 2015-01-23 18:05:13 +0000 | [diff] [blame] | 490 | if mode != 'access' and mode != 'trunk': |
| 491 | raise InputError("Port mode '%s' is not a valid option: try 'access' or 'trunk'" % mode) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 492 | port = db.get_port_by_id(port_id) |
| 493 | if port is None: |
| 494 | raise InputError("Port ID %d does not exist" % port_id) |
Steve McIntyre | 2811409 | 2015-02-13 03:04:40 +0000 | [diff] [blame] | 495 | if port.is_locked: |
| 496 | raise InputError("Port ID %d is locked" % port_id) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 497 | if mode == 'trunk' and port.is_trunk: |
Steve McIntyre | 3aff7da | 2015-02-13 04:00:11 +0000 | [diff] [blame] | 498 | raise InputError("Port ID %d is already in trunk mode" % port_id) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 499 | if mode == 'access' and not port.is_trunk: |
Steve McIntyre | 3aff7da | 2015-02-13 04:00:11 +0000 | [diff] [blame] | 500 | raise InputError("Port ID %d is already in access mode" % port_id) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 501 | base_vlan_tag = db.get_vlan_tag_by_id(port.base_vlan_id) |
| 502 | |
| 503 | # Get the right driver |
| 504 | switch_name = db.get_switch_name_by_id(port.switch_id) |
| 505 | s = self.get_switch_driver(switch_name, config) |
| 506 | |
| 507 | # 2. Now start configuring the switch |
| 508 | try: |
| 509 | s.switch_connect(config.switches[switch_name].username, |
| 510 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 511 | config.switches[switch_name].enable_password) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 512 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 513 | logging.debug('Failed to talk to switch %s!', switch_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 514 | raise |
| 515 | |
| 516 | try: |
| 517 | if port.is_trunk: |
| 518 | # 2a. We're going from a trunk port to an access port |
| 519 | if 'TrunkWildCardVlans' in s.switch_get_capabilities(): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 520 | logging.debug('This switch does not need special trunk port handling') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 521 | else: |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 522 | logging.debug('This switch needs special trunk port handling') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 523 | vlans = s.port_get_trunk_vlan_list(port.name) |
| 524 | if vlans is None: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 525 | logging.debug("But it has no VLANs defined on port %s", port.name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 526 | vlans = [] |
| 527 | else: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 528 | logging.debug('Found %d vlans that may need dropping on port %s', len(vlans), port.name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 529 | |
| 530 | for vlan in vlans: |
Steve McIntyre | 97f5e87 | 2015-01-23 18:07:05 +0000 | [diff] [blame] | 531 | if vlan != state.config.vland.default_vlan_tag: |
| 532 | s.port_remove_trunk_from_vlan(port.name, vlan) |
Steve McIntyre | f903b69 | 2015-07-09 18:29:05 +0100 | [diff] [blame] | 533 | |
| 534 | s.port_set_mode(port.name, "access") |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 535 | |
| 536 | else: |
| 537 | # 2b. We're going from an access port to a trunk port |
| 538 | s.port_set_access_vlan(port.name, base_vlan_tag) |
| 539 | s.port_set_mode(port.name, "trunk") |
| 540 | if 'TrunkWildCardVlans' in s.switch_get_capabilities(): |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 541 | logging.debug('This switch does not need special trunk port handling') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 542 | else: |
| 543 | vlans = db.all_vlans() |
| 544 | for vlan in vlans: |
Steve McIntyre | ee742d9 | 2015-09-03 18:16:09 +0100 | [diff] [blame] | 545 | if vlan.tag != state.config.vland.default_vlan_tag: |
Steve McIntyre | 36ae6ac | 2015-09-03 18:10:33 +0100 | [diff] [blame] | 546 | s.port_add_trunk_to_vlan(port.name, vlan.tag) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 547 | |
| 548 | except IOError: |
Steve McIntyre | 38a24e5 | 2015-07-21 17:54:58 +0100 | [diff] [blame] | 549 | logging.error('set_port_mode failed, resetting switch to recover') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 550 | # 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_port_mode(port_id, mode) |
| 564 | |
| 565 | return port_id # If we're successful |
| 566 | |
| 567 | # Complex call, updating both DB and switch state |
| 568 | # 1. Check validity of inputs |
| 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 set_current_vlan(self, state, port_id, vlan_id): |
| 575 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 576 | logging.debug('set_current_vlan') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 577 | db = state.db |
| 578 | config = state.config |
| 579 | |
| 580 | # 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) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 584 | if port.is_locked: |
| 585 | raise InputError("Port ID %d is locked" % port_id) |
Steve McIntyre | 3bb7bbd | 2015-02-13 03:06:01 +0000 | [diff] [blame] | 586 | if port.is_trunk: |
| 587 | raise InputError("Port ID %d is not an access port" % port_id) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 588 | |
| 589 | vlan = db.get_vlan_by_id(vlan_id) |
| 590 | if vlan is None: |
| 591 | raise InputError("VLAN ID %d does not exist" % vlan_id) |
| 592 | |
| 593 | # Get the right driver |
| 594 | switch_name = db.get_switch_name_by_id(port.switch_id) |
| 595 | s = self.get_switch_driver(switch_name, config) |
| 596 | |
| 597 | # 2. Now start configuring the switch |
| 598 | try: |
| 599 | s.switch_connect(config.switches[switch_name].username, |
| 600 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 601 | config.switches[switch_name].enable_password) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 602 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 603 | logging.debug('Failed to talk to switch %s!', switch_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 604 | raise |
| 605 | |
| 606 | try: |
| 607 | s.port_set_access_vlan(port.name, vlan.tag) |
| 608 | except IOError: |
Steve McIntyre | 38a24e5 | 2015-07-21 17:54:58 +0100 | [diff] [blame] | 609 | logging.error('set_current_vlan failed, resetting switch to recover') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 610 | # Bugger. Looks like one of the switch calls above |
| 611 | # failed. To undo the changes safely, we'll need to reset |
| 612 | # all the config on this switch |
| 613 | s.switch_restart() # Will implicitly also close the connection |
| 614 | del s |
| 615 | raise |
| 616 | |
| 617 | # 3. All seems to have worked so far! |
| 618 | s.switch_save_running_config() |
| 619 | s.switch_disconnect() |
| 620 | del s |
| 621 | |
| 622 | # 4. And update the DB |
| 623 | db.set_current_vlan(port_id, vlan_id) |
| 624 | |
| 625 | return port_id # If we're successful |
| 626 | |
| 627 | # Complex call, updating both DB and switch state |
| 628 | # 1. Check validity of input |
| 629 | # 2. Update the port config on the switch |
| 630 | # 3. If all went OK, save config on the switch |
| 631 | # 4. Change details of the port in the DB |
| 632 | # |
| 633 | # If things fail, we attempt to roll back by rebooting the switch |
| 634 | def restore_base_vlan(self, state, port_id): |
| 635 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 636 | logging.debug('restore_base_vlan') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 637 | db = state.db |
| 638 | config = state.config |
| 639 | |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 640 | # 1. Sanity checks! |
| 641 | port = db.get_port_by_id(port_id) |
| 642 | if port is None: |
| 643 | raise InputError("Port ID %d does not exist" % port_id) |
| 644 | if port.is_trunk: |
| 645 | raise InputError("Port ID %d is not an access port" % port_id) |
| 646 | if port.is_locked: |
| 647 | raise InputError("Port ID %d is locked" % port_id) |
| 648 | |
| 649 | # Bail out early if we're *already* on the base VLAN. This is |
| 650 | # not an error |
| 651 | if port.current_vlan_id == port.base_vlan_id: |
| 652 | return port_id |
| 653 | |
| 654 | vlan = db.get_vlan_by_id(port.base_vlan_id) |
| 655 | |
| 656 | # Get the right driver |
| 657 | switch_name = db.get_switch_name_by_id(port.switch_id) |
| 658 | s = self.get_switch_driver(switch_name, config) |
| 659 | |
| 660 | # 2. Now start configuring the switch |
| 661 | try: |
| 662 | s.switch_connect(config.switches[switch_name].username, |
| 663 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 664 | config.switches[switch_name].enable_password) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 665 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 666 | logging.debug('Failed to talk to switch %s!', switch_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 667 | raise |
| 668 | |
| 669 | try: |
| 670 | s.port_set_access_vlan(port.name, vlan.tag) |
| 671 | except IOError: |
Steve McIntyre | 38a24e5 | 2015-07-21 17:54:58 +0100 | [diff] [blame] | 672 | logging.error('restore_base_vlan failed, resetting switch to recover') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 673 | # Bugger. Looks like one of the switch calls above |
| 674 | # failed. To undo the changes safely, we'll need to reset |
| 675 | # all the config on this switch |
| 676 | s.switch_restart() # Will implicitly also close the connection |
| 677 | del s |
| 678 | raise |
| 679 | |
| 680 | # 3. All seems to have worked so far! |
| 681 | s.switch_save_running_config() |
| 682 | s.switch_disconnect() |
| 683 | del s |
| 684 | |
| 685 | # 4. And update the DB |
| 686 | db.set_current_vlan(port_id, port.base_vlan_id) |
| 687 | |
| 688 | return port_id # If we're successful |
| 689 | |
| 690 | # Complex call, updating both DB and switch state |
| 691 | # * Check validity of input |
| 692 | # * Read all the config from the switch (switch, ports, VLANs) |
| 693 | # * Create initial DB entries to match each of those |
| 694 | # * Merge VLANs across all switches |
| 695 | # * Set up ports appropriately |
| 696 | # |
| 697 | def auto_import_switch(self, state, switch_name): |
| 698 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 699 | logging.debug('auto_import_switch') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 700 | db = state.db |
| 701 | config = state.config |
| 702 | |
Steve McIntyre | a8fe1de | 2015-02-11 17:13:12 +0000 | [diff] [blame] | 703 | port_vlans = {} |
| 704 | |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 705 | # 1. Sanity checks! |
| 706 | switch_id = db.get_switch_id_by_name(switch_name) |
| 707 | if switch_id is not None: |
| 708 | raise InputError("Switch name %s already exists in the DB (ID %d)" % (switch_name, switch_id)) |
| 709 | |
Steve McIntyre | 4b4ab65 | 2014-12-22 17:19:09 +0000 | [diff] [blame] | 710 | if not switch_name in config.switches: |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 711 | raise InputError("Switch name %s not defined in config" % switch_name) |
| 712 | |
| 713 | # 2. Now start reading config from the switch |
| 714 | try: |
| 715 | s = self.get_switch_driver(switch_name, config) |
| 716 | s.switch_connect(config.switches[switch_name].username, |
| 717 | config.switches[switch_name].password, |
Steve McIntyre | 3b655af | 2014-12-23 13:43:19 +0000 | [diff] [blame] | 718 | config.switches[switch_name].enable_password) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 719 | except: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 720 | logging.debug('Failed to talk to switch %s!', switch_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 721 | raise |
| 722 | |
| 723 | # DON'T create the switch record in the DB first - we'll want |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 724 | # to create VLANs on *other* switches, and it's easier to do |
| 725 | # that before we've added our new switch |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 726 | |
| 727 | new_vlan_tags = [] |
| 728 | |
| 729 | # Grab the VLANs defined on this switch |
| 730 | vlan_tags = s.vlan_get_list() |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 731 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 732 | logging.debug(' found %d vlans on the switch', len(vlan_tags)) |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 733 | |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 734 | for vlan_tag in vlan_tags: |
| 735 | vlan_name = s.vlan_get_name(vlan_tag) |
| 736 | |
| 737 | # If a VLAN is already in the database, then that's easy - |
| 738 | # we can just ignore it. However, we have to check that |
| 739 | # there is not a different name for the existing VLAN tag |
Steve McIntyre | b152907 | 2014-12-23 17:17:22 +0000 | [diff] [blame] | 740 | # - bail out if so... UNLESS we're looking at the default |
| 741 | # VLAN |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 742 | # |
| 743 | # If this VLAN tag is not already in the DB, we'll need to |
| 744 | # add it there and to all the other switches (and their |
| 745 | # trunk ports!) too. |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 746 | vlan_id = db.get_vlan_id_by_tag(vlan_tag) |
Steve McIntyre | 6c09c0c | 2015-07-17 17:20:01 +0100 | [diff] [blame] | 747 | if vlan_id != state.default_vlan_id: |
Steve McIntyre | b152907 | 2014-12-23 17:17:22 +0000 | [diff] [blame] | 748 | if vlan_id is not None: |
| 749 | vlan_db_name = db.get_vlan_name_by_id(vlan_id) |
| 750 | if vlan_name != vlan_db_name: |
| 751 | 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 McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 752 | |
Steve McIntyre | b152907 | 2014-12-23 17:17:22 +0000 | [diff] [blame] | 753 | else: |
| 754 | # OK, we'll need to set up the new VLAN now. It can't |
| 755 | # be a base VLAN - switches don't have such a concept! |
| 756 | # Rather than create individually here, add to a |
| 757 | # list. *Only* once we've worked through all the |
| 758 | # switch's VLANs successfully (checking for existing |
| 759 | # records and possible clashes!) should we start |
| 760 | # committing changes |
| 761 | new_vlan_tags.append(vlan_tag) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 762 | |
| 763 | # Now create the VLAN DB entries |
| 764 | for vlan_tag in new_vlan_tags: |
| 765 | vlan_name = s.vlan_get_name(vlan_tag) |
| 766 | vlan_id = self.create_vlan(state, vlan_name, vlan_tag, False) |
| 767 | |
| 768 | # *Now* add this switch itself to the database, after we've |
| 769 | # worked on all the other switches |
| 770 | switch_id = db.create_switch(switch_name) |
| 771 | |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 772 | # And now the ports |
| 773 | trunk_ports = [] |
| 774 | ports = s.switch_get_port_names() |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 775 | logging.debug(' found %d ports on the switch', len(ports)) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 776 | for port_name in ports: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 777 | logging.debug(' trying to import port %s', port_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 778 | port_id = None |
| 779 | port_mode = s.port_get_mode(port_name) |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 780 | port_number = s.port_map_name_to_number(port_name) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 781 | if port_mode == 'access': |
| 782 | # Access ports are easy - just create the port, and |
| 783 | # set both the current and base VLANs to the current |
| 784 | # VLAN on the switch. We'll end up changing this after |
| 785 | # import if needed. |
Steve McIntyre | a8fe1de | 2015-02-11 17:13:12 +0000 | [diff] [blame] | 786 | port_vlans[port_name] = (s.port_get_access_vlan(port_name),) |
| 787 | port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0]) |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 788 | port_id = db.create_port(switch_id, port_name, port_number, |
Steve McIntyre | 6f17b10 | 2014-12-24 02:18:08 +0000 | [diff] [blame] | 789 | port_vlan_id, port_vlan_id) |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 790 | logging.debug(' access port, VLAN %d', int(port_vlans[port_name][0])) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 791 | # Nothing further needed |
| 792 | elif port_mode == 'trunk': |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 793 | logging.debug(' trunk port, VLANs:') |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 794 | # Trunk ports are a little more involved. First, |
| 795 | # create the port in the DB, setting the VLANs to the |
| 796 | # first VLAN found on the trunk port. This will *also* |
| 797 | # be in access mode by default, and unlocked. |
Steve McIntyre | a8fe1de | 2015-02-11 17:13:12 +0000 | [diff] [blame] | 798 | port_vlans[port_name] = s.port_get_trunk_vlan_list(port_name) |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 799 | logging.debug(port_vlans[port_name]) |
Steve McIntyre | a8fe1de | 2015-02-11 17:13:12 +0000 | [diff] [blame] | 800 | if port_vlans[port_name] == [] or port_vlans[port_name] is None or 'ALL' in port_vlans[port_name]: |
| 801 | port_vlans[port_name] = (state.config.vland.default_vlan_tag,) |
| 802 | port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0]) |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 803 | port_id = db.create_port(switch_id, port_name, port_number, |
Steve McIntyre | 6f17b10 | 2014-12-24 02:18:08 +0000 | [diff] [blame] | 804 | port_vlan_id, port_vlan_id) |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 805 | # Append to a list of trunk ports that we will need to |
| 806 | # modify once we're done |
| 807 | trunk_ports.append(port_id) |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 808 | else: |
Steve McIntyre | 857d89f | 2015-07-24 16:22:37 +0100 | [diff] [blame] | 809 | # We've found a port mode we don't want, e.g. the |
| 810 | # "dynamic auto" on a Cisco Catalyst. Handle that here |
| 811 | # - tell the switch to set that port to access and |
| 812 | # handle accordingly. |
| 813 | s.port_set_mode(port_name, 'access') |
| 814 | port_vlans[port_name] = (s.port_get_access_vlan(port_name),) |
| 815 | port_vlan_id = db.get_vlan_id_by_tag(port_vlans[port_name][0]) |
Steve McIntyre | ea75397 | 2015-08-05 13:52:48 +0100 | [diff] [blame] | 816 | port_id = db.create_port(switch_id, port_name, port_number, |
Steve McIntyre | 857d89f | 2015-07-24 16:22:37 +0100 | [diff] [blame] | 817 | port_vlan_id, port_vlan_id) |
| 818 | logging.debug(' Found port in %s mode', port_mode) |
| 819 | logging.debug(' Forcing to access mode, VLAN %d', int(port_vlans[port_name][0])) |
| 820 | port_mode = "access" |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 821 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 822 | logging.debug(" Added port %s, got port ID %d", port_name, port_id) |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 823 | |
Steve McIntyre | 574e334 | 2015-01-23 18:08:33 +0000 | [diff] [blame] | 824 | db.set_port_mode(port_id, port_mode) |
| 825 | |
Steve McIntyre | 6feea57 | 2015-02-09 15:49:20 +0000 | [diff] [blame] | 826 | # Make sure this switch has all the VLANs we need |
| 827 | for vlan in db.all_vlans(): |
Steve McIntyre | 49d3c08 | 2015-07-24 16:17:23 +0100 | [diff] [blame] | 828 | if vlan.tag != state.config.vland.default_vlan_tag: |
Steve McIntyre | d7fb407 | 2015-02-11 17:13:50 +0000 | [diff] [blame] | 829 | if not vlan.tag in vlan_tags: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 830 | logging.debug("Adding VLAN tag %d to this switch", vlan.tag) |
Steve McIntyre | 6feea57 | 2015-02-09 15:49:20 +0000 | [diff] [blame] | 831 | s.vlan_create(vlan.tag) |
| 832 | s.vlan_set_name(vlan.tag, vlan.name) |
| 833 | |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 834 | # Now, on each trunk port on the switch, we need to add all |
| 835 | # the VLANs already configured across our system |
| 836 | if not 'TrunkWildCardVlans' in s.switch_get_capabilities(): |
| 837 | for port_id in trunk_ports: |
| 838 | port = db.get_port_by_id(port_id) |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 839 | |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 840 | for vlan in db.all_vlans(): |
Steve McIntyre | 49d3c08 | 2015-07-24 16:17:23 +0100 | [diff] [blame] | 841 | if vlan.vlan_id != state.default_vlan_id: |
Steve McIntyre | a8fe1de | 2015-02-11 17:13:12 +0000 | [diff] [blame] | 842 | if not vlan.tag in port_vlans[port.name]: |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 843 | logging.debug("Adding allowed VLAN tag %d to trunk port %s", vlan.tag, port.name) |
Steve McIntyre | a8fe1de | 2015-02-11 17:13:12 +0000 | [diff] [blame] | 844 | s.port_add_trunk_to_vlan(port.name, vlan.tag) |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 845 | |
Steve McIntyre | fc51124 | 2014-12-23 22:28:30 +0000 | [diff] [blame] | 846 | # Done with this switch \o/ |
| 847 | s.switch_save_running_config() |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 848 | s.switch_disconnect() |
| 849 | del s |
| 850 | |
Steve McIntyre | 4b4ab65 | 2014-12-22 17:19:09 +0000 | [diff] [blame] | 851 | ret = {} |
Steve McIntyre | 5f6f85e | 2014-12-22 16:42:28 +0000 | [diff] [blame] | 852 | ret['switch_id'] = switch_id |
| 853 | ret['num_ports_added'] = len(ports) |
| 854 | ret['num_vlans_added'] = len(new_vlan_tags) |
| 855 | return ret # If we're successful |