blob: 21169778a2ca9b1e71040ce464949cff2d50c6ea [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>
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 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070079template <typename T>
80class Event : public EventBase
Ratan Gupta90bfaea2017-10-06 20:56:31 +053081{
Brad Bishopd1eac882018-03-29 10:34:05 -040082 public:
83 Event() = delete;
84 Event(const Event&) = delete;
85 Event(Event&&) = default;
86 Event& operator=(const Event&) = delete;
87 Event& operator=(Event&&) = default;
88 ~Event() = default;
Ratan Gupta90bfaea2017-10-06 20:56:31 +053089
Brad Bishopd1eac882018-03-29 10:34:05 -040090 /** @brief Constructor.
91 * @param[in] eventName - Name of the event.
92 * @param[in] eventMessage- Event Message.
93 * @param[in] index - look up index for the properties.
94 */
Patrick Venture0b45a3c2018-10-14 14:22:04 -070095 Event(const std::string& eventName, const std::string& eventMessage,
Brad Bishopd1eac882018-03-29 10:34:05 -040096 const PropertyIndex& index) :
97 EventBase(index),
98 name(eventName), message(eventMessage)
99 {
100 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530101
Brad Bishopd1eac882018-03-29 10:34:05 -0400102 private:
103 /** @brief Create the event Dbus Object.
104 * @param[in] path - Dbus Object Path for which the
105 * property has changed.
106 * @param[in] property - Name of the property whose value
107 * has been changed.
108 * @param[in] value - Changed property value.
109 */
110 void createEvent(const std::string& path, const std::string& property,
111 const any_ns::any& value) const override
112 {
113 std::stringstream ss{};
114 ss << any_ns::any_cast<T>(value);
115 phosphor::events::getManager().create(name, message, path, property,
116 ss.str());
117 }
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530118
Brad Bishopd1eac882018-03-29 10:34:05 -0400119 /** @brief Event Name */
120 std::string name;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530121
Brad Bishopd1eac882018-03-29 10:34:05 -0400122 /** @brief Event Message */
123 std::string message;
Ratan Gupta90bfaea2017-10-06 20:56:31 +0530124};
125
126} // namespace monitoring
127} // namespace dbus
128} // namespace phosphor