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