blob: 97ce3997b76e0b32743b75ed40b3d29fb4cbafe6 [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 Chen8d064162022-05-27 21:06:55 +08006from distutils.sysconfig import customize_compiler
Wei-Yu Chen49950b92021-11-08 19:19:18 +08007import json
8from collections import namedtuple
9from typing import Any, Optional, Union
10
11from lte.protos.mconfig import mconfigs_pb2
12from common.misc_utils import get_ip_from_if
13from configuration.exceptions import LoadConfigError
Wei-Yu Chen5cbdfbb2021-12-02 01:10:21 +080014from configuration.service_configs import load_enb_config, load_common_config
Wei-Yu Chen49950b92021-11-08 19:19:18 +080015from configuration.mconfig_managers import load_service_mconfig_as_json
16from data_models.data_model import DataModel
17from data_models.data_model_parameters import ParameterName
18from device_config.enodeb_config_postprocessor import (
19 EnodebConfigurationPostProcessor,
20)
21from device_config.enodeb_configuration import EnodebConfiguration
22from exceptions import ConfigurationError
23from logger import EnodebdLogger as logger
24from lte_utils import (
25 DuplexMode,
26 map_earfcndl_to_band_earfcnul_mode,
27 map_earfcndl_to_duplex_mode,
28)
29
30# LTE constants
31DEFAULT_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.
34DEFAULT_CELL_IDENTITY = 138777000
35
36SingleEnodebConfig = 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
49def 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
56def build_desired_config(
Wei-Yu Chen49950b92021-11-08 19:19:18 +080057 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 Chen8d064162022-05-27 21:06:55 +080068 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 Chen49950b92021-11-08 19:19:18 +080071 Returns:
72 Desired data model configuration for the device
73 """
Wei-Yu Chen5cbdfbb2021-12-02 01:10:21 +080074
Wei-Yu Chen8d064162022-05-27 21:06:55 +080075 # 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 Chen5cbdfbb2021-12-02 01:10:21 +080080
Wei-Yu Chen8d064162022-05-27 21:06:55 +080081 # 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 Chen49950b92021-11-08 19:19:18 +080085
Wei-Yu Chen8d064162022-05-27 21:06:55 +080086 post_processor.postprocess(desired_configuration)
Wei-Yu Chen49950b92021-11-08 19:19:18 +080087
Wei-Yu Chen8d064162022-05-27 21:06:55 +080088 return desired_configuration
Wei-Yu Chen49950b92021-11-08 19:19:18 +080089
90
91def _get_enb_config(
Wei-Yu Chen8d064162022-05-27 21:06:55 +080092 device_config: EnodebConfiguration,
93) -> dict:
Wei-Yu Chen5cbdfbb2021-12-02 01:10:21 +080094 # 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 Chen8d064162022-05-27 21:06:55 +0800101 def flatten_dictionary(dictionary: dict) -> dict:
102 return {k: v for item in dictionary.values() for k, v in item.items()}
Wei-Yu Chen49950b92021-11-08 19:19:18 +0800103
Wei-Yu Chen5cbdfbb2021-12-02 01:10:21 +0800104 params_dict = dict()
105
Wei-Yu Chen8d064162022-05-27 21:06:55 +0800106 # 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 Chen5cbdfbb2021-12-02 01:10:21 +0800111 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 Chen8d064162022-05-27 21:06:55 +0800115 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 Chen5cbdfbb2021-12-02 01:10:21 +0800120
Wei-Yu Chen8d064162022-05-27 21:06:55 +0800121 return generated_config