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 "CmInterface.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)
151 if(AlarmManager::instance().raiseAlarm(alrm))
153 O1_LOG("\nO1 UnixSocketServer : "
154 "Alarm raised for alarm Id %s",
160 O1_LOG("\nO1 UnixSocketServer : "
161 "Error in raising alarm for alrm Id %s",
167 if(AlarmManager::instance().clearAlarm(alrm))
169 O1_LOG("\nO1 UnixSocketServer : "
170 "Alarm cleared for alarm Id %s",
176 O1_LOG("\nO1 UnixSocketServer : "
177 "Error in clearing alarm for alarm Id %s",
182 O1_LOG("\nO1 UnixSocketServer : No action performed");
191 /*******************************************************************
193 * @brief Open a Unix socket and bind on the port
197 * Function : makeSocket
200 * - Opens a Unix socket and bind on the port
203 * @return O1:SUCCESS - success
204 * O1:FAILURE - failure
205 ******************************************************************/
207 int UnixSocketServer::makeSocket()
209 struct sockaddr_un name;
210 /* Create the socket. */
211 mSock = socket (AF_UNIX, SOCK_STREAM, 0);
214 O1_LOG("\nO1 UnixSocketServer : Socket error");
217 /* Give the socket a name. */
218 bzero(&name, sizeof(name));
219 name.sun_family = AF_UNIX;
221 /* Remove the socket file if it already exists */
222 if ( unlink(mSockPath.c_str()) == 0)
224 O1_LOG("\nO1 UnixSocketServer : "
225 "Removing the existing socket path %s",
228 strcpy(name.sun_path, mSockPath.c_str());
229 if (bind (mSock, (struct sockaddr *) &name, sizeof (name)) < 0)
232 O1_LOG("\nO1 UnixSocketServer : Bind error");
239 /*******************************************************************
241 * @brief A Unix server to handle multiple connection
248 * - A Unix server to handle multiple connection
249 * Uses select multiplexing
252 * @return true - success
254 ******************************************************************/
255 bool UnixSocketServer::run()
258 fd_set active_fd_set, read_fd_set;
260 struct sockaddr_un clientName;
264 /* Create the socket and set it up to accept connections. */
265 if( makeSocket() == O1::SUCCESS )
267 if (listen (mSock, 1) < 0)
269 O1_LOG("\nO1 UnixSocketServer : Listen error");
275 /* Initialize the set of active sockets. */
276 FD_ZERO (&active_fd_set);
277 FD_SET (mSock, &active_fd_set);
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)
285 O1_LOG("\nO1 UnixSocketServer : Select error");
291 /* Service all the sockets with input pending. */
292 for (i = 0; i < FD_SETSIZE; ++i)
294 if (FD_ISSET (i, &read_fd_set))
298 /* Connection request on original socket. */
300 bzero(&clientName, sizeof(clientName));
301 size = sizeof (clientName);
302 newFd = accept(mSock,(struct sockaddr *) &clientName,&size);
305 O1_LOG("\nO1 UnixSocketServer : Accept error");
310 O1_LOG("\nO1 UnixSocketServer : Connected from client\n");
311 FD_SET (newFd, &active_fd_set);
315 /* Data arriving on an already-connected socket. */
316 if (readMessage(i) < 0)
319 FD_CLR (i, &active_fd_set);
324 } /* while(1) ends */
326 } /* outer if ends */
335 /*******************************************************************
337 * @brief Clean up open socket
344 * - Performs any clean ups before stopping the thread
348 ******************************************************************/
349 void UnixSocketServer::cleanUp(void)
352 O1_LOG("\nO1 UnixSocketServer : Cleaning up Closing socket \n");
355 /*******************************************************************
357 * @brief Check if the server is running
361 * Function : isRunning
364 * - Returns the running status of the server
367 * @return true : running
369 ******************************************************************/
370 bool UnixSocketServer::isRunning() const
375 /**********************************************************************
377 **********************************************************************/