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 | 96f2c65 | 2015-02-12 04:25:55 +0000 | [diff] [blame] | 24 | class SwitchErrors: |
| 25 | """ Error logging and statistics class """ |
| 26 | |
| 27 | def __init__(self): |
| 28 | self.errors_in = 0 |
| 29 | self.errors_out = 0 |
| 30 | |
| 31 | def __repr__(self): |
Steve McIntyre | a67473a | 2015-02-12 08:26:33 +0000 | [diff] [blame] | 32 | 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] | 33 | |
| 34 | # For now, just count the error. Later on we might add stats and |
| 35 | # analysis |
| 36 | def log_error_in(self, text): |
| 37 | self.errors_in += 1 |
| 38 | |
| 39 | # For now, just count the error. Later on we might add stats and |
| 40 | # analysis |
| 41 | def log_error_out(self, text): |
| 42 | self.errors_out += 1 |
| 43 | |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 44 | class SwitchDriver(object): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 45 | |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 46 | connection = None |
| 47 | hostname = "" |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 48 | serial_number = '' |
| 49 | |
Steve McIntyre | 9936d00 | 2014-10-01 15:54:10 +0100 | [diff] [blame] | 50 | _allowed_port_modes = [ "trunk", "access" ] |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 51 | _ports = [] |
| 52 | _prompt_name = '' |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 53 | |
Steve McIntyre | 2396f62 | 2015-07-14 15:35:03 +0100 | [diff] [blame] | 54 | def __init__ (self, switch_hostname, debug): |
| 55 | |
| 56 | if debug: |
| 57 | # Configure logging for pexpect output if we have debug |
| 58 | # enabled |
| 59 | |
| 60 | # get the logger |
| 61 | self.logger = logging.getLogger(switch_hostname) |
| 62 | |
| 63 | # give the logger the methods required by pexpect |
| 64 | self.logger.write = self._log_write |
| 65 | self.logger.flush = self._log_do_nothing |
| 66 | |
| 67 | else: |
| 68 | self.logger = None |
| 69 | |
Steve McIntyre | 37870ef | 2015-07-14 16:25:12 +0100 | [diff] [blame] | 70 | self.hostname = switch_hostname |
| 71 | |
| 72 | # Connect to the switch and log in |
| 73 | def switch_connect(self, username, password, enablepassword): |
| 74 | self._username = username |
| 75 | self._password = password |
| 76 | self._enable_password = enablepassword |
| 77 | self._switch_connect() |
| 78 | |
| 79 | # Log out of the switch and drop the connection and all state |
| 80 | def switch_disconnect(self): |
| 81 | self._logout() |
| 82 | logging.debug("Closing connection to %s", self.hostname) |
| 83 | self.connection.close(True) |
| 84 | self._ports = [] |
| 85 | self._prompt_name = '' |
| 86 | self._systemdata = [] |
| 87 | del(self) |
| 88 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 89 | def dump_list(self, data): |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 90 | i = 0 |
Steve McIntyre | 2b4c07b | 2014-12-22 16:10:04 +0000 | [diff] [blame] | 91 | for line in data: |
Steve McIntyre | 30bf4db | 2014-08-12 18:08:22 +0100 | [diff] [blame] | 92 | print "%d: \"%s\"" % (i, line) |
| 93 | i += 1 |
| 94 | |
| 95 | def _delay(self): |
| 96 | time.sleep(0.5) |
| 97 | |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 98 | # List the capabilities of the switch (and driver) - some things |
| 99 | # make no sense to abstract. Returns a dict of strings, each one |
| 100 | # describing an extra feature that that higher levels may care |
| 101 | # about |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 102 | def switch_get_capabilities(self): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 103 | return self._capabilities |
| 104 | |
| 105 | # List the names of all the ports on the switch |
Steve McIntyre | 9b09b9d | 2014-09-24 15:08:10 +0100 | [diff] [blame] | 106 | def switch_get_port_names(self): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 107 | return self._ports |
| 108 | |
| 109 | def _is_port_name_valid(self, name): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 110 | for port in self._ports: |
| 111 | if name == port: |
| 112 | return True |
| 113 | return False |
| 114 | |
| 115 | def _is_port_mode_valid(self, mode): |
Steve McIntyre | 366046f | 2014-08-18 18:57:03 +0100 | [diff] [blame] | 116 | for allowed in self._allowed_port_modes: |
| 117 | if allowed == mode: |
| 118 | return True |
| 119 | return False |
| 120 | |
Steve McIntyre | 2396f62 | 2015-07-14 15:35:03 +0100 | [diff] [blame] | 121 | # Wrappers to adapt logging for pexpect when we've configured on a |
| 122 | # switch. |
| 123 | # This will be the method called by the pexpect object to write a |
| 124 | # log message |
| 125 | def _log_write(self, *args, **kwargs): |
| 126 | # ignore other parameters, pexpect only uses one arg |
| 127 | content = args[0] |
| 128 | |
| 129 | if content in [' ', '', '\n', '\r', '\r\n']: |
| 130 | return # don't log empty lines |
| 131 | |
| 132 | # Split the output into multiple lines so we get a |
| 133 | # well-formatted logfile |
| 134 | for line in content.split('\r\n'): |
| 135 | logging.info(line) |
| 136 | |
| 137 | # This is the flush method for pexpect |
| 138 | def _log_do_nothing(self): |
| 139 | pass |
| 140 | |