Integration issues of o1
[o-du/l2.git] / src / o1 / RrmPolicyCb.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 implementation of RRM Policy get and update handler along
20  * with some supporting funtion. It handles get and change callback for
21  * rrmpolicy yang module  */
22
23 #include <sstream>
24 #include "RrmPolicyCb.hpp"
25
26 RrmPolicyList rrmPolicy[MAX_POLICY];
27
28 uint8_t policyNum = 0;
29 uint8_t policyIndex = 0;
30 uint8_t policyMemberListNum =0;
31
32 /*******************************************************************
33  *
34  * @brief override oper_get_items to handle callback
35  *
36  * @details
37  *
38  *    Function : oper_get_items
39  *
40  *    Functionality:
41  *      - override oper_get_items to handle callback of get of
42  *        o-ran-sc-du-hello-world yang module
43  *
44  *
45  * @params[in] sysrepo::S_Session session, const char *module_name,
46  *             const char *path, const char *request_xpath,
47  *             uint32_t request_id, libyang::S_Data_Node &parent,
48  *             void *private_data
49  * @return SR_ERR_OK  - success
50  *         SR_ERR_OK  - failure
51  ******************************************************************/
52
53 int RrmPolicyCb::oper_get_items(sysrepo::S_Session session, \
54                                        const char *module_name, \
55                                        const char *path, \
56                                        const char *request_xpath, \
57                                        uint32_t request_id, \
58                                        libyang::S_Data_Node &parent, \
59                                        void *private_data)
60 {
61    O1_LOG("\nO1 RrmPolicyCb : Callback called for path=%s on get request", \
62              path);
63    return SR_ERR_OK;
64 }
65
66 /*******************************************************************
67  *
68  * @brief convert string resouce type to enum
69  *
70  * @details
71  *
72  *    Function : resourceTypeToEnum
73  *
74  *    Functionality:
75  *      - convert string resouce type to enum
76  *
77  *
78  * @params[in] string resouceType
79  * @return Enum resouceType
80  ******************************************************************/
81 RrmResourceType RrmPolicyCb::resourceTypeToEnum(string &val)
82 {
83    RrmResourceType ret;
84
85    if(val == "PRB")
86    {
87       ret = PRB;
88    }
89    else if(val == "PRB_UL")
90    {
91       ret = PRB_UL;
92    }
93    else if(val == "PRB_DL")
94    {
95       ret = PRB_DL;
96    }
97    else if(val == "RRC")
98    {
99       ret = RRC;
100    }
101    else if(val == "DRB")
102    {
103       ret = DRB;
104    }
105    else
106    {
107       O1_LOG("\nO1 RrmPolicyCb::updateParams non handeled value of "
108              "ResourceType = %s", val.c_str());
109    }
110    O1_LOG("\nO1 rrmPolicy resourceTypeToEnum : val [%d]", ret);
111    return ret;
112 }
113
114 /*******************************************************************
115  *
116  * @brief fill the parameters value into the structure instance
117  *
118  * @details
119  *
120  *    Function : updateParams
121  *
122  *    Functionality:
123  *      - fill the parameters value into the structure instance
124  *
125  *
126  * @params[in] string leafNode, string val
127  * @return void
128  ******************************************************************/
129
130 bool RrmPolicyCb::updateParams(string &parent, string &leafNode, string &val)
131 {
132
133    O1_LOG("\nO1 RrmPolicyCb::updateParams parameter name=%s val = %s", \
134            leafNode.c_str(), val.c_str());
135    bool ret = true;
136
137    if(val != "")
138    {
139       if (leafNode == "id")
140       {
141          policyIndex++;
142          policyNum = policyIndex -1 ;
143          memset(rrmPolicy[policyNum].id, '\0', ID_MAX_LEN);
144          strncpy(rrmPolicy[policyNum].id, val.c_str(), ID_MAX_LEN-1);
145          O1_LOG("\nO1 RrmPolicyCb::updateParams id = %s", rrmPolicy[policyNum].id);
146          rrmPolicy[policyNum].rRMMemberNum = 0;
147       }
148       else if(leafNode ==  "resourceType")
149       {
150          rrmPolicy[policyNum].resourceType = resourceTypeToEnum(val);
151          O1_LOG("\nO1 RrmPolicyCb::updateParams  ResourceType = %d", \
152                   rrmPolicy[policyNum].resourceType);
153       }
154      else if(leafNode == "rRMPolicyMaxRatio")
155       {
156          rrmPolicy[policyNum].rRMPolicyMaxRatio = atoi(val.c_str());
157          O1_LOG("\nO1 RrmPolicyCb::updateParams rRMPolicyMaxRatio = %d", \
158                    rrmPolicy[policyNum].rRMPolicyMaxRatio);
159       }
160       else if(leafNode == "rRMPolicyMinRatio")
161       {
162          rrmPolicy[policyNum].rRMPolicyMinRatio = atoi(val.c_str());
163          O1_LOG("\nO1 RrmPolicyCb::updateParams rRMPolicyMinRatio = %d", \
164                    rrmPolicy[policyNum].rRMPolicyMinRatio);
165       }
166       else if(leafNode == "rRMPolicyDedicatedRatio")
167       {
168          rrmPolicy[policyNum].rRMPolicyDedicatedRatio = atoi(val.c_str());
169          O1_LOG("\nO1 RrmPolicyCb::updateParams rRMPolicyDedicatedRatio = %d", \
170                    rrmPolicy[policyNum].rRMPolicyDedicatedRatio);
171       }
172       else if((leafNode == "mcc") || (leafNode == "mnc")
173              || leafNode == "sst" || leafNode == "sd")
174       {
175          int memberNum = policyMemberListNum/MAX_POLICY_LIST;
176          rrmPolicy[policyNum].rRMMemberNum = memberNum+1;
177          if(leafNode == "mcc")
178          {
179             memset(rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mcc, \
180                    '\0', MCC_LEN);
181             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mcc[0] = std::stoi(val.substr(0,1).c_str(),0,10);
182             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mcc[1] = std::stoi(val.substr(1,1).c_str(),0,10);
183             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mcc[2] = std::stoi(val.substr(2,1).c_str(),0,10);
184             O1_LOG("\nO1 RrmPolicyCb::updateParams rrmPolicy[%d]."
185                    "rRMPolicyMemberList[%d].mcc[2] = %d", policyNum, memberNum, \
186                    rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mcc[2]);
187          }
188          else if(leafNode == "mnc")
189          {
190             memset(rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mnc, \
191                    '\0', MNC_LEN);
192             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mnc[0] = std::stoi(val.substr(0,1).c_str(),0,10);
193             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mnc[1] = std::stoi(val.substr(1,1).c_str(),0,10);
194             if(strlen(val.c_str())>2)
195             {
196                rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mnc[2] = std::stoi(val.substr(2,1).c_str(),0,10);
197             }
198            O1_LOG("\nO1 RrmPolicyCb::updateParams rrmPolicy[%d]."
199                      "rRMPolicyMemberList[%d].mnc[1] = %d", policyNum, memberNum, \
200                       rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].mnc[1]);
201          }
202          else if(leafNode == "sst")
203          {
204             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sst = \
205                     atoi(val.c_str());
206             O1_LOG("\nO1 RrmPolicyCb::updateParams sst val = %d",
207                       rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sst);
208          }
209          else if(leafNode == "sd")
210          {
211             memset(rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sd, \
212                    '\0', SD_LEN);
213
214             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sd[0] = \
215                     std::stoi(val.substr(0,2).c_str(),0,16);
216            rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sd[1] = \
217                     std::stoi(val.substr(2,2).c_str(),0,16);
218             rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sd[2] = \
219                     std::stoi(val.substr(4,2).c_str(),0,16);
220             O1_LOG("\nO1 RrmPolicyCb::updateParams rrmPolicy[%d]."
221                    "rRMPolicyMemberList[%d].sd[2] = %c", policyNum, memberNum, \
222                    rrmPolicy[policyNum].rRMPolicyMemberList[memberNum].sd[SD_LEN-1]);
223          }
224          policyMemberListNum++;
225       }
226       else
227       {
228          O1_LOG("\nO1 RrmPolicyCb::updateParams some parameter not handled name=%s "
229                 "val = %s", leafNode.c_str(), val.c_str());
230          ret = false;
231       }
232    }
233    else
234    {
235       O1_LOG("\nO1 RrmPolicyCb::updateParams without value node name=%s val = %s",
236                 leafNode.c_str(), val.c_str());
237    }
238    return ret;
239 }
240
241 /*******************************************************************
242  *
243  * @brief override module_change to handle callback
244  *
245  * @details
246  *
247  *    Function : module_change
248  *
249  *    Functionality:
250  *      - override module_change to handle callback of modification of
251  *        o-ran-sc-du-hello-world yang module
252  *
253  *
254  * @params[in] sysrepo::S_Session session, const char *module_name,
255  *             const char *xpath, sr_event_t event, uint32_t request_id,
256  *             void *private_data
257  * @return SR_ERR_OK  - success
258  *         SR_ERR_OK  - failure
259  ******************************************************************/
260
261 int RrmPolicyCb::module_change(sysrepo::S_Session sess, \
262                                     const char *module_name, \
263                                     const char *xpath, \
264                                     sr_event_t event, \
265                                     uint32_t request_id, \
266                                     void *private_data)
267 {
268    char change_path[MAX_LEN];
269    policyNum = 0;
270    policyIndex = 0;
271    policyMemberListNum =0;
272
273    try {
274       O1_LOG("\nO1 RrmPolicyCb : Notification %s", NetconfUtils::evToStr(event));
275       if (SR_EV_CHANGE == event)
276       {
277          snprintf(change_path, MAX_LEN, "/%s:*//.", module_name);
278          auto it = sess->get_changes_iter(change_path);
279          while (auto change = sess->get_change_next(it)) {
280             //NetconfUtils::printChange(change); //enable only for debugging
281             if(nullptr != change->new_val())
282             {
283                O1_LOG("\n\nO1 RrmPolicyCb : Parameter value has been \
284 changed val=%s", change->new_val()->val_to_string().c_str());
285                string val = change->new_val()->val_to_string();
286                string parent, leafNode;
287                NetconfUtils::getLeafInfo(change->new_val()->to_string(), parent, leafNode);
288                O1_LOG("\nO1 RrmPolicy : LeafNode name = [%s]", leafNode.c_str());
289                if(!updateParams(parent, leafNode, val)){
290                   O1_LOG("\nO1 RrmPolicy : LeafNode  [%s] not handled", leafNode.c_str());
291                   //later error handling can be done
292                }
293             }
294          }
295 #ifndef ODU_TEST_STUB
296          if(ROK != setRrmPolicy(rrmPolicy, policyIndex))
297          {
298             O1_LOG("\nO1 RrmPolicy : updateRrmPolicy failed");
299             return SR_ERR_INTERNAL;
300             //later error handling can be done
301          }
302 #endif
303       }
304    }
305    catch( const std::exception& e ) {
306       O1_LOG("\nO1 RrmPolicyCb exception : %s\n", e.what());
307       return SR_ERR_INTERNAL;
308    }
309    return SR_ERR_OK;
310 }
311
312 /**********************************************************************
313          End of file
314 **********************************************************************/