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