blob: e90724585c03e85a6aa51521c3a31f59a35340eb [file] [log] [blame]
Steve McIntyre30bf4db2014-08-12 18:08:22 +01001#! /usr/bin/python
2
3# Copyright 2014 Linaro Limited
Steve McIntyre6103e982014-09-18 23:37:54 +01004
Steve McIntyre30bf4db2014-08-12 18:08:22 +01005#
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
21import time
22import logging
23
Steve McIntyre96f2c652015-02-12 04:25:55 +000024class 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 McIntyrea67473a2015-02-12 08:26:33 +000032 return "<SwitchErrors: errors_in: %d, errors_out: %d>" % (self.errors_in, self.errors_out)
Steve McIntyre96f2c652015-02-12 04:25:55 +000033
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 McIntyre30bf4db2014-08-12 18:08:22 +010044class SwitchDriver(object):
Steve McIntyre366046f2014-08-18 18:57:03 +010045
Steve McIntyre30bf4db2014-08-12 18:08:22 +010046 connection = None
47 hostname = ""
Steve McIntyre366046f2014-08-18 18:57:03 +010048 serial_number = ''
49
Steve McIntyre9936d002014-10-01 15:54:10 +010050 _allowed_port_modes = [ "trunk", "access" ]
Steve McIntyre366046f2014-08-18 18:57:03 +010051 _ports = []
52 _prompt_name = ''
Steve McIntyre115350d2015-07-14 17:11:30 +010053 _username = ''
54 _password = ''
55 _enable_password = ''
56 _systemdata = []
Steve McIntyre30bf4db2014-08-12 18:08:22 +010057
Steve McIntyre2396f622015-07-14 15:35:03 +010058 def __init__ (self, switch_hostname, debug):
59
60 if debug:
61 # Configure logging for pexpect output if we have debug
62 # enabled
63
64 # get the logger
65 self.logger = logging.getLogger(switch_hostname)
66
67 # give the logger the methods required by pexpect
68 self.logger.write = self._log_write
69 self.logger.flush = self._log_do_nothing
70
71 else:
72 self.logger = None
73
Steve McIntyre37870ef2015-07-14 16:25:12 +010074 self.hostname = switch_hostname
75
76 # Connect to the switch and log in
77 def switch_connect(self, username, password, enablepassword):
78 self._username = username
79 self._password = password
80 self._enable_password = enablepassword
81 self._switch_connect()
82
83 # Log out of the switch and drop the connection and all state
84 def switch_disconnect(self):
85 self._logout()
86 logging.debug("Closing connection to %s", self.hostname)
87 self.connection.close(True)
88 self._ports = []
89 self._prompt_name = ''
90 self._systemdata = []
91 del(self)
92
Steve McIntyre5fa22652015-04-01 18:01:45 +010093 def dump_list(self, data):
Steve McIntyre30bf4db2014-08-12 18:08:22 +010094 i = 0
Steve McIntyre2b4c07b2014-12-22 16:10:04 +000095 for line in data:
Steve McIntyre30bf4db2014-08-12 18:08:22 +010096 print "%d: \"%s\"" % (i, line)
97 i += 1
98
99 def _delay(self):
100 time.sleep(0.5)
101
Steve McIntyre366046f2014-08-18 18:57:03 +0100102 # List the capabilities of the switch (and driver) - some things
103 # make no sense to abstract. Returns a dict of strings, each one
104 # describing an extra feature that that higher levels may care
105 # about
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100106 def switch_get_capabilities(self):
Steve McIntyre366046f2014-08-18 18:57:03 +0100107 return self._capabilities
108
109 # List the names of all the ports on the switch
Steve McIntyre9b09b9d2014-09-24 15:08:10 +0100110 def switch_get_port_names(self):
Steve McIntyre366046f2014-08-18 18:57:03 +0100111 return self._ports
112
113 def _is_port_name_valid(self, name):
Steve McIntyre366046f2014-08-18 18:57:03 +0100114 for port in self._ports:
115 if name == port:
116 return True
117 return False
118
119 def _is_port_mode_valid(self, mode):
Steve McIntyre366046f2014-08-18 18:57:03 +0100120 for allowed in self._allowed_port_modes:
121 if allowed == mode:
122 return True
123 return False
124
Steve McIntyre2396f622015-07-14 15:35:03 +0100125 # Wrappers to adapt logging for pexpect when we've configured on a
126 # switch.
127 # This will be the method called by the pexpect object to write a
128 # log message
129 def _log_write(self, *args, **kwargs):
130 # ignore other parameters, pexpect only uses one arg
131 content = args[0]
132
133 if content in [' ', '', '\n', '\r', '\r\n']:
134 return # don't log empty lines
135
136 # Split the output into multiple lines so we get a
137 # well-formatted logfile
138 for line in content.split('\r\n'):
139 logging.info(line)
140
141 # This is the flush method for pexpect
142 def _log_do_nothing(self):
143 pass
144