1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /* This file contains Cell state update handler for CLA use case. It handles
20 get and change callback for o-ran-sc-du-hello-world yang module */
23 #include "NrCellCb.hpp"
28 /*******************************************************************
30 * @brief override oper_get_items to handle callback
34 * Function : oper_get_items
37 * - override oper_get_items to handle callback of get of
38 * o-ran-sc-du-hello-world yang module
41 * @params[in] sysrepo::S_Session session, const char *module_name,
42 * const char *path, const char *request_xpath,
43 * uint32_t request_id, libyang::S_Data_Node &parent,
45 * @return SR_ERR_OK - success
47 ******************************************************************/
49 int NrCellCb::oper_get_items(sysrepo::S_Session session, \
50 const char *module_name, \
52 const char *request_xpath, \
53 uint32_t request_id, \
54 libyang::S_Data_Node &parent, \
57 O1_LOG("\nO1 NrCellCb : Callback called for path=%s on get request", path);
58 libyang::S_Context ctx = session->get_context();
59 libyang::S_Module mod = ctx->get_module(module_name);
61 //first create root of the tree then add nodes and leaves and fill data
62 parent.reset(new libyang::Data_Node(ctx, CELL_STATE_MODULE_PATH, \
63 nullptr, LYD_ANYDATA_CONSTSTRING, 0));
64 libyang::S_Data_Node connection(new libyang::Data_Node(parent, mod, \
65 "du-to-ru-connection"));
67 libyang::S_Data_Node name;
68 libyang::S_Data_Node operational_state;
69 libyang::S_Data_Node cell_state;
70 libyang::S_Data_Node administrative_state;
72 NrCellList & cellList = NrCellList::instance();
74 const NrCellList::CellOpStateMap & cellOpStateMap = cellList.getCellOpStateList();
75 //read the data from CellOpStateMap
76 std::map<uint16_t, NrCellInfo>::const_iterator it;
77 for(it = cellOpStateMap.begin(); it !=cellOpStateMap.end(); it++)
79 O1_LOG("\nO1 NrCellCb : cellId = %d, opState=%d, cellState=%d", \
80 it->first, (int) it->second.getOpState(), (int) it->second.getCellState());
81 name.reset(new libyang::Data_Node(connection, mod, "name", \
82 to_string(it->first).c_str()));
83 operational_state.reset(new libyang::Data_Node(connection, mod, \
84 "operational-state", \
85 cellInfo.enumToOperationalStateString(it->second.getOpState()).c_str()));
86 cell_state.reset(new libyang::Data_Node(connection, mod, "cell-state", \
87 cellInfo.enumToCellStateString(it->second.getCellState()).c_str()));
92 /*******************************************************************
94 * @brief print changes of given operation
98 * Function : oper_get_items
101 * - print changes of given operation, old and new value
104 * @params[in] sysrepo::S_Change change
106 ******************************************************************/
108 void NrCellCb::printChange(sysrepo::S_Change change) {
109 switch(change->oper()) {
111 if (nullptr != change->new_val()) {
112 O1_LOG("\nO1 NrCellCb : CREATED: %s", \
113 change->new_val()->to_string().c_str());
117 if (nullptr != change->old_val()) {
118 O1_LOG("\nO1 NrCellCb : DELETED: %s", \
119 change->old_val()->to_string().c_str());
123 if (nullptr != change->old_val() && nullptr != change->new_val()) {
124 O1_LOG("\nO1 NrCellCb : MODIFIED: old value %s :new value %s", \
125 change->old_val()->to_string().c_str(), \
126 change->new_val()->to_string().c_str());
130 if (nullptr != change->old_val() && nullptr != change->new_val()) {
131 O1_LOG("\nO1 NrCellCb : MOVED: %s :after %s ", \
132 change->new_val()->xpath(), \
133 change->old_val()->xpath());
135 else if (nullptr != change->new_val()) {
136 O1_LOG("\nO1 NrCellCb : MOVED: %s : first", \
137 change->new_val()->xpath());
143 /*******************************************************************
145 * @brief convert event type to string
152 * - convert event type to string
155 * @params[in] sr_event_t event
156 * @return event name in string form
157 ******************************************************************/
159 /* Helper function for printing events. */
160 const char *NrCellCb::evToStr(sr_event_t ev) {
172 /*******************************************************************
174 * @brief override module_change to handle callback
178 * Function : module_change
181 * - override module_change to handle callback of modification of
182 * o-ran-sc-du-hello-world yang module
185 * @params[in] sysrepo::S_Session session, const char *module_name,
186 * const char *xpath, sr_event_t event, uint32_t request_id,
188 * @return SR_ERR_OK - success
189 * SR_ERR_OK - failure
190 ******************************************************************/
192 int NrCellCb::module_change(sysrepo::S_Session sess, \
193 const char *module_name, \
196 uint32_t request_id, \
199 char change_path[MAX_LEN];
202 O1_LOG("\nO1 NrCellCb : Notification %s", evToStr(event));
203 if (SR_EV_CHANGE == event)
205 NrCellList & cellList = NrCellList::instance();
207 const NrCellList::CellOpStateMap & cellOpStateMap = cellList.getCellOpStateList();
208 snprintf(change_path, MAX_LEN, "/%s:*//.", module_name);
209 auto it = sess->get_changes_iter(change_path);
211 while (auto change = sess->get_change_next(it)) {
212 //printChange(change); //enable only for debugging
213 if(nullptr != change->new_val())
215 O1_LOG("\nO1 NrCellCb : Parameter value has been \
216 changed val=%s", change->new_val()->val_to_string().c_str());
217 std::map<uint16_t, NrCellInfo>::const_iterator it;
218 for(it = cellOpStateMap.begin(); it !=cellOpStateMap.end(); it++)
221 xpath << CELL_STATE_MODULE_PATH << "/du-to-ru-connection[name='" \
222 << it->first << "']/administrative-state";
223 O1_LOG("\nO1 NrCellCb : created xpath = %s", \
224 xpath.str().c_str());
226 if((change->new_val()->to_string().find(xpath.str().c_str()) != \
230 string val = change->new_val()->val_to_string();
231 AdminState newVal = cellInfo.adminStateToEnum(val);
232 O1_LOG("\nO1 NrCellCb : Update admin state \
233 cellId =%d with admin-state value=%s", it->first, val.c_str());
234 if(!setAdminState(it->first, newVal)) {
235 O1_LOG("\nO1 NrCellCb : Could not change \
236 parameter value =%s", change->new_val()->val_to_string().c_str());
237 return SR_ERR_INTERNAL;
243 }//if evToStr(event) check
245 catch( const std::exception& e ) {
246 O1_LOG("\nO1 NrCellCb exception : %s\n", e.what());
251 /*******************************************************************
253 * @brief bring cell status according to the admin state
257 * Function : setAdminState
260 * - bring cell status according to the admin state
263 * @params[in] cellId, Admin state
264 * @return true - success
266 ******************************************************************/
267 bool NrCellCb::setAdminState(uint16_t cellId, AdminState newAdminState)
269 if(newAdminState == UNLOCKED)
271 O1_LOG("\nNrCellList : set Admin State UNLOCKED" );
272 #ifndef ODU_TEST_STUB
273 return bringCellUp(cellId);
278 O1_LOG("\nNrCellList : set Admin State LOCKED" );
279 #ifndef ODU_TEST_STUB
280 return bringCellDown(cellId);
287 /**********************************************************************
289 **********************************************************************/