blob: d3c88ec99c8df42883f080a18123c09a22ebc69c [file] [log] [blame]
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +02001#pragma once
2
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01003#include "interfaces/clock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01004#include "interfaces/json_storage.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02005#include "interfaces/metric.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +01006#include "interfaces/metric_listener.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "interfaces/report.hpp"
Szymon Dompkefdb06a12022-02-11 11:04:44 +01008#include "interfaces/report_factory.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009#include "interfaces/report_manager.hpp"
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +020010#include "state.hpp"
11#include "types/error_message.hpp"
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010012#include "types/readings.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010013#include "types/report_action.hpp"
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000014#include "types/report_types.hpp"
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010015#include "types/report_updates.hpp"
16#include "types/reporting_type.hpp"
Szymon Dompke3eb56862021-09-20 15:32:04 +020017#include "utils/circular_vector.hpp"
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020018#include "utils/dbus_path_utils.hpp"
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010019#include "utils/ensure.hpp"
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010020#include "utils/messanger.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020021
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020022#include <boost/asio/io_context.hpp>
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020023#include <boost/asio/steady_timer.hpp>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020024#include <sdbusplus/asio/object_server.hpp>
25
26#include <chrono>
27#include <memory>
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +010028#include <unordered_set>
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020029
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010030class Report : public interfaces::Report, public interfaces::MetricListener
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020031{
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010032 class OnChangeContext
33 {
34 public:
35 OnChangeContext(Report& report) : report(report)
36 {}
37
38 ~OnChangeContext()
39 {
40 if (updated)
41 {
42 report.updateReadings();
43 }
44 }
45
46 void metricUpdated()
47 {
48 updated = true;
49 }
50
51 private:
52 Report& report;
53 bool updated = false;
54 };
55
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020056 public:
57 Report(boost::asio::io_context& ioc,
58 const std::shared_ptr<sdbusplus::asio::object_server>& objServer,
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010059 const std::string& reportId, const std::string& reportName,
60 const ReportingType reportingType,
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010061 std::vector<ReportAction> reportActions, const Milliseconds period,
Szymon Dompke3eb56862021-09-20 15:32:04 +020062 const uint64_t appendLimitIn, const ReportUpdates reportUpdatesIn,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020063 interfaces::ReportManager& reportManager,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010064 interfaces::JsonStorage& reportStorage,
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020065 std::vector<std::shared_ptr<interfaces::Metric>> metrics,
Szymon Dompkefdb06a12022-02-11 11:04:44 +010066 const interfaces::ReportFactory& reportFactory, const bool enabled,
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +010067 std::unique_ptr<interfaces::Clock> clock, Readings);
68 ~Report();
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020069
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020070 Report(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020071 Report(Report&&) = delete;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020072 Report& operator=(const Report&) = delete;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020073 Report& operator=(Report&&) = delete;
74
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010075 std::string getId() const override
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020076 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010077 return id;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020078 }
79
80 std::string getPath() const override
81 {
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020082 return path.str;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020083 }
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020084
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010085 void metricUpdated() override;
86
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +020087 void activate();
88 void deactivate();
89
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +020090 private:
Szymon Dompkefdb06a12022-02-11 11:04:44 +010091 std::unique_ptr<sdbusplus::asio::dbus_interface>
92 makeReportInterface(const interfaces::ReportFactory& reportFactory);
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +010093 static void timerProcForPeriodicReport(boost::system::error_code,
94 Report& self);
95 static void timerProcForOnChangeReport(boost::system::error_code,
96 Report& self);
97 void scheduleTimerForPeriodicReport(Milliseconds interval);
98 void scheduleTimerForOnChangeReport();
Szymon Dompke3eb56862021-09-20 15:32:04 +020099 uint64_t deduceBufferSize(const ReportUpdates reportUpdatesIn,
100 const ReportingType reportingTypeIn) const;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100101 void setReadingBuffer(const ReportUpdates newReportUpdates);
Szymon Dompke3eb56862021-09-20 15:32:04 +0200102 void setReportUpdates(const ReportUpdates newReportUpdates);
Krzysztof Grobelny18e71012022-11-02 13:17:01 +0000103 static uint64_t getMetricCount(
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100104 const std::vector<std::shared_ptr<interfaces::Metric>>& metrics);
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100105 interfaces::JsonStorage::FilePath reportFileName() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100106 std::unordered_set<std::string>
107 collectTriggerIds(boost::asio::io_context& ioc) const;
108 bool storeConfiguration() const;
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100109 bool shouldStoreMetricValues() const;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100110 void updateReadings();
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200111 void scheduleTimer();
112 std::vector<ErrorMessage> verify() const;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200113
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100114 std::string id;
Szymon Dompke1cdd7e42022-06-08 14:43:13 +0200115 const sdbusplus::message::object_path path;
Szymon Dompkee28aa532021-10-27 12:33:12 +0200116 std::string name;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200117 ReportingType reportingType;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000118 Milliseconds interval;
Szymon Dompkefdb06a12022-02-11 11:04:44 +0100119 std::unordered_set<ReportAction> reportActions;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000120 ReadingParametersPastVersion readingParametersPastVersion;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100121 ReadingParameters readingParameters;
Krzysztof Grobelny85db8bd2021-05-28 12:13:23 +0000122 bool persistency = false;
Krzysztof Grobelny18e71012022-11-02 13:17:01 +0000123 uint64_t metricCount;
124 uint64_t appendLimit;
Szymon Dompke3eb56862021-09-20 15:32:04 +0200125 ReportUpdates reportUpdates;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200126 Readings readings = {};
Krzysztof Grobelny493e62e2022-02-14 10:55:50 +0100127 CircularVector<ReadingData> readingsBuffer;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200128 std::shared_ptr<sdbusplus::asio::object_server> objServer;
129 std::unique_ptr<sdbusplus::asio::dbus_interface> reportIface;
130 std::unique_ptr<sdbusplus::asio::dbus_interface> deleteIface;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200131 std::vector<std::shared_ptr<interfaces::Metric>> metrics;
132 boost::asio::steady_timer timer;
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +0100133 std::unordered_set<std::string> triggerIds;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200134
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100135 interfaces::JsonStorage& reportStorage;
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +0100136 std::unique_ptr<interfaces::Clock> clock;
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100137 utils::Messanger messanger;
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100138 std::optional<OnChangeContext> onChangeContext;
139 utils::Ensure<std::function<void()>> unregisterFromMetrics;
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200140 State<ReportFlags, Report, ReportFlags::enabled, ReportFlags::valid> state{
141 *this};
142 std::vector<ErrorMessage> errorMessages;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100143
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200144 public:
145 static constexpr const char* reportIfaceName =
146 "xyz.openbmc_project.Telemetry.Report";
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200147 static constexpr const char* deleteIfaceName =
148 "xyz.openbmc_project.Object.Delete";
Krzysztof Grobelny18e71012022-11-02 13:17:01 +0000149 static constexpr size_t reportVersion = 7;
Wludzik, Jozefcb88cfd2020-09-28 16:38:57 +0200150};