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