e97358b49fcc686660d447ce7a176e160273e7de
[o-du/l2.git] / src / o1 / NrCellCb.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [HCL Technologies Ltd.]                          #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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 *******************************************************************************/
18
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  */
21
22 #include <sstream>
23 #include "NrCellCb.hpp"
24 #define MAX_LEN 100
25
26 using namespace std;
27
28 /*******************************************************************
29  *
30  * @brief override oper_get_items to handle callback
31  *
32  * @details
33  *
34  *    Function : oper_get_items
35  *
36  *    Functionality:
37  *      - override oper_get_items to handle callback of get of
38  *        o-ran-sc-du-hello-world yang module
39  *
40  *
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,
44  *             void *private_data
45  * @return SR_ERR_OK  - success
46  *         SR_ERR_OK  - failure
47  ******************************************************************/
48
49 int NrCellCb::oper_get_items(sysrepo::S_Session session, \
50                                        const char *module_name, \
51                                        const char *path, \
52                                        const char *request_xpath, \
53                                        uint32_t request_id, \
54                                        libyang::S_Data_Node &parent, \
55                                        void *private_data)
56 {
57    O1_LOG("O1 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);
60
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"));
66
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;
71
72    NrCellList & cellList  = NrCellList::instance();
73    NrCellInfo cellInfo;
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++)
78    {
79       O1_LOG("\nO1 NrCellCb : cellId = %d, opState=%d, cellState=%d\n", \
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()));
88    }
89    return SR_ERR_OK;
90 }
91
92 /*******************************************************************
93  *
94  * @brief print changes of given operation
95  *
96  * @details
97  *
98  *    Function : oper_get_items
99  *
100  *    Functionality:
101  *      -  print changes of given operation, old and new value
102  *
103  *
104  * @params[in] sysrepo::S_Change change
105  * @return void
106  ******************************************************************/
107
108 void NrCellCb::printChange(sysrepo::S_Change change) {
109    switch(change->oper()) {
110       case SR_OP_CREATED:
111          if (nullptr != change->new_val()) {
112             O1_LOG("O1 NrCellCb : CREATED: %s", \
113                     change->new_val()->to_string().c_str());
114          }
115          break;
116       case SR_OP_DELETED:
117          if (nullptr != change->old_val()) {
118              O1_LOG("O1 NrCellCb : DELETED:  %s", \
119                      change->old_val()->to_string().c_str());
120          }
121          break;
122       case SR_OP_MODIFIED:
123          if (nullptr != change->old_val() && nullptr != change->new_val()) {
124             O1_LOG("O1 NrCellCb : MODIFIED: old value %s :new value %s", \
125                     change->old_val()->to_string().c_str(), \
126                     change->new_val()->to_string().c_str());
127         }
128         break;
129      case SR_OP_MOVED:
130         if (nullptr != change->old_val() && nullptr != change->new_val()) {
131            O1_LOG("O1 NrCellCb : MOVED: %s :after %s ", \
132                     change->new_val()->xpath(), \
133                     change->old_val()->xpath());
134         }
135         else if (nullptr != change->new_val()) {
136            O1_LOG("O1 NrCellCb : MOVED: %s : first\n", \
137                    change->new_val()->xpath());
138         }
139         break;
140     }
141 }
142
143 /*******************************************************************
144  *
145  * @brief convert event type to string
146  *
147  * @details
148  *
149  *    Function : evToStr
150  *
151  *    Functionality:
152  *      - convert event type to string
153  *
154  *
155  * @params[in] sr_event_t event
156  * @return event name in string form
157  ******************************************************************/
158
159 /* Helper function for printing events. */
160 const char *NrCellCb::evToStr(sr_event_t ev) {
161    switch (ev) {
162       case SR_EV_CHANGE:
163          return "change";
164       case SR_EV_DONE:
165          return "done";
166       case SR_EV_ABORT:
167       default:
168          return "abort";
169    }
170 }
171
172 /*******************************************************************
173  *
174  * @brief override module_change to handle callback
175  *
176  * @details
177  *
178  *    Function : module_change
179  *
180  *    Functionality:
181  *      - override module_change to handle callback of modification of
182  *        o-ran-sc-du-hello-world yang module
183  *
184  *
185  * @params[in] sysrepo::S_Session session, const char *module_name,
186  *             const char *xpath, sr_event_t event, uint32_t request_id,
187  *             void *private_data
188  * @return SR_ERR_OK  - success
189  *         SR_ERR_OK  - failure
190  ******************************************************************/
191
192 int NrCellCb::module_change(sysrepo::S_Session sess, \
193                                     const char *module_name, \
194                                     const char *xpath, \
195                                     sr_event_t event, \
196                                     uint32_t request_id, \
197                                     void *private_data)
198 {
199    char change_path[MAX_LEN];
200
201    try {
202       O1_LOG("O1 NrCellCb : Notification %s\n", evToStr(event));
203       if (SR_EV_CHANGE == event)
204       {
205          NrCellList & cellList  = NrCellList::instance();
206          NrCellInfo cellInfo;
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);
210          uint16_t cellId;
211          while (auto change = sess->get_change_next(it)) {
212          //printChange(change); //enable only for debugging
213          if(nullptr != change->new_val())
214          {
215             O1_LOG("O1 NrCellCb : Parameter value has been \
216 changed val=%s\n", 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++)
219             {
220                stringstream xpath;
221                xpath << CELL_STATE_MODULE_PATH << "/du-to-ru-connection[name='" \
222                << it->first << "']/administrative-state";
223                O1_LOG("O1 NrCellCb : created xpath = %s", \
224                       xpath.str().c_str());
225
226                if((change->new_val()->to_string().find(xpath.str().c_str()) != \
227                    std::string::npos))
228                {
229                   printChange(change);
230                   string val = change->new_val()->val_to_string();
231                   AdminState newVal = cellInfo.adminStateToEnum(val);
232                   O1_LOG("O1 NrCellCb : Update admin state \
233 cellId =%d with admin-state value=%s\n", it->first, val.c_str());
234                  if(!setAdminState(it->first, newVal)) {
235                     O1_LOG("O1 NrCellCb : Could not change \
236 parameter value =%s\n", change->new_val()->val_to_string().c_str());
237                     return SR_ERR_INTERNAL;
238                  }
239                }
240             }
241          }
242      }
243    }//if evToStr(event) check
244    }
245    catch( const std::exception& e ) {
246       O1_LOG("exception : %s\n", e.what());
247    }
248    return SR_ERR_OK;
249 }
250
251 /*******************************************************************
252  *
253  * @brief bring cell status according to the admin state
254  *
255  * @details
256  *
257  *    Function : setAdminState
258  *
259  *    Functionality:
260  *      - bring cell status according to the admin state
261  *
262  *
263  * @params[in] cellId, Admin state
264  * @return true   - success
265  *         false  - failure
266  ******************************************************************/
267 bool NrCellCb::setAdminState(uint16_t cellId, AdminState newAdminState)
268 {
269    if(newAdminState == UNLOCKED)
270    {
271       O1_LOG("\nNrCellList : set Admin State UNLOCKED" );
272 #ifndef ODU_TEST_STUB
273       return bringCellUp(cellId);
274 #endif
275    }
276    else
277    {
278       O1_LOG("\nNrCellList : set Admin State LOCKED" );
279 #ifndef ODU_TEST_STUB
280       return bringCellDown(cellId);
281 #endif
282    }
283    return true;
284 }
285
286
287 /**********************************************************************
288          End of file
289 **********************************************************************/
290