Merge "Fix indentation and style issues in fmAlarmUtils"

This commit is contained in:
Zuul 2019-09-18 13:47:39 +00:00 committed by Gerrit Code Review
commit 8c2ff87e57
2 changed files with 597 additions and 490 deletions

View File

@ -4,13 +4,15 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "fmAlarmUtils.h"
#include <uuid/uuid.h>
#include <stdlib.h>
#include <pthread.h>
#include <map>
#include <vector>
#include <uuid/uuid.h>
#include "fmAlarmUtils.h"
#include "fmDbUtils.h"
#include "fmLog.h"
@ -52,11 +54,13 @@ do{ \
} \
}while(0)
void add_both_tables(int id, const char *str, itos_t &t1, stoi_t &t2 ) {
t1[id] = str;
t2[str] = id;
}
static void init_tables() {
pthread_mutex_lock(&mutex);
static bool has_inited = false;
@ -184,29 +188,40 @@ static void init_tables() {
pthread_mutex_unlock(&mutex);
}
static void add_s(std::string &s) { s+="###"; };
static void add_s(std::string &s) { s+="###"; }
static std::string tostr(int id, const itos_t &t ) {
itos_t::const_iterator it = t.find(id);
if (it!=t.end()) return it->second;
if (it != t.end())
return it->second;
return "unknown";
}
static int toint(const std::string &s, const stoi_t &t) {
stoi_t::const_iterator it = t.find(s);
if (it!=t.end()) return it->second ;
if (it != t.end())
return it->second;
return 0;
}
static std::string chkstr(const std::string &s) {
if (s.length()==0) return " ";
if (s.length() == 0)
return " ";
return s;
}
static void str_to_vector(const std::string &s, std::vector<std::string> &alarm) {
static void str_to_vector(const std::string &s,
std::vector<std::string> &alarm) {
size_t offset = 0;
alarm.clear();
if (s.length() <= 3)
return;
while (true) {
size_t beg = (offset == 0) ? 0 : s.find("###", offset);
if (beg == std::string::npos) break;
@ -217,81 +232,136 @@ static void str_to_vector(const std::string &s, std::vector<std::string> &alarm)
}
}
static void fm_set_uuid(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = chkstr(a->uuid);
else STRCP_TO(a->uuid,s, RETURN);
static void fm_set_uuid(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = chkstr(a->uuid);
else
STRCP_TO(a->uuid, s, RETURN);
}
static void fm_tr_alarm_id(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = chkstr(a->alarm_id);
else STRCP_TO(a->alarm_id,s, RETURN);
static void fm_tr_alarm_id(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = chkstr(a->alarm_id);
else
STRCP_TO(a->alarm_id, s, RETURN);
}
static void fm_alarm_state(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = tostr(a->alarm_state,state_to_str);
else a->alarm_state = (EFmAlarmStateT)toint(s,state_to_int);
static void fm_alarm_state(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->alarm_state, state_to_str);
else
a->alarm_state = (EFmAlarmStateT)toint(s, state_to_int);
}
static void fm_entity_id(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = chkstr(a->entity_type_id);
else STRCP_TO(a->entity_type_id,s, RETURN);
static void fm_entity_id(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = chkstr(a->entity_type_id);
else
STRCP_TO(a->entity_type_id, s, RETURN);
}
static void fm_instance_id(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = chkstr(a->entity_instance_id);
else STRCP_TO(a->entity_instance_id,s, RETURN);
static void fm_instance_id(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = chkstr(a->entity_instance_id);
else
STRCP_TO(a->entity_instance_id, s, RETURN);
}
static void fm_timestamp(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) {
static void fm_timestamp(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
fm_db_util_make_timestamp_string(s, a->timestamp);
} else {
else
fm_db_util_get_timestamp(s.c_str(), a->timestamp);
}
static void fm_alarm_severity(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->severity, severity_to_str);
else
a->severity = (EFmAlarmSeverityT)toint(s, severity_to_int);
}
static void fm_alarm_severity(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = tostr(a->severity,severity_to_str);
else a->severity = (EFmAlarmSeverityT)toint(s,severity_to_int);
static void fm_reason_text(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = chkstr(a->reason_text);
else
STRCP_TO(a->reason_text, s, RETURN);
}
static void fm_reason_text(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = chkstr(a->reason_text);
else STRCP_TO(a->reason_text,s, RETURN);
static void fm_alarm_type(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->alarm_type, type_to_str);
else
a->alarm_type = (EFmAlarmTypeT)toint(s, type_to_int);
}
static void fm_alarm_type(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s =tostr(a->alarm_type,type_to_str);
else a->alarm_type = (EFmAlarmTypeT)toint(s,type_to_int);
static void fm_prop_cause(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->probable_cause, cause_to_str);
else
a->probable_cause = (EFmAlarmProbableCauseT)toint(s, cause_to_int);
}
static void fm_prop_cause(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = tostr(a->probable_cause,cause_to_str);
else a->probable_cause = (EFmAlarmProbableCauseT)toint(s,cause_to_int);
static void fm_repair(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = chkstr(a->proposed_repair_action);
else
STRCP_TO(a->proposed_repair_action, s, RETURN);
}
static void fm_repair(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = chkstr(a->proposed_repair_action);
else STRCP_TO(a->proposed_repair_action,s, RETURN);
static void fm_service_affect(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->service_affecting, bool_to_str);
else
a->service_affecting = (((s == "t") || (s == "True"))? 1 :0);
}
static void fm_service_affect(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = tostr(a->service_affecting,bool_to_str);
else a->service_affecting = (((s == "t") || (s == "True"))? 1 :0);
static void fm_suppression(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->suppression, bool_to_str);
else
a->suppression = (((s == "t") || (s == "True"))? 1 :0);
}
static void fm_suppression(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = tostr(a->suppression,bool_to_str);
else a->suppression = (((s == "t") || (s == "True"))? 1 :0);
static void fm_inhibit_alarm(SFmAlarmDataT *a, std::string &s,
bool is_get) {
if (is_get)
s = tostr(a->inhibit_alarms, bool_to_str);
else
a->inhibit_alarms = (((s == "t") || (s == "True"))? 1 :0);
}
static void fm_inhibit_alarm(SFmAlarmDataT *a, std::string &s, bool is_get) {
if(is_get) s = tostr(a->inhibit_alarms,bool_to_str);
else a->inhibit_alarms = (((s == "t") || (s == "True"))? 1 :0);
}
typedef void (*set_get_field_type)(SFmAlarmDataT *a, std::string &s, bool is_get);
struct alarm_struct_update_t {
EFmAlarmIndexMap id;
set_get_field_type func;
@ -323,7 +393,9 @@ static set_get_field_type find_func_set_get(EFmAlarmIndexMap id) {
return NULL;
}
bool fm_alarm_set_field(EFmAlarmIndexMap field, SFmAlarmDataT *a, std::string &val) {
bool fm_alarm_set_field(EFmAlarmIndexMap field, SFmAlarmDataT *a,
std::string &val) {
init_tables();
set_get_field_type p = find_func_set_get(field);
if (p == NULL) return false;
@ -332,6 +404,7 @@ bool fm_alarm_set_field(EFmAlarmIndexMap field, SFmAlarmDataT *a, std::string &v
return true;
}
struct log_struct_update_t {
EFmLogIndexMap id;
set_get_field_type func;
@ -348,6 +421,7 @@ struct log_struct_update_t{
{ FM_LOG_IX_SERVICE_AFFECT, fm_service_affect }
};
static set_get_field_type fm_log_find_func_set_get(EFmLogIndexMap id) {
size_t ix = 0;
size_t mx = sizeof (fm_log_convert_func) / sizeof(*fm_log_convert_func);
@ -357,7 +431,10 @@ static set_get_field_type fm_log_find_func_set_get(EFmLogIndexMap id) {
}
return NULL;
}
bool fm_log_set_field(EFmLogIndexMap field, SFmAlarmDataT *a, std::string &val) {
bool fm_log_set_field(EFmLogIndexMap field, SFmAlarmDataT *a,
std::string &val) {
init_tables();
set_get_field_type p = fm_log_find_func_set_get(field);
if (p == NULL) return false;
@ -366,6 +443,7 @@ bool fm_log_set_field(EFmLogIndexMap field, SFmAlarmDataT *a, std::string &val)
return true;
}
struct event_log_struct_update_t {
EFmEventLogIndexMap id;
set_get_field_type func;
@ -385,6 +463,7 @@ struct event_log_struct_update_t{
{ FM_EVENT_LOG_IX_SUPPRESSION, fm_suppression }
};
static set_get_field_type fm_event_log_find_func_set_get(EFmEventLogIndexMap id) {
size_t ix = 0;
size_t mx = sizeof (fm_event_log_convert_func) / sizeof(*fm_event_log_convert_func);
@ -394,7 +473,10 @@ static set_get_field_type fm_event_log_find_func_set_get(EFmEventLogIndexMap id)
}
return NULL;
}
bool fm_event_log_set_field(EFmEventLogIndexMap field, SFmAlarmDataT *a, std::string &val) {
bool fm_event_log_set_field(EFmEventLogIndexMap field, SFmAlarmDataT *a,
std::string &val) {
init_tables();
set_get_field_type p = fm_event_log_find_func_set_get(field);
if (p == NULL) return false;
@ -404,7 +486,8 @@ bool fm_event_log_set_field(EFmEventLogIndexMap field, SFmAlarmDataT *a, std::st
}
bool fm_alarm_get_field(EFmAlarmIndexMap field, const SFmAlarmDataT *a, std::string &val) {
bool fm_alarm_get_field(EFmAlarmIndexMap field, const SFmAlarmDataT *a,
std::string &val) {
init_tables();
set_get_field_type p = find_func_set_get(field);
if (p == NULL) return false;
@ -412,11 +495,13 @@ bool fm_alarm_get_field(EFmAlarmIndexMap field, const SFmAlarmDataT *a, std::str
return true;
}
static std::string fm_alarm_to_string(const SFmAlarmDataT *a) {
std::string s;
size_t ix = 0;
size_t mx = FM_ALM_IX_INHIBIT_ALARM;
std::string field;
for ( ; ix <= mx ; ++ix ) {
fm_alarm_get_field(((EFmAlarmIndexMap)ix), a, field);
s += field;
@ -425,20 +510,25 @@ static std::string fm_alarm_to_string(const SFmAlarmDataT *a) {
return s;
}
void fm_alarm_to_list(const SFmAlarmDataT *a, std::vector<std::string> &list) {
size_t ix = 0;
size_t mx = FM_ALM_IX_INHIBIT_ALARM;
std::string field;
for ( ; ix <= mx ; ++ix ) {
fm_alarm_get_field(((EFmAlarmIndexMap)ix), a, field);
list.push_back(field);
}
}
void fm_formatted_str_to_vector(const std::string &s, std::vector<std::string> &alarm) {
void fm_formatted_str_to_vector(const std::string &s,
std::vector<std::string> &alarm) {
str_to_vector(s, alarm);
}
/**
* public APIs
*/
@ -447,10 +537,12 @@ EFmErrorT fm_error_from_string(const std::string &str){
return (EFmErrorT)toint(str, err_to_int);
}
std::string fm_error_from_int(EFmErrorT id) {
return tostr((int)id, err_to_str);
}
bool fm_alarm_filter_to_string(const AlarmFilter *filter, std::string &str) {
init_tables();
@ -461,13 +553,16 @@ bool fm_alarm_filter_to_string(const AlarmFilter *filter, std::string &str) {
return true;
}
bool fm_alarm_filter_from_string(const std::string &str, AlarmFilter *filter) {
strvect_t s;
str_to_vector(str, s);
if (s.size() < 2) {
FM_ERROR_LOG("Alarm filter wrong format: %s", str.c_str());
return false;
}
init_tables();
STRCP_TO(filter->alarm_id, s[0], RETURN_FALSE);
@ -475,6 +570,7 @@ bool fm_alarm_filter_from_string(const std::string &str, AlarmFilter *filter) {
return true;
}
bool fm_alarm_to_string(const SFmAlarmDataT *alarm, std::string &str) {
init_tables();
str += fm_alarm_to_string(alarm);
@ -485,7 +581,8 @@ bool fm_alarm_from_string(const std::string &alstr, SFmAlarmDataT *a) {
strvect_t s;
str_to_vector(alstr, s);
if (s.size()<(FM_ALM_IX_MAX)) { //includes adjustment for last entry + 1 (for starting at 0)
// includes adjustment for last entry + 1 (for starting at 0)
if (s.size() < (FM_ALM_IX_MAX)) {
return false;
}
@ -500,6 +597,7 @@ bool fm_alarm_from_string(const std::string &alstr, SFmAlarmDataT *a) {
return true;
}
void fm_uuid_create(fm_uuid_t &uuid) {
uuid_t uu;
@ -508,6 +606,7 @@ void fm_uuid_create(fm_uuid_t &uuid){
uuid_unparse_lower(uu, uuid);
}
void fm_log_request(fm_buff_t &req, bool failed) {
SFmMsgHdrT *hdr = ptr_to_hdr(req);
std::string description;
@ -517,6 +616,7 @@ void fm_log_request(fm_buff_t &req, bool failed){
} else {
description.assign("Sending FM");
}
switch (hdr->action) {
case EFmCreateFault: {
SFmAlarmDataT *data = (SFmAlarmDataT *)ptr_to_data(req);
@ -586,4 +686,3 @@ void fm_log_response(fm_buff_t &req, fm_buff_t &resp, bool failed){
break;
}
}

View File

@ -63,24 +63,32 @@ enum EFmEventLogIndexMap {
FM_EVENT_LOG_IX_MAX
};
bool fm_alarm_set_field(EFmAlarmIndexMap field, SFmAlarmDataT *a, std::string &val);
bool fm_alarm_get_field(EFmAlarmIndexMap field, const SFmAlarmDataT *a, std::string &val);
bool fm_log_set_field(EFmLogIndexMap field, SFmAlarmDataT *a, std::string &val);
bool fm_event_log_set_field(EFmEventLogIndexMap field, SFmAlarmDataT *a, std::string &val);
void fm_formatted_str_to_vector(const std::string &s, std::vector<std::string> &alarm);
bool fm_alarm_to_string(const SFmAlarmDataT *alarm, std::string &str);
bool fm_alarm_from_string(const std::string &str,SFmAlarmDataT *alarm);
bool fm_alarm_set_field(EFmAlarmIndexMap field, SFmAlarmDataT *a,
std::string &val);
bool fm_alarm_get_field(EFmAlarmIndexMap field, const SFmAlarmDataT *a,
std::string &val);
bool fm_log_set_field(EFmLogIndexMap field, SFmAlarmDataT *a,
std::string &val);
bool fm_event_log_set_field(EFmEventLogIndexMap field, SFmAlarmDataT *a,
std::string &val);
void fm_formatted_str_to_vector(const std::string &s,
std::vector<std::string> &alarm);
bool fm_alarm_to_string(const SFmAlarmDataT *alarm,
std::string &str);
bool fm_alarm_from_string(const std::string &str,
SFmAlarmDataT *alarm);
/**
* This will create an alarm list from an alarm - will translate to string.
* The indexes of this API are based on EFmAlarmIndexMap
*/
void fm_alarm_to_list(const SFmAlarmDataT *a, std::vector<std::string> &list);
bool fm_alarm_filter_to_string(const AlarmFilter *alarm, std::string &str);
bool fm_alarm_filter_from_string(const std::string &str, AlarmFilter *alarm);
void fm_alarm_to_list(const SFmAlarmDataT *a,
std::vector<std::string> &list);
bool fm_alarm_filter_to_string(const AlarmFilter *alarm,
std::string &str);
bool fm_alarm_filter_from_string(const std::string &str,
AlarmFilter *alarm);
/**
* Generate a FM UUID