Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame^] | 1 | # 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 Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 6 | import textwrap |
| 7 | from typing import Optional, Union |
| 8 | |
| 9 | from exceptions import ConfigurationError |
| 10 | from logger import EnodebdLogger as logger |
| 11 | |
| 12 | DUPLEX_MAP = { |
| 13 | '01': 'TDDMode', |
| 14 | '02': 'FDDMode', |
| 15 | } |
| 16 | |
| 17 | BANDWIDTH_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 | |
| 26 | BANDWIDTH_MHZ_LIST = {1.4, 3, 5, 10, 15, 20} |
| 27 | |
| 28 | |
| 29 | def duplex_mode(value: str) -> Optional[str]: |
| 30 | return DUPLEX_MAP.get(value) |
| 31 | |
| 32 | |
| 33 | def band_capability(value: str) -> str: |
| 34 | return ','.join([str(int(b, 16)) for b in textwrap.wrap(value, 2)]) |
| 35 | |
| 36 | |
| 37 | def 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 | |
| 54 | def 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 | ) |