Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import json |
| 15 | from typing import Any, List |
| 16 | |
| 17 | from data_models.data_model import DataModel |
| 18 | from data_models.data_model_parameters import ParameterName |
| 19 | from exceptions import ConfigurationError |
| 20 | from logger import EnodebdLogger as logger |
| 21 | |
| 22 | |
| 23 | class EnodebConfiguration(): |
| 24 | """ |
| 25 | This represents the data model configuration for a single |
| 26 | eNodeB device. This can correspond to either the current configuration |
| 27 | of the device, or what configuration we desire to have for the device. |
| 28 | """ |
| 29 | |
| 30 | def __init__(self, data_model: DataModel) -> None: |
| 31 | """ |
| 32 | The fields initialized in the constructor here should be enough to |
| 33 | track state across any data model configuration. |
| 34 | |
| 35 | Most objects for eNodeB data models cannot be added or deleted. |
| 36 | For those objects, we just track state with a simple mapping from |
| 37 | parameter name to value. |
| 38 | |
| 39 | For objects which can be added/deleted, we track them separately. |
| 40 | """ |
| 41 | |
| 42 | # DataModel |
| 43 | self._data_model = data_model |
| 44 | |
| 45 | # Dict[ParameterName, Any] |
| 46 | self._param_to_value = {} |
| 47 | |
| 48 | # Dict[ParameterName, Dict[ParameterName, Any]] |
| 49 | self._numbered_objects = {} |
| 50 | # If adding a PLMN object, then you would set something like |
| 51 | # self._numbered_objects['PLMN_1'] = {'PLMN_1_ENABLED': True} |
| 52 | |
| 53 | @property |
| 54 | def data_model(self) -> DataModel: |
| 55 | """ |
| 56 | The data model configuration is tied to a single data model |
| 57 | """ |
| 58 | return self._data_model |
| 59 | |
| 60 | def get_parameter_names(self) -> List[ParameterName]: |
| 61 | """ |
| 62 | Returns: list of ParameterName |
| 63 | """ |
| 64 | return list(self._param_to_value.keys()) |
| 65 | |
| 66 | def has_parameter(self, param_name: ParameterName) -> bool: |
| 67 | return param_name in self._param_to_value |
| 68 | |
| 69 | def get_parameter(self, param_name: ParameterName) -> Any: |
| 70 | """ |
| 71 | Args: |
| 72 | param_name: ParameterName |
| 73 | Returns: |
| 74 | Any, value of the parameter, formatted to be understood by enodebd |
| 75 | """ |
| 76 | self._assert_param_in_model(param_name) |
| 77 | return self._param_to_value[param_name] |
| 78 | |
| 79 | def set_parameter( |
| 80 | self, |
| 81 | param_name: ParameterName, |
| 82 | value: Any, |
| 83 | ) -> None: |
| 84 | """ |
| 85 | Args: |
| 86 | param_name: the parameter name to configure |
| 87 | value: the value to set, formatted to be understood by enodebd |
| 88 | """ |
| 89 | self._assert_param_in_model(param_name) |
| 90 | self._param_to_value[param_name] = value |
| 91 | |
| 92 | def delete_parameter(self, param_name: ParameterName) -> None: |
| 93 | del self._param_to_value[param_name] |
| 94 | |
| 95 | def get_object_names(self) -> List[ParameterName]: |
| 96 | return list(self._numbered_objects.keys()) |
| 97 | |
| 98 | def has_object(self, param_name: ParameterName) -> bool: |
| 99 | """ |
| 100 | Args: |
| 101 | param_name: The ParameterName of the object |
| 102 | Returns: True if set in configuration |
| 103 | """ |
| 104 | self._assert_param_in_model(param_name) |
| 105 | return param_name in self._numbered_objects |
| 106 | |
| 107 | def add_object(self, param_name: ParameterName) -> None: |
| 108 | if param_name in self._numbered_objects: |
| 109 | raise ConfigurationError("Configuration already has object") |
| 110 | self._numbered_objects[param_name] = {} |
| 111 | |
| 112 | def delete_object(self, param_name: ParameterName) -> None: |
| 113 | if param_name not in self._numbered_objects: |
| 114 | raise ConfigurationError("Configuration does not have object") |
| 115 | del self._numbered_objects[param_name] |
| 116 | |
| 117 | def get_parameter_for_object( |
| 118 | self, |
| 119 | param_name: ParameterName, |
| 120 | object_name: ParameterName, |
| 121 | ) -> Any: |
| 122 | return self._numbered_objects[object_name].get(param_name) |
| 123 | |
| 124 | def set_parameter_for_object( |
| 125 | self, |
| 126 | param_name: ParameterName, |
| 127 | value: Any, |
| 128 | object_name: ParameterName, |
| 129 | ) -> None: |
| 130 | """ |
| 131 | Args: |
| 132 | param_name: the parameter name to configure |
| 133 | value: the value to set, formatted to be understood by enodebd |
| 134 | object_name: ParameterName of object |
| 135 | """ |
| 136 | self._assert_param_in_model(object_name) |
| 137 | self._assert_param_in_model(param_name) |
| 138 | self._numbered_objects[object_name][param_name] = value |
| 139 | |
| 140 | def get_parameter_names_for_object( |
| 141 | self, |
| 142 | object_name: ParameterName, |
| 143 | ) -> List[ParameterName]: |
| 144 | return list(self._numbered_objects[object_name].keys()) |
| 145 | |
| 146 | def get_debug_info(self) -> str: |
| 147 | debug_info = 'Param values: {}, \n Object values: {}' |
| 148 | return debug_info.format( |
| 149 | json.dumps(self._param_to_value, indent=2), |
| 150 | json.dumps( |
| 151 | self._numbered_objects, |
| 152 | indent=2, |
| 153 | ), |
| 154 | ) |
| 155 | |
| 156 | def _assert_param_in_model(self, param_name: ParameterName) -> None: |
| 157 | trparam_model = self.data_model |
| 158 | tr_param = trparam_model.get_parameter(param_name) |
| 159 | if tr_param is None: |
| 160 | logger.warning('Parameter <%s> not defined in model', param_name) |
Wei-Yu Chen | 5cbdfbb | 2021-12-02 01:10:21 +0800 | [diff] [blame] | 161 | raise ConfigurationError("Parameter %s not defined in model." % param_name) |