1#!/usr/bin/env python3
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import enum
18"""This module has the global key values that are used across framework
19modules.
20"""
21
22
23class Config(enum.Enum):
24    """Enum values for test config related lookups.
25    """
26    # Keys used to look up values from test config files.
27    # These keys define the wording of test configs and their internal
28    # references.
29    key_log_path = 'logpath'
30    key_testbeds_under_test = 'testbeds_under_test'
31    key_testbed = 'testbed'
32    key_testbed_name = 'name'
33    # configpath is the directory. key_config_full_path is the file path.
34    key_config_path = 'configpath'
35    key_config_full_path = 'config_full_path'
36    key_test_paths = 'testpaths'
37    key_port = 'Port'
38    key_address = 'Address'
39    key_test_case_iterations = 'test_case_iterations'
40    key_test_failure_tracebacks = 'test_failure_tracebacks'
41    # Config names for controllers packaged in ACTS.
42    key_android_device = 'AndroidDevice'
43    key_bluetooth_pts_device = 'BluetoothPtsDevice'
44    key_fuchsia_device = 'FuchsiaDevice'
45    key_buds_device = 'BudsDevice'
46    key_chameleon_device = 'ChameleonDevice'
47    key_native_android_device = 'NativeAndroidDevice'
48    key_relay_device = 'RelayDevice'
49    key_access_point = 'AccessPoint'
50    key_attenuator = 'Attenuator'
51    key_iperf_server = 'IPerfServer'
52    key_iperf_client = 'IPerfClient'
53    key_packet_sender = 'PacketSender'
54    key_monsoon = 'Monsoon'
55    key_sniffer = 'Sniffer'
56    key_arduino_wifi_dongle = 'ArduinoWifiDongle'
57    key_packet_capture = 'PacketCapture'
58    # Internal keys, used internally, not exposed to user's config files.
59    ikey_user_param = 'user_params'
60    ikey_testbed_name = 'testbed_name'
61    ikey_logger = 'log'
62    ikey_logpath = 'log_path'
63    ikey_summary_writer = 'summary_writer'
64    # module name of controllers packaged in ACTS.
65    m_key_monsoon = 'monsoon'
66    m_key_android_device = 'android_device'
67    m_key_fuchsia_device = 'fuchsia_device'
68    m_key_bluetooth_pts_device = 'bluetooth_pts_device'
69    m_key_buds_device = 'buds_controller'
70    m_key_chameleon_device = 'chameleon_controller'
71    m_key_native_android_device = 'native_android_device'
72    m_key_relay_device = 'relay_device_controller'
73    m_key_access_point = 'access_point'
74    m_key_attenuator = 'attenuator'
75    m_key_iperf_server = 'iperf_server'
76    m_key_iperf_client = 'iperf_client'
77    m_key_packet_sender = 'packet_sender'
78    m_key_sniffer = 'sniffer'
79    m_key_arduino_wifi_dongle = 'arduino_wifi_dongle'
80    m_key_packet_capture = 'packet_capture'
81
82    # A list of keys whose values in configs should not be passed to test
83    # classes without unpacking first.
84    reserved_keys = (key_testbed, key_log_path, key_test_paths)
85
86    # Controller names packaged with ACTS.
87    builtin_controller_names = [
88        key_android_device,
89        key_bluetooth_pts_device,
90        key_fuchsia_device,
91        key_buds_device,
92        key_native_android_device,
93        key_relay_device,
94        key_access_point,
95        key_attenuator,
96        key_iperf_server,
97        key_iperf_client,
98        key_packet_sender,
99        key_monsoon,
100        key_sniffer,
101        key_chameleon_device,
102        key_arduino_wifi_dongle,
103        key_packet_capture,
104    ]
105
106    # Keys that are file or folder paths.
107    file_path_keys = [key_relay_device]
108
109
110def get_name_by_value(value):
111    for name, member in Config.__members__.items():
112        if member.value == value:
113            return name
114    return None
115
116
117def get_module_name(name_in_config):
118    """Translates the name of a controller in config file to its module name.
119    """
120    return value_to_value(name_in_config, 'm_%s')
121
122
123def value_to_value(ref_value, pattern):
124    """Translates the value of a key to the value of its corresponding key. The
125    corresponding key is chosen based on the variable name pattern.
126    """
127    ref_key_name = get_name_by_value(ref_value)
128    if not ref_key_name:
129        return None
130    target_key_name = pattern % ref_key_name
131    try:
132        return getattr(Config, target_key_name).value
133    except AttributeError:
134        return None
135