blob: 7ce764bd99d97a5ab367ec9dba6e2d60b66c80c3 [file] [log] [blame]
Ratan Gupta90bfaea2017-10-06 20:56:31 +05301#pragma once
2
Ratan Gupta90bfaea2017-10-06 20:56:31 +05303#include "callback.hpp"
Ratan Gupta3e84ec62017-10-06 21:37:01 +05304#include "event_manager.hpp"
5
Patrick Venture3d6d3182018-08-31 09:33:09 -07006#include <phosphor-logging/log.hpp>
Ratan Gupta3e84ec62017-10-06 21:37:01 +05307#include <sstream>
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05008#include <string>
Ratan Gupta90bfaea2017-10-06 20:56:31 +05309
10namespace phosphor
11{
12namespace dbus
13{
14namespace monitoring
15{
16
17/** @class EventBase
18 * @brief Event callback implementation.
19 *
20 * The event callback creates the event dbus object
21 * which has event message and metadata as key value pairs
22 * as specified by the client supplied property index.
23 */
24class EventBase : public IndexedCallback
25{
Brad Bishopd1eac882018-03-29 10:34:05 -040026 public:
27 EventBase() = delete;
28 EventBase(const EventBase&) = delete;
29 EventBase(EventBase&&) = default;
30 EventBase& operator=(const EventBase&) = delete;
31 EventBase& operator=(EventBase&&) = default;
32 virtual ~EventBase() = default;
33 EventBase(const PropertyIndex& index) : IndexedCallback(index)
34 {
35 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053036
Brad Bishopd1eac882018-03-29 10:34:05 -040037 /** @brief Callback interface implementation. */
38 void operator()(Context ctx) override
39 {
40 if (ctx == Context::START)
Ratan Gupta90bfaea2017-10-06 20:56:31 +053041 {
Brad Bishopd1eac882018-03-29 10:34:05 -040042 // No action should be taken
43 // as this call back is being called from
44 // daemon Startup.
45 return;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053046 }
47
Brad Bishopd1eac882018-03-29 10:34:05 -040048 for (const auto& n : index)
49 {
50 const auto& path = std::get<pathIndex>(n.first);
51 const auto& propertyMeta = std::get<propertyIndex>(n.first);
52 const auto& storage = std::get<storageIndex>(n.second);
53 const auto& value = std::get<valueIndex>(storage.get());
Ratan Gupta90bfaea2017-10-06 20:56:31 +053054
Brad Bishopd1eac882018-03-29 10:34:05 -040055 if (!value.empty())
56 {
57 createEvent(path, propertyMeta, value);
58 }
59 }
60 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053061
Brad Bishopd1eac882018-03-29 10:34:05 -040062 private:
63 /** @brief Create the event Dbus Object.
64 * @param[in] path - Dbus Object Path for which the
65 * property has changed.
66 * @param[in] property - Name of the property whose value
67 * has been changed.
68 * @param[in] value - Changed property value.
69 */
70 virtual void createEvent(const std::string& path,
71 const std::string& property,
72 const any_ns::any& value) const = 0;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053073};
74
75/** @class Event
76 * @brief C++ type specific logic for the event callback.
77 *
78 * @tparam T - The C++ type of the property values being traced.
79 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070080template <typename T>
81class Event : public EventBase
Ratan Gupta90bfaea2017-10-06 20:56:31 +053082{
Brad Bishopd1eac882018-03-29 10:34:05 -040083 public:
84 Event() = delete;
85 Event(const Event&) = delete;
86 Event(Event&&) = default;
87 Event& operator=(const Event&) = delete;
88 Event& operator=(Event&&) = default;
89 ~Event() = default;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053090
Brad Bishopd1eac882018-03-29 10:34:05 -040091 /** @brief Constructor.
92 * @param[in] eventName - Name of the event.
93 * @param[in] eventMessage- Event Message.
94 * @param[in] index - look up index for the properties.
95 */
Patrick Venture0b45a3c2018-10-14 14:22:04 -070096 Event(const std::string& eventName, const std::string& eventMessage,
Brad Bishopd1eac882018-03-29 10:34:05 -040097 const PropertyIndex& index) :
98 EventBase(index),
99 name(eventName), message(eventMessage)
100 {
101 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530102
Brad Bishopd1eac882018-03-29 10:34:05 -0400103 private:
104 /** @brief Create the event Dbus Object.
105 * @param[in] path - Dbus Object Path for which the
106 * property has changed.
107 * @param[in] property - Name of the property whose value
108 * has been changed.
109 * @param[in] value - Changed property value.
110 */
111 void createEvent(const std::string& path, const std::string& property,
112 const any_ns::any& value) const override
113 {
114 std::stringstream ss{};
115 ss << any_ns::any_cast<T>(value);
116 phosphor::events::getManager().create(name, message, path, property,
117 ss.str());
118 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530119
Brad Bishopd1eac882018-03-29 10:34:05 -0400120 /** @brief Event Name */
121 std::string name;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530122
Brad Bishopd1eac882018-03-29 10:34:05 -0400123 /** @brief Event Message */
124 std::string message;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530125};
126
127} // namespace monitoring
128} // namespace dbus
129} // namespace phosphor