Fix of VES alarm issue
[o-du/l2.git] / src / o1 / ves / CellStateChangeStdDef.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 #include <iostream>
20 #include <chrono>
21 #include <iomanip>
22 #include <sstream>
23 #include "CellStateChangeStdDef.hpp"
24 #include "JsonHelper.hpp"
25 /* Default constructor*/
26 CellStateChangeStdDef::CellStateChangeStdDef() : Notification(VesEventType::FAULT_NOTIFICATION)
27 {
28
29 }
30
31 /* Default Destructor*/
32 CellStateChangeStdDef::~CellStateChangeStdDef()
33 {
34
35 }
36
37
38 /*******************************************************************
39  *
40  * @brief Returns ISO time String 
41  *
42  * @details
43  *
44  *    Function :getISOEventTime
45  *
46  *    Functionality:
47  *      - Returns ISO time String
48  *
49  * @params[in] IN - void
50  * @return value of string     - success
51  *         empty string        - failure
52  *
53  * ****************************************************************/
54
55 std::string CellStateChangeStdDef::getISOEventTime() {
56    auto now = std::chrono::system_clock::now();
57    auto itt = std::chrono::system_clock::to_time_t(now);
58    std::ostringstream os;
59    os << std::put_time(gmtime(&itt), "%FT%TZ");
60    return os.str();
61 }
62
63 /*******************************************************************
64  *
65  * @brief Returns Event Severity String
66  *
67  * @details
68  *
69  *    Function :getEventSeverity
70  *
71  *    Functionality:
72  *      - Returns Event Severity String
73  *
74  * @params[in] IN - int severity
75  * @return value of string     - success
76  *         empty string        - failure
77  *
78  * ****************************************************************/
79
80
81 std::string CellStateChangeStdDef::getEventSeverity(int severity) {
82    string str;
83    switch(severity)
84    {
85       case 3:
86          str = "CRITICAL";
87          break;
88       case 4:
89          str = "MAJOR";
90          break;
91       case 5:
92          str = "MINOR";
93          break;
94       case 6:
95          str = "WARNING";
96          break;
97       case 7:
98          str = "INDETERMINATE";
99          break;
100       case 8:
101          str = "CLEARED";
102          break;
103       default:
104          O1_LOG("\nO1 CellStateChangeStdDef: severity was not mentioned");
105          break;
106    }
107    return str;
108 }
109
110 /*******************************************************************
111  *
112  * @brief prepare CellStateChangeStdDef Fields
113  *
114  * @details
115  *
116  *    Function : prepareCellStateChangeStdDefEventFields
117  *
118  *    Functionality:
119  *      - prepare CellStateChangeStdDef Fields in json format
120  *
121  * @params[in] IN   - pointer to pnfFields
122  * @return true     - success
123  *         false    - failure
124  *
125  * ****************************************************************/
126
127 bool CellStateChangeStdDef::prepareEventFields(const Message* msg) {
128    const Alarm* alarm = dynamic_cast<const Alarm*> (msg);
129    if(!alarm) {
130       O1_LOG("\nO1 CellStateChangeStdDef : Casting failed in prepareEventFields");
131       return false;
132    }
133    bool ret = true;
134
135
136    cJSON* stndDefinedFields = this->mVesEventFields;
137
138    if(JsonHelper::addNodeToObject(stndDefinedFields, "schemaReference", FAULT_SCHEMA) == 0) {
139       ret = false;
140    }
141
142    cJSON* data = JsonHelper::createNode();
143    if(data == 0) {
144       O1_LOG("\nO1 CellStateChangeStdDef : could not create data JSON object");
145       return false;
146    }
147    else if(JsonHelper::addJsonNodeToObject(stndDefinedFields, "data", \
148                                        data) == 0) {
149       ret = false;
150    }
151    else if(JsonHelper::addNodeToObject(data, "href", HREF) == 0) {
152       ret = false;
153    }
154    else if(JsonHelper::addNodeToObject(data, "uri", URI) == 0) {
155       ret = false;
156    }
157    else if(JsonHelper::addNodeToObject(data, "notificationId", NOTIFICATION_ID) == 0) {
158       ret = false;
159    }
160    else if(JsonHelper::addNodeToObject(data, "notificationType", NOTIFICATION_TYPE) == 0) {
161       ret = false;
162    }
163    else if(JsonHelper::addNodeToObject(data, "eventTime", getISOEventTime().c_str()) == 0) {
164       ret = false;
165    }
166    else if(JsonHelper::addNodeToObject(data, "systemDN", "") == 0) {
167       ret = false;
168    }
169    else if(JsonHelper::addNodeToObject(data, "probableCause", PROBABLE_CAUSE) == 0) {
170       ret = false;
171    }
172    else if(JsonHelper::addNodeToObject(data, "perceivedSeverity", getEventSeverity(alarm->getPerceivedSeverity()).c_str()) == 0) {
173       ret = false;
174    }
175    else if(JsonHelper::addNodeToObject(data, "rootCauseIndicator", (bool)false ) == 0) {
176       ret = false;
177    }
178    else if(JsonHelper::addNodeToObject(data, "specificProblem", alarm->getAdditionalInfo().c_str()) == 0) {
179       ret = false;
180    }
181
182    cJSON*  correlatedNotificationsArr= cJSON_CreateArray();
183    if (correlatedNotificationsArr == NULL)
184    {
185       ret = false;
186    }
187    cJSON_AddItemToObject(data, "correlatedNotifications", correlatedNotificationsArr);
188
189    if(JsonHelper::addNodeToObject(data, "backedUpStatus", (bool)true) == 0) {
190       ret = false;
191    }
192    else if(JsonHelper::addNodeToObject(data, "backUpObject", "") == 0) {
193       ret = false;
194    }
195    else if(JsonHelper::addNodeToObject(data, "trendIndication", TRND_INDICATION) == 0) {
196       ret = false;
197    }
198
199    cJSON* thresholdInfo = JsonHelper::createNode();
200    if(thresholdInfo == 0) {
201       O1_LOG("\nO1 CellStateChange : could not create thresholdInfo JSON object");
202       return false;
203    }
204    else if(JsonHelper::addJsonNodeToObject(data, "thresholdInfo", \
205                                       thresholdInfo ) == 0) {
206       ret = false;
207    }
208    else if(JsonHelper::addNodeToObject(thresholdInfo, "observedMeasurement", OBSRVED_MEASUREMENT) == 0) {
209       ret = false;
210    }
211    else if(JsonHelper::addNodeToObject(thresholdInfo, "observedValue", OBSERVED_VALUE) == 0) {
212       ret = false;
213    }
214
215    cJSON*  stateChangeDefinitionArr= cJSON_CreateArray();
216    if (stateChangeDefinitionArr == NULL)
217    {
218       ret = false;
219    }
220    cJSON_AddItemToObject(data, "stateChangeDefinition", stateChangeDefinitionArr);
221
222    cJSON* monitoredAttributes = JsonHelper::createNode();
223    if( monitoredAttributes == 0) {
224       O1_LOG("\nO1 CellStateChange : could not create monitoredAttributes JSON object");
225       return false;
226    }
227    else if(JsonHelper::addJsonNodeToObject(data , "monitoredAttributes", \
228                                        monitoredAttributes ) == 0) {
229       ret = false;
230    }
231    else if(JsonHelper::addNodeToObject(monitoredAttributes, "newAtt", NEW_ATT) == 0) {
232       ret = false;
233    }
234    else if(JsonHelper::addNodeToObject(data, "proposedRepairActions", PROPOSED_REPAIR_ACTION) == 0) {
235       ret = false;
236    }
237    else if(JsonHelper::addNodeToObject(data, "additionalText", alarm->getAdditionalText().c_str())== 0) {
238       ret = false;
239    }
240
241    cJSON* additionalInformation = JsonHelper::createNode();
242    if(additionalInformation == 0) {
243       O1_LOG("\nO1 CellStateChangeStdDef : could not create additionalInformation JSON object");
244       return false;
245    }
246    else if(JsonHelper::addJsonNodeToObject(data , "additionalInformation", \
247                                        additionalInformation ) == 0) {
248       ret = false;
249    }
250    else if(JsonHelper::addNodeToObject( additionalInformation, "addInfo", alarm->getAdditionalInfo().c_str()) == 0) {
251       ret = false;
252    }
253    else if(JsonHelper::addNodeToObject(data , "alarmId", to_string(alarm->getAlarmId()).c_str()) == 0) {
254       ret = false;
255    }
256    else if(JsonHelper::addNodeToObject(data , "alarmType",ALRAM_TYPE ) == 0) {
257       ret = false;
258    }
259
260    else if(JsonHelper::addNodeToObject(stndDefinedFields, "stndDefinedFieldsVersion", STND_DEFINED_FEILD_VERSION) == 0) {
261       ret = false;
262    }
263
264
265
266    return ret;
267 }
268
269
270 /**********************************************************************
271   End of file
272  **********************************************************************/