• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2017 The Chromium OS Authors. All rights reserved.
2# # Use of this source code is governed by a BSD-style license that can be
3# # found in the LICENSE file.
4
5import itertools
6
7from autotest_lib.server import frontend
8from autotest_lib.server import site_utils
9
10
11CLIENT_BOX_STR = 'client_box_'
12RF_SWITCH_STR = 'rf_switch_'
13RF_SWITCH_DUT = 'rf_switch_dut'
14RF_SWITCH_CLIENT = 'rf_switch_client'
15
16
17class ClientBox(object):
18    """Class to manage devices in the Client Box."""
19
20
21    def __init__(self, client_box_host):
22        """Constructor for the ClientBox.
23
24        @param client_box_host: Client Box AFE Host."""
25        self.client_box_host = client_box_host
26        self.labels = self.client_box_host.labels
27        for s in self.labels:
28            if s.startswith(CLIENT_BOX_STR):
29                self.client_box_label = s
30        self.devices = self._get_devices()
31
32
33    def _get_devices(self):
34        """Return all devices in the Client Box.
35
36        @returns a list of autotest_lib.server.frontend.Host objects."""
37        rf_switch_label = ''
38        for label in self.labels:
39            if label.startswith(RF_SWITCH_STR) and (
40                    label is not RF_SWITCH_CLIENT):
41                rf_switch_label = label
42        afe = frontend.AFE(
43                debug=True, server=site_utils.get_global_afe_hostname())
44        hosts = afe.get_hosts(label=self.client_box_label)
45        devices = []
46        for host in hosts:
47            labels = list(itertools.ifilter(
48                    lambda x: x in host.labels, [RF_SWITCH_DUT, rf_switch_label]))
49            # If host has both labels, then add to the devices list.
50            if len(labels) == 2:
51                devices.append(host)
52        return devices
53
54
55    def get_devices(self):
56        """Return all devices in the Client Box.
57
58        @returns a list of autotest_lib.server.frontend.Host objects."""
59        return self.devices
60