blob: a2a22361b114eb0f89dca6034e1faea104fc8281 [file] [log] [blame]
Ratan Gupta90bfaea2017-10-06 20:56:31 +05301#pragma once
2
3#include <phosphor-logging/log.hpp>
4#include "callback.hpp"
Ratan Gupta3e84ec62017-10-06 21:37:01 +05305#include "event_manager.hpp"
6
7#include <sstream>
Ratan Gupta90bfaea2017-10-06 20:56:31 +05308
9namespace phosphor
10{
11namespace dbus
12{
13namespace monitoring
14{
15
16/** @class EventBase
17 * @brief Event callback implementation.
18 *
19 * The event callback creates the event dbus object
20 * which has event message and metadata as key value pairs
21 * as specified by the client supplied property index.
22 */
23class EventBase : public IndexedCallback
24{
Brad Bishopd1eac882018-03-29 10:34:05 -040025 public:
26 EventBase() = delete;
27 EventBase(const EventBase&) = delete;
28 EventBase(EventBase&&) = default;
29 EventBase& operator=(const EventBase&) = delete;
30 EventBase& operator=(EventBase&&) = default;
31 virtual ~EventBase() = default;
32 EventBase(const PropertyIndex& index) : IndexedCallback(index)
33 {
34 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053035
Brad Bishopd1eac882018-03-29 10:34:05 -040036 /** @brief Callback interface implementation. */
37 void operator()(Context ctx) override
38 {
39 if (ctx == Context::START)
Ratan Gupta90bfaea2017-10-06 20:56:31 +053040 {
Brad Bishopd1eac882018-03-29 10:34:05 -040041 // No action should be taken
42 // as this call back is being called from
43 // daemon Startup.
44 return;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053045 }
46
Brad Bishopd1eac882018-03-29 10:34:05 -040047 for (const auto& n : index)
48 {
49 const auto& path = std::get<pathIndex>(n.first);
50 const auto& propertyMeta = std::get<propertyIndex>(n.first);
51 const auto& storage = std::get<storageIndex>(n.second);
52 const auto& value = std::get<valueIndex>(storage.get());
Ratan Gupta90bfaea2017-10-06 20:56:31 +053053
Brad Bishopd1eac882018-03-29 10:34:05 -040054 if (!value.empty())
55 {
56 createEvent(path, propertyMeta, value);
57 }
58 }
59 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +053060
Brad Bishopd1eac882018-03-29 10:34:05 -040061 private:
62 /** @brief Create the event Dbus Object.
63 * @param[in] path - Dbus Object Path for which the
64 * property has changed.
65 * @param[in] property - Name of the property whose value
66 * has been changed.
67 * @param[in] value - Changed property value.
68 */
69 virtual void createEvent(const std::string& path,
70 const std::string& property,
71 const any_ns::any& value) const = 0;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053072};
73
74/** @class Event
75 * @brief C++ type specific logic for the event callback.
76 *
77 * @tparam T - The C++ type of the property values being traced.
78 */
Brad Bishopd1eac882018-03-29 10:34:05 -040079template <typename T> class Event : public EventBase
Ratan Gupta90bfaea2017-10-06 20:56:31 +053080{
Brad Bishopd1eac882018-03-29 10:34:05 -040081 public:
82 Event() = delete;
83 Event(const Event&) = delete;
84 Event(Event&&) = default;
85 Event& operator=(const Event&) = delete;
86 Event& operator=(Event&&) = default;
87 ~Event() = default;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053088
Brad Bishopd1eac882018-03-29 10:34:05 -040089 /** @brief Constructor.
90 * @param[in] eventName - Name of the event.
91 * @param[in] eventMessage- Event Message.
92 * @param[in] index - look up index for the properties.
93 */
94 Event(std::string eventName, std::string eventMessage,
95 const PropertyIndex& index) :
96 EventBase(index),
97 name(eventName), message(eventMessage)
98 {
99 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530100
Brad Bishopd1eac882018-03-29 10:34:05 -0400101 private:
102 /** @brief Create the event Dbus Object.
103 * @param[in] path - Dbus Object Path for which the
104 * property has changed.
105 * @param[in] property - Name of the property whose value
106 * has been changed.
107 * @param[in] value - Changed property value.
108 */
109 void createEvent(const std::string& path, const std::string& property,
110 const any_ns::any& value) const override
111 {
112 std::stringstream ss{};
113 ss << any_ns::any_cast<T>(value);
114 phosphor::events::getManager().create(name, message, path, property,
115 ss.str());
116 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530117
Brad Bishopd1eac882018-03-29 10:34:05 -0400118 /** @brief Event Name */
119 std::string name;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530120
Brad Bishopd1eac882018-03-29 10:34:05 -0400121 /** @brief Event Message */
122 std::string message;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530123};
124
125} // namespace monitoring
126} // namespace dbus
127} // namespace phosphor