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