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
19"""This module has the global key values that are used across framework
20modules.
21"""
22
23
24class Config(enum.Enum):
25    """Enum values for test config related lookups.
26    """
27    # Keys used to look up values from test config files.
28    # These keys define the wording of test configs and their internal
29    # references.
30    key_log_path = 'logpath'
31    key_testbeds_under_test = 'testbeds_under_test'
32    key_testbed = 'testbed'
33    key_testbed_name = 'name'
34    # configpath is the directory. key_config_full_path is the file path.
35    key_config_path = 'configpath'
36    key_config_full_path = 'config_full_path'
37    key_test_paths = 'testpaths'
38    key_port = 'Port'
39    key_address = 'Address'
40    key_test_case_iterations = 'test_case_iterations'
41    key_test_failure_tracebacks = 'test_failure_tracebacks'
42    # Config names for controllers packaged in ACTS.
43    key_android_device = 'AndroidDevice'
44    key_bluetooth_pts_device = 'BluetoothPtsDevice'
45    key_fuchsia_device = 'FuchsiaDevice'
46    key_buds_device = 'BudsDevice'
47    key_chameleon_device = 'ChameleonDevice'
48    key_native_android_device = 'NativeAndroidDevice'
49    key_relay_device = 'RelayDevice'
50    key_access_point = 'AccessPoint'
51    key_attenuator = 'Attenuator'
52    key_iperf_server = 'IPerfServer'
53    key_iperf_client = 'IPerfClient'
54    key_packet_sender = 'PacketSender'
55    key_monsoon = 'Monsoon'
56    key_sniffer = 'Sniffer'
57    key_arduino_wifi_dongle = 'ArduinoWifiDongle'
58    key_packet_capture = 'PacketCapture'
59    key_pdu = 'PduDevice'
60    # Internal keys, used internally, not exposed to user's config files.
61    ikey_user_param = 'user_params'
62    ikey_testbed_name = 'testbed_name'
63    ikey_logger = 'log'
64    ikey_logpath = 'log_path'
65    ikey_summary_writer = 'summary_writer'
66    # module name of controllers packaged in ACTS.
67    m_key_monsoon = 'monsoon'
68    m_key_android_device = 'android_device'
69    m_key_fuchsia_device = 'fuchsia_device'
70    m_key_bluetooth_pts_device = 'bluetooth_pts_device'
71    m_key_buds_device = 'buds_controller'
72    m_key_chameleon_device = 'chameleon_controller'
73    m_key_native_android_device = 'native_android_device'
74    m_key_relay_device = 'relay_device_controller'
75    m_key_access_point = 'access_point'
76    m_key_attenuator = 'attenuator'
77    m_key_iperf_server = 'iperf_server'
78    m_key_iperf_client = 'iperf_client'
79    m_key_packet_sender = 'packet_sender'
80    m_key_sniffer = 'sniffer'
81    m_key_arduino_wifi_dongle = 'arduino_wifi_dongle'
82    m_key_packet_capture = 'packet_capture'
83    m_key_pdu = 'pdu'
84
85    # A list of keys whose values in configs should not be passed to test
86    # classes without unpacking first.
87    reserved_keys = (key_testbed, key_log_path, key_test_paths)
88
89    # Controller names packaged with ACTS.
90    builtin_controller_names = [
91        key_android_device,
92        key_bluetooth_pts_device,
93        key_fuchsia_device,
94        key_buds_device,
95        key_native_android_device,
96        key_relay_device,
97        key_access_point,
98        key_attenuator,
99        key_iperf_server,
100        key_iperf_client,
101        key_packet_sender,
102        key_monsoon,
103        key_sniffer,
104        key_chameleon_device,
105        key_arduino_wifi_dongle,
106        key_packet_capture,
107        key_pdu
108    ]
109
110    # Keys that are file or folder paths.
111    file_path_keys = [key_relay_device]
112
113
114def get_name_by_value(value):
115    for name, member in Config.__members__.items():
116        if member.value == value:
117            return name
118    return None
119
120
121def get_module_name(name_in_config):
122    """Translates the name of a controller in config file to its module name.
123    """
124    return value_to_value(name_in_config, 'm_%s')
125
126
127def value_to_value(ref_value, pattern):
128    """Translates the value of a key to the value of its corresponding key. The
129    corresponding key is chosen based on the variable name pattern.
130    """
131    ref_key_name = get_name_by_value(ref_value)
132    if not ref_key_name:
133        return None
134    target_key_name = pattern % ref_key_name
135    try:
136        return getattr(Config, target_key_name).value
137    except AttributeError:
138        return None
139