• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - 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"""
22class Config(enum.Enum):
23    """Enum values for test config related lookups.
24    """
25    # Keys used to look up values from test config files.
26    # These keys define the wording of test configs and their internal
27    # references.
28    key_log_path = "logpath"
29    key_testbed = "testbed"
30    key_testbed_name = "name"
31    key_config_path = "configpath"
32    key_test_paths = "testpaths"
33    key_port = "Port"
34    key_address = "Address"
35    # Config names for controllers packaged in ACTS.
36    key_android_device = "AndroidDevice"
37    key_native_android_device = "NativeAndroidDevice"
38    key_access_point = "AP"
39    key_attenuator = "Attenuator"
40    key_iperf_server = "IPerfServer"
41    key_monsoon = "Monsoon"
42    key_sniffer = "Sniffer"
43    # Internal keys, used internally, not exposed to user's config files.
44    ikey_user_param = "user_params"
45    ikey_testbed_name = "testbed_name"
46    ikey_logger = "log"
47    ikey_logpath = "log_path"
48    ikey_cli_args = "cli_args"
49    # module name of controllers packaged in ACTS.
50    m_key_monsoon = "monsoon"
51    m_key_android_device = "android_device"
52    m_key_native_android_device = "native_android_device"
53    m_key_access_point = "access_point"
54    m_key_attenuator = "attenuator"
55    m_key_iperf_server = "iperf_server"
56    m_key_sniffer = "sniffer"
57
58    # A list of keys whose values in configs should not be passed to test
59    # classes without unpacking first.
60    reserved_keys = (key_testbed, key_log_path, key_test_paths)
61
62    # Controller names packaged with ACTS.
63    builtin_controller_names = [
64        key_android_device,
65        key_native_android_device,
66        key_access_point,
67        key_attenuator,
68        key_iperf_server,
69        key_monsoon,
70        key_sniffer
71    ]
72
73def get_name_by_value(value):
74    for name, member in Config.__members__.items():
75        if member.value == value:
76            return name
77    return None
78
79def get_internal_value(external_value):
80    """Translates the value of an external key to the value of its
81    corresponding internal key.
82    """
83    return value_to_value(external_value, "i%s")
84
85def get_module_name(name_in_config):
86    """Translates the name of a controller in config file to its module name.
87    """
88    return value_to_value(name_in_config, "m_%s")
89
90def value_to_value(ref_value, pattern):
91    """Translates the value of a key to the value of its corresponding key. The
92    corresponding key is chosen based on the variable name pattern.
93    """
94    ref_key_name = get_name_by_value(ref_value)
95    if not ref_key_name:
96        return None
97    target_key_name = pattern % ref_key_name
98    try:
99        return getattr(Config, target_key_name).value
100    except AttributeError:
101        return None
102