d63498f38c35e2c4fbf30f78db5b97dbbae74b6b
[o-du/l2.git] / src / o1 / O1App.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 the O1App class which is responsible for running/starting
20    all the O1 modules in a thread. It inherits the Thread class and Singleton
21    class.
22 */
23
24 #include "O1App.hpp"
25 #include "GlobalDefs.hpp"
26 #include "SessionHandler.hpp"
27 #include "CmInterface.h"
28 #include <unistd.h>
29
30 #include "VesUtils.hpp"
31 #include "VesEventHandler.hpp"
32
33
34 /*******************************************************************
35  *
36  * @brief Constructor
37  *
38  * @details
39  *
40  *    Function : O1App
41  *
42  *    Functionality:
43  *      - Constructor intialization
44  *
45  * @params[in] None
46  * @return None
47  ******************************************************************/
48
49 O1App::O1App() : mUxSocketServer(O1::ALARM_SOCK_PATH)
50 {
51
52 }
53
54 /*******************************************************************
55  *
56  * @brief Destructor
57  *
58  * @details
59  *
60  *    Function : ~O1App
61  *
62  *    Functionality:
63  *      - Destructor
64  *
65  * @params[in] None
66  * @return None
67  ******************************************************************/
68
69 O1App::~O1App()
70 {
71
72 }
73
74 /*******************************************************************
75  *
76  * @brief Runs the O1 modules as a thread 
77  *
78  * @details
79  *
80  *    Function : run
81  *
82  *    Functionality:
83  *      - Implements the "run" function of Thread class.
84  *        Starts all the O1 modules.
85  *
86  *
87  * @params[in] void
88  * @return true  : success
89  *         false : failure
90  ******************************************************************/
91
92 bool O1App::run()
93 {
94    const int SLEEP_INTERVAL = 2;
95    const int DEFAUL_CELL_ID = 1;
96    SessionHandler sessHdlr;
97
98    /*setting default cell state disabled*/
99    setCellOpState(DEFAUL_CELL_ID, DISABLED, INACTIVE);
100
101    /* Start Netconf session and subscribe to yang modules */
102    try
103    {
104       if( !sessHdlr.init() )
105       {
106          O1_LOG("\nO1 O1App : SessionHandler initialization failed ");         
107          return false;
108       }
109    }
110    catch( const std::exception& e ) 
111    {
112       O1_LOG("\nO1 O1App : Exception : %s", e.what());
113       return false;
114    }
115    
116    /* Start the Unix Socket Server to listen for alarm messages */
117    if( mUxSocketServer.start() )
118    {  
119       
120       if(mUxSocketServer.setAffinity(O1::CPU_CORE))
121       {
122          O1_LOG("\nO1 O1App : CPU affinity set for UnixSocketServer thread to " );
123          mUxSocketServer.printAffinity();
124       }
125       
126       sleep(SLEEP_INTERVAL);
127       if( mUxSocketServer.isRunning() )  
128       {
129          mStartupStatus = true;
130          O1_LOG("\nO1 O1App : Unix Socket server started");
131       }
132       else
133       {
134          O1_LOG("\nO1 O1App : Unix Socket server failed to start");
135          return false;
136       }
137       /* Wait for the Unix Socket Server thread to end*/
138       mUxSocketServer.join();
139    }
140    else
141    {
142       O1_LOG("\nO1 O1App : Unix Socket server failed to start");
143       return false;
144    }
145    return true;
146 }
147
148 /*******************************************************************
149  *
150  * @brief Check if the O1 Module is fully up and running
151  *
152  * @details
153  *
154  *    Function : getStartupStatus
155  *
156  *    Functionality:
157  *      - Returns the status of O1App whether it is fully up
158  *
159  *
160  * @params[in] void
161  * @return true  : started
162  *         false : not started
163  ******************************************************************/
164
165 bool O1App::getStartupStatus() const
166 {
167    return mStartupStatus;
168 }
169
170 /*******************************************************************
171  *
172  * @brief Cleanup O1App
173  *
174  * @details
175  *
176  *    Function : cleanUp
177  *
178  *    Functionality:
179  *      - Implements the "cleanUp" function of Thread class to
180  *        take care of any clean up required for O1 components 
181  *        before stopping the thread.
182  *
183  * @params[in] void
184  * @return void
185  ******************************************************************/
186
187 void O1App::cleanUp(void)
188 {
189    /* Stop the socket server thread */
190    mUxSocketServer.stop();   
191 }