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"
187f4eb588SAppaRao Puli #include "registries.hpp"
197f4eb588SAppaRao Puli #include "registries/base_message_registry.hpp"
207f4eb588SAppaRao Puli #include "registries/openbmc_message_registry.hpp"
217f4eb588SAppaRao Puli 
227f4eb588SAppaRao 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 
497f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
507f4eb588SAppaRao Puli std::shared_ptr<boost::asio::posix::stream_descriptor> inotifyConn = nullptr;
51*b792cc56SAppaRao Puli static constexpr const char* redfishEventLogDir = "/var/log";
52*b792cc56SAppaRao Puli static constexpr const char* redfishEventLogFile = "/var/log/redfish";
53*b792cc56SAppaRao Puli static constexpr const size_t iEventSize = sizeof(inotify_event);
54*b792cc56SAppaRao Puli static int inotifyFd = -1;
55*b792cc56SAppaRao Puli static int dirWatchDesc = -1;
56*b792cc56SAppaRao Puli static int fileWatchDesc = -1;
577f4eb588SAppaRao Puli 
587f4eb588SAppaRao Puli // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs>
597f4eb588SAppaRao Puli using EventLogObjectsType =
607f4eb588SAppaRao Puli     std::tuple<std::string, std::string, std::string, std::string, std::string,
617f4eb588SAppaRao Puli                boost::beast::span<std::string>>;
627f4eb588SAppaRao Puli 
637f4eb588SAppaRao Puli namespace message_registries
647f4eb588SAppaRao Puli {
657f4eb588SAppaRao Puli static const Message*
667f4eb588SAppaRao Puli     getMsgFromRegistry(const std::string& messageKey,
677f4eb588SAppaRao Puli                        const boost::beast::span<const MessageEntry>& registry)
687f4eb588SAppaRao Puli {
697f4eb588SAppaRao Puli     boost::beast::span<const MessageEntry>::const_iterator messageIt =
707f4eb588SAppaRao Puli         std::find_if(registry.cbegin(), registry.cend(),
717f4eb588SAppaRao Puli                      [&messageKey](const MessageEntry& messageEntry) {
727f4eb588SAppaRao Puli                          return !messageKey.compare(messageEntry.first);
737f4eb588SAppaRao Puli                      });
747f4eb588SAppaRao Puli     if (messageIt != registry.cend())
757f4eb588SAppaRao Puli     {
767f4eb588SAppaRao Puli         return &messageIt->second;
777f4eb588SAppaRao Puli     }
787f4eb588SAppaRao Puli 
797f4eb588SAppaRao Puli     return nullptr;
807f4eb588SAppaRao Puli }
817f4eb588SAppaRao Puli 
827f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID)
837f4eb588SAppaRao Puli {
847f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
857f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
867f4eb588SAppaRao Puli     // the right Message
877f4eb588SAppaRao Puli     std::vector<std::string> fields;
887f4eb588SAppaRao Puli     fields.reserve(4);
897f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
907f4eb588SAppaRao Puli     if (fields.size() != 4)
917f4eb588SAppaRao Puli     {
927f4eb588SAppaRao Puli         return nullptr;
937f4eb588SAppaRao Puli     }
947f4eb588SAppaRao Puli     std::string& registryName = fields[0];
957f4eb588SAppaRao Puli     std::string& messageKey = fields[3];
967f4eb588SAppaRao Puli 
977f4eb588SAppaRao Puli     // Find the right registry and check it for the MessageKey
987f4eb588SAppaRao Puli     if (std::string(base::header.registryPrefix) == registryName)
997f4eb588SAppaRao Puli     {
1007f4eb588SAppaRao Puli         return getMsgFromRegistry(
1017f4eb588SAppaRao Puli             messageKey, boost::beast::span<const MessageEntry>(base::registry));
1027f4eb588SAppaRao Puli     }
1037f4eb588SAppaRao Puli     if (std::string(openbmc::header.registryPrefix) == registryName)
1047f4eb588SAppaRao Puli     {
1057f4eb588SAppaRao Puli         return getMsgFromRegistry(
1067f4eb588SAppaRao Puli             messageKey,
1077f4eb588SAppaRao Puli             boost::beast::span<const MessageEntry>(openbmc::registry));
1087f4eb588SAppaRao Puli     }
1097f4eb588SAppaRao Puli     return nullptr;
1107f4eb588SAppaRao Puli }
1117f4eb588SAppaRao Puli } // namespace message_registries
1127f4eb588SAppaRao Puli 
1137f4eb588SAppaRao Puli namespace event_log
1147f4eb588SAppaRao Puli {
1157f4eb588SAppaRao Puli bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
1167f4eb588SAppaRao Puli                       const bool firstEntry = true)
1177f4eb588SAppaRao Puli {
1187f4eb588SAppaRao Puli     static time_t prevTs = 0;
1197f4eb588SAppaRao Puli     static int index = 0;
1207f4eb588SAppaRao Puli     if (firstEntry)
1217f4eb588SAppaRao Puli     {
1227f4eb588SAppaRao Puli         prevTs = 0;
1237f4eb588SAppaRao Puli     }
1247f4eb588SAppaRao Puli 
1257f4eb588SAppaRao Puli     // Get the entry timestamp
1267f4eb588SAppaRao Puli     std::time_t curTs = 0;
1277f4eb588SAppaRao Puli     std::tm timeStruct = {};
1287f4eb588SAppaRao Puli     std::istringstream entryStream(logEntry);
1297f4eb588SAppaRao Puli     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1307f4eb588SAppaRao Puli     {
1317f4eb588SAppaRao Puli         curTs = std::mktime(&timeStruct);
1327f4eb588SAppaRao Puli         if (curTs == -1)
1337f4eb588SAppaRao Puli         {
1347f4eb588SAppaRao Puli             return false;
1357f4eb588SAppaRao Puli         }
1367f4eb588SAppaRao Puli     }
1377f4eb588SAppaRao Puli     // If the timestamp isn't unique, increment the index
1387f4eb588SAppaRao Puli     index = (curTs == prevTs) ? index + 1 : 0;
1397f4eb588SAppaRao Puli 
1407f4eb588SAppaRao Puli     // Save the timestamp
1417f4eb588SAppaRao Puli     prevTs = curTs;
1427f4eb588SAppaRao Puli 
1437f4eb588SAppaRao Puli     entryID = std::to_string(curTs);
1447f4eb588SAppaRao Puli     if (index > 0)
1457f4eb588SAppaRao Puli     {
1467f4eb588SAppaRao Puli         entryID += "_" + std::to_string(index);
1477f4eb588SAppaRao Puli     }
1487f4eb588SAppaRao Puli     return true;
1497f4eb588SAppaRao Puli }
1507f4eb588SAppaRao Puli 
1517f4eb588SAppaRao Puli int getEventLogParams(const std::string& logEntry, std::string& timestamp,
1527f4eb588SAppaRao Puli                       std::string& messageID,
1537f4eb588SAppaRao Puli                       boost::beast::span<std::string>& messageArgs)
1547f4eb588SAppaRao Puli {
1557f4eb588SAppaRao Puli     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1567f4eb588SAppaRao Puli     // First get the Timestamp
1577f4eb588SAppaRao Puli     size_t space = logEntry.find_first_of(" ");
1587f4eb588SAppaRao Puli     if (space == std::string::npos)
1597f4eb588SAppaRao Puli     {
1607f4eb588SAppaRao Puli         return -EINVAL;
1617f4eb588SAppaRao Puli     }
1627f4eb588SAppaRao Puli     timestamp = logEntry.substr(0, space);
1637f4eb588SAppaRao Puli     // Then get the log contents
1647f4eb588SAppaRao Puli     size_t entryStart = logEntry.find_first_not_of(" ", space);
1657f4eb588SAppaRao Puli     if (entryStart == std::string::npos)
1667f4eb588SAppaRao Puli     {
1677f4eb588SAppaRao Puli         return -EINVAL;
1687f4eb588SAppaRao Puli     }
1697f4eb588SAppaRao Puli     std::string_view entry(logEntry);
1707f4eb588SAppaRao Puli     entry.remove_prefix(entryStart);
1717f4eb588SAppaRao Puli     // Use split to separate the entry into its fields
1727f4eb588SAppaRao Puli     std::vector<std::string> logEntryFields;
1737f4eb588SAppaRao Puli     boost::split(logEntryFields, entry, boost::is_any_of(","),
1747f4eb588SAppaRao Puli                  boost::token_compress_on);
1757f4eb588SAppaRao Puli     // We need at least a MessageId to be valid
1767f4eb588SAppaRao Puli     if (logEntryFields.size() < 1)
1777f4eb588SAppaRao Puli     {
1787f4eb588SAppaRao Puli         return -EINVAL;
1797f4eb588SAppaRao Puli     }
1807f4eb588SAppaRao Puli     messageID = logEntryFields[0];
1817f4eb588SAppaRao Puli 
1827f4eb588SAppaRao Puli     // Get the MessageArgs from the log if there are any
1837f4eb588SAppaRao Puli     if (logEntryFields.size() > 1)
1847f4eb588SAppaRao Puli     {
1857f4eb588SAppaRao Puli         std::string& messageArgsStart = logEntryFields[1];
1867f4eb588SAppaRao Puli         // If the first string is empty, assume there are no MessageArgs
1877f4eb588SAppaRao Puli         std::size_t messageArgsSize = 0;
1887f4eb588SAppaRao Puli         if (!messageArgsStart.empty())
1897f4eb588SAppaRao Puli         {
1907f4eb588SAppaRao Puli             messageArgsSize = logEntryFields.size() - 1;
1917f4eb588SAppaRao Puli         }
1927f4eb588SAppaRao Puli 
1937f4eb588SAppaRao Puli         messageArgs = boost::beast::span(&messageArgsStart, messageArgsSize);
1947f4eb588SAppaRao Puli     }
1957f4eb588SAppaRao Puli 
1967f4eb588SAppaRao Puli     return 0;
1977f4eb588SAppaRao Puli }
1987f4eb588SAppaRao Puli 
1997f4eb588SAppaRao Puli void getRegistryAndMessageKey(const std::string& messageID,
2007f4eb588SAppaRao Puli                               std::string& registryName,
2017f4eb588SAppaRao Puli                               std::string& messageKey)
2027f4eb588SAppaRao Puli {
2037f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
2047f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
2057f4eb588SAppaRao Puli     // the right Message
2067f4eb588SAppaRao Puli     std::vector<std::string> fields;
2077f4eb588SAppaRao Puli     fields.reserve(4);
2087f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
2097f4eb588SAppaRao Puli     if (fields.size() == 4)
2107f4eb588SAppaRao Puli     {
2117f4eb588SAppaRao Puli         registryName = fields[0];
2127f4eb588SAppaRao Puli         messageKey = fields[3];
2137f4eb588SAppaRao Puli     }
2147f4eb588SAppaRao Puli }
2157f4eb588SAppaRao Puli 
2167f4eb588SAppaRao Puli int formatEventLogEntry(const std::string& logEntryID,
2177f4eb588SAppaRao Puli                         const std::string& messageID,
2187f4eb588SAppaRao Puli                         const boost::beast::span<std::string>& messageArgs,
2197f4eb588SAppaRao Puli                         std::string timestamp, const std::string customText,
2207f4eb588SAppaRao Puli                         nlohmann::json& logEntryJson)
2217f4eb588SAppaRao Puli {
2227f4eb588SAppaRao Puli     // Get the Message from the MessageRegistry
2237f4eb588SAppaRao Puli     const message_registries::Message* message =
2247f4eb588SAppaRao Puli         message_registries::formatMessage(messageID);
2257f4eb588SAppaRao Puli 
2267f4eb588SAppaRao Puli     std::string msg;
2277f4eb588SAppaRao Puli     std::string severity;
2287f4eb588SAppaRao Puli     if (message != nullptr)
2297f4eb588SAppaRao Puli     {
2307f4eb588SAppaRao Puli         msg = message->message;
2317f4eb588SAppaRao Puli         severity = message->severity;
2327f4eb588SAppaRao Puli     }
2337f4eb588SAppaRao Puli 
2347f4eb588SAppaRao Puli     // Fill the MessageArgs into the Message
2357f4eb588SAppaRao Puli     int i = 0;
2367f4eb588SAppaRao Puli     for (const std::string& messageArg : messageArgs)
2377f4eb588SAppaRao Puli     {
2387f4eb588SAppaRao Puli         std::string argStr = "%" + std::to_string(++i);
2397f4eb588SAppaRao Puli         size_t argPos = msg.find(argStr);
2407f4eb588SAppaRao Puli         if (argPos != std::string::npos)
2417f4eb588SAppaRao Puli         {
2427f4eb588SAppaRao Puli             msg.replace(argPos, argStr.length(), messageArg);
2437f4eb588SAppaRao Puli         }
2447f4eb588SAppaRao Puli     }
2457f4eb588SAppaRao Puli 
2467f4eb588SAppaRao Puli     // Get the Created time from the timestamp. The log timestamp is in
2477f4eb588SAppaRao Puli     // RFC3339 format which matches the Redfish format except for the
2487f4eb588SAppaRao Puli     // fractional seconds between the '.' and the '+', so just remove them.
2497f4eb588SAppaRao Puli     std::size_t dot = timestamp.find_first_of(".");
2507f4eb588SAppaRao Puli     std::size_t plus = timestamp.find_first_of("+");
2517f4eb588SAppaRao Puli     if (dot != std::string::npos && plus != std::string::npos)
2527f4eb588SAppaRao Puli     {
2537f4eb588SAppaRao Puli         timestamp.erase(dot, plus - dot);
2547f4eb588SAppaRao Puli     }
2557f4eb588SAppaRao Puli 
2567f4eb588SAppaRao Puli     // Fill in the log entry with the gathered data
2577f4eb588SAppaRao Puli     logEntryJson = {{"EventId", logEntryID},
2587f4eb588SAppaRao Puli                     {"EventType", "Event"},
2597f4eb588SAppaRao Puli                     {"Severity", std::move(severity)},
2607f4eb588SAppaRao Puli                     {"Message", std::move(msg)},
2617f4eb588SAppaRao Puli                     {"MessageId", std::move(messageID)},
2627f4eb588SAppaRao Puli                     {"MessageArgs", std::move(messageArgs)},
2637f4eb588SAppaRao Puli                     {"EventTimestamp", std::move(timestamp)},
2647f4eb588SAppaRao Puli                     {"Context", customText}};
2657f4eb588SAppaRao Puli     return 0;
2667f4eb588SAppaRao Puli }
2677f4eb588SAppaRao Puli 
2687f4eb588SAppaRao Puli } // namespace event_log
2697f4eb588SAppaRao Puli #endif
2707f4eb588SAppaRao Puli 
271b52664e2SAppaRao Puli class Subscription
272b52664e2SAppaRao Puli {
273b52664e2SAppaRao Puli   public:
274b52664e2SAppaRao Puli     std::string id;
275b52664e2SAppaRao Puli     std::string destinationUrl;
276b52664e2SAppaRao Puli     std::string protocol;
277b52664e2SAppaRao Puli     std::string retryPolicy;
278b52664e2SAppaRao Puli     std::string customText;
279b52664e2SAppaRao Puli     std::string eventFormatType;
280b52664e2SAppaRao Puli     std::string subscriptionType;
281b52664e2SAppaRao Puli     std::vector<std::string> registryMsgIds;
282b52664e2SAppaRao Puli     std::vector<std::string> registryPrefixes;
283b52664e2SAppaRao Puli     std::vector<nlohmann::json> httpHeaders; // key-value pair
284156d6b00SAppaRao Puli     std::vector<nlohmann::json> metricReportDefinitions;
285b52664e2SAppaRao Puli 
286b52664e2SAppaRao Puli     Subscription(const Subscription&) = delete;
287b52664e2SAppaRao Puli     Subscription& operator=(const Subscription&) = delete;
288b52664e2SAppaRao Puli     Subscription(Subscription&&) = delete;
289b52664e2SAppaRao Puli     Subscription& operator=(Subscription&&) = delete;
290b52664e2SAppaRao Puli 
291b52664e2SAppaRao Puli     Subscription(const std::string& inHost, const std::string& inPort,
292b52664e2SAppaRao Puli                  const std::string& inPath, const std::string& inUriProto) :
2930b4bdd93SAppaRao Puli         eventSeqNum(1),
2940b4bdd93SAppaRao Puli         host(inHost), port(inPort), path(inPath), uriProto(inUriProto)
295b52664e2SAppaRao Puli     {
296b52664e2SAppaRao Puli         conn = std::make_shared<crow::HttpClient>(
2972a5689a7SAppaRao Puli             crow::connections::systemBus->get_io_context(), host, port, path);
298b52664e2SAppaRao Puli     }
299b52664e2SAppaRao Puli     ~Subscription()
3001214b7e7SGunnar Mills     {}
301b52664e2SAppaRao Puli 
302b52664e2SAppaRao Puli     void sendEvent(const std::string& msg)
303b52664e2SAppaRao Puli     {
304b52664e2SAppaRao Puli         std::vector<std::pair<std::string, std::string>> reqHeaders;
305b52664e2SAppaRao Puli         for (const auto& header : httpHeaders)
306b52664e2SAppaRao Puli         {
307b52664e2SAppaRao Puli             for (const auto& item : header.items())
308b52664e2SAppaRao Puli             {
309b52664e2SAppaRao Puli                 std::string key = item.key();
310b52664e2SAppaRao Puli                 std::string val = item.value();
311b52664e2SAppaRao Puli                 reqHeaders.emplace_back(std::pair(key, val));
312b52664e2SAppaRao Puli             }
313b52664e2SAppaRao Puli         }
314b52664e2SAppaRao Puli         conn->setHeaders(reqHeaders);
3152a5689a7SAppaRao Puli         conn->sendData(msg);
316b52664e2SAppaRao Puli     }
317b52664e2SAppaRao Puli 
3180b4bdd93SAppaRao Puli     void sendTestEventLog()
3190b4bdd93SAppaRao Puli     {
3200b4bdd93SAppaRao Puli         nlohmann::json logEntryArray;
3210b4bdd93SAppaRao Puli         logEntryArray.push_back({});
3220b4bdd93SAppaRao Puli         nlohmann::json& logEntryJson = logEntryArray.back();
3230b4bdd93SAppaRao Puli 
3240b4bdd93SAppaRao Puli         logEntryJson = {{"EventId", "TestID"},
3250b4bdd93SAppaRao Puli                         {"EventType", "Event"},
3260b4bdd93SAppaRao Puli                         {"Severity", "OK"},
3270b4bdd93SAppaRao Puli                         {"Message", "Generated test event"},
3280b4bdd93SAppaRao Puli                         {"MessageId", "OpenBMC.0.1.TestEventLog"},
3290b4bdd93SAppaRao Puli                         {"MessageArgs", nlohmann::json::array()},
3300b4bdd93SAppaRao Puli                         {"EventTimestamp", crow::utility::dateTimeNow()},
3310b4bdd93SAppaRao Puli                         {"Context", customText}};
3320b4bdd93SAppaRao Puli 
3330b4bdd93SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
3340b4bdd93SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
3350b4bdd93SAppaRao Puli                               {"Name", "Event Log"},
3360b4bdd93SAppaRao Puli                               {"Events", logEntryArray}};
3370b4bdd93SAppaRao Puli 
3380b4bdd93SAppaRao Puli         this->sendEvent(msg.dump());
3390b4bdd93SAppaRao Puli         this->eventSeqNum++;
3400b4bdd93SAppaRao Puli     }
3410b4bdd93SAppaRao Puli 
3427f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
3437f4eb588SAppaRao Puli     void filterAndSendEventLogs(
3447f4eb588SAppaRao Puli         const std::vector<EventLogObjectsType>& eventRecords)
3457f4eb588SAppaRao Puli     {
3467f4eb588SAppaRao Puli         nlohmann::json logEntryArray;
3477f4eb588SAppaRao Puli         for (const EventLogObjectsType& logEntry : eventRecords)
3487f4eb588SAppaRao Puli         {
3497f4eb588SAppaRao Puli             const std::string& idStr = std::get<0>(logEntry);
3507f4eb588SAppaRao Puli             const std::string& timestamp = std::get<1>(logEntry);
3517f4eb588SAppaRao Puli             const std::string& messageID = std::get<2>(logEntry);
3527f4eb588SAppaRao Puli             const std::string& registryName = std::get<3>(logEntry);
3537f4eb588SAppaRao Puli             const std::string& messageKey = std::get<4>(logEntry);
3547f4eb588SAppaRao Puli             const boost::beast::span<std::string>& messageArgs =
3557f4eb588SAppaRao Puli                 std::get<5>(logEntry);
3567f4eb588SAppaRao Puli 
3577f4eb588SAppaRao Puli             // If registryPrefixes list is empty, don't filter events
3587f4eb588SAppaRao Puli             // send everything.
3597f4eb588SAppaRao Puli             if (registryPrefixes.size())
3607f4eb588SAppaRao Puli             {
3617f4eb588SAppaRao Puli                 auto obj = std::find(registryPrefixes.begin(),
3627f4eb588SAppaRao Puli                                      registryPrefixes.end(), registryName);
3637f4eb588SAppaRao Puli                 if (obj == registryPrefixes.end())
3647f4eb588SAppaRao Puli                 {
3657f4eb588SAppaRao Puli                     continue;
3667f4eb588SAppaRao Puli                 }
3677f4eb588SAppaRao Puli             }
3687f4eb588SAppaRao Puli 
3697f4eb588SAppaRao Puli             // If registryMsgIds list is empty, don't filter events
3707f4eb588SAppaRao Puli             // send everything.
3717f4eb588SAppaRao Puli             if (registryMsgIds.size())
3727f4eb588SAppaRao Puli             {
3737f4eb588SAppaRao Puli                 auto obj = std::find(registryMsgIds.begin(),
3747f4eb588SAppaRao Puli                                      registryMsgIds.end(), messageKey);
3757f4eb588SAppaRao Puli                 if (obj == registryMsgIds.end())
3767f4eb588SAppaRao Puli                 {
3777f4eb588SAppaRao Puli                     continue;
3787f4eb588SAppaRao Puli                 }
3797f4eb588SAppaRao Puli             }
3807f4eb588SAppaRao Puli 
3817f4eb588SAppaRao Puli             logEntryArray.push_back({});
3827f4eb588SAppaRao Puli             nlohmann::json& bmcLogEntry = logEntryArray.back();
3837f4eb588SAppaRao Puli             if (event_log::formatEventLogEntry(idStr, messageID, messageArgs,
3847f4eb588SAppaRao Puli                                                timestamp, customText,
3857f4eb588SAppaRao Puli                                                bmcLogEntry) != 0)
3867f4eb588SAppaRao Puli             {
3877f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry failed";
3887f4eb588SAppaRao Puli                 continue;
3897f4eb588SAppaRao Puli             }
3907f4eb588SAppaRao Puli         }
3917f4eb588SAppaRao Puli 
3927f4eb588SAppaRao Puli         if (logEntryArray.size() < 1)
3937f4eb588SAppaRao Puli         {
3947f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "No log entries available to be transferred.";
3957f4eb588SAppaRao Puli             return;
3967f4eb588SAppaRao Puli         }
3977f4eb588SAppaRao Puli 
3987f4eb588SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
3997f4eb588SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
4007f4eb588SAppaRao Puli                               {"Name", "Event Log"},
4017f4eb588SAppaRao Puli                               {"Events", logEntryArray}};
4027f4eb588SAppaRao Puli 
4037f4eb588SAppaRao Puli         this->sendEvent(msg.dump());
4047f4eb588SAppaRao Puli         this->eventSeqNum++;
4057f4eb588SAppaRao Puli     }
4067f4eb588SAppaRao Puli #endif
4077f4eb588SAppaRao Puli 
408156d6b00SAppaRao Puli     void filterAndSendReports(const std::string& id,
409156d6b00SAppaRao Puli                               const std::string& readingsTs,
410156d6b00SAppaRao Puli                               const ReadingsObjType& readings)
411156d6b00SAppaRao Puli     {
412156d6b00SAppaRao Puli         std::string metricReportDef =
413156d6b00SAppaRao Puli             "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id;
414156d6b00SAppaRao Puli 
415156d6b00SAppaRao Puli         // Empty list means no filter. Send everything.
416156d6b00SAppaRao Puli         if (metricReportDefinitions.size())
417156d6b00SAppaRao Puli         {
418156d6b00SAppaRao Puli             if (std::find(metricReportDefinitions.begin(),
419156d6b00SAppaRao Puli                           metricReportDefinitions.end(),
420156d6b00SAppaRao Puli                           metricReportDef) == metricReportDefinitions.end())
421156d6b00SAppaRao Puli             {
422156d6b00SAppaRao Puli                 return;
423156d6b00SAppaRao Puli             }
424156d6b00SAppaRao Puli         }
425156d6b00SAppaRao Puli 
426156d6b00SAppaRao Puli         nlohmann::json metricValuesArray = nlohmann::json::array();
427156d6b00SAppaRao Puli         for (const auto& it : readings)
428156d6b00SAppaRao Puli         {
429156d6b00SAppaRao Puli             metricValuesArray.push_back({});
430156d6b00SAppaRao Puli             nlohmann::json& entry = metricValuesArray.back();
431156d6b00SAppaRao Puli 
432156d6b00SAppaRao Puli             entry = {{"MetricId", std::get<0>(it)},
433156d6b00SAppaRao Puli                      {"MetricProperty", std::get<1>(it)},
434156d6b00SAppaRao Puli                      {"MetricValue", std::to_string(std::get<2>(it))},
435156d6b00SAppaRao Puli                      {"Timestamp", std::get<3>(it)}};
436156d6b00SAppaRao Puli         }
437156d6b00SAppaRao Puli 
438156d6b00SAppaRao Puli         nlohmann::json msg = {
439156d6b00SAppaRao Puli             {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id},
440156d6b00SAppaRao Puli             {"@odata.type", "#MetricReport.v1_3_0.MetricReport"},
441156d6b00SAppaRao Puli             {"Id", id},
442156d6b00SAppaRao Puli             {"Name", id},
443156d6b00SAppaRao Puli             {"Timestamp", readingsTs},
444156d6b00SAppaRao Puli             {"MetricReportDefinition", {{"@odata.id", metricReportDef}}},
445156d6b00SAppaRao Puli             {"MetricValues", metricValuesArray}};
446156d6b00SAppaRao Puli 
447156d6b00SAppaRao Puli         this->sendEvent(msg.dump());
448156d6b00SAppaRao Puli     }
449156d6b00SAppaRao Puli 
450b52664e2SAppaRao Puli   private:
4510b4bdd93SAppaRao Puli     uint64_t eventSeqNum;
452b52664e2SAppaRao Puli     std::string host;
453b52664e2SAppaRao Puli     std::string port;
454b52664e2SAppaRao Puli     std::string path;
455b52664e2SAppaRao Puli     std::string uriProto;
456b52664e2SAppaRao Puli     std::shared_ptr<crow::HttpClient> conn;
457b52664e2SAppaRao Puli };
458b52664e2SAppaRao Puli 
4591bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true;
4601bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3;
4611bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30;
4621bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event";
4631bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent";
4641bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries";
4651bf712bcSAyushi Smriti 
466b52664e2SAppaRao Puli class EventServiceManager
467b52664e2SAppaRao Puli {
468b52664e2SAppaRao Puli   private:
4697d1cc387SAppaRao Puli     bool serviceEnabled;
4707d1cc387SAppaRao Puli     uint32_t retryAttempts;
4717d1cc387SAppaRao Puli     uint32_t retryTimeoutInterval;
4727d1cc387SAppaRao Puli 
473b52664e2SAppaRao Puli     EventServiceManager(const EventServiceManager&) = delete;
474b52664e2SAppaRao Puli     EventServiceManager& operator=(const EventServiceManager&) = delete;
475b52664e2SAppaRao Puli     EventServiceManager(EventServiceManager&&) = delete;
476b52664e2SAppaRao Puli     EventServiceManager& operator=(EventServiceManager&&) = delete;
477b52664e2SAppaRao Puli 
4787d1cc387SAppaRao Puli     EventServiceManager() :
4797d1cc387SAppaRao Puli         noOfEventLogSubscribers(0), noOfMetricReportSubscribers(0)
480b52664e2SAppaRao Puli     {
4811bf712bcSAyushi Smriti         // Load config from persist store.
4821bf712bcSAyushi Smriti         initConfig();
483b52664e2SAppaRao Puli     }
484b52664e2SAppaRao Puli 
4857f4eb588SAppaRao Puli     std::string lastEventTStr;
4867d1cc387SAppaRao Puli     size_t noOfEventLogSubscribers;
487156d6b00SAppaRao Puli     size_t noOfMetricReportSubscribers;
488156d6b00SAppaRao Puli     std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor;
489b52664e2SAppaRao Puli     boost::container::flat_map<std::string, std::shared_ptr<Subscription>>
490b52664e2SAppaRao Puli         subscriptionsMap;
491b52664e2SAppaRao Puli 
492b52664e2SAppaRao Puli   public:
493b52664e2SAppaRao Puli     static EventServiceManager& getInstance()
494b52664e2SAppaRao Puli     {
495b52664e2SAppaRao Puli         static EventServiceManager handler;
496b52664e2SAppaRao Puli         return handler;
497b52664e2SAppaRao Puli     }
498b52664e2SAppaRao Puli 
4991bf712bcSAyushi Smriti     void loadDefaultConfig()
5001bf712bcSAyushi Smriti     {
5011bf712bcSAyushi Smriti         serviceEnabled = defaultEnabledState;
5021bf712bcSAyushi Smriti         retryAttempts = defaultRetryAttempts;
5031bf712bcSAyushi Smriti         retryTimeoutInterval = defaultRetryInterval;
5041bf712bcSAyushi Smriti     }
5051bf712bcSAyushi Smriti 
5061bf712bcSAyushi Smriti     void initConfig()
5071bf712bcSAyushi Smriti     {
5081bf712bcSAyushi Smriti         std::ifstream eventConfigFile(eventServiceFile);
5091bf712bcSAyushi Smriti         if (!eventConfigFile.good())
5101bf712bcSAyushi Smriti         {
5111bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService config not exist";
5121bf712bcSAyushi Smriti             loadDefaultConfig();
5131bf712bcSAyushi Smriti             return;
5141bf712bcSAyushi Smriti         }
5151bf712bcSAyushi Smriti         auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false);
5161bf712bcSAyushi Smriti         if (jsonData.is_discarded())
5171bf712bcSAyushi Smriti         {
5181bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "EventService config parse error.";
5191bf712bcSAyushi Smriti             loadDefaultConfig();
5201bf712bcSAyushi Smriti             return;
5211bf712bcSAyushi Smriti         }
5221bf712bcSAyushi Smriti 
5231bf712bcSAyushi Smriti         nlohmann::json jsonConfig;
5241bf712bcSAyushi Smriti         if (json_util::getValueFromJsonObject(jsonData, "Configuration",
5251bf712bcSAyushi Smriti                                               jsonConfig))
5261bf712bcSAyushi Smriti         {
5271bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled",
5281bf712bcSAyushi Smriti                                                    serviceEnabled))
5291bf712bcSAyushi Smriti             {
5301bf712bcSAyushi Smriti                 serviceEnabled = defaultEnabledState;
5311bf712bcSAyushi Smriti             }
5321bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5331bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryAttempts", retryAttempts))
5341bf712bcSAyushi Smriti             {
5351bf712bcSAyushi Smriti                 retryAttempts = defaultRetryAttempts;
5361bf712bcSAyushi Smriti             }
5371bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5381bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryIntervalSeconds",
5391bf712bcSAyushi Smriti                     retryTimeoutInterval))
5401bf712bcSAyushi Smriti             {
5411bf712bcSAyushi Smriti                 retryTimeoutInterval = defaultRetryInterval;
5421bf712bcSAyushi Smriti             }
5431bf712bcSAyushi Smriti         }
5441bf712bcSAyushi Smriti         else
5451bf712bcSAyushi Smriti         {
5461bf712bcSAyushi Smriti             loadDefaultConfig();
5471bf712bcSAyushi Smriti         }
5481bf712bcSAyushi Smriti 
5491bf712bcSAyushi Smriti         nlohmann::json subscriptionsList;
5501bf712bcSAyushi Smriti         if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions",
5511bf712bcSAyushi Smriti                                                subscriptionsList))
5521bf712bcSAyushi Smriti         {
5531bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist.";
5541bf712bcSAyushi Smriti             return;
5551bf712bcSAyushi Smriti         }
5561bf712bcSAyushi Smriti 
5571bf712bcSAyushi Smriti         for (nlohmann::json& jsonObj : subscriptionsList)
5581bf712bcSAyushi Smriti         {
5591bf712bcSAyushi Smriti             std::string protocol;
5601bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Protocol",
5611bf712bcSAyushi Smriti                                                    protocol))
5621bf712bcSAyushi Smriti             {
5631bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist.";
5641bf712bcSAyushi Smriti                 continue;
5651bf712bcSAyushi Smriti             }
5661bf712bcSAyushi Smriti             std::string destination;
5671bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Destination",
5681bf712bcSAyushi Smriti                                                    destination))
5691bf712bcSAyushi Smriti             {
5701bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription destination exist.";
5711bf712bcSAyushi Smriti                 continue;
5721bf712bcSAyushi Smriti             }
5731bf712bcSAyushi Smriti             std::string host;
5741bf712bcSAyushi Smriti             std::string urlProto;
5751bf712bcSAyushi Smriti             std::string port;
5761bf712bcSAyushi Smriti             std::string path;
5771bf712bcSAyushi Smriti             bool status =
5781bf712bcSAyushi Smriti                 validateAndSplitUrl(destination, urlProto, host, port, path);
5791bf712bcSAyushi Smriti 
5801bf712bcSAyushi Smriti             if (!status)
5811bf712bcSAyushi Smriti             {
5821bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR
5831bf712bcSAyushi Smriti                     << "Failed to validate and split destination url";
5841bf712bcSAyushi Smriti                 continue;
5851bf712bcSAyushi Smriti             }
5861bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue =
5871bf712bcSAyushi Smriti                 std::make_shared<Subscription>(host, port, path, urlProto);
5881bf712bcSAyushi Smriti 
5891bf712bcSAyushi Smriti             subValue->destinationUrl = destination;
5901bf712bcSAyushi Smriti             subValue->protocol = protocol;
5911bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
5921bf712bcSAyushi Smriti                     jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy))
5931bf712bcSAyushi Smriti             {
5941bf712bcSAyushi Smriti                 subValue->retryPolicy = defaulRetryPolicy;
5951bf712bcSAyushi Smriti             }
5961bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType",
5971bf712bcSAyushi Smriti                                                    subValue->eventFormatType))
5981bf712bcSAyushi Smriti             {
5991bf712bcSAyushi Smriti                 subValue->eventFormatType = defaulEventFormatType;
6001bf712bcSAyushi Smriti             }
6011bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType",
6021bf712bcSAyushi Smriti                                                    subValue->subscriptionType))
6031bf712bcSAyushi Smriti             {
6041bf712bcSAyushi Smriti                 subValue->subscriptionType = defaulSubscriptionType;
6051bf712bcSAyushi Smriti             }
6061bf712bcSAyushi Smriti 
6071bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "Context",
6081bf712bcSAyushi Smriti                                               subValue->customText);
6091bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "MessageIds",
6101bf712bcSAyushi Smriti                                               subValue->registryMsgIds);
6111bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes",
6121bf712bcSAyushi Smriti                                               subValue->registryPrefixes);
6131bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "HttpHeaders",
6141bf712bcSAyushi Smriti                                               subValue->httpHeaders);
6151bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(
6161bf712bcSAyushi Smriti                 jsonObj, "MetricReportDefinitions",
6171bf712bcSAyushi Smriti                 subValue->metricReportDefinitions);
6181bf712bcSAyushi Smriti 
6191bf712bcSAyushi Smriti             std::string id = addSubscription(subValue, false);
6201bf712bcSAyushi Smriti             if (id.empty())
6211bf712bcSAyushi Smriti             {
6221bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR << "Failed to add subscription";
6231bf712bcSAyushi Smriti             }
6241bf712bcSAyushi Smriti         }
6251bf712bcSAyushi Smriti         return;
6261bf712bcSAyushi Smriti     }
6271bf712bcSAyushi Smriti 
628b52664e2SAppaRao Puli     void updateSubscriptionData()
629b52664e2SAppaRao Puli     {
630b52664e2SAppaRao Puli         // Persist the config and subscription data.
6311bf712bcSAyushi Smriti         nlohmann::json jsonData;
6321bf712bcSAyushi Smriti 
6331bf712bcSAyushi Smriti         nlohmann::json& configObj = jsonData["Configuration"];
6341bf712bcSAyushi Smriti         configObj["ServiceEnabled"] = serviceEnabled;
6351bf712bcSAyushi Smriti         configObj["DeliveryRetryAttempts"] = retryAttempts;
6361bf712bcSAyushi Smriti         configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval;
6371bf712bcSAyushi Smriti 
6381bf712bcSAyushi Smriti         nlohmann::json& subListArray = jsonData["Subscriptions"];
6391bf712bcSAyushi Smriti         subListArray = nlohmann::json::array();
6401bf712bcSAyushi Smriti 
6411bf712bcSAyushi Smriti         for (const auto& it : subscriptionsMap)
6421bf712bcSAyushi Smriti         {
6431bf712bcSAyushi Smriti             nlohmann::json entry;
6441bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue = it.second;
6451bf712bcSAyushi Smriti 
6461bf712bcSAyushi Smriti             entry["Context"] = subValue->customText;
6471bf712bcSAyushi Smriti             entry["DeliveryRetryPolicy"] = subValue->retryPolicy;
6481bf712bcSAyushi Smriti             entry["Destination"] = subValue->destinationUrl;
6491bf712bcSAyushi Smriti             entry["EventFormatType"] = subValue->eventFormatType;
6501bf712bcSAyushi Smriti             entry["HttpHeaders"] = subValue->httpHeaders;
6511bf712bcSAyushi Smriti             entry["MessageIds"] = subValue->registryMsgIds;
6521bf712bcSAyushi Smriti             entry["Protocol"] = subValue->protocol;
6531bf712bcSAyushi Smriti             entry["RegistryPrefixes"] = subValue->registryPrefixes;
6541bf712bcSAyushi Smriti             entry["SubscriptionType"] = subValue->subscriptionType;
6551bf712bcSAyushi Smriti             entry["MetricReportDefinitions"] =
6561bf712bcSAyushi Smriti                 subValue->metricReportDefinitions;
6571bf712bcSAyushi Smriti 
6581bf712bcSAyushi Smriti             subListArray.push_back(entry);
6591bf712bcSAyushi Smriti         }
6601bf712bcSAyushi Smriti 
6611bf712bcSAyushi Smriti         const std::string tmpFile(std::string(eventServiceFile) + "_tmp");
6621bf712bcSAyushi Smriti         std::ofstream ofs(tmpFile, std::ios::out);
6631bf712bcSAyushi Smriti         const auto& writeData = jsonData.dump();
6641bf712bcSAyushi Smriti         ofs << writeData;
6651bf712bcSAyushi Smriti         ofs.close();
6661bf712bcSAyushi Smriti 
6671bf712bcSAyushi Smriti         BMCWEB_LOG_DEBUG << "EventService config updated to file.";
6681bf712bcSAyushi Smriti         if (std::rename(tmpFile.c_str(), eventServiceFile) != 0)
6691bf712bcSAyushi Smriti         {
6701bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "Error in renaming temporary file: "
6711bf712bcSAyushi Smriti                              << tmpFile.c_str();
6721bf712bcSAyushi Smriti         }
673b52664e2SAppaRao Puli     }
674b52664e2SAppaRao Puli 
6757d1cc387SAppaRao Puli     EventServiceConfig getEventServiceConfig()
6767d1cc387SAppaRao Puli     {
6777d1cc387SAppaRao Puli         return {serviceEnabled, retryAttempts, retryTimeoutInterval};
6787d1cc387SAppaRao Puli     }
6797d1cc387SAppaRao Puli 
6807d1cc387SAppaRao Puli     void setEventServiceConfig(const EventServiceConfig& cfg)
6817d1cc387SAppaRao Puli     {
6827d1cc387SAppaRao Puli         bool updateConfig = false;
6837d1cc387SAppaRao Puli 
6847d1cc387SAppaRao Puli         if (serviceEnabled != std::get<0>(cfg))
6857d1cc387SAppaRao Puli         {
6867d1cc387SAppaRao Puli             serviceEnabled = std::get<0>(cfg);
6877d1cc387SAppaRao Puli             if (serviceEnabled && noOfMetricReportSubscribers)
6887d1cc387SAppaRao Puli             {
6897d1cc387SAppaRao Puli                 registerMetricReportSignal();
6907d1cc387SAppaRao Puli             }
6917d1cc387SAppaRao Puli             else
6927d1cc387SAppaRao Puli             {
6937d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
6947d1cc387SAppaRao Puli             }
6957d1cc387SAppaRao Puli             updateConfig = true;
6967d1cc387SAppaRao Puli         }
6977d1cc387SAppaRao Puli 
6987d1cc387SAppaRao Puli         if (retryAttempts != std::get<1>(cfg))
6997d1cc387SAppaRao Puli         {
7007d1cc387SAppaRao Puli             retryAttempts = std::get<1>(cfg);
7017d1cc387SAppaRao Puli             updateConfig = true;
7027d1cc387SAppaRao Puli         }
7037d1cc387SAppaRao Puli 
7047d1cc387SAppaRao Puli         if (retryTimeoutInterval != std::get<2>(cfg))
7057d1cc387SAppaRao Puli         {
7067d1cc387SAppaRao Puli             retryTimeoutInterval = std::get<2>(cfg);
7077d1cc387SAppaRao Puli             updateConfig = true;
7087d1cc387SAppaRao Puli         }
7097d1cc387SAppaRao Puli 
7107d1cc387SAppaRao Puli         if (updateConfig)
7117d1cc387SAppaRao Puli         {
7127d1cc387SAppaRao Puli             updateSubscriptionData();
7137d1cc387SAppaRao Puli         }
7147d1cc387SAppaRao Puli     }
7157d1cc387SAppaRao Puli 
7167d1cc387SAppaRao Puli     void updateNoOfSubscribersCount()
7177d1cc387SAppaRao Puli     {
7187d1cc387SAppaRao Puli         size_t eventLogSubCount = 0;
7197d1cc387SAppaRao Puli         size_t metricReportSubCount = 0;
7207d1cc387SAppaRao Puli         for (const auto& it : subscriptionsMap)
7217d1cc387SAppaRao Puli         {
7227d1cc387SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
7237d1cc387SAppaRao Puli             if (entry->eventFormatType == eventFormatType)
7247d1cc387SAppaRao Puli             {
7257d1cc387SAppaRao Puli                 eventLogSubCount++;
7267d1cc387SAppaRao Puli             }
7277d1cc387SAppaRao Puli             else if (entry->eventFormatType == metricReportFormatType)
7287d1cc387SAppaRao Puli             {
7297d1cc387SAppaRao Puli                 metricReportSubCount++;
7307d1cc387SAppaRao Puli             }
7317d1cc387SAppaRao Puli         }
7327d1cc387SAppaRao Puli 
7337d1cc387SAppaRao Puli         noOfEventLogSubscribers = eventLogSubCount;
7347d1cc387SAppaRao Puli         if (noOfMetricReportSubscribers != metricReportSubCount)
7357d1cc387SAppaRao Puli         {
7367d1cc387SAppaRao Puli             noOfMetricReportSubscribers = metricReportSubCount;
7377d1cc387SAppaRao Puli             if (noOfMetricReportSubscribers)
7387d1cc387SAppaRao Puli             {
7397d1cc387SAppaRao Puli                 registerMetricReportSignal();
7407d1cc387SAppaRao Puli             }
7417d1cc387SAppaRao Puli             else
7427d1cc387SAppaRao Puli             {
7437d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
7447d1cc387SAppaRao Puli             }
7457d1cc387SAppaRao Puli         }
7467d1cc387SAppaRao Puli     }
7477d1cc387SAppaRao Puli 
748b52664e2SAppaRao Puli     std::shared_ptr<Subscription> getSubscription(const std::string& id)
749b52664e2SAppaRao Puli     {
750b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
751b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
752b52664e2SAppaRao Puli         {
753b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id;
754b52664e2SAppaRao Puli             return nullptr;
755b52664e2SAppaRao Puli         }
756b52664e2SAppaRao Puli         std::shared_ptr<Subscription> subValue = obj->second;
757b52664e2SAppaRao Puli         return subValue;
758b52664e2SAppaRao Puli     }
759b52664e2SAppaRao Puli 
7601bf712bcSAyushi Smriti     std::string addSubscription(const std::shared_ptr<Subscription> subValue,
7611bf712bcSAyushi Smriti                                 const bool updateFile = true)
762b52664e2SAppaRao Puli     {
763b52664e2SAppaRao Puli         std::srand(static_cast<uint32_t>(std::time(0)));
764b52664e2SAppaRao Puli         std::string id;
765b52664e2SAppaRao Puli 
766b52664e2SAppaRao Puli         int retry = 3;
767b52664e2SAppaRao Puli         while (retry)
768b52664e2SAppaRao Puli         {
769b52664e2SAppaRao Puli             id = std::to_string(std::rand());
770b52664e2SAppaRao Puli             auto inserted = subscriptionsMap.insert(std::pair(id, subValue));
771b52664e2SAppaRao Puli             if (inserted.second)
772b52664e2SAppaRao Puli             {
773b52664e2SAppaRao Puli                 break;
774b52664e2SAppaRao Puli             }
775b52664e2SAppaRao Puli             --retry;
776b52664e2SAppaRao Puli         };
777b52664e2SAppaRao Puli 
778b52664e2SAppaRao Puli         if (retry <= 0)
779b52664e2SAppaRao Puli         {
780b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "Failed to generate random number";
781b52664e2SAppaRao Puli             return std::string("");
782b52664e2SAppaRao Puli         }
783b52664e2SAppaRao Puli 
7847d1cc387SAppaRao Puli         updateNoOfSubscribersCount();
7851bf712bcSAyushi Smriti 
7861bf712bcSAyushi Smriti         if (updateFile)
7871bf712bcSAyushi Smriti         {
788b52664e2SAppaRao Puli             updateSubscriptionData();
7891bf712bcSAyushi Smriti         }
7907f4eb588SAppaRao Puli 
7917f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
7927f4eb588SAppaRao Puli         if (lastEventTStr.empty())
7937f4eb588SAppaRao Puli         {
7947f4eb588SAppaRao Puli             cacheLastEventTimestamp();
7957f4eb588SAppaRao Puli         }
7967f4eb588SAppaRao Puli #endif
797b52664e2SAppaRao Puli         return id;
798b52664e2SAppaRao Puli     }
799b52664e2SAppaRao Puli 
800b52664e2SAppaRao Puli     bool isSubscriptionExist(const std::string& id)
801b52664e2SAppaRao Puli     {
802b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
803b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
804b52664e2SAppaRao Puli         {
805b52664e2SAppaRao Puli             return false;
806b52664e2SAppaRao Puli         }
807b52664e2SAppaRao Puli         return true;
808b52664e2SAppaRao Puli     }
809b52664e2SAppaRao Puli 
810b52664e2SAppaRao Puli     void deleteSubscription(const std::string& id)
811b52664e2SAppaRao Puli     {
812b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
813b52664e2SAppaRao Puli         if (obj != subscriptionsMap.end())
814b52664e2SAppaRao Puli         {
815b52664e2SAppaRao Puli             subscriptionsMap.erase(obj);
8167d1cc387SAppaRao Puli             updateNoOfSubscribersCount();
817b52664e2SAppaRao Puli             updateSubscriptionData();
818b52664e2SAppaRao Puli         }
819b52664e2SAppaRao Puli     }
820b52664e2SAppaRao Puli 
821b52664e2SAppaRao Puli     size_t getNumberOfSubscriptions()
822b52664e2SAppaRao Puli     {
823b52664e2SAppaRao Puli         return subscriptionsMap.size();
824b52664e2SAppaRao Puli     }
825b52664e2SAppaRao Puli 
826b52664e2SAppaRao Puli     std::vector<std::string> getAllIDs()
827b52664e2SAppaRao Puli     {
828b52664e2SAppaRao Puli         std::vector<std::string> idList;
829b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
830b52664e2SAppaRao Puli         {
831b52664e2SAppaRao Puli             idList.emplace_back(it.first);
832b52664e2SAppaRao Puli         }
833b52664e2SAppaRao Puli         return idList;
834b52664e2SAppaRao Puli     }
835b52664e2SAppaRao Puli 
836b52664e2SAppaRao Puli     bool isDestinationExist(const std::string& destUrl)
837b52664e2SAppaRao Puli     {
838b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
839b52664e2SAppaRao Puli         {
840b52664e2SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
841b52664e2SAppaRao Puli             if (entry->destinationUrl == destUrl)
842b52664e2SAppaRao Puli             {
843b52664e2SAppaRao Puli                 BMCWEB_LOG_ERROR << "Destination exist already" << destUrl;
844b52664e2SAppaRao Puli                 return true;
845b52664e2SAppaRao Puli             }
846b52664e2SAppaRao Puli         }
847b52664e2SAppaRao Puli         return false;
848b52664e2SAppaRao Puli     }
8490b4bdd93SAppaRao Puli 
8500b4bdd93SAppaRao Puli     void sendTestEventLog()
8510b4bdd93SAppaRao Puli     {
8520b4bdd93SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
8530b4bdd93SAppaRao Puli         {
8540b4bdd93SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
8550b4bdd93SAppaRao Puli             entry->sendTestEventLog();
8560b4bdd93SAppaRao Puli         }
8570b4bdd93SAppaRao Puli     }
858e9a14131SAppaRao Puli 
8597f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
8607f4eb588SAppaRao Puli     void cacheLastEventTimestamp()
8617f4eb588SAppaRao Puli     {
8627f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
8637f4eb588SAppaRao Puli         if (!logStream.good())
8647f4eb588SAppaRao Puli         {
8657f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed \n";
8667f4eb588SAppaRao Puli             return;
8677f4eb588SAppaRao Puli         }
8687f4eb588SAppaRao Puli         std::string logEntry;
8697f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
8707f4eb588SAppaRao Puli         {
8717f4eb588SAppaRao Puli             size_t space = logEntry.find_first_of(" ");
8727f4eb588SAppaRao Puli             if (space == std::string::npos)
8737f4eb588SAppaRao Puli             {
8747f4eb588SAppaRao Puli                 // Shouldn't enter here but lets skip it.
8757f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Invalid log entry found.";
8767f4eb588SAppaRao Puli                 continue;
8777f4eb588SAppaRao Puli             }
8787f4eb588SAppaRao Puli             lastEventTStr = logEntry.substr(0, space);
8797f4eb588SAppaRao Puli         }
8807f4eb588SAppaRao Puli         BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr;
8817f4eb588SAppaRao Puli     }
8827f4eb588SAppaRao Puli 
8837f4eb588SAppaRao Puli     void readEventLogsFromFile()
8847f4eb588SAppaRao Puli     {
8857f4eb588SAppaRao Puli         if (!serviceEnabled || !noOfEventLogSubscribers)
8867f4eb588SAppaRao Puli         {
8877f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions.";
8887f4eb588SAppaRao Puli             return;
8897f4eb588SAppaRao Puli         }
8907f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
8917f4eb588SAppaRao Puli         if (!logStream.good())
8927f4eb588SAppaRao Puli         {
8937f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed";
8947f4eb588SAppaRao Puli             return;
8957f4eb588SAppaRao Puli         }
8967f4eb588SAppaRao Puli 
8977f4eb588SAppaRao Puli         std::vector<EventLogObjectsType> eventRecords;
8987f4eb588SAppaRao Puli 
8997f4eb588SAppaRao Puli         bool startLogCollection = false;
9007f4eb588SAppaRao Puli         bool firstEntry = true;
9017f4eb588SAppaRao Puli 
9027f4eb588SAppaRao Puli         std::string logEntry;
9037f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
9047f4eb588SAppaRao Puli         {
9057f4eb588SAppaRao Puli             if (!startLogCollection)
9067f4eb588SAppaRao Puli             {
9077f4eb588SAppaRao Puli                 if (boost::starts_with(logEntry, lastEventTStr))
9087f4eb588SAppaRao Puli                 {
9097f4eb588SAppaRao Puli                     startLogCollection = true;
9107f4eb588SAppaRao Puli                 }
9117f4eb588SAppaRao Puli                 continue;
9127f4eb588SAppaRao Puli             }
9137f4eb588SAppaRao Puli 
9147f4eb588SAppaRao Puli             std::string idStr;
9157f4eb588SAppaRao Puli             if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry))
9167f4eb588SAppaRao Puli             {
9177f4eb588SAppaRao Puli                 continue;
9187f4eb588SAppaRao Puli             }
9197f4eb588SAppaRao Puli             firstEntry = false;
9207f4eb588SAppaRao Puli 
9217f4eb588SAppaRao Puli             std::string timestamp;
9227f4eb588SAppaRao Puli             std::string messageID;
9237f4eb588SAppaRao Puli             boost::beast::span<std::string> messageArgs;
9247f4eb588SAppaRao Puli             if (event_log::getEventLogParams(logEntry, timestamp, messageID,
9257f4eb588SAppaRao Puli                                              messageArgs) != 0)
9267f4eb588SAppaRao Puli             {
9277f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry params failed";
9287f4eb588SAppaRao Puli                 continue;
9297f4eb588SAppaRao Puli             }
9307f4eb588SAppaRao Puli 
9317f4eb588SAppaRao Puli             std::string registryName;
9327f4eb588SAppaRao Puli             std::string messageKey;
9337f4eb588SAppaRao Puli             event_log::getRegistryAndMessageKey(messageID, registryName,
9347f4eb588SAppaRao Puli                                                 messageKey);
9357f4eb588SAppaRao Puli             if (registryName.empty() || messageKey.empty())
9367f4eb588SAppaRao Puli             {
9377f4eb588SAppaRao Puli                 continue;
9387f4eb588SAppaRao Puli             }
9397f4eb588SAppaRao Puli 
9407f4eb588SAppaRao Puli             lastEventTStr = timestamp;
9417f4eb588SAppaRao Puli             eventRecords.emplace_back(idStr, timestamp, messageID, registryName,
9427f4eb588SAppaRao Puli                                       messageKey, messageArgs);
9437f4eb588SAppaRao Puli         }
9447f4eb588SAppaRao Puli 
9457f4eb588SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
9467f4eb588SAppaRao Puli         {
9477f4eb588SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
9487f4eb588SAppaRao Puli             if (entry->eventFormatType == "Event")
9497f4eb588SAppaRao Puli             {
9507f4eb588SAppaRao Puli                 entry->filterAndSendEventLogs(eventRecords);
9517f4eb588SAppaRao Puli             }
9527f4eb588SAppaRao Puli         }
9537f4eb588SAppaRao Puli     }
9547f4eb588SAppaRao Puli 
9557f4eb588SAppaRao Puli     static void watchRedfishEventLogFile()
9567f4eb588SAppaRao Puli     {
9577f4eb588SAppaRao Puli         if (inotifyConn == nullptr)
9587f4eb588SAppaRao Puli         {
9597f4eb588SAppaRao Puli             return;
9607f4eb588SAppaRao Puli         }
9617f4eb588SAppaRao Puli 
9627f4eb588SAppaRao Puli         static std::array<char, 1024> readBuffer;
9637f4eb588SAppaRao Puli 
9647f4eb588SAppaRao Puli         inotifyConn->async_read_some(
9657f4eb588SAppaRao Puli             boost::asio::buffer(readBuffer),
9667f4eb588SAppaRao Puli             [&](const boost::system::error_code& ec,
9677f4eb588SAppaRao Puli                 const std::size_t& bytesTransferred) {
9687f4eb588SAppaRao Puli                 if (ec)
9697f4eb588SAppaRao Puli                 {
9707f4eb588SAppaRao Puli                     BMCWEB_LOG_ERROR << "Callback Error: " << ec.message();
9717f4eb588SAppaRao Puli                     return;
9727f4eb588SAppaRao Puli                 }
9737f4eb588SAppaRao Puli                 std::size_t index = 0;
974*b792cc56SAppaRao Puli                 while ((index + iEventSize) <= bytesTransferred)
9757f4eb588SAppaRao Puli                 {
9767f4eb588SAppaRao Puli                     struct inotify_event event;
977*b792cc56SAppaRao Puli                     std::memcpy(&event, &readBuffer[index], iEventSize);
978*b792cc56SAppaRao Puli                     if (event.wd == dirWatchDesc)
979*b792cc56SAppaRao Puli                     {
980*b792cc56SAppaRao Puli                         if ((event.len == 0) ||
981*b792cc56SAppaRao Puli                             (index + iEventSize + event.len > bytesTransferred))
982*b792cc56SAppaRao Puli                         {
983*b792cc56SAppaRao Puli                             index += (iEventSize + event.len);
984*b792cc56SAppaRao Puli                             continue;
985*b792cc56SAppaRao Puli                         }
986*b792cc56SAppaRao Puli 
987*b792cc56SAppaRao Puli                         std::string fileName(&readBuffer[index + iEventSize],
988*b792cc56SAppaRao Puli                                              event.len);
989*b792cc56SAppaRao Puli                         if (std::strcmp(fileName.c_str(), "redfish") != 0)
990*b792cc56SAppaRao Puli                         {
991*b792cc56SAppaRao Puli                             index += (iEventSize + event.len);
992*b792cc56SAppaRao Puli                             continue;
993*b792cc56SAppaRao Puli                         }
994*b792cc56SAppaRao Puli 
995*b792cc56SAppaRao Puli                         BMCWEB_LOG_DEBUG
996*b792cc56SAppaRao Puli                             << "Redfish log file created/deleted. event.name: "
997*b792cc56SAppaRao Puli                             << fileName;
998*b792cc56SAppaRao Puli                         if (event.mask == IN_CREATE)
999*b792cc56SAppaRao Puli                         {
1000*b792cc56SAppaRao Puli                             if (fileWatchDesc != -1)
1001*b792cc56SAppaRao Puli                             {
1002*b792cc56SAppaRao Puli                                 BMCWEB_LOG_DEBUG
1003*b792cc56SAppaRao Puli                                     << "Redfish log file is already on "
1004*b792cc56SAppaRao Puli                                        "inotify_add_watch.";
1005*b792cc56SAppaRao Puli                                 return;
1006*b792cc56SAppaRao Puli                             }
1007*b792cc56SAppaRao Puli 
1008*b792cc56SAppaRao Puli                             fileWatchDesc = inotify_add_watch(
1009*b792cc56SAppaRao Puli                                 inotifyFd, redfishEventLogFile, IN_MODIFY);
1010*b792cc56SAppaRao Puli                             if (fileWatchDesc == -1)
1011*b792cc56SAppaRao Puli                             {
1012*b792cc56SAppaRao Puli                                 BMCWEB_LOG_ERROR
1013*b792cc56SAppaRao Puli                                     << "inotify_add_watch failed for "
1014*b792cc56SAppaRao Puli                                        "redfish log file.";
1015*b792cc56SAppaRao Puli                                 return;
1016*b792cc56SAppaRao Puli                             }
1017*b792cc56SAppaRao Puli 
1018*b792cc56SAppaRao Puli                             EventServiceManager::getInstance()
1019*b792cc56SAppaRao Puli                                 .cacheLastEventTimestamp();
1020*b792cc56SAppaRao Puli                             EventServiceManager::getInstance()
1021*b792cc56SAppaRao Puli                                 .readEventLogsFromFile();
1022*b792cc56SAppaRao Puli                         }
1023*b792cc56SAppaRao Puli                         else if ((event.mask == IN_DELETE) ||
1024*b792cc56SAppaRao Puli                                  (event.mask == IN_MOVED_TO))
1025*b792cc56SAppaRao Puli                         {
1026*b792cc56SAppaRao Puli                             if (fileWatchDesc != -1)
1027*b792cc56SAppaRao Puli                             {
1028*b792cc56SAppaRao Puli                                 inotify_rm_watch(inotifyFd, fileWatchDesc);
1029*b792cc56SAppaRao Puli                                 fileWatchDesc = -1;
1030*b792cc56SAppaRao Puli                             }
1031*b792cc56SAppaRao Puli                         }
1032*b792cc56SAppaRao Puli                     }
1033*b792cc56SAppaRao Puli                     else if (event.wd == fileWatchDesc)
1034*b792cc56SAppaRao Puli                     {
1035*b792cc56SAppaRao Puli                         if (event.mask == IN_MODIFY)
10367f4eb588SAppaRao Puli                         {
10377f4eb588SAppaRao Puli                             EventServiceManager::getInstance()
10387f4eb588SAppaRao Puli                                 .readEventLogsFromFile();
10397f4eb588SAppaRao Puli                         }
1040*b792cc56SAppaRao Puli                     }
1041*b792cc56SAppaRao Puli                     index += (iEventSize + event.len);
10427f4eb588SAppaRao Puli                 }
10437f4eb588SAppaRao Puli 
10447f4eb588SAppaRao Puli                 watchRedfishEventLogFile();
10457f4eb588SAppaRao Puli             });
10467f4eb588SAppaRao Puli     }
10477f4eb588SAppaRao Puli 
10487f4eb588SAppaRao Puli     static int startEventLogMonitor(boost::asio::io_context& ioc)
10497f4eb588SAppaRao Puli     {
10507f4eb588SAppaRao Puli         inotifyConn =
10517f4eb588SAppaRao Puli             std::make_shared<boost::asio::posix::stream_descriptor>(ioc);
1052*b792cc56SAppaRao Puli         inotifyFd = inotify_init1(IN_NONBLOCK);
1053*b792cc56SAppaRao Puli         if (inotifyFd == -1)
10547f4eb588SAppaRao Puli         {
10557f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << "inotify_init1 failed.";
10567f4eb588SAppaRao Puli             return -1;
10577f4eb588SAppaRao Puli         }
1058*b792cc56SAppaRao Puli 
1059*b792cc56SAppaRao Puli         // Add watch on directory to handle redfish event log file
1060*b792cc56SAppaRao Puli         // create/delete.
1061*b792cc56SAppaRao Puli         dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir,
1062*b792cc56SAppaRao Puli                                          IN_CREATE | IN_MOVED_TO | IN_DELETE);
1063*b792cc56SAppaRao Puli         if (dirWatchDesc == -1)
10647f4eb588SAppaRao Puli         {
10657f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR
1066*b792cc56SAppaRao Puli                 << "inotify_add_watch failed for event log directory.";
10677f4eb588SAppaRao Puli             return -1;
10687f4eb588SAppaRao Puli         }
10697f4eb588SAppaRao Puli 
1070*b792cc56SAppaRao Puli         // Watch redfish event log file for modifications.
1071*b792cc56SAppaRao Puli         fileWatchDesc =
1072*b792cc56SAppaRao Puli             inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY);
1073*b792cc56SAppaRao Puli         if (fileWatchDesc == -1)
1074*b792cc56SAppaRao Puli         {
1075*b792cc56SAppaRao Puli             BMCWEB_LOG_ERROR
1076*b792cc56SAppaRao Puli                 << "inotify_add_watch failed for redfish log file.";
1077*b792cc56SAppaRao Puli             // Don't return error if file not exist.
1078*b792cc56SAppaRao Puli             // Watch on directory will handle create/delete of file.
1079*b792cc56SAppaRao Puli         }
1080*b792cc56SAppaRao Puli 
10817f4eb588SAppaRao Puli         // monitor redfish event log file
1082*b792cc56SAppaRao Puli         inotifyConn->assign(inotifyFd);
10837f4eb588SAppaRao Puli         watchRedfishEventLogFile();
10847f4eb588SAppaRao Puli 
10857f4eb588SAppaRao Puli         return 0;
10867f4eb588SAppaRao Puli     }
10877f4eb588SAppaRao Puli 
10887f4eb588SAppaRao Puli #endif
10897f4eb588SAppaRao Puli 
1090156d6b00SAppaRao Puli     void getMetricReading(const std::string& service,
1091156d6b00SAppaRao Puli                           const std::string& objPath, const std::string& intf)
1092156d6b00SAppaRao Puli     {
1093156d6b00SAppaRao Puli         std::size_t found = objPath.find_last_of("/");
1094156d6b00SAppaRao Puli         if (found == std::string::npos)
1095156d6b00SAppaRao Puli         {
1096156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid objPath received";
1097156d6b00SAppaRao Puli             return;
1098156d6b00SAppaRao Puli         }
1099156d6b00SAppaRao Puli 
1100156d6b00SAppaRao Puli         std::string idStr = objPath.substr(found + 1);
1101156d6b00SAppaRao Puli         if (idStr.empty())
1102156d6b00SAppaRao Puli         {
1103156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid ID in objPath";
1104156d6b00SAppaRao Puli             return;
1105156d6b00SAppaRao Puli         }
1106156d6b00SAppaRao Puli 
1107156d6b00SAppaRao Puli         crow::connections::systemBus->async_method_call(
1108156d6b00SAppaRao Puli             [idStr{std::move(idStr)}](
1109156d6b00SAppaRao Puli                 const boost::system::error_code ec,
1110156d6b00SAppaRao Puli                 boost::container::flat_map<
1111156d6b00SAppaRao Puli                     std::string, std::variant<std::string, ReadingsObjType>>&
1112156d6b00SAppaRao Puli                     resp) {
1113156d6b00SAppaRao Puli                 if (ec)
1114156d6b00SAppaRao Puli                 {
1115156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG
1116156d6b00SAppaRao Puli                         << "D-Bus call failed to GetAll metric readings.";
1117156d6b00SAppaRao Puli                     return;
1118156d6b00SAppaRao Puli                 }
1119156d6b00SAppaRao Puli 
1120156d6b00SAppaRao Puli                 const std::string* timestampPtr =
1121156d6b00SAppaRao Puli                     std::get_if<std::string>(&resp["Timestamp"]);
1122156d6b00SAppaRao Puli                 if (!timestampPtr)
1123156d6b00SAppaRao Puli                 {
1124156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get timestamp.";
1125156d6b00SAppaRao Puli                     return;
1126156d6b00SAppaRao Puli                 }
1127156d6b00SAppaRao Puli 
1128156d6b00SAppaRao Puli                 ReadingsObjType* readingsPtr =
1129156d6b00SAppaRao Puli                     std::get_if<ReadingsObjType>(&resp["Readings"]);
1130156d6b00SAppaRao Puli                 if (!readingsPtr)
1131156d6b00SAppaRao Puli                 {
1132156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get Readings property.";
1133156d6b00SAppaRao Puli                     return;
1134156d6b00SAppaRao Puli                 }
1135156d6b00SAppaRao Puli 
1136156d6b00SAppaRao Puli                 if (!readingsPtr->size())
1137156d6b00SAppaRao Puli                 {
1138156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "No metrics report to be transferred";
1139156d6b00SAppaRao Puli                     return;
1140156d6b00SAppaRao Puli                 }
1141156d6b00SAppaRao Puli 
1142156d6b00SAppaRao Puli                 for (const auto& it :
1143156d6b00SAppaRao Puli                      EventServiceManager::getInstance().subscriptionsMap)
1144156d6b00SAppaRao Puli                 {
1145156d6b00SAppaRao Puli                     std::shared_ptr<Subscription> entry = it.second;
1146156d6b00SAppaRao Puli                     if (entry->eventFormatType == metricReportFormatType)
1147156d6b00SAppaRao Puli                     {
1148156d6b00SAppaRao Puli                         entry->filterAndSendReports(idStr, *timestampPtr,
1149156d6b00SAppaRao Puli                                                     *readingsPtr);
1150156d6b00SAppaRao Puli                     }
1151156d6b00SAppaRao Puli                 }
1152156d6b00SAppaRao Puli             },
1153156d6b00SAppaRao Puli             service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
1154156d6b00SAppaRao Puli             intf);
1155156d6b00SAppaRao Puli     }
1156156d6b00SAppaRao Puli 
1157156d6b00SAppaRao Puli     void unregisterMetricReportSignal()
1158156d6b00SAppaRao Puli     {
11597d1cc387SAppaRao Puli         if (matchTelemetryMonitor)
11607d1cc387SAppaRao Puli         {
1161156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister";
1162156d6b00SAppaRao Puli             matchTelemetryMonitor.reset();
1163156d6b00SAppaRao Puli             matchTelemetryMonitor = nullptr;
1164156d6b00SAppaRao Puli         }
11657d1cc387SAppaRao Puli     }
1166156d6b00SAppaRao Puli 
1167156d6b00SAppaRao Puli     void registerMetricReportSignal()
1168156d6b00SAppaRao Puli     {
11697d1cc387SAppaRao Puli         if (!serviceEnabled || matchTelemetryMonitor)
1170156d6b00SAppaRao Puli         {
11717d1cc387SAppaRao Puli             BMCWEB_LOG_DEBUG << "Not registering metric report signal.";
1172156d6b00SAppaRao Puli             return;
1173156d6b00SAppaRao Puli         }
1174156d6b00SAppaRao Puli 
1175156d6b00SAppaRao Puli         BMCWEB_LOG_DEBUG << "Metrics report signal - Register";
1176156d6b00SAppaRao Puli         std::string matchStr(
1177156d6b00SAppaRao Puli             "type='signal',member='ReportUpdate', "
1178156d6b00SAppaRao Puli             "interface='xyz.openbmc_project.MonitoringService.Report'");
1179156d6b00SAppaRao Puli 
1180156d6b00SAppaRao Puli         matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>(
1181156d6b00SAppaRao Puli             *crow::connections::systemBus, matchStr,
1182156d6b00SAppaRao Puli             [this](sdbusplus::message::message& msg) {
1183156d6b00SAppaRao Puli                 if (msg.is_method_error())
1184156d6b00SAppaRao Puli                 {
1185156d6b00SAppaRao Puli                     BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error";
1186156d6b00SAppaRao Puli                     return;
1187156d6b00SAppaRao Puli                 }
1188156d6b00SAppaRao Puli 
1189156d6b00SAppaRao Puli                 std::string service = msg.get_sender();
1190156d6b00SAppaRao Puli                 std::string objPath = msg.get_path();
1191156d6b00SAppaRao Puli                 std::string intf = msg.get_interface();
1192156d6b00SAppaRao Puli                 getMetricReading(service, objPath, intf);
1193156d6b00SAppaRao Puli             });
1194156d6b00SAppaRao Puli     }
11951bf712bcSAyushi Smriti 
11961bf712bcSAyushi Smriti     bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto,
11971bf712bcSAyushi Smriti                              std::string& host, std::string& port,
11981bf712bcSAyushi Smriti                              std::string& path)
11991bf712bcSAyushi Smriti     {
12001bf712bcSAyushi Smriti         // Validate URL using regex expression
12011bf712bcSAyushi Smriti         // Format: <protocol>://<host>:<port>/<path>
12021bf712bcSAyushi Smriti         // protocol: http/https
12031bf712bcSAyushi Smriti         const std::regex urlRegex(
12041bf712bcSAyushi Smriti             "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
12051bf712bcSAyushi Smriti             "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
12061bf712bcSAyushi Smriti         std::cmatch match;
12071bf712bcSAyushi Smriti         if (!std::regex_match(destUrl.c_str(), match, urlRegex))
12081bf712bcSAyushi Smriti         {
12091bf712bcSAyushi Smriti             BMCWEB_LOG_INFO << "Dest. url did not match ";
12101bf712bcSAyushi Smriti             return false;
12111bf712bcSAyushi Smriti         }
12121bf712bcSAyushi Smriti 
12131bf712bcSAyushi Smriti         urlProto = std::string(match[1].first, match[1].second);
12141bf712bcSAyushi Smriti         if (urlProto == "http")
12151bf712bcSAyushi Smriti         {
12161bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
12171bf712bcSAyushi Smriti             return false;
12181bf712bcSAyushi Smriti #endif
12191bf712bcSAyushi Smriti         }
12201bf712bcSAyushi Smriti 
12211bf712bcSAyushi Smriti         host = std::string(match[2].first, match[2].second);
12221bf712bcSAyushi Smriti         port = std::string(match[3].first, match[3].second);
12231bf712bcSAyushi Smriti         path = std::string(match[4].first, match[4].second);
12241bf712bcSAyushi Smriti         if (port.empty())
12251bf712bcSAyushi Smriti         {
12261bf712bcSAyushi Smriti             if (urlProto == "http")
12271bf712bcSAyushi Smriti             {
12281bf712bcSAyushi Smriti                 port = "80";
12291bf712bcSAyushi Smriti             }
12301bf712bcSAyushi Smriti             else
12311bf712bcSAyushi Smriti             {
12321bf712bcSAyushi Smriti                 port = "443";
12331bf712bcSAyushi Smriti             }
12341bf712bcSAyushi Smriti         }
12351bf712bcSAyushi Smriti         if (path.empty())
12361bf712bcSAyushi Smriti         {
12371bf712bcSAyushi Smriti             path = "/";
12381bf712bcSAyushi Smriti         }
12391bf712bcSAyushi Smriti         return true;
12401bf712bcSAyushi Smriti     }
12417f4eb588SAppaRao Puli }; // namespace redfish
1242b52664e2SAppaRao Puli 
1243b52664e2SAppaRao Puli } // namespace redfish
1244