Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
Steve McIntyre | 6103e98 | 2014-09-18 23:37:54 +0100 | [diff] [blame] | 4 | |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | |
| 21 | import time |
| 22 | import logging |
| 23 | |
Steve McIntyre | b826fc7 | 2015-07-27 17:57:40 +0100 | [diff] [blame] | 24 | from errors import InputError, PExpectError |
| 25 | |
Steve McIntyre | 96f2c65 | 2015-02-12 04:25:55 +0000 | [diff] [blame] | 26 | class SwitchErrors: |
| 27 | """ Error logging and statistics class """ |
| 28 | |
| 29 | def __init__(self): |
| 30 | self.errors_in = 0 |
| 31 | self.errors_out = 0 |
| 32 | |
| 33 | def __repr__(self): |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 34 | return "<SwitchErrors: errors_in: %d, errors_out: %d>" % (self.errors_in, self.errors_out) |
Steve McIntyre | 96f2c65 | 2015-02-12 04:25:55 +0000 | [diff] [blame] | 35 | |
| 36 | # For now, just count the error. Later on we might add stats and |
| 37 | # analysis |
| 38 | def log_error_in(self, text): |
| 39 | self.errors_in += 1 |
| 40 | |
| 41 | # For now, just count the error. Later on we might add stats and |
| 42 | # analysis |
| 43 | def log_error_out(self, text): |
| 44 | self.errors_out += 1 |
| 45 | |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 46 | class SwitchDriver(object): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 47 | |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 48 | connection = None |
| 49 | hostname = "" |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 50 | serial_number = '' |
| 51 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 52 | _allowed_port_modes = [ "trunk", "access" ] |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 53 | _ports = [] |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 54 | _port_modes = {} |
Steve McIntyre | d5cfde1 | 2015-08-05 13:49:42 +0100 | [diff] [blame^] | 55 | _port_numbers = {} |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 56 | _prompt_name = '' |
Steve McIntyre | 115350d | 2015-07-14 17:11:30 +0100 | [diff] [blame] | 57 | _username = '' |
| 58 | _password = '' |
| 59 | _enable_password = '' |
| 60 | _systemdata = [] |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 61 | |
Steve McIntyre | 2396f62 | 2015-07-14 15:35:03 +0100 | [diff] [blame] | 62 | def __init__ (self, switch_hostname, debug): |
| 63 | |
| 64 | if debug: |
| 65 | # Configure logging for pexpect output if we have debug |
| 66 | # enabled |
| 67 | |
| 68 | # get the logger |
| 69 | self.logger = logging.getLogger(switch_hostname) |
| 70 | |
| 71 | # give the logger the methods required by pexpect |
| 72 | self.logger.write = self._log_write |
| 73 | self.logger.flush = self._log_do_nothing |
| 74 | |
| 75 | else: |
| 76 | self.logger = None |
| 77 | |
Steve McIntyre | 37870ef | 2015-07-14 16:25:12 +0100 | [diff] [blame] | 78 | self.hostname = switch_hostname |
| 79 | |
| 80 | # Connect to the switch and log in |
| 81 | def switch_connect(self, username, password, enablepassword): |
| 82 | self._username = username |
| 83 | self._password = password |
| 84 | self._enable_password = enablepassword |
| 85 | self._switch_connect() |
| 86 | |
| 87 | # Log out of the switch and drop the connection and all state |
| 88 | def switch_disconnect(self): |
| 89 | self._logout() |
| 90 | logging.debug("Closing connection to %s", self.hostname) |
| 91 | self.connection.close(True) |
| 92 | self._ports = [] |
Steve McIntyre | 8de82cf | 2015-07-21 17:45:44 +0100 | [diff] [blame] | 93 | self._port_modes.clear() |
Steve McIntyre | d5cfde1 | 2015-08-05 13:49:42 +0100 | [diff] [blame^] | 94 | self._port_numbers.clear() |
Steve McIntyre | 37870ef | 2015-07-14 16:25:12 +0100 | [diff] [blame] | 95 | self._prompt_name = '' |
| 96 | self._systemdata = [] |
| 97 | del(self) |
| 98 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 99 | def dump_list(self, data): |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 100 | i = 0 |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 101 | for line in data: |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 102 | print "%d: \"%s\"" % (i, line) |
| 103 | i += 1 |
| 104 | |
| 105 | def _delay(self): |
| 106 | time.sleep(0.5) |
| 107 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 108 | # List the capabilities of the switch (and driver) - some things |
| 109 | # make no sense to abstract. Returns a dict of strings, each one |
| 110 | # describing an extra feature that that higher levels may care |
| 111 | # about |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 112 | def switch_get_capabilities(self): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 113 | return self._capabilities |
| 114 | |
| 115 | # List the names of all the ports on the switch |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 116 | def switch_get_port_names(self): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 117 | return self._ports |
| 118 | |
| 119 | def _is_port_name_valid(self, name): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 120 | for port in self._ports: |
| 121 | if name == port: |
| 122 | return True |
| 123 | return False |
| 124 | |
| 125 | def _is_port_mode_valid(self, mode): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 126 | for allowed in self._allowed_port_modes: |
| 127 | if allowed == mode: |
| 128 | return True |
| 129 | return False |
| 130 | |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 131 | # Try to look up a port mode in our cache. If not there, go ask |
| 132 | # the switch and cache the result |
| 133 | def port_get_mode(self, port): |
| 134 | if not self._is_port_name_valid(port): |
| 135 | raise InputError("Port name %s not recognised" % port) |
| 136 | if port in self._port_modes: |
Steve McIntyre | 4e75c5b | 2015-07-21 17:46:05 +0100 | [diff] [blame] | 137 | logging.debug("port_get_mode: returning mode %s from cache for port %s", self._port_modes[port], port) |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 138 | return self._port_modes[port] |
| 139 | else: |
| 140 | mode = self._port_get_mode(port) |
| 141 | self._port_modes[port] = mode |
Steve McIntyre | 4e75c5b | 2015-07-21 17:46:05 +0100 | [diff] [blame] | 142 | logging.debug("port_get_mode: found mode %s for port %s, adding to cache", self._port_modes[port], port) |
Steve McIntyre | 1a7bad5 | 2015-07-20 15:21:42 +0100 | [diff] [blame] | 143 | return mode |
| 144 | |
Steve McIntyre | d5cfde1 | 2015-08-05 13:49:42 +0100 | [diff] [blame^] | 145 | def port_map_name_to_number(self, port_name): |
| 146 | if not self._is_port_name_valid(port_name): |
| 147 | raise InputError("Port name %s not recognised" % port_name) |
| 148 | logging.debug("port_map_name_to_number: returning %d for port_name %s", self._port_numbers[port_name], port_name) |
| 149 | return self._port_numbers[port_name] |
| 150 | |
Steve McIntyre | 2396f62 | 2015-07-14 15:35:03 +0100 | [diff] [blame] | 151 | # Wrappers to adapt logging for pexpect when we've configured on a |
| 152 | # switch. |
| 153 | # This will be the method called by the pexpect object to write a |
| 154 | # log message |
| 155 | def _log_write(self, *args, **kwargs): |
| 156 | # ignore other parameters, pexpect only uses one arg |
| 157 | content = args[0] |
| 158 | |
| 159 | if content in [' ', '', '\n', '\r', '\r\n']: |
| 160 | return # don't log empty lines |
| 161 | |
| 162 | # Split the output into multiple lines so we get a |
| 163 | # well-formatted logfile |
| 164 | for line in content.split('\r\n'): |
| 165 | logging.info(line) |
| 166 | |
| 167 | # This is the flush method for pexpect |
| 168 | def _log_do_nothing(self): |
| 169 | pass |
| 170 | |