blob: e7c2b81139c5f54028765fe8047d5d148cc354d3 [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# SPDX-FileCopyrightText: 2020 The Magma Authors.
2# SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org>
3#
4# SPDX-License-Identifier: BSD-3-Clause
Wei-Yu Chen49950b92021-11-08 19:19:18 +08005
Wei-Yu Chen49950b92021-11-08 19:19:18 +08006import textwrap
7from typing import Optional, Union
8
9from exceptions import ConfigurationError
10from logger import EnodebdLogger as logger
11
12DUPLEX_MAP = {
13 '01': 'TDDMode',
14 '02': 'FDDMode',
15}
16
17BANDWIDTH_RBS_TO_MHZ_MAP = {
18 'n6': 1.4,
19 'n15': 3,
20 'n25': 5,
21 'n50': 10,
22 'n75': 15,
23 'n100': 20,
24}
25
26BANDWIDTH_MHZ_LIST = {1.4, 3, 5, 10, 15, 20}
27
28
29def duplex_mode(value: str) -> Optional[str]:
30 return DUPLEX_MAP.get(value)
31
32
33def band_capability(value: str) -> str:
34 return ','.join([str(int(b, 16)) for b in textwrap.wrap(value, 2)])
35
36
37def gps_tr181(value: str) -> str:
38 """Convert GPS value (lat or lng) to float
39
40 Per TR-181 specification, coordinates are returned in degrees,
41 multiplied by 1,000,000.
42
43 Args:
44 value (string): GPS value (latitude or longitude)
45 Returns:
46 str: GPS value (latitude/longitude) in degrees
47 """
48 try:
49 return str(float(value) / 1e6)
50 except Exception: # pylint: disable=broad-except
51 return value
52
53
54def bandwidth(bandwidth_rbs: Union[str, int, float]) -> float:
55 """
56 Map bandwidth in number of RBs to MHz
57 TODO: TR-196 spec says this should be '6' rather than 'n6', but
58 BaiCells eNodeB uses 'n6'. Need to resolve this.
59
60 Args:
61 bandwidth_rbs (str): Bandwidth in number of RBs
62 Returns:
63 str: Bandwidth in MHz
64 """
65 if bandwidth_rbs in BANDWIDTH_RBS_TO_MHZ_MAP:
66 return BANDWIDTH_RBS_TO_MHZ_MAP[bandwidth_rbs]
67
68 logger.warning('Unknown bandwidth_rbs (%s)', str(bandwidth_rbs))
69 if bandwidth_rbs in BANDWIDTH_MHZ_LIST:
70 return bandwidth_rbs
71 elif isinstance(bandwidth_rbs, str):
72 mhz = None
73 if bandwidth_rbs.isdigit():
74 mhz = int(bandwidth_rbs)
75 elif bandwidth_rbs.replace('.', '', 1).isdigit():
76 mhz = float(bandwidth_rbs)
77 if mhz in BANDWIDTH_MHZ_LIST:
78 return mhz
79 raise ConfigurationError(
80 'Unknown bandwidth specification (%s)' %
81 str(bandwidth_rbs),
82 )