dcb9447d28127988f7b1a1f4a2937513c21a79be
[o-du/l2.git] / src / o1 / o1_client / AlarmInterface.c
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 file contains interfaces to raise and clear alarms */
20
21 #include "AlarmInterface.h"
22 #include "TcpClient.h"
23
24
25 /*******************************************************************
26  *
27  * @brief Raise an alarm
28  *
29  * @details
30  *
31  *    Function : raiseAlarm
32  *
33  *    Functionality:
34  *      - Raise an alarm by sending alarm info to O1 module over
35  *        TCP socket with action set to RAISE
36  *
37  * @params[in] alarm information  
38  * @return ROK     - success
39  *         RFAILED - failure
40  ******************************************************************/
41 uint8_t raiseAlarm(AlarmRecord* alrm)
42 {
43    if (openSocket(TCP_SERVER_IP,TCP_PORT) == RFAILED)
44    {
45       return RFAILED;
46    }
47    alrm->msgHeader.msgType = ALARM;
48    alrm->msgHeader.action = RAISE_ALARM;
49    if (sendData(alrm,sizeof(AlarmRecord)) < 0 )
50    {
51       closeSocket();
52       return RFAILED;
53    }
54    closeSocket();
55    return ROK;
56 }
57
58 /*******************************************************************
59  *
60  * @brief Clear an alarm
61  *
62  * @details
63  *
64  *    Function : clearAlarm
65  *
66  *    Functionality:
67  *      - Clears an alarm raised earlier by sending the alrm
68  *        information to O1 module over TCP socket with action
69  *        set to CLEAR
70  *
71  * @params[in] alarm information  
72  * @return ROK     - success
73  *         RFAILED - failure
74  ******************************************************************/
75 uint8_t clearAlarm(AlarmRecord* alrm)
76 {
77    if (openSocket(TCP_SERVER_IP,TCP_PORT) == RFAILED)
78    {
79       return RFAILED;
80    }
81    alrm->msgHeader.msgType = ALARM;
82    alrm->msgHeader.action = CLEAR_ALARM;
83    if (sendData(alrm,sizeof(AlarmRecord)) < 0)
84    {
85       closeSocket();
86       return RFAILED;
87    }
88    closeSocket();
89    return ROK;
90 }
91
92
93 /*******************************************************************
94  *
95  * @brief Raise a cell specific alarm
96  *
97  * @details
98  *
99  *    Function : raiseCellAlrm
100  *
101  *    Functionality:
102  *      - Fills the cell specific alarm parmeters and generate
103  *        the alarm
104  *
105  * @params[in] alarm Id, cell Id  
106  * @return ROK     - success
107  *         RFAILED - failure
108  ******************************************************************/
109 uint8_t raiseCellAlrm(uint16_t alrmId, uint16_t cellId)
110 {
111   char buff[BUFF_SIZE];
112   time_t now = time(NULL);
113
114   AlarmRecord alrm;
115   alrm.eventType = COMMUNICATIONS_ALARM;
116   snprintf (alrm.alarmId, sizeof(alrm.alarmId), "%d",alrmId);
117   alrm.perceivedSeverity = INDETERMINATE;
118   
119   strftime(buff, BUFF_SIZE, "%Y-%m-%d %H:%M:%S", localtime(&now));
120   if(CELL_UP_ALARM_ID == alrmId)
121   {
122     /* Fill cell up parameters */
123     snprintf (alrm.additionalText, sizeof(alrm.additionalText), \
124              "cell id  [%d] is up",cellId);
125     strcpy(alrm.additionalInfo , "cell UP");
126     strcpy(alrm.specificProblem, "Active");
127     strcpy(alrm.alarmRaiseTime, buff);
128   }
129   else
130   {
131     /* Clear the cell up alarm */
132     clearCellAlrm(CELL_UP_ALARM_ID);
133     /* Fill the cell down parameters */
134     snprintf (alrm.additionalText, sizeof(alrm.additionalText), \
135               "cell [%d] is down",cellId);
136     strcpy(alrm.additionalInfo , "cell down");
137     strcpy(alrm.specificProblem, "Active");
138     strcpy(alrm.alarmRaiseTime, buff);
139   }
140   /*Raise the alarm */
141   return raiseAlarm(&alrm);
142 }
143
144 /*******************************************************************
145  *
146  * @brief Clear the cell alarm
147  *
148  * @details
149  *
150  *    Function : clearCellAlrm
151  *
152  *    Functionality:
153  *      - Clears the cell specific alarm using alarm id
154  *
155  * @params[in] alarm Id  
156  * @return ROK     - success
157  *         RFAILED - failure
158  ******************************************************************/
159 uint8_t clearCellAlrm(uint16_t alrmId)
160 {
161   AlarmRecord alrm;
162   snprintf (alrm.alarmId, sizeof(alrm.alarmId), "%d",alrmId);
163   return clearAlarm(&alrm);
164 }
165   
166
167 /**********************************************************************
168          End of file
169 **********************************************************************/