blob: f0bdbe72b2d7700319d22fe243d325b02c97bd95 [file] [log] [blame]
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +01001#! /usr/bin/python
2
3# Copyright 2013 Linaro Limited
4# Author Matt Hart <matthew.hart@linaro.org>
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
Matt Hart76fb2542014-06-01 14:24:56 +010021import logging
Matt Hartd4a09982015-07-17 16:13:11 +010022log = logging.getLogger(__name__)
Neil Williams16579dc2014-02-13 11:20:04 +000023
Matt Hart132d43c2015-02-25 18:19:53 +000024
Matt Hart76fb2542014-06-01 14:24:56 +010025class PDUDriver(object):
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010026 connection = None
Matt Hart2af71142014-07-22 10:40:41 +010027 hostname = ""
matthew.hart@linaro.org5e4fce92013-08-22 11:29:21 +010028
Matt Hart132d43c2015-02-25 18:19:53 +000029 def __init__(self):
30 super(PDUDriver, self).__init__()
31
32 @classmethod
33 def select(cls, drivername):
Matt Hartd4a09982015-07-17 16:13:11 +010034 log.debug("adding PDUDriver subclasses: %s",
35 cls.__subclasses__()) # pylint: disable=no-member
Matt Hart132d43c2015-02-25 18:19:53 +000036 candidates = cls.__subclasses__() # pylint: disable=no-member
Matt Hartfe042212015-07-09 14:13:29 +010037 for subc in cls.__subclasses__(): # pylint: disable=no-member
Matt Hartd4a09982015-07-17 16:13:11 +010038 log.debug("adding %s subclasses: %s", subc,
39 subc.__subclasses__())
Matt Hart132d43c2015-02-25 18:19:53 +000040 candidates = candidates + (subc.__subclasses__())
41 for subsubc in subc.__subclasses__():
Matt Hartd4a09982015-07-17 16:13:11 +010042 log.debug("adding %s subclasses: %s", subsubc,
43 subsubc.__subclasses__())
Matt Hart132d43c2015-02-25 18:19:53 +000044 candidates = candidates + (subsubc.__subclasses__())
Matt Hartd4a09982015-07-17 16:13:11 +010045 log.debug(candidates)
Matt Hart132d43c2015-02-25 18:19:53 +000046 willing = [c for c in candidates if c.accepts(drivername)]
47 if len(willing) == 0:
48 raise NotImplementedError(
49 "No driver accepted the request "
Matt Hartfe042212015-07-09 14:13:29 +010050 "'%s' with the specified job parameters. %s" %
51 (drivername, cls)
Matt Hart132d43c2015-02-25 18:19:53 +000052 )
Matt Hartd4a09982015-07-17 16:13:11 +010053 log.debug("%s accepted the request", willing[0])
Matt Hart132d43c2015-02-25 18:19:53 +000054 return willing[0]
Matt Hart76fb2542014-06-01 14:24:56 +010055
Matt Hart2af71142014-07-22 10:40:41 +010056 def handle(self, request, port_number, delay=0):
Matt Hartd4a09982015-07-17 16:13:11 +010057 log.debug("Driving PDU hostname: %s "
58 "PORT: %s REQUEST: %s (delay %s)",
59 self.hostname, port_number, request, delay)
Matt Hart132d43c2015-02-25 18:19:53 +000060 if request == "on":
Matt Hart2af71142014-07-22 10:40:41 +010061 self.port_on(port_number)
62 elif request == "off":
63 self.port_off(port_number)
64 else:
Matt Hartd4a09982015-07-17 16:13:11 +010065 log.debug("Unknown request to handle - oops")
Matt Hart132d43c2015-02-25 18:19:53 +000066 raise NotImplementedError(
Matt Hartfe042212015-07-09 14:13:29 +010067 "Driver doesn't know how to %s " % request
Matt Hart132d43c2015-02-25 18:19:53 +000068 )
69 self._cleanup()
Matt Hart76fb2542014-06-01 14:24:56 +010070
Matt Hart76fb2542014-06-01 14:24:56 +010071 def port_on(self, port_number):
Matt Hart2af71142014-07-22 10:40:41 +010072 self.port_interaction("on", port_number)
Matt Hart76fb2542014-06-01 14:24:56 +010073
74 def port_off(self, port_number):
Matt Hartfe042212015-07-09 14:13:29 +010075 self.port_interaction("off", port_number)
76
77 def port_interaction(self, command, port_number):
78 pass
79
80 def _bombout(self):
81 pass
82
83 def _cleanup(self):
84 pass