Restructure O1 module to run as a thread in O-DU High binary [Issue-Id: ODUHIGH-297]
[o-du/l2.git] / src / o1 / NetconfManager.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 class is the netconf manager class. It will handle netopeer-server start and stop along with session handler init. Additionally it will provide the
20    signal handling. */
21
22 #include "NetconfManager.hpp"
23
24 /* Default constructor */
25 NetconfManager::NetconfManager()
26 {
27 }
28
29
30 /* Destructor */
31 NetconfManager::~NetconfManager()
32 {
33    if( NULL != mSessHndl)
34    {
35       delete mSessHndl;
36    }  
37 }
38
39 /********************************************************************** 
40    Description : This function will start the netopeer2-server and 
41                  redirect logs to the /etc/netopeer2-server.log file
42    Params[In]  : void
43    Return      : true  - started successful
44                  false - start failed
45 **********************************************************************/
46
47 bool NetconfManager::startNetopeerServer ()
48 {
49     int status = system("netopeer2-server -d -v3 > /etc/netopeer2-server.log 2>&1 &");
50     if (status < 0)
51     {
52        O1_LOG("\nO1 NetconfManager : Error during netopeer server start status : %s",\
53                strerror(errno));
54     }
55     else
56     {
57        if (WIFEXITED(status))
58           O1_LOG("\nO1 NetconfManager : netopeer server started normally with status : %d",\
59                   WEXITSTATUS(status));
60        else
61           O1_LOG("\nO1 NetconfManager : netopeer server started abnormally with status : %d",\
62                   WEXITSTATUS(status));
63     }
64     return (status == 0);
65 }
66
67
68 /********************************************************************** 
69    Description : This function will stop the netopeer2-server 
70    Params[In]  : void
71    Return      : true  - started successful
72                  false - start failed
73 **********************************************************************/
74 bool NetconfManager::stopNetopeerServer(void)
75 {
76    int status = system("kill -9 `pidof netopeer2-server`");
77    if (status < 0)
78    {
79       O1_LOG("\nO1 NetconfManager : Error during Netopeer server stopped status : %s\n",\
80               strerror(errno));
81    }
82    else
83    {
84       if (WIFEXITED(status))
85       {
86          O1_LOG("\nO1 NetconfManager : Netopeer server stopped normally with status : %d\n",\
87                  WEXITSTATUS(status));
88       }
89       else
90       {
91          O1_LOG("\nO1  NetconfManager : Netopeer server stopped abnormally with status : %d\n",\
92                  WEXITSTATUS(status));
93       }
94    }
95    return (status == 0);
96 }
97
98
99 /********************************************************************** 
100    Description : catch and handle the SIGINT signal 
101    Params[In]  : signum
102    Return      : void 
103 **********************************************************************/
104 void NetconfManager::sigintHandler(int signum)
105 {
106    if (true != NetconfManager::stopNetopeerServer())
107    {
108       O1_LOG("\nO1 NetconfManager : Error stopping Netopeer server");
109    }
110 }
111
112
113 /********************************************************************** 
114    Description : This function will start the netopeer2-server and 
115                  session handler  
116    Params[In]  : void
117    Return      : true  - started successful
118                  false - start failed
119 **********************************************************************/
120 bool NetconfManager::init(void)
121 {
122   /* if(startNetopeerServer())
123    {
124       O1_LOG("\nO1 NetconfManager : netopeer server started");
125    }
126    */
127    try
128    {
129       mSessHndl = new SessionHandler;
130       
131       O1_LOG("\nO1 NetconfManager : SessionHandler created ");
132
133       if( !mSessHndl->init())
134       {
135          O1_LOG("\nO1 NetconfManager : SessionHandler init failed \n");         
136          return false;
137       }
138       return true;
139    }
140    catch( const std::exception& e ) 
141    {
142       O1_LOG("\nO1 O1_main : Exception : %s", e.what());
143       return false;
144    }
145 }
146
147
148 /**********************************************************************
149          End of file
150 **********************************************************************/