xref: /openbmc/bmcweb/features/redfish/include/event_service_manager.hpp (revision 7f4eb5887f9a52a2832ee9b6e06749575903128a)
1b52664e2SAppaRao Puli /*
2b52664e2SAppaRao Puli // Copyright (c) 2020 Intel Corporation
3b52664e2SAppaRao Puli //
4b52664e2SAppaRao Puli // Licensed under the Apache License, Version 2.0 (the "License");
5b52664e2SAppaRao Puli // you may not use this file except in compliance with the License.
6b52664e2SAppaRao Puli // You may obtain a copy of the License at
7b52664e2SAppaRao Puli //
8b52664e2SAppaRao Puli //      http://www.apache.org/licenses/LICENSE-2.0
9b52664e2SAppaRao Puli //
10b52664e2SAppaRao Puli // Unless required by applicable law or agreed to in writing, software
11b52664e2SAppaRao Puli // distributed under the License is distributed on an "AS IS" BASIS,
12b52664e2SAppaRao Puli // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b52664e2SAppaRao Puli // See the License for the specific language governing permissions and
14b52664e2SAppaRao Puli // limitations under the License.
15b52664e2SAppaRao Puli */
16b52664e2SAppaRao Puli #pragma once
17b52664e2SAppaRao Puli #include "node.hpp"
18*7f4eb588SAppaRao Puli #include "registries.hpp"
19*7f4eb588SAppaRao Puli #include "registries/base_message_registry.hpp"
20*7f4eb588SAppaRao Puli #include "registries/openbmc_message_registry.hpp"
21*7f4eb588SAppaRao Puli 
22*7f4eb588SAppaRao Puli #include <sys/inotify.h>
23b52664e2SAppaRao Puli 
24fb4fd5d4SZhenfei Tai #include <boost/asio/io_context.hpp>
25b52664e2SAppaRao Puli #include <boost/container/flat_map.hpp>
261214b7e7SGunnar Mills #include <error_messages.hpp>
271214b7e7SGunnar Mills #include <http_client.hpp>
281214b7e7SGunnar Mills #include <utils/json_utils.hpp>
291214b7e7SGunnar Mills 
30b52664e2SAppaRao Puli #include <cstdlib>
31b52664e2SAppaRao Puli #include <ctime>
321bf712bcSAyushi Smriti #include <fstream>
33b52664e2SAppaRao Puli #include <memory>
34b52664e2SAppaRao Puli #include <variant>
35b52664e2SAppaRao Puli 
36b52664e2SAppaRao Puli namespace redfish
37b52664e2SAppaRao Puli {
38156d6b00SAppaRao Puli 
39156d6b00SAppaRao Puli using ReadingsObjType =
40156d6b00SAppaRao Puli     std::vector<std::tuple<std::string, std::string, double, std::string>>;
417d1cc387SAppaRao Puli using EventServiceConfig = std::tuple<bool, uint32_t, uint32_t>;
42156d6b00SAppaRao Puli 
43156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event";
44156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport";
45156d6b00SAppaRao Puli 
461bf712bcSAyushi Smriti static constexpr const char* eventServiceFile =
471bf712bcSAyushi Smriti     "/var/lib/bmcweb/eventservice_config.json";
481bf712bcSAyushi Smriti 
49*7f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
50*7f4eb588SAppaRao Puli constexpr const char* redfishEventLogFile = "/var/log/redfish";
51*7f4eb588SAppaRao Puli constexpr const uint32_t inotifyFileAction = IN_MODIFY;
52*7f4eb588SAppaRao Puli std::shared_ptr<boost::asio::posix::stream_descriptor> inotifyConn = nullptr;
53*7f4eb588SAppaRao Puli 
54*7f4eb588SAppaRao Puli // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs>
55*7f4eb588SAppaRao Puli using EventLogObjectsType =
56*7f4eb588SAppaRao Puli     std::tuple<std::string, std::string, std::string, std::string, std::string,
57*7f4eb588SAppaRao Puli                boost::beast::span<std::string>>;
58*7f4eb588SAppaRao Puli 
59*7f4eb588SAppaRao Puli namespace message_registries
60*7f4eb588SAppaRao Puli {
61*7f4eb588SAppaRao Puli static const Message*
62*7f4eb588SAppaRao Puli     getMsgFromRegistry(const std::string& messageKey,
63*7f4eb588SAppaRao Puli                        const boost::beast::span<const MessageEntry>& registry)
64*7f4eb588SAppaRao Puli {
65*7f4eb588SAppaRao Puli     boost::beast::span<const MessageEntry>::const_iterator messageIt =
66*7f4eb588SAppaRao Puli         std::find_if(registry.cbegin(), registry.cend(),
67*7f4eb588SAppaRao Puli                      [&messageKey](const MessageEntry& messageEntry) {
68*7f4eb588SAppaRao Puli                          return !messageKey.compare(messageEntry.first);
69*7f4eb588SAppaRao Puli                      });
70*7f4eb588SAppaRao Puli     if (messageIt != registry.cend())
71*7f4eb588SAppaRao Puli     {
72*7f4eb588SAppaRao Puli         return &messageIt->second;
73*7f4eb588SAppaRao Puli     }
74*7f4eb588SAppaRao Puli 
75*7f4eb588SAppaRao Puli     return nullptr;
76*7f4eb588SAppaRao Puli }
77*7f4eb588SAppaRao Puli 
78*7f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID)
79*7f4eb588SAppaRao Puli {
80*7f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
81*7f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
82*7f4eb588SAppaRao Puli     // the right Message
83*7f4eb588SAppaRao Puli     std::vector<std::string> fields;
84*7f4eb588SAppaRao Puli     fields.reserve(4);
85*7f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
86*7f4eb588SAppaRao Puli     if (fields.size() != 4)
87*7f4eb588SAppaRao Puli     {
88*7f4eb588SAppaRao Puli         return nullptr;
89*7f4eb588SAppaRao Puli     }
90*7f4eb588SAppaRao Puli     std::string& registryName = fields[0];
91*7f4eb588SAppaRao Puli     std::string& messageKey = fields[3];
92*7f4eb588SAppaRao Puli 
93*7f4eb588SAppaRao Puli     // Find the right registry and check it for the MessageKey
94*7f4eb588SAppaRao Puli     if (std::string(base::header.registryPrefix) == registryName)
95*7f4eb588SAppaRao Puli     {
96*7f4eb588SAppaRao Puli         return getMsgFromRegistry(
97*7f4eb588SAppaRao Puli             messageKey, boost::beast::span<const MessageEntry>(base::registry));
98*7f4eb588SAppaRao Puli     }
99*7f4eb588SAppaRao Puli     if (std::string(openbmc::header.registryPrefix) == registryName)
100*7f4eb588SAppaRao Puli     {
101*7f4eb588SAppaRao Puli         return getMsgFromRegistry(
102*7f4eb588SAppaRao Puli             messageKey,
103*7f4eb588SAppaRao Puli             boost::beast::span<const MessageEntry>(openbmc::registry));
104*7f4eb588SAppaRao Puli     }
105*7f4eb588SAppaRao Puli     return nullptr;
106*7f4eb588SAppaRao Puli }
107*7f4eb588SAppaRao Puli } // namespace message_registries
108*7f4eb588SAppaRao Puli 
109*7f4eb588SAppaRao Puli namespace event_log
110*7f4eb588SAppaRao Puli {
111*7f4eb588SAppaRao Puli bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
112*7f4eb588SAppaRao Puli                       const bool firstEntry = true)
113*7f4eb588SAppaRao Puli {
114*7f4eb588SAppaRao Puli     static time_t prevTs = 0;
115*7f4eb588SAppaRao Puli     static int index = 0;
116*7f4eb588SAppaRao Puli     if (firstEntry)
117*7f4eb588SAppaRao Puli     {
118*7f4eb588SAppaRao Puli         prevTs = 0;
119*7f4eb588SAppaRao Puli     }
120*7f4eb588SAppaRao Puli 
121*7f4eb588SAppaRao Puli     // Get the entry timestamp
122*7f4eb588SAppaRao Puli     std::time_t curTs = 0;
123*7f4eb588SAppaRao Puli     std::tm timeStruct = {};
124*7f4eb588SAppaRao Puli     std::istringstream entryStream(logEntry);
125*7f4eb588SAppaRao Puli     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
126*7f4eb588SAppaRao Puli     {
127*7f4eb588SAppaRao Puli         curTs = std::mktime(&timeStruct);
128*7f4eb588SAppaRao Puli         if (curTs == -1)
129*7f4eb588SAppaRao Puli         {
130*7f4eb588SAppaRao Puli             return false;
131*7f4eb588SAppaRao Puli         }
132*7f4eb588SAppaRao Puli     }
133*7f4eb588SAppaRao Puli     // If the timestamp isn't unique, increment the index
134*7f4eb588SAppaRao Puli     index = (curTs == prevTs) ? index + 1 : 0;
135*7f4eb588SAppaRao Puli 
136*7f4eb588SAppaRao Puli     // Save the timestamp
137*7f4eb588SAppaRao Puli     prevTs = curTs;
138*7f4eb588SAppaRao Puli 
139*7f4eb588SAppaRao Puli     entryID = std::to_string(curTs);
140*7f4eb588SAppaRao Puli     if (index > 0)
141*7f4eb588SAppaRao Puli     {
142*7f4eb588SAppaRao Puli         entryID += "_" + std::to_string(index);
143*7f4eb588SAppaRao Puli     }
144*7f4eb588SAppaRao Puli     return true;
145*7f4eb588SAppaRao Puli }
146*7f4eb588SAppaRao Puli 
147*7f4eb588SAppaRao Puli int getEventLogParams(const std::string& logEntry, std::string& timestamp,
148*7f4eb588SAppaRao Puli                       std::string& messageID,
149*7f4eb588SAppaRao Puli                       boost::beast::span<std::string>& messageArgs)
150*7f4eb588SAppaRao Puli {
151*7f4eb588SAppaRao Puli     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
152*7f4eb588SAppaRao Puli     // First get the Timestamp
153*7f4eb588SAppaRao Puli     size_t space = logEntry.find_first_of(" ");
154*7f4eb588SAppaRao Puli     if (space == std::string::npos)
155*7f4eb588SAppaRao Puli     {
156*7f4eb588SAppaRao Puli         return -EINVAL;
157*7f4eb588SAppaRao Puli     }
158*7f4eb588SAppaRao Puli     timestamp = logEntry.substr(0, space);
159*7f4eb588SAppaRao Puli     // Then get the log contents
160*7f4eb588SAppaRao Puli     size_t entryStart = logEntry.find_first_not_of(" ", space);
161*7f4eb588SAppaRao Puli     if (entryStart == std::string::npos)
162*7f4eb588SAppaRao Puli     {
163*7f4eb588SAppaRao Puli         return -EINVAL;
164*7f4eb588SAppaRao Puli     }
165*7f4eb588SAppaRao Puli     std::string_view entry(logEntry);
166*7f4eb588SAppaRao Puli     entry.remove_prefix(entryStart);
167*7f4eb588SAppaRao Puli     // Use split to separate the entry into its fields
168*7f4eb588SAppaRao Puli     std::vector<std::string> logEntryFields;
169*7f4eb588SAppaRao Puli     boost::split(logEntryFields, entry, boost::is_any_of(","),
170*7f4eb588SAppaRao Puli                  boost::token_compress_on);
171*7f4eb588SAppaRao Puli     // We need at least a MessageId to be valid
172*7f4eb588SAppaRao Puli     if (logEntryFields.size() < 1)
173*7f4eb588SAppaRao Puli     {
174*7f4eb588SAppaRao Puli         return -EINVAL;
175*7f4eb588SAppaRao Puli     }
176*7f4eb588SAppaRao Puli     messageID = logEntryFields[0];
177*7f4eb588SAppaRao Puli 
178*7f4eb588SAppaRao Puli     // Get the MessageArgs from the log if there are any
179*7f4eb588SAppaRao Puli     if (logEntryFields.size() > 1)
180*7f4eb588SAppaRao Puli     {
181*7f4eb588SAppaRao Puli         std::string& messageArgsStart = logEntryFields[1];
182*7f4eb588SAppaRao Puli         // If the first string is empty, assume there are no MessageArgs
183*7f4eb588SAppaRao Puli         std::size_t messageArgsSize = 0;
184*7f4eb588SAppaRao Puli         if (!messageArgsStart.empty())
185*7f4eb588SAppaRao Puli         {
186*7f4eb588SAppaRao Puli             messageArgsSize = logEntryFields.size() - 1;
187*7f4eb588SAppaRao Puli         }
188*7f4eb588SAppaRao Puli 
189*7f4eb588SAppaRao Puli         messageArgs = boost::beast::span(&messageArgsStart, messageArgsSize);
190*7f4eb588SAppaRao Puli     }
191*7f4eb588SAppaRao Puli 
192*7f4eb588SAppaRao Puli     return 0;
193*7f4eb588SAppaRao Puli }
194*7f4eb588SAppaRao Puli 
195*7f4eb588SAppaRao Puli void getRegistryAndMessageKey(const std::string& messageID,
196*7f4eb588SAppaRao Puli                               std::string& registryName,
197*7f4eb588SAppaRao Puli                               std::string& messageKey)
198*7f4eb588SAppaRao Puli {
199*7f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
200*7f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
201*7f4eb588SAppaRao Puli     // the right Message
202*7f4eb588SAppaRao Puli     std::vector<std::string> fields;
203*7f4eb588SAppaRao Puli     fields.reserve(4);
204*7f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
205*7f4eb588SAppaRao Puli     if (fields.size() == 4)
206*7f4eb588SAppaRao Puli     {
207*7f4eb588SAppaRao Puli         registryName = fields[0];
208*7f4eb588SAppaRao Puli         messageKey = fields[3];
209*7f4eb588SAppaRao Puli     }
210*7f4eb588SAppaRao Puli }
211*7f4eb588SAppaRao Puli 
212*7f4eb588SAppaRao Puli int formatEventLogEntry(const std::string& logEntryID,
213*7f4eb588SAppaRao Puli                         const std::string& messageID,
214*7f4eb588SAppaRao Puli                         const boost::beast::span<std::string>& messageArgs,
215*7f4eb588SAppaRao Puli                         std::string timestamp, const std::string customText,
216*7f4eb588SAppaRao Puli                         nlohmann::json& logEntryJson)
217*7f4eb588SAppaRao Puli {
218*7f4eb588SAppaRao Puli     // Get the Message from the MessageRegistry
219*7f4eb588SAppaRao Puli     const message_registries::Message* message =
220*7f4eb588SAppaRao Puli         message_registries::formatMessage(messageID);
221*7f4eb588SAppaRao Puli 
222*7f4eb588SAppaRao Puli     std::string msg;
223*7f4eb588SAppaRao Puli     std::string severity;
224*7f4eb588SAppaRao Puli     if (message != nullptr)
225*7f4eb588SAppaRao Puli     {
226*7f4eb588SAppaRao Puli         msg = message->message;
227*7f4eb588SAppaRao Puli         severity = message->severity;
228*7f4eb588SAppaRao Puli     }
229*7f4eb588SAppaRao Puli 
230*7f4eb588SAppaRao Puli     // Fill the MessageArgs into the Message
231*7f4eb588SAppaRao Puli     int i = 0;
232*7f4eb588SAppaRao Puli     for (const std::string& messageArg : messageArgs)
233*7f4eb588SAppaRao Puli     {
234*7f4eb588SAppaRao Puli         std::string argStr = "%" + std::to_string(++i);
235*7f4eb588SAppaRao Puli         size_t argPos = msg.find(argStr);
236*7f4eb588SAppaRao Puli         if (argPos != std::string::npos)
237*7f4eb588SAppaRao Puli         {
238*7f4eb588SAppaRao Puli             msg.replace(argPos, argStr.length(), messageArg);
239*7f4eb588SAppaRao Puli         }
240*7f4eb588SAppaRao Puli     }
241*7f4eb588SAppaRao Puli 
242*7f4eb588SAppaRao Puli     // Get the Created time from the timestamp. The log timestamp is in
243*7f4eb588SAppaRao Puli     // RFC3339 format which matches the Redfish format except for the
244*7f4eb588SAppaRao Puli     // fractional seconds between the '.' and the '+', so just remove them.
245*7f4eb588SAppaRao Puli     std::size_t dot = timestamp.find_first_of(".");
246*7f4eb588SAppaRao Puli     std::size_t plus = timestamp.find_first_of("+");
247*7f4eb588SAppaRao Puli     if (dot != std::string::npos && plus != std::string::npos)
248*7f4eb588SAppaRao Puli     {
249*7f4eb588SAppaRao Puli         timestamp.erase(dot, plus - dot);
250*7f4eb588SAppaRao Puli     }
251*7f4eb588SAppaRao Puli 
252*7f4eb588SAppaRao Puli     // Fill in the log entry with the gathered data
253*7f4eb588SAppaRao Puli     logEntryJson = {{"EventId", logEntryID},
254*7f4eb588SAppaRao Puli                     {"EventType", "Event"},
255*7f4eb588SAppaRao Puli                     {"Severity", std::move(severity)},
256*7f4eb588SAppaRao Puli                     {"Message", std::move(msg)},
257*7f4eb588SAppaRao Puli                     {"MessageId", std::move(messageID)},
258*7f4eb588SAppaRao Puli                     {"MessageArgs", std::move(messageArgs)},
259*7f4eb588SAppaRao Puli                     {"EventTimestamp", std::move(timestamp)},
260*7f4eb588SAppaRao Puli                     {"Context", customText}};
261*7f4eb588SAppaRao Puli     return 0;
262*7f4eb588SAppaRao Puli }
263*7f4eb588SAppaRao Puli 
264*7f4eb588SAppaRao Puli } // namespace event_log
265*7f4eb588SAppaRao Puli #endif
266*7f4eb588SAppaRao Puli 
267b52664e2SAppaRao Puli class Subscription
268b52664e2SAppaRao Puli {
269b52664e2SAppaRao Puli   public:
270b52664e2SAppaRao Puli     std::string id;
271b52664e2SAppaRao Puli     std::string destinationUrl;
272b52664e2SAppaRao Puli     std::string protocol;
273b52664e2SAppaRao Puli     std::string retryPolicy;
274b52664e2SAppaRao Puli     std::string customText;
275b52664e2SAppaRao Puli     std::string eventFormatType;
276b52664e2SAppaRao Puli     std::string subscriptionType;
277b52664e2SAppaRao Puli     std::vector<std::string> registryMsgIds;
278b52664e2SAppaRao Puli     std::vector<std::string> registryPrefixes;
279b52664e2SAppaRao Puli     std::vector<nlohmann::json> httpHeaders; // key-value pair
280156d6b00SAppaRao Puli     std::vector<nlohmann::json> metricReportDefinitions;
281b52664e2SAppaRao Puli 
282b52664e2SAppaRao Puli     Subscription(const Subscription&) = delete;
283b52664e2SAppaRao Puli     Subscription& operator=(const Subscription&) = delete;
284b52664e2SAppaRao Puli     Subscription(Subscription&&) = delete;
285b52664e2SAppaRao Puli     Subscription& operator=(Subscription&&) = delete;
286b52664e2SAppaRao Puli 
287b52664e2SAppaRao Puli     Subscription(const std::string& inHost, const std::string& inPort,
288b52664e2SAppaRao Puli                  const std::string& inPath, const std::string& inUriProto) :
2890b4bdd93SAppaRao Puli         eventSeqNum(1),
2900b4bdd93SAppaRao Puli         host(inHost), port(inPort), path(inPath), uriProto(inUriProto)
291b52664e2SAppaRao Puli     {
292b52664e2SAppaRao Puli         conn = std::make_shared<crow::HttpClient>(
2932a5689a7SAppaRao Puli             crow::connections::systemBus->get_io_context(), host, port, path);
294b52664e2SAppaRao Puli     }
295b52664e2SAppaRao Puli     ~Subscription()
2961214b7e7SGunnar Mills     {}
297b52664e2SAppaRao Puli 
298b52664e2SAppaRao Puli     void sendEvent(const std::string& msg)
299b52664e2SAppaRao Puli     {
300b52664e2SAppaRao Puli         std::vector<std::pair<std::string, std::string>> reqHeaders;
301b52664e2SAppaRao Puli         for (const auto& header : httpHeaders)
302b52664e2SAppaRao Puli         {
303b52664e2SAppaRao Puli             for (const auto& item : header.items())
304b52664e2SAppaRao Puli             {
305b52664e2SAppaRao Puli                 std::string key = item.key();
306b52664e2SAppaRao Puli                 std::string val = item.value();
307b52664e2SAppaRao Puli                 reqHeaders.emplace_back(std::pair(key, val));
308b52664e2SAppaRao Puli             }
309b52664e2SAppaRao Puli         }
310b52664e2SAppaRao Puli         conn->setHeaders(reqHeaders);
3112a5689a7SAppaRao Puli         conn->sendData(msg);
312b52664e2SAppaRao Puli     }
313b52664e2SAppaRao Puli 
3140b4bdd93SAppaRao Puli     void sendTestEventLog()
3150b4bdd93SAppaRao Puli     {
3160b4bdd93SAppaRao Puli         nlohmann::json logEntryArray;
3170b4bdd93SAppaRao Puli         logEntryArray.push_back({});
3180b4bdd93SAppaRao Puli         nlohmann::json& logEntryJson = logEntryArray.back();
3190b4bdd93SAppaRao Puli 
3200b4bdd93SAppaRao Puli         logEntryJson = {{"EventId", "TestID"},
3210b4bdd93SAppaRao Puli                         {"EventType", "Event"},
3220b4bdd93SAppaRao Puli                         {"Severity", "OK"},
3230b4bdd93SAppaRao Puli                         {"Message", "Generated test event"},
3240b4bdd93SAppaRao Puli                         {"MessageId", "OpenBMC.0.1.TestEventLog"},
3250b4bdd93SAppaRao Puli                         {"MessageArgs", nlohmann::json::array()},
3260b4bdd93SAppaRao Puli                         {"EventTimestamp", crow::utility::dateTimeNow()},
3270b4bdd93SAppaRao Puli                         {"Context", customText}};
3280b4bdd93SAppaRao Puli 
3290b4bdd93SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
3300b4bdd93SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
3310b4bdd93SAppaRao Puli                               {"Name", "Event Log"},
3320b4bdd93SAppaRao Puli                               {"Events", logEntryArray}};
3330b4bdd93SAppaRao Puli 
3340b4bdd93SAppaRao Puli         this->sendEvent(msg.dump());
3350b4bdd93SAppaRao Puli         this->eventSeqNum++;
3360b4bdd93SAppaRao Puli     }
3370b4bdd93SAppaRao Puli 
338*7f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
339*7f4eb588SAppaRao Puli     void filterAndSendEventLogs(
340*7f4eb588SAppaRao Puli         const std::vector<EventLogObjectsType>& eventRecords)
341*7f4eb588SAppaRao Puli     {
342*7f4eb588SAppaRao Puli         nlohmann::json logEntryArray;
343*7f4eb588SAppaRao Puli         for (const EventLogObjectsType& logEntry : eventRecords)
344*7f4eb588SAppaRao Puli         {
345*7f4eb588SAppaRao Puli             const std::string& idStr = std::get<0>(logEntry);
346*7f4eb588SAppaRao Puli             const std::string& timestamp = std::get<1>(logEntry);
347*7f4eb588SAppaRao Puli             const std::string& messageID = std::get<2>(logEntry);
348*7f4eb588SAppaRao Puli             const std::string& registryName = std::get<3>(logEntry);
349*7f4eb588SAppaRao Puli             const std::string& messageKey = std::get<4>(logEntry);
350*7f4eb588SAppaRao Puli             const boost::beast::span<std::string>& messageArgs =
351*7f4eb588SAppaRao Puli                 std::get<5>(logEntry);
352*7f4eb588SAppaRao Puli 
353*7f4eb588SAppaRao Puli             // If registryPrefixes list is empty, don't filter events
354*7f4eb588SAppaRao Puli             // send everything.
355*7f4eb588SAppaRao Puli             if (registryPrefixes.size())
356*7f4eb588SAppaRao Puli             {
357*7f4eb588SAppaRao Puli                 auto obj = std::find(registryPrefixes.begin(),
358*7f4eb588SAppaRao Puli                                      registryPrefixes.end(), registryName);
359*7f4eb588SAppaRao Puli                 if (obj == registryPrefixes.end())
360*7f4eb588SAppaRao Puli                 {
361*7f4eb588SAppaRao Puli                     continue;
362*7f4eb588SAppaRao Puli                 }
363*7f4eb588SAppaRao Puli             }
364*7f4eb588SAppaRao Puli 
365*7f4eb588SAppaRao Puli             // If registryMsgIds list is empty, don't filter events
366*7f4eb588SAppaRao Puli             // send everything.
367*7f4eb588SAppaRao Puli             if (registryMsgIds.size())
368*7f4eb588SAppaRao Puli             {
369*7f4eb588SAppaRao Puli                 auto obj = std::find(registryMsgIds.begin(),
370*7f4eb588SAppaRao Puli                                      registryMsgIds.end(), messageKey);
371*7f4eb588SAppaRao Puli                 if (obj == registryMsgIds.end())
372*7f4eb588SAppaRao Puli                 {
373*7f4eb588SAppaRao Puli                     continue;
374*7f4eb588SAppaRao Puli                 }
375*7f4eb588SAppaRao Puli             }
376*7f4eb588SAppaRao Puli 
377*7f4eb588SAppaRao Puli             logEntryArray.push_back({});
378*7f4eb588SAppaRao Puli             nlohmann::json& bmcLogEntry = logEntryArray.back();
379*7f4eb588SAppaRao Puli             if (event_log::formatEventLogEntry(idStr, messageID, messageArgs,
380*7f4eb588SAppaRao Puli                                                timestamp, customText,
381*7f4eb588SAppaRao Puli                                                bmcLogEntry) != 0)
382*7f4eb588SAppaRao Puli             {
383*7f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry failed";
384*7f4eb588SAppaRao Puli                 continue;
385*7f4eb588SAppaRao Puli             }
386*7f4eb588SAppaRao Puli         }
387*7f4eb588SAppaRao Puli 
388*7f4eb588SAppaRao Puli         if (logEntryArray.size() < 1)
389*7f4eb588SAppaRao Puli         {
390*7f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "No log entries available to be transferred.";
391*7f4eb588SAppaRao Puli             return;
392*7f4eb588SAppaRao Puli         }
393*7f4eb588SAppaRao Puli 
394*7f4eb588SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
395*7f4eb588SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
396*7f4eb588SAppaRao Puli                               {"Name", "Event Log"},
397*7f4eb588SAppaRao Puli                               {"Events", logEntryArray}};
398*7f4eb588SAppaRao Puli 
399*7f4eb588SAppaRao Puli         this->sendEvent(msg.dump());
400*7f4eb588SAppaRao Puli         this->eventSeqNum++;
401*7f4eb588SAppaRao Puli     }
402*7f4eb588SAppaRao Puli #endif
403*7f4eb588SAppaRao Puli 
404156d6b00SAppaRao Puli     void filterAndSendReports(const std::string& id,
405156d6b00SAppaRao Puli                               const std::string& readingsTs,
406156d6b00SAppaRao Puli                               const ReadingsObjType& readings)
407156d6b00SAppaRao Puli     {
408156d6b00SAppaRao Puli         std::string metricReportDef =
409156d6b00SAppaRao Puli             "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id;
410156d6b00SAppaRao Puli 
411156d6b00SAppaRao Puli         // Empty list means no filter. Send everything.
412156d6b00SAppaRao Puli         if (metricReportDefinitions.size())
413156d6b00SAppaRao Puli         {
414156d6b00SAppaRao Puli             if (std::find(metricReportDefinitions.begin(),
415156d6b00SAppaRao Puli                           metricReportDefinitions.end(),
416156d6b00SAppaRao Puli                           metricReportDef) == metricReportDefinitions.end())
417156d6b00SAppaRao Puli             {
418156d6b00SAppaRao Puli                 return;
419156d6b00SAppaRao Puli             }
420156d6b00SAppaRao Puli         }
421156d6b00SAppaRao Puli 
422156d6b00SAppaRao Puli         nlohmann::json metricValuesArray = nlohmann::json::array();
423156d6b00SAppaRao Puli         for (const auto& it : readings)
424156d6b00SAppaRao Puli         {
425156d6b00SAppaRao Puli             metricValuesArray.push_back({});
426156d6b00SAppaRao Puli             nlohmann::json& entry = metricValuesArray.back();
427156d6b00SAppaRao Puli 
428156d6b00SAppaRao Puli             entry = {{"MetricId", std::get<0>(it)},
429156d6b00SAppaRao Puli                      {"MetricProperty", std::get<1>(it)},
430156d6b00SAppaRao Puli                      {"MetricValue", std::to_string(std::get<2>(it))},
431156d6b00SAppaRao Puli                      {"Timestamp", std::get<3>(it)}};
432156d6b00SAppaRao Puli         }
433156d6b00SAppaRao Puli 
434156d6b00SAppaRao Puli         nlohmann::json msg = {
435156d6b00SAppaRao Puli             {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id},
436156d6b00SAppaRao Puli             {"@odata.type", "#MetricReport.v1_3_0.MetricReport"},
437156d6b00SAppaRao Puli             {"Id", id},
438156d6b00SAppaRao Puli             {"Name", id},
439156d6b00SAppaRao Puli             {"Timestamp", readingsTs},
440156d6b00SAppaRao Puli             {"MetricReportDefinition", {{"@odata.id", metricReportDef}}},
441156d6b00SAppaRao Puli             {"MetricValues", metricValuesArray}};
442156d6b00SAppaRao Puli 
443156d6b00SAppaRao Puli         this->sendEvent(msg.dump());
444156d6b00SAppaRao Puli     }
445156d6b00SAppaRao Puli 
446b52664e2SAppaRao Puli   private:
4470b4bdd93SAppaRao Puli     uint64_t eventSeqNum;
448b52664e2SAppaRao Puli     std::string host;
449b52664e2SAppaRao Puli     std::string port;
450b52664e2SAppaRao Puli     std::string path;
451b52664e2SAppaRao Puli     std::string uriProto;
452b52664e2SAppaRao Puli     std::shared_ptr<crow::HttpClient> conn;
453b52664e2SAppaRao Puli };
454b52664e2SAppaRao Puli 
4551bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true;
4561bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3;
4571bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30;
4581bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event";
4591bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent";
4601bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries";
4611bf712bcSAyushi Smriti 
462b52664e2SAppaRao Puli class EventServiceManager
463b52664e2SAppaRao Puli {
464b52664e2SAppaRao Puli   private:
4657d1cc387SAppaRao Puli     bool serviceEnabled;
4667d1cc387SAppaRao Puli     uint32_t retryAttempts;
4677d1cc387SAppaRao Puli     uint32_t retryTimeoutInterval;
4687d1cc387SAppaRao Puli 
469b52664e2SAppaRao Puli     EventServiceManager(const EventServiceManager&) = delete;
470b52664e2SAppaRao Puli     EventServiceManager& operator=(const EventServiceManager&) = delete;
471b52664e2SAppaRao Puli     EventServiceManager(EventServiceManager&&) = delete;
472b52664e2SAppaRao Puli     EventServiceManager& operator=(EventServiceManager&&) = delete;
473b52664e2SAppaRao Puli 
4747d1cc387SAppaRao Puli     EventServiceManager() :
4757d1cc387SAppaRao Puli         noOfEventLogSubscribers(0), noOfMetricReportSubscribers(0)
476b52664e2SAppaRao Puli     {
4771bf712bcSAyushi Smriti         // Load config from persist store.
4781bf712bcSAyushi Smriti         initConfig();
479b52664e2SAppaRao Puli     }
480b52664e2SAppaRao Puli 
481*7f4eb588SAppaRao Puli     std::string lastEventTStr;
4827d1cc387SAppaRao Puli     size_t noOfEventLogSubscribers;
483156d6b00SAppaRao Puli     size_t noOfMetricReportSubscribers;
484156d6b00SAppaRao Puli     std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor;
485b52664e2SAppaRao Puli     boost::container::flat_map<std::string, std::shared_ptr<Subscription>>
486b52664e2SAppaRao Puli         subscriptionsMap;
487b52664e2SAppaRao Puli 
488b52664e2SAppaRao Puli   public:
489b52664e2SAppaRao Puli     static EventServiceManager& getInstance()
490b52664e2SAppaRao Puli     {
491b52664e2SAppaRao Puli         static EventServiceManager handler;
492b52664e2SAppaRao Puli         return handler;
493b52664e2SAppaRao Puli     }
494b52664e2SAppaRao Puli 
4951bf712bcSAyushi Smriti     void loadDefaultConfig()
4961bf712bcSAyushi Smriti     {
4971bf712bcSAyushi Smriti         serviceEnabled = defaultEnabledState;
4981bf712bcSAyushi Smriti         retryAttempts = defaultRetryAttempts;
4991bf712bcSAyushi Smriti         retryTimeoutInterval = defaultRetryInterval;
5001bf712bcSAyushi Smriti     }
5011bf712bcSAyushi Smriti 
5021bf712bcSAyushi Smriti     void initConfig()
5031bf712bcSAyushi Smriti     {
5041bf712bcSAyushi Smriti         std::ifstream eventConfigFile(eventServiceFile);
5051bf712bcSAyushi Smriti         if (!eventConfigFile.good())
5061bf712bcSAyushi Smriti         {
5071bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService config not exist";
5081bf712bcSAyushi Smriti             loadDefaultConfig();
5091bf712bcSAyushi Smriti             return;
5101bf712bcSAyushi Smriti         }
5111bf712bcSAyushi Smriti         auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false);
5121bf712bcSAyushi Smriti         if (jsonData.is_discarded())
5131bf712bcSAyushi Smriti         {
5141bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "EventService config parse error.";
5151bf712bcSAyushi Smriti             loadDefaultConfig();
5161bf712bcSAyushi Smriti             return;
5171bf712bcSAyushi Smriti         }
5181bf712bcSAyushi Smriti 
5191bf712bcSAyushi Smriti         nlohmann::json jsonConfig;
5201bf712bcSAyushi Smriti         if (json_util::getValueFromJsonObject(jsonData, "Configuration",
5211bf712bcSAyushi Smriti                                               jsonConfig))
5221bf712bcSAyushi Smriti         {
5231bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled",
5241bf712bcSAyushi Smriti                                                    serviceEnabled))
5251bf712bcSAyushi Smriti             {
5261bf712bcSAyushi Smriti                 serviceEnabled = defaultEnabledState;
5271bf712bcSAyushi Smriti             }
5281bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5291bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryAttempts", retryAttempts))
5301bf712bcSAyushi Smriti             {
5311bf712bcSAyushi Smriti                 retryAttempts = defaultRetryAttempts;
5321bf712bcSAyushi Smriti             }
5331bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5341bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryIntervalSeconds",
5351bf712bcSAyushi Smriti                     retryTimeoutInterval))
5361bf712bcSAyushi Smriti             {
5371bf712bcSAyushi Smriti                 retryTimeoutInterval = defaultRetryInterval;
5381bf712bcSAyushi Smriti             }
5391bf712bcSAyushi Smriti         }
5401bf712bcSAyushi Smriti         else
5411bf712bcSAyushi Smriti         {
5421bf712bcSAyushi Smriti             loadDefaultConfig();
5431bf712bcSAyushi Smriti         }
5441bf712bcSAyushi Smriti 
5451bf712bcSAyushi Smriti         nlohmann::json subscriptionsList;
5461bf712bcSAyushi Smriti         if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions",
5471bf712bcSAyushi Smriti                                                subscriptionsList))
5481bf712bcSAyushi Smriti         {
5491bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist.";
5501bf712bcSAyushi Smriti             return;
5511bf712bcSAyushi Smriti         }
5521bf712bcSAyushi Smriti 
5531bf712bcSAyushi Smriti         for (nlohmann::json& jsonObj : subscriptionsList)
5541bf712bcSAyushi Smriti         {
5551bf712bcSAyushi Smriti             std::string protocol;
5561bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Protocol",
5571bf712bcSAyushi Smriti                                                    protocol))
5581bf712bcSAyushi Smriti             {
5591bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist.";
5601bf712bcSAyushi Smriti                 continue;
5611bf712bcSAyushi Smriti             }
5621bf712bcSAyushi Smriti             std::string destination;
5631bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Destination",
5641bf712bcSAyushi Smriti                                                    destination))
5651bf712bcSAyushi Smriti             {
5661bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription destination exist.";
5671bf712bcSAyushi Smriti                 continue;
5681bf712bcSAyushi Smriti             }
5691bf712bcSAyushi Smriti             std::string host;
5701bf712bcSAyushi Smriti             std::string urlProto;
5711bf712bcSAyushi Smriti             std::string port;
5721bf712bcSAyushi Smriti             std::string path;
5731bf712bcSAyushi Smriti             bool status =
5741bf712bcSAyushi Smriti                 validateAndSplitUrl(destination, urlProto, host, port, path);
5751bf712bcSAyushi Smriti 
5761bf712bcSAyushi Smriti             if (!status)
5771bf712bcSAyushi Smriti             {
5781bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR
5791bf712bcSAyushi Smriti                     << "Failed to validate and split destination url";
5801bf712bcSAyushi Smriti                 continue;
5811bf712bcSAyushi Smriti             }
5821bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue =
5831bf712bcSAyushi Smriti                 std::make_shared<Subscription>(host, port, path, urlProto);
5841bf712bcSAyushi Smriti 
5851bf712bcSAyushi Smriti             subValue->destinationUrl = destination;
5861bf712bcSAyushi Smriti             subValue->protocol = protocol;
5871bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5881bf712bcSAyushi Smriti                     jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy))
5891bf712bcSAyushi Smriti             {
5901bf712bcSAyushi Smriti                 subValue->retryPolicy = defaulRetryPolicy;
5911bf712bcSAyushi Smriti             }
5921bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType",
5931bf712bcSAyushi Smriti                                                    subValue->eventFormatType))
5941bf712bcSAyushi Smriti             {
5951bf712bcSAyushi Smriti                 subValue->eventFormatType = defaulEventFormatType;
5961bf712bcSAyushi Smriti             }
5971bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType",
5981bf712bcSAyushi Smriti                                                    subValue->subscriptionType))
5991bf712bcSAyushi Smriti             {
6001bf712bcSAyushi Smriti                 subValue->subscriptionType = defaulSubscriptionType;
6011bf712bcSAyushi Smriti             }
6021bf712bcSAyushi Smriti 
6031bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "Context",
6041bf712bcSAyushi Smriti                                               subValue->customText);
6051bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "MessageIds",
6061bf712bcSAyushi Smriti                                               subValue->registryMsgIds);
6071bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes",
6081bf712bcSAyushi Smriti                                               subValue->registryPrefixes);
6091bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "HttpHeaders",
6101bf712bcSAyushi Smriti                                               subValue->httpHeaders);
6111bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(
6121bf712bcSAyushi Smriti                 jsonObj, "MetricReportDefinitions",
6131bf712bcSAyushi Smriti                 subValue->metricReportDefinitions);
6141bf712bcSAyushi Smriti 
6151bf712bcSAyushi Smriti             std::string id = addSubscription(subValue, false);
6161bf712bcSAyushi Smriti             if (id.empty())
6171bf712bcSAyushi Smriti             {
6181bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR << "Failed to add subscription";
6191bf712bcSAyushi Smriti             }
6201bf712bcSAyushi Smriti         }
6211bf712bcSAyushi Smriti         return;
6221bf712bcSAyushi Smriti     }
6231bf712bcSAyushi Smriti 
624b52664e2SAppaRao Puli     void updateSubscriptionData()
625b52664e2SAppaRao Puli     {
626b52664e2SAppaRao Puli         // Persist the config and subscription data.
6271bf712bcSAyushi Smriti         nlohmann::json jsonData;
6281bf712bcSAyushi Smriti 
6291bf712bcSAyushi Smriti         nlohmann::json& configObj = jsonData["Configuration"];
6301bf712bcSAyushi Smriti         configObj["ServiceEnabled"] = serviceEnabled;
6311bf712bcSAyushi Smriti         configObj["DeliveryRetryAttempts"] = retryAttempts;
6321bf712bcSAyushi Smriti         configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval;
6331bf712bcSAyushi Smriti 
6341bf712bcSAyushi Smriti         nlohmann::json& subListArray = jsonData["Subscriptions"];
6351bf712bcSAyushi Smriti         subListArray = nlohmann::json::array();
6361bf712bcSAyushi Smriti 
6371bf712bcSAyushi Smriti         for (const auto& it : subscriptionsMap)
6381bf712bcSAyushi Smriti         {
6391bf712bcSAyushi Smriti             nlohmann::json entry;
6401bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue = it.second;
6411bf712bcSAyushi Smriti 
6421bf712bcSAyushi Smriti             entry["Context"] = subValue->customText;
6431bf712bcSAyushi Smriti             entry["DeliveryRetryPolicy"] = subValue->retryPolicy;
6441bf712bcSAyushi Smriti             entry["Destination"] = subValue->destinationUrl;
6451bf712bcSAyushi Smriti             entry["EventFormatType"] = subValue->eventFormatType;
6461bf712bcSAyushi Smriti             entry["HttpHeaders"] = subValue->httpHeaders;
6471bf712bcSAyushi Smriti             entry["MessageIds"] = subValue->registryMsgIds;
6481bf712bcSAyushi Smriti             entry["Protocol"] = subValue->protocol;
6491bf712bcSAyushi Smriti             entry["RegistryPrefixes"] = subValue->registryPrefixes;
6501bf712bcSAyushi Smriti             entry["SubscriptionType"] = subValue->subscriptionType;
6511bf712bcSAyushi Smriti             entry["MetricReportDefinitions"] =
6521bf712bcSAyushi Smriti                 subValue->metricReportDefinitions;
6531bf712bcSAyushi Smriti 
6541bf712bcSAyushi Smriti             subListArray.push_back(entry);
6551bf712bcSAyushi Smriti         }
6561bf712bcSAyushi Smriti 
6571bf712bcSAyushi Smriti         const std::string tmpFile(std::string(eventServiceFile) + "_tmp");
6581bf712bcSAyushi Smriti         std::ofstream ofs(tmpFile, std::ios::out);
6591bf712bcSAyushi Smriti         const auto& writeData = jsonData.dump();
6601bf712bcSAyushi Smriti         ofs << writeData;
6611bf712bcSAyushi Smriti         ofs.close();
6621bf712bcSAyushi Smriti 
6631bf712bcSAyushi Smriti         BMCWEB_LOG_DEBUG << "EventService config updated to file.";
6641bf712bcSAyushi Smriti         if (std::rename(tmpFile.c_str(), eventServiceFile) != 0)
6651bf712bcSAyushi Smriti         {
6661bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "Error in renaming temporary file: "
6671bf712bcSAyushi Smriti                              << tmpFile.c_str();
6681bf712bcSAyushi Smriti         }
669b52664e2SAppaRao Puli     }
670b52664e2SAppaRao Puli 
6717d1cc387SAppaRao Puli     EventServiceConfig getEventServiceConfig()
6727d1cc387SAppaRao Puli     {
6737d1cc387SAppaRao Puli         return {serviceEnabled, retryAttempts, retryTimeoutInterval};
6747d1cc387SAppaRao Puli     }
6757d1cc387SAppaRao Puli 
6767d1cc387SAppaRao Puli     void setEventServiceConfig(const EventServiceConfig& cfg)
6777d1cc387SAppaRao Puli     {
6787d1cc387SAppaRao Puli         bool updateConfig = false;
6797d1cc387SAppaRao Puli 
6807d1cc387SAppaRao Puli         if (serviceEnabled != std::get<0>(cfg))
6817d1cc387SAppaRao Puli         {
6827d1cc387SAppaRao Puli             serviceEnabled = std::get<0>(cfg);
6837d1cc387SAppaRao Puli             if (serviceEnabled && noOfMetricReportSubscribers)
6847d1cc387SAppaRao Puli             {
6857d1cc387SAppaRao Puli                 registerMetricReportSignal();
6867d1cc387SAppaRao Puli             }
6877d1cc387SAppaRao Puli             else
6887d1cc387SAppaRao Puli             {
6897d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
6907d1cc387SAppaRao Puli             }
6917d1cc387SAppaRao Puli             updateConfig = true;
6927d1cc387SAppaRao Puli         }
6937d1cc387SAppaRao Puli 
6947d1cc387SAppaRao Puli         if (retryAttempts != std::get<1>(cfg))
6957d1cc387SAppaRao Puli         {
6967d1cc387SAppaRao Puli             retryAttempts = std::get<1>(cfg);
6977d1cc387SAppaRao Puli             updateConfig = true;
6987d1cc387SAppaRao Puli         }
6997d1cc387SAppaRao Puli 
7007d1cc387SAppaRao Puli         if (retryTimeoutInterval != std::get<2>(cfg))
7017d1cc387SAppaRao Puli         {
7027d1cc387SAppaRao Puli             retryTimeoutInterval = std::get<2>(cfg);
7037d1cc387SAppaRao Puli             updateConfig = true;
7047d1cc387SAppaRao Puli         }
7057d1cc387SAppaRao Puli 
7067d1cc387SAppaRao Puli         if (updateConfig)
7077d1cc387SAppaRao Puli         {
7087d1cc387SAppaRao Puli             updateSubscriptionData();
7097d1cc387SAppaRao Puli         }
7107d1cc387SAppaRao Puli     }
7117d1cc387SAppaRao Puli 
7127d1cc387SAppaRao Puli     void updateNoOfSubscribersCount()
7137d1cc387SAppaRao Puli     {
7147d1cc387SAppaRao Puli         size_t eventLogSubCount = 0;
7157d1cc387SAppaRao Puli         size_t metricReportSubCount = 0;
7167d1cc387SAppaRao Puli         for (const auto& it : subscriptionsMap)
7177d1cc387SAppaRao Puli         {
7187d1cc387SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
7197d1cc387SAppaRao Puli             if (entry->eventFormatType == eventFormatType)
7207d1cc387SAppaRao Puli             {
7217d1cc387SAppaRao Puli                 eventLogSubCount++;
7227d1cc387SAppaRao Puli             }
7237d1cc387SAppaRao Puli             else if (entry->eventFormatType == metricReportFormatType)
7247d1cc387SAppaRao Puli             {
7257d1cc387SAppaRao Puli                 metricReportSubCount++;
7267d1cc387SAppaRao Puli             }
7277d1cc387SAppaRao Puli         }
7287d1cc387SAppaRao Puli 
7297d1cc387SAppaRao Puli         noOfEventLogSubscribers = eventLogSubCount;
7307d1cc387SAppaRao Puli         if (noOfMetricReportSubscribers != metricReportSubCount)
7317d1cc387SAppaRao Puli         {
7327d1cc387SAppaRao Puli             noOfMetricReportSubscribers = metricReportSubCount;
7337d1cc387SAppaRao Puli             if (noOfMetricReportSubscribers)
7347d1cc387SAppaRao Puli             {
7357d1cc387SAppaRao Puli                 registerMetricReportSignal();
7367d1cc387SAppaRao Puli             }
7377d1cc387SAppaRao Puli             else
7387d1cc387SAppaRao Puli             {
7397d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
7407d1cc387SAppaRao Puli             }
7417d1cc387SAppaRao Puli         }
7427d1cc387SAppaRao Puli     }
7437d1cc387SAppaRao Puli 
744b52664e2SAppaRao Puli     std::shared_ptr<Subscription> getSubscription(const std::string& id)
745b52664e2SAppaRao Puli     {
746b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
747b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
748b52664e2SAppaRao Puli         {
749b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id;
750b52664e2SAppaRao Puli             return nullptr;
751b52664e2SAppaRao Puli         }
752b52664e2SAppaRao Puli         std::shared_ptr<Subscription> subValue = obj->second;
753b52664e2SAppaRao Puli         return subValue;
754b52664e2SAppaRao Puli     }
755b52664e2SAppaRao Puli 
7561bf712bcSAyushi Smriti     std::string addSubscription(const std::shared_ptr<Subscription> subValue,
7571bf712bcSAyushi Smriti                                 const bool updateFile = true)
758b52664e2SAppaRao Puli     {
759b52664e2SAppaRao Puli         std::srand(static_cast<uint32_t>(std::time(0)));
760b52664e2SAppaRao Puli         std::string id;
761b52664e2SAppaRao Puli 
762b52664e2SAppaRao Puli         int retry = 3;
763b52664e2SAppaRao Puli         while (retry)
764b52664e2SAppaRao Puli         {
765b52664e2SAppaRao Puli             id = std::to_string(std::rand());
766b52664e2SAppaRao Puli             auto inserted = subscriptionsMap.insert(std::pair(id, subValue));
767b52664e2SAppaRao Puli             if (inserted.second)
768b52664e2SAppaRao Puli             {
769b52664e2SAppaRao Puli                 break;
770b52664e2SAppaRao Puli             }
771b52664e2SAppaRao Puli             --retry;
772b52664e2SAppaRao Puli         };
773b52664e2SAppaRao Puli 
774b52664e2SAppaRao Puli         if (retry <= 0)
775b52664e2SAppaRao Puli         {
776b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "Failed to generate random number";
777b52664e2SAppaRao Puli             return std::string("");
778b52664e2SAppaRao Puli         }
779b52664e2SAppaRao Puli 
7807d1cc387SAppaRao Puli         updateNoOfSubscribersCount();
7811bf712bcSAyushi Smriti 
7821bf712bcSAyushi Smriti         if (updateFile)
7831bf712bcSAyushi Smriti         {
784b52664e2SAppaRao Puli             updateSubscriptionData();
7851bf712bcSAyushi Smriti         }
786*7f4eb588SAppaRao Puli 
787*7f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
788*7f4eb588SAppaRao Puli         if (lastEventTStr.empty())
789*7f4eb588SAppaRao Puli         {
790*7f4eb588SAppaRao Puli             cacheLastEventTimestamp();
791*7f4eb588SAppaRao Puli         }
792*7f4eb588SAppaRao Puli #endif
793b52664e2SAppaRao Puli         return id;
794b52664e2SAppaRao Puli     }
795b52664e2SAppaRao Puli 
796b52664e2SAppaRao Puli     bool isSubscriptionExist(const std::string& id)
797b52664e2SAppaRao Puli     {
798b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
799b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
800b52664e2SAppaRao Puli         {
801b52664e2SAppaRao Puli             return false;
802b52664e2SAppaRao Puli         }
803b52664e2SAppaRao Puli         return true;
804b52664e2SAppaRao Puli     }
805b52664e2SAppaRao Puli 
806b52664e2SAppaRao Puli     void deleteSubscription(const std::string& id)
807b52664e2SAppaRao Puli     {
808b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
809b52664e2SAppaRao Puli         if (obj != subscriptionsMap.end())
810b52664e2SAppaRao Puli         {
811b52664e2SAppaRao Puli             subscriptionsMap.erase(obj);
8127d1cc387SAppaRao Puli             updateNoOfSubscribersCount();
813b52664e2SAppaRao Puli             updateSubscriptionData();
814b52664e2SAppaRao Puli         }
815b52664e2SAppaRao Puli     }
816b52664e2SAppaRao Puli 
817b52664e2SAppaRao Puli     size_t getNumberOfSubscriptions()
818b52664e2SAppaRao Puli     {
819b52664e2SAppaRao Puli         return subscriptionsMap.size();
820b52664e2SAppaRao Puli     }
821b52664e2SAppaRao Puli 
822b52664e2SAppaRao Puli     std::vector<std::string> getAllIDs()
823b52664e2SAppaRao Puli     {
824b52664e2SAppaRao Puli         std::vector<std::string> idList;
825b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
826b52664e2SAppaRao Puli         {
827b52664e2SAppaRao Puli             idList.emplace_back(it.first);
828b52664e2SAppaRao Puli         }
829b52664e2SAppaRao Puli         return idList;
830b52664e2SAppaRao Puli     }
831b52664e2SAppaRao Puli 
832b52664e2SAppaRao Puli     bool isDestinationExist(const std::string& destUrl)
833b52664e2SAppaRao Puli     {
834b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
835b52664e2SAppaRao Puli         {
836b52664e2SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
837b52664e2SAppaRao Puli             if (entry->destinationUrl == destUrl)
838b52664e2SAppaRao Puli             {
839b52664e2SAppaRao Puli                 BMCWEB_LOG_ERROR << "Destination exist already" << destUrl;
840b52664e2SAppaRao Puli                 return true;
841b52664e2SAppaRao Puli             }
842b52664e2SAppaRao Puli         }
843b52664e2SAppaRao Puli         return false;
844b52664e2SAppaRao Puli     }
8450b4bdd93SAppaRao Puli 
8460b4bdd93SAppaRao Puli     void sendTestEventLog()
8470b4bdd93SAppaRao Puli     {
8480b4bdd93SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
8490b4bdd93SAppaRao Puli         {
8500b4bdd93SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
8510b4bdd93SAppaRao Puli             entry->sendTestEventLog();
8520b4bdd93SAppaRao Puli         }
8530b4bdd93SAppaRao Puli     }
854e9a14131SAppaRao Puli 
855*7f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
856*7f4eb588SAppaRao Puli     void cacheLastEventTimestamp()
857*7f4eb588SAppaRao Puli     {
858*7f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
859*7f4eb588SAppaRao Puli         if (!logStream.good())
860*7f4eb588SAppaRao Puli         {
861*7f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed \n";
862*7f4eb588SAppaRao Puli             return;
863*7f4eb588SAppaRao Puli         }
864*7f4eb588SAppaRao Puli         std::string logEntry;
865*7f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
866*7f4eb588SAppaRao Puli         {
867*7f4eb588SAppaRao Puli             size_t space = logEntry.find_first_of(" ");
868*7f4eb588SAppaRao Puli             if (space == std::string::npos)
869*7f4eb588SAppaRao Puli             {
870*7f4eb588SAppaRao Puli                 // Shouldn't enter here but lets skip it.
871*7f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Invalid log entry found.";
872*7f4eb588SAppaRao Puli                 continue;
873*7f4eb588SAppaRao Puli             }
874*7f4eb588SAppaRao Puli             lastEventTStr = logEntry.substr(0, space);
875*7f4eb588SAppaRao Puli         }
876*7f4eb588SAppaRao Puli         BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr;
877*7f4eb588SAppaRao Puli     }
878*7f4eb588SAppaRao Puli 
879*7f4eb588SAppaRao Puli     void readEventLogsFromFile()
880*7f4eb588SAppaRao Puli     {
881*7f4eb588SAppaRao Puli         if (!serviceEnabled || !noOfEventLogSubscribers)
882*7f4eb588SAppaRao Puli         {
883*7f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions.";
884*7f4eb588SAppaRao Puli             return;
885*7f4eb588SAppaRao Puli         }
886*7f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
887*7f4eb588SAppaRao Puli         if (!logStream.good())
888*7f4eb588SAppaRao Puli         {
889*7f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed";
890*7f4eb588SAppaRao Puli             return;
891*7f4eb588SAppaRao Puli         }
892*7f4eb588SAppaRao Puli 
893*7f4eb588SAppaRao Puli         std::vector<EventLogObjectsType> eventRecords;
894*7f4eb588SAppaRao Puli 
895*7f4eb588SAppaRao Puli         bool startLogCollection = false;
896*7f4eb588SAppaRao Puli         bool firstEntry = true;
897*7f4eb588SAppaRao Puli 
898*7f4eb588SAppaRao Puli         std::string logEntry;
899*7f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
900*7f4eb588SAppaRao Puli         {
901*7f4eb588SAppaRao Puli             if (!startLogCollection)
902*7f4eb588SAppaRao Puli             {
903*7f4eb588SAppaRao Puli                 if (boost::starts_with(logEntry, lastEventTStr))
904*7f4eb588SAppaRao Puli                 {
905*7f4eb588SAppaRao Puli                     startLogCollection = true;
906*7f4eb588SAppaRao Puli                 }
907*7f4eb588SAppaRao Puli                 continue;
908*7f4eb588SAppaRao Puli             }
909*7f4eb588SAppaRao Puli 
910*7f4eb588SAppaRao Puli             std::string idStr;
911*7f4eb588SAppaRao Puli             if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry))
912*7f4eb588SAppaRao Puli             {
913*7f4eb588SAppaRao Puli                 continue;
914*7f4eb588SAppaRao Puli             }
915*7f4eb588SAppaRao Puli             firstEntry = false;
916*7f4eb588SAppaRao Puli 
917*7f4eb588SAppaRao Puli             std::string timestamp;
918*7f4eb588SAppaRao Puli             std::string messageID;
919*7f4eb588SAppaRao Puli             boost::beast::span<std::string> messageArgs;
920*7f4eb588SAppaRao Puli             if (event_log::getEventLogParams(logEntry, timestamp, messageID,
921*7f4eb588SAppaRao Puli                                              messageArgs) != 0)
922*7f4eb588SAppaRao Puli             {
923*7f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry params failed";
924*7f4eb588SAppaRao Puli                 continue;
925*7f4eb588SAppaRao Puli             }
926*7f4eb588SAppaRao Puli 
927*7f4eb588SAppaRao Puli             std::string registryName;
928*7f4eb588SAppaRao Puli             std::string messageKey;
929*7f4eb588SAppaRao Puli             event_log::getRegistryAndMessageKey(messageID, registryName,
930*7f4eb588SAppaRao Puli                                                 messageKey);
931*7f4eb588SAppaRao Puli             if (registryName.empty() || messageKey.empty())
932*7f4eb588SAppaRao Puli             {
933*7f4eb588SAppaRao Puli                 continue;
934*7f4eb588SAppaRao Puli             }
935*7f4eb588SAppaRao Puli 
936*7f4eb588SAppaRao Puli             lastEventTStr = timestamp;
937*7f4eb588SAppaRao Puli             eventRecords.emplace_back(idStr, timestamp, messageID, registryName,
938*7f4eb588SAppaRao Puli                                       messageKey, messageArgs);
939*7f4eb588SAppaRao Puli         }
940*7f4eb588SAppaRao Puli 
941*7f4eb588SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
942*7f4eb588SAppaRao Puli         {
943*7f4eb588SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
944*7f4eb588SAppaRao Puli             if (entry->eventFormatType == "Event")
945*7f4eb588SAppaRao Puli             {
946*7f4eb588SAppaRao Puli                 entry->filterAndSendEventLogs(eventRecords);
947*7f4eb588SAppaRao Puli             }
948*7f4eb588SAppaRao Puli         }
949*7f4eb588SAppaRao Puli     }
950*7f4eb588SAppaRao Puli 
951*7f4eb588SAppaRao Puli     static void watchRedfishEventLogFile()
952*7f4eb588SAppaRao Puli     {
953*7f4eb588SAppaRao Puli         if (inotifyConn == nullptr)
954*7f4eb588SAppaRao Puli         {
955*7f4eb588SAppaRao Puli             return;
956*7f4eb588SAppaRao Puli         }
957*7f4eb588SAppaRao Puli 
958*7f4eb588SAppaRao Puli         static std::array<char, 1024> readBuffer;
959*7f4eb588SAppaRao Puli 
960*7f4eb588SAppaRao Puli         inotifyConn->async_read_some(
961*7f4eb588SAppaRao Puli             boost::asio::buffer(readBuffer),
962*7f4eb588SAppaRao Puli             [&](const boost::system::error_code& ec,
963*7f4eb588SAppaRao Puli                 const std::size_t& bytesTransferred) {
964*7f4eb588SAppaRao Puli                 if (ec)
965*7f4eb588SAppaRao Puli                 {
966*7f4eb588SAppaRao Puli                     BMCWEB_LOG_ERROR << "Callback Error: " << ec.message();
967*7f4eb588SAppaRao Puli                     return;
968*7f4eb588SAppaRao Puli                 }
969*7f4eb588SAppaRao Puli                 std::size_t index = 0;
970*7f4eb588SAppaRao Puli                 while ((index + sizeof(inotify_event)) <= bytesTransferred)
971*7f4eb588SAppaRao Puli                 {
972*7f4eb588SAppaRao Puli                     struct inotify_event event;
973*7f4eb588SAppaRao Puli                     std::memcpy(&event, &readBuffer[index],
974*7f4eb588SAppaRao Puli                                 sizeof(inotify_event));
975*7f4eb588SAppaRao Puli                     if (event.mask == inotifyFileAction)
976*7f4eb588SAppaRao Puli                     {
977*7f4eb588SAppaRao Puli                         EventServiceManager::getInstance()
978*7f4eb588SAppaRao Puli                             .readEventLogsFromFile();
979*7f4eb588SAppaRao Puli                     }
980*7f4eb588SAppaRao Puli                     index += (sizeof(inotify_event) + event.len);
981*7f4eb588SAppaRao Puli                 }
982*7f4eb588SAppaRao Puli 
983*7f4eb588SAppaRao Puli                 watchRedfishEventLogFile();
984*7f4eb588SAppaRao Puli             });
985*7f4eb588SAppaRao Puli     }
986*7f4eb588SAppaRao Puli 
987*7f4eb588SAppaRao Puli     static int startEventLogMonitor(boost::asio::io_context& ioc)
988*7f4eb588SAppaRao Puli     {
989*7f4eb588SAppaRao Puli         inotifyConn =
990*7f4eb588SAppaRao Puli             std::make_shared<boost::asio::posix::stream_descriptor>(ioc);
991*7f4eb588SAppaRao Puli         int fd = inotify_init1(IN_NONBLOCK);
992*7f4eb588SAppaRao Puli         if (fd == -1)
993*7f4eb588SAppaRao Puli         {
994*7f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << "inotify_init1 failed.";
995*7f4eb588SAppaRao Puli             return -1;
996*7f4eb588SAppaRao Puli         }
997*7f4eb588SAppaRao Puli         auto wd = inotify_add_watch(fd, redfishEventLogFile, inotifyFileAction);
998*7f4eb588SAppaRao Puli         if (wd == -1)
999*7f4eb588SAppaRao Puli         {
1000*7f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR
1001*7f4eb588SAppaRao Puli                 << "inotify_add_watch failed for redfish log file.";
1002*7f4eb588SAppaRao Puli             return -1;
1003*7f4eb588SAppaRao Puli         }
1004*7f4eb588SAppaRao Puli 
1005*7f4eb588SAppaRao Puli         // monitor redfish event log file
1006*7f4eb588SAppaRao Puli         inotifyConn->assign(fd);
1007*7f4eb588SAppaRao Puli         watchRedfishEventLogFile();
1008*7f4eb588SAppaRao Puli 
1009*7f4eb588SAppaRao Puli         return 0;
1010*7f4eb588SAppaRao Puli     }
1011*7f4eb588SAppaRao Puli 
1012*7f4eb588SAppaRao Puli #endif
1013*7f4eb588SAppaRao Puli 
1014156d6b00SAppaRao Puli     void getMetricReading(const std::string& service,
1015156d6b00SAppaRao Puli                           const std::string& objPath, const std::string& intf)
1016156d6b00SAppaRao Puli     {
1017156d6b00SAppaRao Puli         std::size_t found = objPath.find_last_of("/");
1018156d6b00SAppaRao Puli         if (found == std::string::npos)
1019156d6b00SAppaRao Puli         {
1020156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid objPath received";
1021156d6b00SAppaRao Puli             return;
1022156d6b00SAppaRao Puli         }
1023156d6b00SAppaRao Puli 
1024156d6b00SAppaRao Puli         std::string idStr = objPath.substr(found + 1);
1025156d6b00SAppaRao Puli         if (idStr.empty())
1026156d6b00SAppaRao Puli         {
1027156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid ID in objPath";
1028156d6b00SAppaRao Puli             return;
1029156d6b00SAppaRao Puli         }
1030156d6b00SAppaRao Puli 
1031156d6b00SAppaRao Puli         crow::connections::systemBus->async_method_call(
1032156d6b00SAppaRao Puli             [idStr{std::move(idStr)}](
1033156d6b00SAppaRao Puli                 const boost::system::error_code ec,
1034156d6b00SAppaRao Puli                 boost::container::flat_map<
1035156d6b00SAppaRao Puli                     std::string, std::variant<std::string, ReadingsObjType>>&
1036156d6b00SAppaRao Puli                     resp) {
1037156d6b00SAppaRao Puli                 if (ec)
1038156d6b00SAppaRao Puli                 {
1039156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG
1040156d6b00SAppaRao Puli                         << "D-Bus call failed to GetAll metric readings.";
1041156d6b00SAppaRao Puli                     return;
1042156d6b00SAppaRao Puli                 }
1043156d6b00SAppaRao Puli 
1044156d6b00SAppaRao Puli                 const std::string* timestampPtr =
1045156d6b00SAppaRao Puli                     std::get_if<std::string>(&resp["Timestamp"]);
1046156d6b00SAppaRao Puli                 if (!timestampPtr)
1047156d6b00SAppaRao Puli                 {
1048156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get timestamp.";
1049156d6b00SAppaRao Puli                     return;
1050156d6b00SAppaRao Puli                 }
1051156d6b00SAppaRao Puli 
1052156d6b00SAppaRao Puli                 ReadingsObjType* readingsPtr =
1053156d6b00SAppaRao Puli                     std::get_if<ReadingsObjType>(&resp["Readings"]);
1054156d6b00SAppaRao Puli                 if (!readingsPtr)
1055156d6b00SAppaRao Puli                 {
1056156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get Readings property.";
1057156d6b00SAppaRao Puli                     return;
1058156d6b00SAppaRao Puli                 }
1059156d6b00SAppaRao Puli 
1060156d6b00SAppaRao Puli                 if (!readingsPtr->size())
1061156d6b00SAppaRao Puli                 {
1062156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "No metrics report to be transferred";
1063156d6b00SAppaRao Puli                     return;
1064156d6b00SAppaRao Puli                 }
1065156d6b00SAppaRao Puli 
1066156d6b00SAppaRao Puli                 for (const auto& it :
1067156d6b00SAppaRao Puli                      EventServiceManager::getInstance().subscriptionsMap)
1068156d6b00SAppaRao Puli                 {
1069156d6b00SAppaRao Puli                     std::shared_ptr<Subscription> entry = it.second;
1070156d6b00SAppaRao Puli                     if (entry->eventFormatType == metricReportFormatType)
1071156d6b00SAppaRao Puli                     {
1072156d6b00SAppaRao Puli                         entry->filterAndSendReports(idStr, *timestampPtr,
1073156d6b00SAppaRao Puli                                                     *readingsPtr);
1074156d6b00SAppaRao Puli                     }
1075156d6b00SAppaRao Puli                 }
1076156d6b00SAppaRao Puli             },
1077156d6b00SAppaRao Puli             service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
1078156d6b00SAppaRao Puli             intf);
1079156d6b00SAppaRao Puli     }
1080156d6b00SAppaRao Puli 
1081156d6b00SAppaRao Puli     void unregisterMetricReportSignal()
1082156d6b00SAppaRao Puli     {
10837d1cc387SAppaRao Puli         if (matchTelemetryMonitor)
10847d1cc387SAppaRao Puli         {
1085156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister";
1086156d6b00SAppaRao Puli             matchTelemetryMonitor.reset();
1087156d6b00SAppaRao Puli             matchTelemetryMonitor = nullptr;
1088156d6b00SAppaRao Puli         }
10897d1cc387SAppaRao Puli     }
1090156d6b00SAppaRao Puli 
1091156d6b00SAppaRao Puli     void registerMetricReportSignal()
1092156d6b00SAppaRao Puli     {
10937d1cc387SAppaRao Puli         if (!serviceEnabled || matchTelemetryMonitor)
1094156d6b00SAppaRao Puli         {
10957d1cc387SAppaRao Puli             BMCWEB_LOG_DEBUG << "Not registering metric report signal.";
1096156d6b00SAppaRao Puli             return;
1097156d6b00SAppaRao Puli         }
1098156d6b00SAppaRao Puli 
1099156d6b00SAppaRao Puli         BMCWEB_LOG_DEBUG << "Metrics report signal - Register";
1100156d6b00SAppaRao Puli         std::string matchStr(
1101156d6b00SAppaRao Puli             "type='signal',member='ReportUpdate', "
1102156d6b00SAppaRao Puli             "interface='xyz.openbmc_project.MonitoringService.Report'");
1103156d6b00SAppaRao Puli 
1104156d6b00SAppaRao Puli         matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>(
1105156d6b00SAppaRao Puli             *crow::connections::systemBus, matchStr,
1106156d6b00SAppaRao Puli             [this](sdbusplus::message::message& msg) {
1107156d6b00SAppaRao Puli                 if (msg.is_method_error())
1108156d6b00SAppaRao Puli                 {
1109156d6b00SAppaRao Puli                     BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error";
1110156d6b00SAppaRao Puli                     return;
1111156d6b00SAppaRao Puli                 }
1112156d6b00SAppaRao Puli 
1113156d6b00SAppaRao Puli                 std::string service = msg.get_sender();
1114156d6b00SAppaRao Puli                 std::string objPath = msg.get_path();
1115156d6b00SAppaRao Puli                 std::string intf = msg.get_interface();
1116156d6b00SAppaRao Puli                 getMetricReading(service, objPath, intf);
1117156d6b00SAppaRao Puli             });
1118156d6b00SAppaRao Puli     }
11191bf712bcSAyushi Smriti 
11201bf712bcSAyushi Smriti     bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto,
11211bf712bcSAyushi Smriti                              std::string& host, std::string& port,
11221bf712bcSAyushi Smriti                              std::string& path)
11231bf712bcSAyushi Smriti     {
11241bf712bcSAyushi Smriti         // Validate URL using regex expression
11251bf712bcSAyushi Smriti         // Format: <protocol>://<host>:<port>/<path>
11261bf712bcSAyushi Smriti         // protocol: http/https
11271bf712bcSAyushi Smriti         const std::regex urlRegex(
11281bf712bcSAyushi Smriti             "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
11291bf712bcSAyushi Smriti             "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
11301bf712bcSAyushi Smriti         std::cmatch match;
11311bf712bcSAyushi Smriti         if (!std::regex_match(destUrl.c_str(), match, urlRegex))
11321bf712bcSAyushi Smriti         {
11331bf712bcSAyushi Smriti             BMCWEB_LOG_INFO << "Dest. url did not match ";
11341bf712bcSAyushi Smriti             return false;
11351bf712bcSAyushi Smriti         }
11361bf712bcSAyushi Smriti 
11371bf712bcSAyushi Smriti         urlProto = std::string(match[1].first, match[1].second);
11381bf712bcSAyushi Smriti         if (urlProto == "http")
11391bf712bcSAyushi Smriti         {
11401bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
11411bf712bcSAyushi Smriti             return false;
11421bf712bcSAyushi Smriti #endif
11431bf712bcSAyushi Smriti         }
11441bf712bcSAyushi Smriti 
11451bf712bcSAyushi Smriti         host = std::string(match[2].first, match[2].second);
11461bf712bcSAyushi Smriti         port = std::string(match[3].first, match[3].second);
11471bf712bcSAyushi Smriti         path = std::string(match[4].first, match[4].second);
11481bf712bcSAyushi Smriti         if (port.empty())
11491bf712bcSAyushi Smriti         {
11501bf712bcSAyushi Smriti             if (urlProto == "http")
11511bf712bcSAyushi Smriti             {
11521bf712bcSAyushi Smriti                 port = "80";
11531bf712bcSAyushi Smriti             }
11541bf712bcSAyushi Smriti             else
11551bf712bcSAyushi Smriti             {
11561bf712bcSAyushi Smriti                 port = "443";
11571bf712bcSAyushi Smriti             }
11581bf712bcSAyushi Smriti         }
11591bf712bcSAyushi Smriti         if (path.empty())
11601bf712bcSAyushi Smriti         {
11611bf712bcSAyushi Smriti             path = "/";
11621bf712bcSAyushi Smriti         }
11631bf712bcSAyushi Smriti         return true;
11641bf712bcSAyushi Smriti     }
1165*7f4eb588SAppaRao Puli }; // namespace redfish
1166b52664e2SAppaRao Puli 
1167b52664e2SAppaRao Puli } // namespace redfish
1168