ef954fa8743349388a62f876d9caa1fa50405a55
[o-du/l2.git] / src / o1 / UnixSocketServer.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 UnixSocketServer class that listens for Netconf Alarm 
20    messages on a Unix socket from ODU. It calls the AlarmManager functions
21    for raising or clearing the alarms based on the actions received 
22 */
23
24 #include "UnixSocketServer.hpp"
25 #include "Alarm.hpp"
26 #include "AlarmManager.hpp"
27 #include "CmInterface.h"
28 #include "GlobalDefs.hpp"
29 #include <iostream>
30 #include <cstdio>
31 #include <cerrno>
32 #include <cstdlib>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
38 #include <arpa/inet.h>
39 #include <sys/un.h>
40 #include "InitConfig.hpp"
41
42
43 using std::map;
44 using std::pair;
45
46 /*******************************************************************
47  *
48  * @brief Constructor
49  *
50  * @details
51  *
52  *    Function : UnixSocketServer
53  *
54  *    Functionality:
55  *      - Constructor intialization
56  *
57  * @params[in] socket path
58  * @return None
59  ******************************************************************/
60 UnixSocketServer::UnixSocketServer(const string& sockPath)
61                                    : mSockPath(sockPath),
62                                      mIsRunning(false)
63 {
64
65 }  
66   
67 /*******************************************************************
68  *
69  * @brief Destructor
70  *
71  * @details
72  *
73  *    Function : ~UnixSocketServer
74  *
75  *    Functionality:
76  *      - Destructor
77  *
78  * @params[in] None
79  * @return None
80  ******************************************************************/
81 UnixSocketServer::~UnixSocketServer()
82 {
83
84 }
85
86
87 /*******************************************************************
88  *
89  * @brief Read the data from the connected client application
90  *
91  * @details
92  *
93  *    Function : readMessage
94  *
95  *    Functionality:
96  *      - Reads the data from the connected client application
97  *
98  * @params[in] File descriptor
99  * @return No. of bytes read
100  *
101  ******************************************************************/
102 int UnixSocketServer::readMessage(int fd)
103 {
104    AlarmRecord *alrmRec = NULL;
105    char recvBuf[BUFLEN];
106    Alarm alrm;
107    bzero(&recvBuf,sizeof(recvBuf));
108    
109    int nbytes = read (fd, &recvBuf, sizeof(recvBuf));
110    
111    if (nbytes > 0)
112    {
113       MsgHeader *msgHdr = (MsgHeader*)recvBuf;
114
115       O1_LOG("\nO1 UnixSocketServer :\nMsgType %d",msgHdr->msgType);
116       
117       if ( msgHdr->msgType == ALARM ){
118          uint16_t alrmId;
119          alrmRec = (AlarmRecord*) recvBuf;
120          O1_LOG("\nO1 UnixSocketServer :\n"
121                    "Action %d\n"
122                    "Alarm ID %s\n" 
123                    "Severity %d\n" 
124                    "Additional Text %s\n"
125                    "Specific Problem %s\n"
126                    "Additional Info %s\n"
127                    "Alarm Raise Time %s\n",
128                    alrmRec->msgHeader.action,
129                    alrmRec->alarmId,
130                    alrmRec->perceivedSeverity,
131                    alrmRec->additionalText,
132                    alrmRec->specificProblem,
133                    alrmRec->additionalInfo,
134                    alrmRec->alarmRaiseTime
135                 );
136       
137          /*Fill the alarm structure */
138          sscanf(alrmRec->alarmId,"%hu",&alrmId);
139          alrm.setAlarmId(alrmId);
140          alrm.setPerceivedSeverity(alrmRec->perceivedSeverity);
141          alrm.setAdditionalText(alrmRec->additionalText);
142          alrm.setEventType(alrmRec->eventType);
143          alrm.setSpecificProblem(alrmRec->specificProblem);
144          alrm.setAdditionalInfo(alrmRec->additionalInfo);
145       }
146
147       switch(msgHdr->action)
148       {
149          case RAISE_ALARM: 
150
151                      if(AlarmManager::instance().raiseAlarm(alrm))
152                      {
153                         O1_LOG("\nO1 UnixSocketServer : "
154                                "Alarm raised for alarm Id %s",
155                                 alrmRec->alarmId);
156                      }
157
158                      else
159                      {
160                         O1_LOG("\nO1 UnixSocketServer : "
161                                "Error in raising alarm for alrm Id %s",
162                                 alrmRec->alarmId);
163                      }
164                      break;  
165                      
166          case CLEAR_ALARM: 
167                      if(AlarmManager::instance().clearAlarm(alrm))
168                      {
169                         O1_LOG("\nO1 UnixSocketServer : "
170                                "Alarm cleared for alarm Id %s",
171                                 alrmRec->alarmId);
172
173                      }
174                      else
175                      {
176                         O1_LOG("\nO1 UnixSocketServer : "
177                                "Error in clearing alarm for alarm Id %s",
178                                 alrmRec->alarmId);
179                      }
180                      break;
181          default:    
182                      O1_LOG("\nO1 UnixSocketServer : No action performed"); 
183                      break;
184       }
185
186    }
187    return nbytes;
188 }
189
190
191 /*******************************************************************
192  *
193  * @brief Open a Unix socket and bind on the port
194  *
195  * @details
196  *
197  *    Function : makeSocket
198  *
199  *    Functionality:
200  *      -  Opens a Unix socket and bind on the port
201  *
202  * @params[in] void
203  * @return O1:SUCCESS - success
204  *         O1:FAILURE - failure
205  ******************************************************************/
206
207 int UnixSocketServer::makeSocket() 
208 {
209    struct sockaddr_un name;
210    /* Create the socket. */
211    mSock = socket (AF_UNIX, SOCK_STREAM, 0);
212    if (mSock < 0)
213    {
214       O1_LOG("\nO1 UnixSocketServer : Socket error");
215       return O1::FAILURE;
216    }
217    /* Give the socket a name. */
218    bzero(&name, sizeof(name));
219    name.sun_family = AF_UNIX;
220
221    /* Remove the socket file if it already exists */ 
222    if ( unlink(mSockPath.c_str()) == 0)
223    {
224       O1_LOG("\nO1 UnixSocketServer : "
225              "Removing the existing socket path %s",
226               mSockPath.c_str());
227    }        
228    strcpy(name.sun_path, mSockPath.c_str());
229    if (bind (mSock, (struct sockaddr *) &name, sizeof (name)) < 0)
230    {
231       close(mSock);
232       O1_LOG("\nO1 UnixSocketServer : Bind error");
233       return O1::FAILURE;
234    }
235    return O1::SUCCESS;
236 }
237
238
239 /*******************************************************************
240  *
241  * @brief A Unix server to handle multiple connection
242  *
243  * @details
244  *
245  *    Function : run
246  *
247  *    Functionality:
248  *      -  A Unix server to handle multiple connection 
249  *         Uses select multiplexing
250  *
251  * @params[in] void
252  * @return true  - success
253  *         false - failure
254  ******************************************************************/
255 bool UnixSocketServer::run()
256 {
257
258    fd_set active_fd_set, read_fd_set;
259    int i;
260    struct sockaddr_un clientName;
261    socklen_t size;
262    mIsRunning = true;
263
264    /* Create the socket and set it up to accept connections. */
265    if( makeSocket() == O1::SUCCESS )
266    {
267       if (listen (mSock, 1) < 0)
268       {
269          O1_LOG("\nO1 UnixSocketServer : Listen error");
270          close(mSock);
271          mIsRunning = false;
272       }
273       else
274       {
275          /* Initialize the set of active sockets. */
276          FD_ZERO (&active_fd_set);
277          FD_SET (mSock, &active_fd_set);
278
279          while (1)
280          {
281             /* Block until input arrives on one or more active sockets. */
282             read_fd_set = active_fd_set;
283             if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
284             {
285                O1_LOG("\nO1 UnixSocketServer : Select error");
286                close(mSock);
287                mIsRunning = false;
288                break;
289             }
290
291             /* Service all the sockets with input pending. */
292             for (i = 0; i < FD_SETSIZE; ++i)
293             {
294                 if (FD_ISSET (i, &read_fd_set))
295                 {
296                    if (i == mSock)
297                    {
298                       /* Connection request on original socket. */
299                       int newFd;
300                       bzero(&clientName, sizeof(clientName));
301                       size = sizeof (clientName);
302                       newFd = accept(mSock,(struct sockaddr *) &clientName,&size);
303                       if (newFd < 0)
304                       {
305                          O1_LOG("\nO1 UnixSocketServer : Accept error");
306                          close(mSock);
307                          mIsRunning = false;
308                          break;
309                       }
310                       O1_LOG("\nO1 UnixSocketServer : Connected from client\n");
311                       FD_SET (newFd, &active_fd_set);
312                    }      
313                    else
314                    {
315                       /* Data arriving on an already-connected socket. */
316                       if (readMessage(i) < 0)
317                       {
318                          close (i);
319                          FD_CLR (i, &active_fd_set);
320                       }
321                    }
322                 }    
323              }/* for loop ends */     
324           } /* while(1) ends */
325       } /* else ends */
326    } /* outer if ends */
327    else
328    {
329       mIsRunning = false;
330    }
331    return mIsRunning;
332 }
333
334
335 /*******************************************************************
336  *
337  * @brief Clean up open socket
338  *
339  * @details
340  *
341  *    Function : cleanUp
342  *
343  *    Functionality:
344  *      -  Performs any clean ups before stopping the thread
345  *
346  * @params[in] void
347  * @return void
348  ******************************************************************/
349 void UnixSocketServer::cleanUp(void)
350 {
351    close(mSock);
352    O1_LOG("\nO1 UnixSocketServer : Cleaning up Closing socket \n");
353 }
354
355 /*******************************************************************
356  *
357  * @brief Check if the server is running
358  *
359  * @details
360  *
361  *    Function : isRunning
362  *
363  *    Functionality:
364  *      -  Returns the running status of the server
365  *
366  * @params[in] void
367  * @return true : running
368  *         false: not running
369  ******************************************************************/
370 bool UnixSocketServer::isRunning() const
371 {
372    return mIsRunning;
373 }
374
375 /**********************************************************************
376          End of file
377 **********************************************************************/