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 | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 6 | from distutils.sysconfig import customize_compiler |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 7 | import json |
| 8 | from collections import namedtuple |
| 9 | from typing import Any, Optional, Union |
| 10 | |
| 11 | from lte.protos.mconfig import mconfigs_pb2 |
| 12 | from common.misc_utils import get_ip_from_if |
| 13 | from configuration.exceptions import LoadConfigError |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 14 | from configuration.service_configs import load_enb_config, load_common_config |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 15 | from configuration.mconfig_managers import load_service_mconfig_as_json |
| 16 | from data_models.data_model import DataModel |
| 17 | from data_models.data_model_parameters import ParameterName |
| 18 | from device_config.enodeb_config_postprocessor import ( |
| 19 | EnodebConfigurationPostProcessor, |
| 20 | ) |
| 21 | from device_config.enodeb_configuration import EnodebConfiguration |
| 22 | from exceptions import ConfigurationError |
| 23 | from logger import EnodebdLogger as logger |
| 24 | from lte_utils import ( |
| 25 | DuplexMode, |
| 26 | map_earfcndl_to_band_earfcnul_mode, |
| 27 | map_earfcndl_to_duplex_mode, |
| 28 | ) |
| 29 | |
| 30 | # LTE constants |
| 31 | DEFAULT_S1_PORT = 36412 |
| 32 | # This is a known working value for supported eNB devices. |
| 33 | # Cell Identity is a 28 bit number, but not all values are supported. |
| 34 | DEFAULT_CELL_IDENTITY = 138777000 |
| 35 | |
| 36 | SingleEnodebConfig = namedtuple( |
| 37 | 'SingleEnodebConfig', |
| 38 | [ |
| 39 | 'earfcndl', 'subframe_assignment', |
| 40 | 'special_subframe_pattern', |
| 41 | 'pci', 'plmnid_list', 'tac', |
| 42 | 'bandwidth_mhz', 'cell_id', |
| 43 | 'allow_enodeb_transmit', |
| 44 | 'mme_address', 'mme_port', |
| 45 | ], |
| 46 | ) |
| 47 | |
| 48 | |
| 49 | def config_assert(condition: bool, message: str = None) -> None: |
| 50 | """ To be used in place of 'assert' so that ConfigurationError is raised |
| 51 | for all config-related exceptions. """ |
| 52 | if not condition: |
| 53 | raise ConfigurationError(message) |
| 54 | |
| 55 | |
| 56 | def build_desired_config( |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 57 | device_config: EnodebConfiguration, |
| 58 | data_model: DataModel, |
| 59 | post_processor: EnodebConfigurationPostProcessor, |
| 60 | ) -> EnodebConfiguration: |
| 61 | """ |
| 62 | Factory for initializing DESIRED data model configuration. |
| 63 | |
| 64 | When working with the configuration of an eNodeB, we track the |
| 65 | current state of configuration for that device, as well as what |
| 66 | configuration we want to set on the device. |
| 67 | Args: |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 68 | device_config: The current configuration of the device. |
| 69 | data_model: The data model for the device. |
| 70 | post_processor: The post-processor to use for the device. |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 71 | Returns: |
| 72 | Desired data model configuration for the device |
| 73 | """ |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 74 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 75 | # The configuration we want to push to the target device |
| 76 | desired_configuration = EnodebConfiguration(data_model) |
| 77 | # The configuration read from local configuration files by serial number specific |
| 78 | customized_configuration = _get_enb_config(device_config) |
| 79 | # device_config is the current configuration read from the target device |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 80 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 81 | # Check if the customized configuration is valid |
| 82 | desired_configuration.check_desired_configuration(device_config, customized_configuration) |
| 83 | # Apply the customized configuration to the target device |
| 84 | desired_configuration.apply_desired_configuration(device_config, customized_configuration) |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 85 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 86 | post_processor.postprocess(desired_configuration) |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 87 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 88 | return desired_configuration |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 89 | |
| 90 | |
| 91 | def _get_enb_config( |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 92 | device_config: EnodebConfiguration, |
| 93 | ) -> dict: |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 94 | # The eNodeB parameters to be generated with default value, |
| 95 | # It will load from eNB configs based on serial number or default value |
| 96 | # The params is a nested list which contains 2 format of parameter names. |
| 97 | # The first parameter is the name of eNB / ACS configuration in |
| 98 | # magma_configs/serial_number/ and magma_configs/acs_common.yml |
| 99 | # The second parameter is the name of gateway configuration in |
| 100 | # override_configs/gateway.mconfig |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 101 | def flatten_dictionary(dictionary: dict) -> dict: |
| 102 | return {k: v for item in dictionary.values() for k, v in item.items()} |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 103 | |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 104 | params_dict = dict() |
| 105 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 106 | # The common configuration file is loaded from magma_configs/acs_common.yml |
| 107 | common_config = flatten_dictionary(load_common_config()) |
| 108 | |
| 109 | # The serial number configuration file is loaded from magma_configs/serial_number/ |
| 110 | # Get the specific configuration with the serial number as the key |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 111 | enb_configs = load_enb_config() |
| 112 | enb_serial = device_config.get_parameter(ParameterName.SERIAL_NUMBER) |
| 113 | enb_config = enb_configs.get(enb_serial, dict()) |
| 114 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 115 | generated_config = dict() |
| 116 | for parameter_name in ParameterName.all_paramters(): |
| 117 | parameter_value = enb_config.get(parameter_name, common_config.get(parameter_name)) |
| 118 | if parameter_value is not None: |
| 119 | generated_config[parameter_name] = parameter_value |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 120 | |
Wei-Yu Chen | 8d06416 | 2022-05-27 21:06:55 +0800 | [diff] [blame] | 121 | return generated_config |