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 ConfigParser 6import logging 7import os 8 9from autotest_lib.server.cros import ap_config 10 11AP_BOX_STR = 'ap_box_' 12RF_SWITCH_STR = 'rf_switch_' 13RF_SWITCH_APS = 'rf_switch_aps' 14FILE_NAME = '%s_%s_ap_list.conf' 15 16 17class APBoxException(Exception): 18 pass 19 20 21class APBox(object): 22 """Class to manage APs in an AP Box.""" 23 24 25 def __init__(self, ap_box_host): 26 """Constructor for the AP Box. 27 28 @param ap_box_host: AP Box AFE Host object.""" 29 self.ap_box_host = ap_box_host 30 labels = self.ap_box_host.labels 31 self.ap_box_label = '' 32 self.rf_switch_label = '' 33 for label in labels: 34 if label.startswith(AP_BOX_STR): 35 self.ap_box_label = label 36 elif label.startswith(RF_SWITCH_STR) and ( 37 label is not RF_SWITCH_APS): 38 self.rf_switch_label = label 39 if not self.ap_box_label or not self.rf_switch_label: 40 raise APBoxException( 41 'AP Box %s does not have ap_box and/or rf_switch labels' % 42 self.ap_box_host.hostname) 43 self.aps = self._get_ap_list() 44 45 46 def _get_ap_list(self): 47 """Returns a list of all APs in the AP Box. 48 49 @returns a list of autotest_lib.server.cros.AP objects. 50 """ 51 aps = [] 52 # FILE_NAME is formed using rf_switch and ap_box labels. 53 # for example, rf_switch_1 and ap_box_1, the configuration 54 # filename is rf_switch_1_ap_box_1_ap_list.conf 55 file_name = FILE_NAME % ( 56 self.rf_switch_label.lower(), self.ap_box_label.lower()) 57 ap_config_parser = ConfigParser.RawConfigParser() 58 path = os.path.join( 59 os.path.dirname(os.path.abspath(__file__)), '..', 60 file_name) 61 logging.debug('Reading the static configurations from %s', path) 62 ap_config_parser.read(path) 63 for bss in ap_config_parser.sections(): 64 aps.append(ap_config.AP(bss, ap_config_parser)) 65 return aps 66 67 68 def get_ap_list(self): 69 """Returns a list of all APs in the AP Box. 70 71 @returns a list of autotest_lib.server.cros.AP objects. 72 """ 73 return self.aps 74