1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
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
24 #include "UnixSocketServer.hpp"
26 #include "AlarmManager.hpp"
27 #include "ConfigInterface.h"
28 #include "GlobalDefs.hpp"
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
40 #include "InitConfig.hpp"
46 /*******************************************************************
52 * Function : UnixSocketServer
55 * - Constructor intialization
57 * @params[in] socket path
59 ******************************************************************/
60 UnixSocketServer::UnixSocketServer(const string& sockPath)
61 : mSockPath(sockPath),
67 /*******************************************************************
73 * Function : ~UnixSocketServer
80 ******************************************************************/
81 UnixSocketServer::~UnixSocketServer()
87 /*******************************************************************
89 * @brief Read the data from the connected client application
93 * Function : readMessage
96 * - Reads the data from the connected client application
98 * @params[in] File descriptor
99 * @return No. of bytes read
101 ******************************************************************/
102 int UnixSocketServer::readMessage(int fd)
104 AlarmRecord *alrmRec = NULL;
105 char recvBuf[BUFLEN];
107 bzero(&recvBuf,sizeof(recvBuf));
109 int nbytes = read (fd, &recvBuf, sizeof(recvBuf));
113 MsgHeader *msgHdr = (MsgHeader*)recvBuf;
115 O1_LOG("\nO1 UnixSocketServer :\nMsgType %d",msgHdr->msgType);
117 if ( msgHdr->msgType == ALARM ){
119 alrmRec = (AlarmRecord*) recvBuf;
120 O1_LOG("\nO1 UnixSocketServer :\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,
130 alrmRec->perceivedSeverity,
131 alrmRec->additionalText,
132 alrmRec->specificProblem,
133 alrmRec->additionalInfo,
134 alrmRec->alarmRaiseTime
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);
147 switch(msgHdr->action)
150 if(AlarmManager::instance().raiseAlarm(alrm))
152 O1_LOG("\nO1 UnixSocketServer : "
153 "Alarm raised for alarm Id %s",
158 O1_LOG("\nO1 UnixSocketServer : "
159 "Error in raising alarm for alrm Id %s",
164 if(AlarmManager::instance().clearAlarm(alrm))
166 O1_LOG("\nO1 UnixSocketServer : "
167 "Alarm cleared for alarm Id %s",
172 O1_LOG("\nO1 UnixSocketServer : "
173 "Error in clearing alarm for alarm Id %s",
178 case GET_STARTUP_CONFIG:
181 InitConfig::instance().getCurrInterfaceConfig(cfg);
182 O1_LOG("\nO1 UnixSocketServer : "
183 "cfg.DU_IPV4_Addr [%s]",
185 O1_LOG("\nO1 UnixSocketServer : "
188 O1_LOG("\nO1 UnixSocketServer : "
189 "cfg.CU_IPV4_Addr [%s]",
191 O1_LOG("\nO1 UnixSocketServer : "
194 O1_LOG("\nO1 UnixSocketServer : "
195 "cfg.RIC_IPV4_Addr [%s]",
197 O1_LOG("\nO1 UnixSocketServer : "
200 if (write (fd, &cfg, sizeof(cfg)) < 0)
202 O1_LOG("\nO1 UnixSocketServer : "
203 "Error sending startup configuration \n");
209 O1_LOG("\nO1 UnixSocketServer : No action performed");
218 /*******************************************************************
220 * @brief Open a Unix socket and bind on the port
224 * Function : makeSocket
227 * - Opens a Unix socket and bind on the port
230 * @return O1:SUCCESS - success
231 * O1:FAILURE - failure
232 ******************************************************************/
234 int UnixSocketServer::makeSocket()
236 struct sockaddr_un name;
237 /* Create the socket. */
238 mSock = socket (AF_UNIX, SOCK_STREAM, 0);
241 O1_LOG("\nO1 UnixSocketServer : Socket error");
244 /* Give the socket a name. */
245 bzero(&name, sizeof(name));
246 name.sun_family = AF_UNIX;
248 /* Remove the socket file if it already exists */
249 if ( unlink(mSockPath.c_str()) == 0)
251 O1_LOG("\nO1 UnixSocketServer : "
252 "Removing the existing socket path %s",
255 strcpy(name.sun_path, mSockPath.c_str());
256 if (bind (mSock, (struct sockaddr *) &name, sizeof (name)) < 0)
259 O1_LOG("\nO1 UnixSocketServer : Bind error");
266 /*******************************************************************
268 * @brief A Unix server to handle multiple connection
275 * - A Unix server to handle multiple connection
276 * Uses select multiplexing
279 * @return true - success
281 ******************************************************************/
282 bool UnixSocketServer::run()
285 fd_set active_fd_set, read_fd_set;
287 struct sockaddr_un clientName;
291 /* Create the socket and set it up to accept connections. */
292 if( makeSocket() == O1::SUCCESS )
294 if (listen (mSock, 1) < 0)
296 O1_LOG("\nO1 UnixSocketServer : Listen error");
302 /* Initialize the set of active sockets. */
303 FD_ZERO (&active_fd_set);
304 FD_SET (mSock, &active_fd_set);
308 /* Block until input arrives on one or more active sockets. */
309 read_fd_set = active_fd_set;
310 if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
312 O1_LOG("\nO1 UnixSocketServer : Select error");
318 /* Service all the sockets with input pending. */
319 for (i = 0; i < FD_SETSIZE; ++i)
321 if (FD_ISSET (i, &read_fd_set))
325 /* Connection request on original socket. */
327 bzero(&clientName, sizeof(clientName));
328 size = sizeof (clientName);
329 newFd = accept(mSock,(struct sockaddr *) &clientName,&size);
332 O1_LOG("\nO1 UnixSocketServer : Accept error");
337 O1_LOG("\nO1 UnixSocketServer : Connected from client\n");
338 FD_SET (newFd, &active_fd_set);
342 /* Data arriving on an already-connected socket. */
343 if (readMessage(i) < 0)
346 FD_CLR (i, &active_fd_set);
351 } /* while(1) ends */
353 } /* outer if ends */
362 /*******************************************************************
364 * @brief Clean up open socket
371 * - Performs any clean ups before stopping the thread
375 ******************************************************************/
376 void UnixSocketServer::cleanUp(void)
379 O1_LOG("\nO1 UnixSocketServer : Cleaning up Closing socket \n");
382 /*******************************************************************
384 * @brief Check if the server is running
388 * Function : isRunning
391 * - Returns the running status of the server
394 * @return true : running
396 ******************************************************************/
397 bool UnixSocketServer::isRunning() const
402 /**********************************************************************
404 **********************************************************************/