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 common 6import itertools 7 8from autotest_lib.server import frontend 9from autotest_lib.server import site_utils 10 11 12CLIENT_BOX_STR = 'client_box_' 13RF_SWITCH_STR = 'rf_switch_' 14RF_SWITCH_DUT = 'rf_switch_dut' 15RF_SWITCH_CLIENT = 'rf_switch_client' 16 17 18class ClientBoxException(Exception): 19 pass 20 21 22class ClientBox(object): 23 """Class to manage devices in the Client Box.""" 24 25 26 def __init__(self, client_box_host): 27 """Constructor for the ClientBox. 28 29 @param client_box_host: Client Box AFE Host. 30 """ 31 self.client_box_host = client_box_host 32 self.client_box_label = '' 33 self.rf_switch_label = '' 34 for label in client_box_host.labels: 35 if label.startswith(CLIENT_BOX_STR): 36 self.client_box_label = label 37 elif label.startswith(RF_SWITCH_STR) and ( 38 label is not RF_SWITCH_CLIENT): 39 self.rf_switch_label = label 40 if not self.client_box_label or not self.rf_switch_label: 41 msg = 'CleintBoxLabels: %s \t RfSwitchLabels: %s' % ( 42 self.client_box_label, self.rf_switch_label) 43 raise ClientBoxException( 44 'Labels not found:: %s' % msg) 45 self.devices = None 46 47 48 def get_devices(self): 49 """Return all devices in the Client Box. 50 51 @returns a list of autotest_lib.server.frontend.Host objects. 52 """ 53 if self.devices is None: 54 self.devices = self.get_devices_using_labels([RF_SWITCH_DUT]) 55 return self.devices 56 57 def get_devices_using_labels(self, labels): 58 """Returns all devices with the passed labels in the Client Box. 59 60 @params labels: List of host labels. 61 62 @returns a list of string containing the hostnames. 63 """ 64 afe = frontend.AFE( 65 debug=True, server=site_utils.get_global_afe_hostname()) 66 hosts = afe.get_hosts(label=self.client_box_label) 67 labels.append(self.rf_switch_label) 68 devices = [] 69 for host in hosts: 70 labels_list = list(itertools.ifilter( 71 lambda x: x in host.labels, labels)) 72 if len(labels) == len(labels_list): 73 devices.append(host.hostname) 74 return devices 75 76