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