aboutsummaryrefslogtreecommitdiff
path: root/Vland/drivers/common.py
blob: e564c9ef2218716e85d878b4f0e12fde25d49280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /usr/bin/python

#  Copyright 2014-2015 Linaro Limited
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

import time
import logging

from errors import InputError, PExpectError

class SwitchErrors:
    """ Error logging and statistics class """

    def __init__(self):
        self.errors_in = 0
        self.errors_out = 0

    def __repr__(self):
        return "<SwitchErrors: errors_in: %d, errors_out: %d>" % (self.errors_in, self.errors_out)

    # For now, just count the error. Later on we might add stats and
    # analysis
    def log_error_in(self, text):
        self.errors_in += 1

    # For now, just count the error. Later on we might add stats and
    # analysis
    def log_error_out(self, text):
        self.errors_out += 1

class SwitchDriver(object):

    connection = None
    hostname = ""
    serial_number = ''

    _allowed_port_modes = [ "trunk", "access" ]
    _ports = []
    _port_modes = {}
    _port_numbers = {}
    _prompt_name = ''
    _username = ''
    _password = ''
    _enable_password = ''
    _systemdata = []

    def __init__ (self, switch_hostname, debug):

        if debug:
            # Configure logging for pexpect output if we have debug
            # enabled

            # get the logger
            self.logger = logging.getLogger(switch_hostname)

            # give the logger the methods required by pexpect
            self.logger.write = self._log_write
            self.logger.flush = self._log_do_nothing

        else:
            self.logger = None

        self.hostname = switch_hostname

    # Connect to the switch and log in
    def switch_connect(self, username, password, enablepassword):
        self._username = username
        self._password = password
        self._enable_password = enablepassword
        self._switch_connect()

    # Log out of the switch and drop the connection and all state
    def switch_disconnect(self):
        self._logout()
        logging.debug("Closing connection to %s", self.hostname)
        self._ports = []
        self._port_modes.clear()
        self._port_numbers.clear()
        self._prompt_name = ''
        self._systemdata = []
        del(self)

    def dump_list(self, data):
        i = 0
        for line in data:
            print "%d: \"%s\"" % (i, line)
            i += 1

    def _delay(self):
        time.sleep(0.5)

    # List the capabilities of the switch (and driver) - some things
    # make no sense to abstract. Returns a dict of strings, each one
    # describing an extra feature that that higher levels may care
    # about
    def switch_get_capabilities(self):
        return self._capabilities

    # List the names of all the ports on the switch
    def switch_get_port_names(self):
        return self._ports

    def _is_port_name_valid(self, name):
        for port in self._ports:
            if name == port:
                return True
        return False

    def _is_port_mode_valid(self, mode):
        for allowed in self._allowed_port_modes:
            if allowed == mode:
                return True
        return False

    # Try to look up a port mode in our cache. If not there, go ask
    # the switch and cache the result
    def port_get_mode(self, port):
        if not self._is_port_name_valid(port):
            raise InputError("Port name %s not recognised" % port)
        if port in self._port_modes:
            logging.debug("port_get_mode: returning mode %s from cache for port %s", self._port_modes[port], port)
            return self._port_modes[port]
        else:
            mode = self._port_get_mode(port)
            self._port_modes[port] = mode
            logging.debug("port_get_mode: found mode %s for port %s, adding to cache", self._port_modes[port], port)
            return mode

    def port_map_name_to_number(self, port_name):
        if not self._is_port_name_valid(port_name):
            raise InputError("Port name %s not recognised" % port_name)
        logging.debug("port_map_name_to_number: returning %d for port_name %s", self._port_numbers[port_name], port_name)
        return self._port_numbers[port_name]

    # Wrappers to adapt logging for pexpect when we've configured on a
    # switch.
    # This will be the method called by the pexpect object to write a
    # log message
    def _log_write(self, *args, **kwargs):
        # ignore other parameters, pexpect only uses one arg
        content = args[0]

        if content in [' ', '', '\n', '\r', '\r\n']:
            return # don't log empty lines

        # Split the output into multiple lines so we get a
        # well-formatted logfile
        for line in content.split('\r\n'):
            logging.info(line)

    # This is the flush method for pexpect
    def _log_do_nothing(self):
        pass