xref: /openbmc/telemetry/src/types/trigger_types.hpp (revision 583ba441654657bb4ba9d051b747144a7258c159)
1 #pragma once
2 
3 #include "types/sensor_types.hpp"
4 #include "utils/conversion.hpp"
5 #include "utils/labeled_tuple.hpp"
6 #include "utils/tstring.hpp"
7 #include "utils/variant_utils.hpp"
8 
9 #include <string>
10 #include <tuple>
11 #include <utility>
12 #include <variant>
13 #include <vector>
14 
15 enum class TriggerAction
16 {
17     LogToJournal = 0,
18     LogToRedfishEventLog,
19     UpdateReport,
20 };
21 
22 namespace details
23 {
24 constexpr std::array<std::pair<std::string_view, TriggerAction>, 3>
25     convDataTriggerAction = {
26         std::make_pair(
27             "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal",
28             TriggerAction::LogToJournal),
29         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.TriggerAction."
30                        "LogToRedfishEventLog",
31                        TriggerAction::LogToRedfishEventLog),
32         std::make_pair(
33             "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport",
34             TriggerAction::UpdateReport)};
35 }
36 
toTriggerAction(const std::string & str)37 inline TriggerAction toTriggerAction(const std::string& str)
38 {
39     return utils::toEnum(details::convDataTriggerAction, str);
40 }
41 
actionToString(TriggerAction v)42 inline std::string actionToString(TriggerAction v)
43 {
44     return std::string(utils::enumToString(details::convDataTriggerAction, v));
45 }
46 
47 namespace discrete
48 {
49 
50 enum class Severity
51 {
52     ok = 0,
53     warning,
54     critical
55 };
56 
57 using ThresholdParam =
58     std::tuple<std::string, std::string, uint64_t, std::string>;
59 
60 using LabeledThresholdParam = utils::LabeledTuple<
61     std::tuple<std::string, Severity, uint64_t, std::string>,
62     utils::tstring::UserId, utils::tstring::Severity, utils::tstring::DwellTime,
63     utils::tstring::ThresholdValue>;
64 } // namespace discrete
65 
66 namespace numeric
67 {
68 
69 enum class Type
70 {
71     lowerCritical = 0,
72     lowerWarning,
73     upperWarning,
74     upperCritical
75 };
76 
77 enum class Direction
78 {
79     either = 0,
80     decreasing,
81     increasing
82 };
83 
84 namespace details
85 {
86 
87 constexpr std::array<std::pair<std::string_view, Type>, 4> convDataType = {
88     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical",
89                    Type::lowerCritical),
90     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning",
91                    Type::lowerWarning),
92     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning",
93                    Type::upperWarning),
94     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical",
95                    Type::upperCritical)};
96 
97 constexpr std::array<std::pair<std::string_view, Direction>, 3>
98     convDataDirection = {
99         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Direction.Either",
100                        Direction::either),
101         std::make_pair(
102             "xyz.openbmc_project.Telemetry.Trigger.Direction.Decreasing",
103             Direction::decreasing),
104         std::make_pair(
105             "xyz.openbmc_project.Telemetry.Trigger.Direction.Increasing",
106             Direction::increasing)};
107 
108 } // namespace details
109 
toType(const std::string & str)110 inline Type toType(const std::string& str)
111 {
112     return utils::toEnum(details::convDataType, str);
113 }
114 
typeToString(Type v)115 inline std::string typeToString(Type v)
116 {
117     return std::string(utils::enumToString(details::convDataType, v));
118 }
119 
toDirection(const std::string & str)120 inline Direction toDirection(const std::string& str)
121 {
122     return utils::toEnum(details::convDataDirection, str);
123 }
124 
directionToString(Direction v)125 inline std::string directionToString(Direction v)
126 {
127     return std::string(utils::enumToString(details::convDataDirection, v));
128 }
129 
130 using ThresholdParam = std::tuple<std::string, uint64_t, std::string, double>;
131 
132 using LabeledThresholdParam = utils::LabeledTuple<
133     std::tuple<Type, uint64_t, Direction, double>, utils::tstring::Type,
134     utils::tstring::DwellTime, utils::tstring::Direction,
135     utils::tstring::ThresholdValue>;
136 } // namespace numeric
137 
138 using TriggerThresholdParams =
139     std::variant<std::vector<numeric::ThresholdParam>,
140                  std::vector<discrete::ThresholdParam>>;
141 
142 using LabeledTriggerThresholdParams =
143     std::variant<std::vector<numeric::LabeledThresholdParam>,
144                  std::vector<discrete::LabeledThresholdParam>>;
145 
146 using LabeledThresholdParam =
147     std::variant<std::monostate, numeric::LabeledThresholdParam,
148                  discrete::LabeledThresholdParam>;
149 
isTriggerThresholdDiscrete(const LabeledTriggerThresholdParams & params)150 inline bool isTriggerThresholdDiscrete(
151     const LabeledTriggerThresholdParams& params)
152 {
153     return std::holds_alternative<std::vector<discrete::LabeledThresholdParam>>(
154         params);
155 }
156 
157 using TriggerId = std::unique_ptr<const std::string>;
158 using TriggerValue = std::variant<double, std::string>;
159 using ThresholdName = std::optional<std::reference_wrapper<const std::string>>;
160 
triggerValueToString(TriggerValue val)161 inline std::string triggerValueToString(TriggerValue val)
162 {
163     if (auto* doubleVal = std::get_if<double>(&val); doubleVal != nullptr)
164     {
165         return std::to_string(*doubleVal);
166     }
167     else
168     {
169         return std::get<std::string>(val);
170     }
171 }
172 
173 namespace utils
174 {
175 
176 constexpr std::array<std::pair<std::string_view, discrete::Severity>, 3>
177     convDataSeverity = {
178         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Severity.OK",
179                        discrete::Severity::ok),
180         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Severity.Warning",
181                        discrete::Severity::warning),
182         std::make_pair(
183             "xyz.openbmc_project.Telemetry.Trigger.Severity.Critical",
184             discrete::Severity::critical)};
185 
toSeverity(const std::string & str)186 inline discrete::Severity toSeverity(const std::string& str)
187 {
188     return utils::toEnum(convDataSeverity, str);
189 }
190 
enumToString(discrete::Severity v)191 inline std::string enumToString(discrete::Severity v)
192 {
193     return std::string(enumToString(convDataSeverity, v));
194 }
195 
196 template <>
197 struct EnumTraits<TriggerAction>
198 {
199     static constexpr auto propertyName = ConstexprString{"TriggerAction"};
200 };
201 
202 template <>
203 struct EnumTraits<discrete::Severity>
204 {
205     static constexpr auto propertyName = ConstexprString{"discrete::Severity"};
206 };
207 
208 template <>
209 struct EnumTraits<numeric::Type>
210 {
211     static constexpr auto propertyName = ConstexprString{"numeric::Type"};
212 };
213 
214 template <>
215 struct EnumTraits<numeric::Direction>
216 {
217     static constexpr auto propertyName = ConstexprString{"numeric::Direction"};
218 };
219 
220 } // namespace utils
221