[Epic-Id: ODUHIGH-611][Task-Id: ODUHIGH-627] | Issue in SCTP connection in CU_STUB
[o-du/l2.git] / src / o1 / AlarmManager.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2022] [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 AlarmManager singleton class which is responsible for 
20    storing and managing alarms.
21 */
22
23 #include <iostream>
24 #include "AlarmManager.hpp"
25
26 using std::pair;
27
28
29 /* Default constructor */
30 AlarmManager::AlarmManager()
31 {
32
33 }
34
35 /* Destructor */
36 AlarmManager::~AlarmManager()
37 {
38
39 }
40
41
42 /*******************************************************************
43  *
44  * @brief Read the data from the connected client application
45  *
46  * @details
47  *
48  *    Function : update
49  *
50  *    Functionality:
51  *      - Reads the data from the connected client application
52  *
53  *
54  * @params[in] char*
55  * @return void
56  ******************************************************************/
57
58 void AlarmManager::update(char* recvBuf)
59 {
60    O1_LOG("\nO1 AlarmManager update received");
61    AlarmRecord *alrmRec = NULL;
62    Alarm alrm;
63    MsgHeader *msgHdr = (MsgHeader*)recvBuf;
64    O1_LOG("\nO1 AlarmManager: MsgType %d",msgHdr->msgType);
65
66
67       if ( msgHdr->msgType == ALARM ){
68          uint16_t alrmId;
69          alrmRec = (AlarmRecord*) recvBuf;
70          O1_LOG("\nO1 AlarmManager:Alarm info\n"
71                    "                Action           %d\n"
72                    "                Alarm ID         %s\n"
73                    "                Severity         %d\n"
74                    "                Additional Text  %s\n"
75                    "                Specific Problem %s\n"
76                    "                Additional Info  %s\n"
77                    "                Alarm Raise Time %s\n",
78                    alrmRec->msgHeader.action,
79                    alrmRec->alarmId,
80                    alrmRec->perceivedSeverity,
81                    alrmRec->additionalText,
82                    alrmRec->specificProblem,
83                    alrmRec->additionalInfo,
84                    alrmRec->alarmRaiseTime
85                 );
86
87          /*Fill the alarm structure */
88          sscanf(alrmRec->alarmId,"%hu",&alrmId);
89          alrm.setAlarmId(alrmId);
90          alrm.setPerceivedSeverity(alrmRec->perceivedSeverity);
91          alrm.setAdditionalText(alrmRec->additionalText);
92          alrm.setEventType(alrmRec->eventType);
93          alrm.setSpecificProblem(alrmRec->specificProblem);
94          alrm.setAdditionalInfo(alrmRec->additionalInfo);
95       }
96
97       switch(msgHdr->action)
98       {
99          case RAISE_ALARM:
100
101             if(raiseAlarm(alrm))
102             {
103                 VesEventHandler vesEventHandler;
104                 if (!vesEventHandler.prepare(VesEventType::FAULT_NOTIFICATION, &alrm))
105                     return ;//O1::FAILURE;
106
107                 O1_LOG("\nO1 AlarmManager : Sending Alarm Data");
108                 if ( !vesEventHandler.send() )
109                     return ;//O1::FAILURE;
110
111                     O1_LOG("\nO1 AlarmManager : "
112                                "Alarm raised for alarm Id %s",
113                                 alrmRec->alarmId);
114             }
115
116             else
117             {
118                 O1_LOG("\nO1 AlarmManager : "
119                         "Error in raising alarm for alrm Id %s",
120                         alrmRec->alarmId);
121             }
122             break;
123
124          case CLEAR_ALARM: 
125                      if(clearAlarm(alrm))
126                      {
127                         O1_LOG("\nO1 AlarmManager : "
128                                "Alarm cleared for alarm Id %s",
129                                 alrmRec->alarmId);
130
131                      }
132                      else
133                      {
134                         O1_LOG("\nO1 AlarmManager : "
135                                "Error in clearing alarm for alarm Id %s",
136                                 alrmRec->alarmId);
137                      }
138                      break;
139          default:
140                      O1_LOG("\nO1 AlarmManager : No action performed");
141                      break;
142       }
143
144 }
145
146
147 /**********************************************************************
148    Description : Raise an Alarm and store it in alarm list
149    Params[In]  : Alarm
150    Return      : true  - insert in map successful
151                  false - insert in map unsuccessful
152 **********************************************************************/
153 bool AlarmManager::raiseAlarm(const Alarm& alarm)
154 {
155
156    pair<map<uint16_t,Alarm>::iterator,bool> ret;
157    ret = mAlarmList.insert(pair<uint16_t,Alarm>(alarm.getAlarmId(),alarm));
158    return ret.second;
159
160  
161 }
162
163
164
165 /**********************************************************************
166    Description : Clear an Alarm and delete it from alarm list
167    Params[In]  : Alarm instance
168    Return      : true  - delete successful
169                  false - delete unsuccessful
170 **********************************************************************/
171 bool AlarmManager::clearAlarm(const Alarm& alarm)
172 {
173    map<uint16_t,Alarm>::iterator it;
174    bool ret = false;
175    it = mAlarmList.find(alarm.getAlarmId());
176    if( it != mAlarmList.end() )
177    {
178       mAlarmList.erase(it);
179       ret = true;
180    }
181    return ret;
182 }
183
184
185
186 /**********************************************************************
187    Description : Clear an Alarm and delete it from alarm list with
188                  alarmId
189    Params[In]  : Alarm Id
190    Return      : true  - delete successful
191                  false - delete unsuccessful
192 **********************************************************************/
193 bool AlarmManager::clearAlarm(const uint16_t& alarmId)
194 {
195    return (mAlarmList.erase(alarmId) > 0);
196 }
197
198
199 /**********************************************************************
200    Description : Return the list of active alarms
201    Params[In]  : None
202    Return      : constant reference to the map<int, Alarm> 
203 **********************************************************************/
204 const map<uint16_t, Alarm>& AlarmManager::getAlarmList()const
205 {
206    return mAlarmList;
207 }
208
209
210 /**********************************************************************
211          End of file
212 **********************************************************************/