Adding new commiter to ODU-High repo
[o-du/l2.git] / src / o1 / InitConfig.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020] [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 methods of InitConfig and Interfaces to
20    YANG modules */
21
22 #include "InitConfig.hpp"
23
24
25 /* Default constructor */
26 InitConfig::InitConfig()
27 {    
28 }
29
30
31 /* Destructor */
32 InitConfig::~InitConfig()
33 {  
34 }
35
36 /*******************************************************************
37  *
38  * @brief Initialize Configuration
39  *
40  * @details
41  *
42  *    Function : init 
43  *
44  *    Functionality:
45  *      - Initialize Configuration
46  *
47  * @params[in] Reference to S_Session 
48  * @return bool
49  *
50  ******************************************************************/
51
52 bool InitConfig::init(sysrepo::S_Session sess)
53 {
54    mSess =  sess;
55    InitConfig::InterfaceMap map;
56    if(!getInterfaceConfig(sess, map))
57    {
58       O1_LOG("\nInitConfig::getInterfaceConfig InitConfig data error ");
59       return false;
60    }
61    return true;
62 }
63
64 /*******************************************************************
65  *
66  * @brief Get running Interface Configuration
67  *
68  * @details
69  *
70  *    Function : getCurrInterfaceConfig 
71  *
72  *    Functionality:
73  *      - Get running Interface Configuration
74  *
75  * @params[in] Reference of StartupConfig
76  * @return bool
77  *
78  ******************************************************************/
79
80 bool InitConfig::getCurrInterfaceConfig(StartupConfig & cfg)
81 {
82    //O1_LOG("\n InitConfig::getCurrInterfaceConfig entry");
83    InitConfig::InterfaceMap::iterator it;
84    for (it=mInterfaceList.begin(); it != mInterfaceList.end(); it++)
85    {
86       //O1_LOG("\n InitConfig::getCurrInterfaceConfig iterator");
87       switch (it->first)
88       {
89          case Interface::ODU :
90             if(it->second.first != "")
91                strcpy(cfg.DU_IPV4_Addr, it->second.first.c_str());
92             else
93                return false;
94
95             if(it->second.second != 0)
96                cfg.DU_Port = (uint16_t)it->second.second;
97             else
98                return false;
99             break;
100  
101          case Interface::OCU :
102            
103             if(it->second.first != "")
104                strcpy(cfg.CU_IPV4_Addr, it->second.first.c_str());
105             else
106                return false;
107             
108           
109             if(it->second.second != 0)
110                cfg.CU_Port = (uint16_t) it->second.second;
111             else
112                return false;
113          
114             break;
115
116          case Interface::RIC :
117
118             if(it->second.first != "")
119                strcpy(cfg.RIC_IPV4_Addr, it->second.first.c_str());
120             else
121                return false;
122                
123             
124             if(it->second.second != 0)
125                cfg.RIC_Port = (uint16_t) it->second.second;
126             else
127                return false;
128
129             break;
130
131          default :
132             O1_LOG("\nno matching interface");
133             break;
134     }
135    }
136    return true;
137 }
138
139 /*******************************************************************
140  *
141  * @brief Get Interface Configuration
142  *
143  * @details
144  *
145  *    Function : getInterfaceConfig 
146  *
147  *    Functionality:
148  *      - Get running Interface Configuration
149  *
150  * @params[in] Reference of S_Session
151  * @return reference of InterfaceMap
152  *
153  ******************************************************************/
154 bool InitConfig::getInterfaceConfig(sysrepo::S_Session sess , InitConfig::InterfaceMap &map)
155 {
156    InitConfig::Address oduAddr; 
157    InitConfig::Address ocuAddr; 
158    InitConfig::Address ricAddr; 
159    if(getInterfaceData(sess,Interface::ODU, oduAddr) && \
160       getInterfaceData(sess,Interface::OCU, ocuAddr) && \
161       getInterfaceData(sess,Interface::RIC, ricAddr))
162    {
163       mInterfaceList.insert(std::make_pair(Interface::ODU, oduAddr));
164       mInterfaceList.insert(std::make_pair(Interface::OCU, ocuAddr));
165       mInterfaceList.insert(std::make_pair(Interface::RIC, ricAddr));
166       map =  mInterfaceList; 
167       return true;
168    }
169    else
170    {
171       return false;
172    }
173 }
174
175 /*******************************************************************
176  *
177  * @brief Get Interface Data
178  *
179  * @details
180  *
181  *    Function : getInterfaceData 
182  *
183  *    Functionality:
184  *      - Get running Interface Data 
185  *
186  * @params[in] Reference of S_Session and Interface
187  * @return reference of Address
188  *
189  ******************************************************************/
190 bool InitConfig::getInterfaceData(sysrepo::S_Session sess, \
191                                   Interface inf, InitConfig::Address & addr)
192 {
193    //O1_LOG("\nInitConfig::getInterfaceData started");
194    string sInf = interfaceToString(inf);
195    string ip;
196    string port;
197
198    if(getData(sess, getInterfaceXpath(sInf, IP_ADDRESS), ip))
199    {
200       if(getData(sess, getInterfaceXpath(sInf, PORT), port))
201       {
202          addr = std::make_pair(ip, atoi(port.c_str()));
203          return true;
204       }
205       else
206          return false;
207    }
208    else
209       return false;
210 }
211
212 /*******************************************************************
213  *
214  * @brief create xpath for interfaces
215  *
216  * @details
217  *
218  *    Function : getInterfaceXpath
219  *
220  *    Functionality:
221  *      - create xpath for interfaces
222  *
223  * @params[in] String of sInf and String of param
224  * @return pointer to xpath
225  *
226  ******************************************************************/
227 char * InitConfig::getInterfaceXpath( string sInf, string param)
228 {
229    sprintf(xpath, "%s/interfaces/interface[interface-name='%s']/%s", \
230            INTERFACE_MODULE_NAME_ORAN, sInf.c_str(),param.c_str());
231    return xpath;
232 }
233
234 /*******************************************************************
235  *
236  * @brief  Get data of the xpath
237  *
238  * @details
239  *
240  *    Function : getData
241  *
242  *    Functionality:
243  *      -  get data of the xpath
244  *
245  * @params[in] reference of S_Session and pointer to xpath
246  * @return value against the xpath
247  *
248  ******************************************************************/
249
250 bool InitConfig::getData(sysrepo::S_Session sess,char* xpath, string &val)
251 {
252    //O1_LOG("\nInitConfig::getData of xpath = %s", \
253               xpath); //enable for debugging only
254    try
255    {
256    auto value = sess->get_item(xpath);
257    if (value == nullptr)
258    {
259       O1_LOG("\nInitConfig::getData no data available at xpath= %s", \
260               xpath);
261       //O1_LOG("\nget_item value are null for xpath = %s", \
262                  xpath); //enable for debugging only
263       return false;
264    }
265    val = value->val_to_string();
266    return true;
267
268    }
269    catch (...)
270    {
271       O1_LOG("\nInitConfig::getData exception occured for xpath= %s", \
272               xpath);
273       return false; 
274    }
275 }
276  
277 /*******************************************************************
278  *
279  * @brief  Convert interface value to string
280  *
281  * @details
282  *
283  *    Function : interfaceToString
284  *
285  *    Functionality:
286  *      -  Convert interface value to string
287  *
288  * @params[in] reference to Interface
289  * @return string value of Interface
290  *
291  ******************************************************************/
292 string InitConfig::interfaceToString(Interface inf)
293 {
294    string interface;
295     switch (inf)
296     {
297         case Interface::ODU :
298            interface = "odu";
299            break; 
300    case Interface::OCU :
301            interface = "ocu"; 
302            break;
303         case Interface::RIC :
304            interface = "ric";
305            break;
306    default :
307           O1_LOG("\n\n no matching interface ");
308           break;
309     }
310     return interface;
311  
312 }
313
314
315 /*******************************************************************
316  *
317  * @brief  Print Interface configuration
318  *
319  * @details
320  *
321  *    Function : printInterfaceConfig
322  *
323  *    Functionality:
324  *      -  Print Interface configuration
325  *
326  * @params[in] void
327  * @return bool
328  *
329  ******************************************************************/
330 bool InitConfig::printInterfaceConfig()
331 {
332    InitConfig::InterfaceMap::iterator it;
333    for (it=mInterfaceList.begin(); it != mInterfaceList.end(); it++)
334    {
335       O1_LOG("\ninterface [%s] : \n \t IP = %s \n \t Port = %d", \
336               interfaceToString(it->first).c_str(), it->second.first.c_str(), \
337               it->second.second);
338    }
339    return true;
340 }
341
342 /**********************************************************************
343          End of file
344 **********************************************************************/