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>
284bbf237fSAppaRao Puli #include <server_sent_events.hpp>
291214b7e7SGunnar Mills #include <utils/json_utils.hpp>
301214b7e7SGunnar Mills 
31b52664e2SAppaRao Puli #include <cstdlib>
32b52664e2SAppaRao Puli #include <ctime>
331bf712bcSAyushi Smriti #include <fstream>
34b52664e2SAppaRao Puli #include <memory>
35b52664e2SAppaRao Puli #include <variant>
36b52664e2SAppaRao Puli 
37b52664e2SAppaRao Puli namespace redfish
38b52664e2SAppaRao Puli {
39156d6b00SAppaRao Puli 
40156d6b00SAppaRao Puli using ReadingsObjType =
41156d6b00SAppaRao Puli     std::vector<std::tuple<std::string, std::string, double, std::string>>;
427d1cc387SAppaRao Puli using EventServiceConfig = std::tuple<bool, uint32_t, uint32_t>;
43156d6b00SAppaRao Puli 
44156d6b00SAppaRao Puli static constexpr const char* eventFormatType = "Event";
45156d6b00SAppaRao Puli static constexpr const char* metricReportFormatType = "MetricReport";
46156d6b00SAppaRao Puli 
471bf712bcSAyushi Smriti static constexpr const char* eventServiceFile =
481bf712bcSAyushi Smriti     "/var/lib/bmcweb/eventservice_config.json";
491bf712bcSAyushi Smriti 
507f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
517f4eb588SAppaRao Puli std::shared_ptr<boost::asio::posix::stream_descriptor> inotifyConn = nullptr;
52b792cc56SAppaRao Puli static constexpr const char* redfishEventLogDir = "/var/log";
53b792cc56SAppaRao Puli static constexpr const char* redfishEventLogFile = "/var/log/redfish";
54b792cc56SAppaRao Puli static constexpr const size_t iEventSize = sizeof(inotify_event);
55b792cc56SAppaRao Puli static int inotifyFd = -1;
56b792cc56SAppaRao Puli static int dirWatchDesc = -1;
57b792cc56SAppaRao Puli static int fileWatchDesc = -1;
587f4eb588SAppaRao Puli 
597f4eb588SAppaRao Puli // <ID, timestamp, RedfishLogId, registryPrefix, MessageId, MessageArgs>
607f4eb588SAppaRao Puli using EventLogObjectsType =
617f4eb588SAppaRao Puli     std::tuple<std::string, std::string, std::string, std::string, std::string,
625e715de6SAppaRao Puli                std::vector<std::string>>;
637f4eb588SAppaRao Puli 
647f4eb588SAppaRao Puli namespace message_registries
657f4eb588SAppaRao Puli {
667f4eb588SAppaRao Puli static const Message*
677f4eb588SAppaRao Puli     getMsgFromRegistry(const std::string& messageKey,
687f4eb588SAppaRao Puli                        const boost::beast::span<const MessageEntry>& registry)
697f4eb588SAppaRao Puli {
707f4eb588SAppaRao Puli     boost::beast::span<const MessageEntry>::const_iterator messageIt =
717f4eb588SAppaRao Puli         std::find_if(registry.cbegin(), registry.cend(),
727f4eb588SAppaRao Puli                      [&messageKey](const MessageEntry& messageEntry) {
737f4eb588SAppaRao Puli                          return !messageKey.compare(messageEntry.first);
747f4eb588SAppaRao Puli                      });
757f4eb588SAppaRao Puli     if (messageIt != registry.cend())
767f4eb588SAppaRao Puli     {
777f4eb588SAppaRao Puli         return &messageIt->second;
787f4eb588SAppaRao Puli     }
797f4eb588SAppaRao Puli 
807f4eb588SAppaRao Puli     return nullptr;
817f4eb588SAppaRao Puli }
827f4eb588SAppaRao Puli 
837f4eb588SAppaRao Puli static const Message* formatMessage(const std::string_view& messageID)
847f4eb588SAppaRao Puli {
857f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
867f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
877f4eb588SAppaRao Puli     // the right Message
887f4eb588SAppaRao Puli     std::vector<std::string> fields;
897f4eb588SAppaRao Puli     fields.reserve(4);
907f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
917f4eb588SAppaRao Puli     if (fields.size() != 4)
927f4eb588SAppaRao Puli     {
937f4eb588SAppaRao Puli         return nullptr;
947f4eb588SAppaRao Puli     }
957f4eb588SAppaRao Puli     std::string& registryName = fields[0];
967f4eb588SAppaRao Puli     std::string& messageKey = fields[3];
977f4eb588SAppaRao Puli 
987f4eb588SAppaRao Puli     // Find the right registry and check it for the MessageKey
997f4eb588SAppaRao Puli     if (std::string(base::header.registryPrefix) == registryName)
1007f4eb588SAppaRao Puli     {
1017f4eb588SAppaRao Puli         return getMsgFromRegistry(
1027f4eb588SAppaRao Puli             messageKey, boost::beast::span<const MessageEntry>(base::registry));
1037f4eb588SAppaRao Puli     }
1047f4eb588SAppaRao Puli     if (std::string(openbmc::header.registryPrefix) == registryName)
1057f4eb588SAppaRao Puli     {
1067f4eb588SAppaRao Puli         return getMsgFromRegistry(
1077f4eb588SAppaRao Puli             messageKey,
1087f4eb588SAppaRao Puli             boost::beast::span<const MessageEntry>(openbmc::registry));
1097f4eb588SAppaRao Puli     }
1107f4eb588SAppaRao Puli     return nullptr;
1117f4eb588SAppaRao Puli }
1127f4eb588SAppaRao Puli } // namespace message_registries
1137f4eb588SAppaRao Puli 
1147f4eb588SAppaRao Puli namespace event_log
1157f4eb588SAppaRao Puli {
1167f4eb588SAppaRao Puli bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
1177f4eb588SAppaRao Puli                       const bool firstEntry = true)
1187f4eb588SAppaRao Puli {
1197f4eb588SAppaRao Puli     static time_t prevTs = 0;
1207f4eb588SAppaRao Puli     static int index = 0;
1217f4eb588SAppaRao Puli     if (firstEntry)
1227f4eb588SAppaRao Puli     {
1237f4eb588SAppaRao Puli         prevTs = 0;
1247f4eb588SAppaRao Puli     }
1257f4eb588SAppaRao Puli 
1267f4eb588SAppaRao Puli     // Get the entry timestamp
1277f4eb588SAppaRao Puli     std::time_t curTs = 0;
1287f4eb588SAppaRao Puli     std::tm timeStruct = {};
1297f4eb588SAppaRao Puli     std::istringstream entryStream(logEntry);
1307f4eb588SAppaRao Puli     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1317f4eb588SAppaRao Puli     {
1327f4eb588SAppaRao Puli         curTs = std::mktime(&timeStruct);
1337f4eb588SAppaRao Puli         if (curTs == -1)
1347f4eb588SAppaRao Puli         {
1357f4eb588SAppaRao Puli             return false;
1367f4eb588SAppaRao Puli         }
1377f4eb588SAppaRao Puli     }
1387f4eb588SAppaRao Puli     // If the timestamp isn't unique, increment the index
1397f4eb588SAppaRao Puli     index = (curTs == prevTs) ? index + 1 : 0;
1407f4eb588SAppaRao Puli 
1417f4eb588SAppaRao Puli     // Save the timestamp
1427f4eb588SAppaRao Puli     prevTs = curTs;
1437f4eb588SAppaRao Puli 
1447f4eb588SAppaRao Puli     entryID = std::to_string(curTs);
1457f4eb588SAppaRao Puli     if (index > 0)
1467f4eb588SAppaRao Puli     {
1477f4eb588SAppaRao Puli         entryID += "_" + std::to_string(index);
1487f4eb588SAppaRao Puli     }
1497f4eb588SAppaRao Puli     return true;
1507f4eb588SAppaRao Puli }
1517f4eb588SAppaRao Puli 
1527f4eb588SAppaRao Puli int getEventLogParams(const std::string& logEntry, std::string& timestamp,
1537f4eb588SAppaRao Puli                       std::string& messageID,
1545e715de6SAppaRao Puli                       std::vector<std::string>& messageArgs)
1557f4eb588SAppaRao Puli {
1567f4eb588SAppaRao Puli     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1577f4eb588SAppaRao Puli     // First get the Timestamp
1587f4eb588SAppaRao Puli     size_t space = logEntry.find_first_of(" ");
1597f4eb588SAppaRao Puli     if (space == std::string::npos)
1607f4eb588SAppaRao Puli     {
1617f4eb588SAppaRao Puli         return -EINVAL;
1627f4eb588SAppaRao Puli     }
1637f4eb588SAppaRao Puli     timestamp = logEntry.substr(0, space);
1647f4eb588SAppaRao Puli     // Then get the log contents
1657f4eb588SAppaRao Puli     size_t entryStart = logEntry.find_first_not_of(" ", space);
1667f4eb588SAppaRao Puli     if (entryStart == std::string::npos)
1677f4eb588SAppaRao Puli     {
1687f4eb588SAppaRao Puli         return -EINVAL;
1697f4eb588SAppaRao Puli     }
1707f4eb588SAppaRao Puli     std::string_view entry(logEntry);
1717f4eb588SAppaRao Puli     entry.remove_prefix(entryStart);
1727f4eb588SAppaRao Puli     // Use split to separate the entry into its fields
1737f4eb588SAppaRao Puli     std::vector<std::string> logEntryFields;
1747f4eb588SAppaRao Puli     boost::split(logEntryFields, entry, boost::is_any_of(","),
1757f4eb588SAppaRao Puli                  boost::token_compress_on);
1767f4eb588SAppaRao Puli     // We need at least a MessageId to be valid
1777f4eb588SAppaRao Puli     if (logEntryFields.size() < 1)
1787f4eb588SAppaRao Puli     {
1797f4eb588SAppaRao Puli         return -EINVAL;
1807f4eb588SAppaRao Puli     }
1817f4eb588SAppaRao Puli     messageID = logEntryFields[0];
1827f4eb588SAppaRao Puli 
1837f4eb588SAppaRao Puli     // Get the MessageArgs from the log if there are any
1847f4eb588SAppaRao Puli     if (logEntryFields.size() > 1)
1857f4eb588SAppaRao Puli     {
1867f4eb588SAppaRao Puli         std::string& messageArgsStart = logEntryFields[1];
1877f4eb588SAppaRao Puli         // If the first string is empty, assume there are no MessageArgs
1887f4eb588SAppaRao Puli         if (!messageArgsStart.empty())
1897f4eb588SAppaRao Puli         {
1905e715de6SAppaRao Puli             messageArgs.assign(logEntryFields.begin() + 1,
1915e715de6SAppaRao Puli                                logEntryFields.end());
1927f4eb588SAppaRao Puli         }
1937f4eb588SAppaRao Puli     }
1947f4eb588SAppaRao Puli 
1957f4eb588SAppaRao Puli     return 0;
1967f4eb588SAppaRao Puli }
1977f4eb588SAppaRao Puli 
1987f4eb588SAppaRao Puli void getRegistryAndMessageKey(const std::string& messageID,
1997f4eb588SAppaRao Puli                               std::string& registryName,
2007f4eb588SAppaRao Puli                               std::string& messageKey)
2017f4eb588SAppaRao Puli {
2027f4eb588SAppaRao Puli     // Redfish MessageIds are in the form
2037f4eb588SAppaRao Puli     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
2047f4eb588SAppaRao Puli     // the right Message
2057f4eb588SAppaRao Puli     std::vector<std::string> fields;
2067f4eb588SAppaRao Puli     fields.reserve(4);
2077f4eb588SAppaRao Puli     boost::split(fields, messageID, boost::is_any_of("."));
2087f4eb588SAppaRao Puli     if (fields.size() == 4)
2097f4eb588SAppaRao Puli     {
2107f4eb588SAppaRao Puli         registryName = fields[0];
2117f4eb588SAppaRao Puli         messageKey = fields[3];
2127f4eb588SAppaRao Puli     }
2137f4eb588SAppaRao Puli }
2147f4eb588SAppaRao Puli 
2157f4eb588SAppaRao Puli int formatEventLogEntry(const std::string& logEntryID,
2167f4eb588SAppaRao Puli                         const std::string& messageID,
2175e715de6SAppaRao Puli                         const std::vector<std::string>& messageArgs,
2187f4eb588SAppaRao Puli                         std::string timestamp, const std::string customText,
2197f4eb588SAppaRao Puli                         nlohmann::json& logEntryJson)
2207f4eb588SAppaRao Puli {
2217f4eb588SAppaRao Puli     // Get the Message from the MessageRegistry
2227f4eb588SAppaRao Puli     const message_registries::Message* message =
2237f4eb588SAppaRao Puli         message_registries::formatMessage(messageID);
2247f4eb588SAppaRao Puli 
2257f4eb588SAppaRao Puli     std::string msg;
2267f4eb588SAppaRao Puli     std::string severity;
2277f4eb588SAppaRao Puli     if (message != nullptr)
2287f4eb588SAppaRao Puli     {
2297f4eb588SAppaRao Puli         msg = message->message;
2307f4eb588SAppaRao Puli         severity = message->severity;
2317f4eb588SAppaRao Puli     }
2327f4eb588SAppaRao Puli 
2337f4eb588SAppaRao Puli     // Fill the MessageArgs into the Message
2347f4eb588SAppaRao Puli     int i = 0;
2357f4eb588SAppaRao Puli     for (const std::string& messageArg : messageArgs)
2367f4eb588SAppaRao Puli     {
2377f4eb588SAppaRao Puli         std::string argStr = "%" + std::to_string(++i);
2387f4eb588SAppaRao Puli         size_t argPos = msg.find(argStr);
2397f4eb588SAppaRao Puli         if (argPos != std::string::npos)
2407f4eb588SAppaRao Puli         {
2417f4eb588SAppaRao Puli             msg.replace(argPos, argStr.length(), messageArg);
2427f4eb588SAppaRao Puli         }
2437f4eb588SAppaRao Puli     }
2447f4eb588SAppaRao Puli 
2457f4eb588SAppaRao Puli     // Get the Created time from the timestamp. The log timestamp is in
2467f4eb588SAppaRao Puli     // RFC3339 format which matches the Redfish format except for the
2477f4eb588SAppaRao Puli     // fractional seconds between the '.' and the '+', so just remove them.
2487f4eb588SAppaRao Puli     std::size_t dot = timestamp.find_first_of(".");
2497f4eb588SAppaRao Puli     std::size_t plus = timestamp.find_first_of("+");
2507f4eb588SAppaRao Puli     if (dot != std::string::npos && plus != std::string::npos)
2517f4eb588SAppaRao Puli     {
2527f4eb588SAppaRao Puli         timestamp.erase(dot, plus - dot);
2537f4eb588SAppaRao Puli     }
2547f4eb588SAppaRao Puli 
2557f4eb588SAppaRao Puli     // Fill in the log entry with the gathered data
2567f4eb588SAppaRao Puli     logEntryJson = {{"EventId", logEntryID},
2577f4eb588SAppaRao Puli                     {"EventType", "Event"},
2587f4eb588SAppaRao Puli                     {"Severity", std::move(severity)},
2597f4eb588SAppaRao Puli                     {"Message", std::move(msg)},
2607f4eb588SAppaRao Puli                     {"MessageId", std::move(messageID)},
2617f4eb588SAppaRao Puli                     {"MessageArgs", std::move(messageArgs)},
2627f4eb588SAppaRao Puli                     {"EventTimestamp", std::move(timestamp)},
2637f4eb588SAppaRao Puli                     {"Context", customText}};
2647f4eb588SAppaRao Puli     return 0;
2657f4eb588SAppaRao Puli }
2667f4eb588SAppaRao Puli 
2677f4eb588SAppaRao Puli } // namespace event_log
2687f4eb588SAppaRao Puli #endif
2697f4eb588SAppaRao Puli 
27007941a88SAyushi Smriti bool isFilterQuerySpecialChar(char c)
27107941a88SAyushi Smriti {
27207941a88SAyushi Smriti     switch (c)
27307941a88SAyushi Smriti     {
27407941a88SAyushi Smriti         case '(':
27507941a88SAyushi Smriti         case ')':
27607941a88SAyushi Smriti         case '\'':
27707941a88SAyushi Smriti             return true;
27807941a88SAyushi Smriti         default:
27907941a88SAyushi Smriti             return false;
28007941a88SAyushi Smriti     }
28107941a88SAyushi Smriti }
28207941a88SAyushi Smriti 
28307941a88SAyushi Smriti bool readSSEQueryParams(std::string sseFilter, std::string& formatType,
28407941a88SAyushi Smriti                         std::vector<std::string>& messageIds,
28507941a88SAyushi Smriti                         std::vector<std::string>& registryPrefixes,
28607941a88SAyushi Smriti                         std::vector<nlohmann::json>& metricReportDefinitions)
28707941a88SAyushi Smriti {
28807941a88SAyushi Smriti     sseFilter.erase(std::remove_if(sseFilter.begin(), sseFilter.end(),
28907941a88SAyushi Smriti                                    isFilterQuerySpecialChar),
29007941a88SAyushi Smriti                     sseFilter.end());
29107941a88SAyushi Smriti 
29207941a88SAyushi Smriti     std::vector<std::string> result;
29307941a88SAyushi Smriti     boost::split(result, sseFilter, boost::is_any_of(" "),
29407941a88SAyushi Smriti                  boost::token_compress_on);
29507941a88SAyushi Smriti 
29607941a88SAyushi Smriti     BMCWEB_LOG_DEBUG << "No of tokens in SEE query: " << result.size();
29707941a88SAyushi Smriti 
29807941a88SAyushi Smriti     constexpr uint8_t divisor = 4;
29907941a88SAyushi Smriti     constexpr uint8_t minTokenSize = 3;
30007941a88SAyushi Smriti     if (result.size() % divisor != minTokenSize)
30107941a88SAyushi Smriti     {
30207941a88SAyushi Smriti         BMCWEB_LOG_ERROR << "Invalid SSE filter specified.";
30307941a88SAyushi Smriti         return false;
30407941a88SAyushi Smriti     }
30507941a88SAyushi Smriti 
30607941a88SAyushi Smriti     for (std::size_t i = 0; i < result.size(); i += divisor)
30707941a88SAyushi Smriti     {
30807941a88SAyushi Smriti         std::string& key = result[i];
30907941a88SAyushi Smriti         std::string& op = result[i + 1];
31007941a88SAyushi Smriti         std::string& value = result[i + 2];
31107941a88SAyushi Smriti 
31207941a88SAyushi Smriti         if ((i + minTokenSize) < result.size())
31307941a88SAyushi Smriti         {
3144e0453b1SGunnar Mills             std::string& separator = result[i + minTokenSize];
31507941a88SAyushi Smriti             // SSE supports only "or" and "and" in query params.
3164e0453b1SGunnar Mills             if ((separator != "or") && (separator != "and"))
31707941a88SAyushi Smriti             {
31807941a88SAyushi Smriti                 BMCWEB_LOG_ERROR
31907941a88SAyushi Smriti                     << "Invalid group operator in SSE query parameters";
32007941a88SAyushi Smriti                 return false;
32107941a88SAyushi Smriti             }
32207941a88SAyushi Smriti         }
32307941a88SAyushi Smriti 
32407941a88SAyushi Smriti         // SSE supports only "eq" as per spec.
32507941a88SAyushi Smriti         if (op != "eq")
32607941a88SAyushi Smriti         {
32707941a88SAyushi Smriti             BMCWEB_LOG_ERROR
32807941a88SAyushi Smriti                 << "Invalid assignment operator in SSE query parameters";
32907941a88SAyushi Smriti             return false;
33007941a88SAyushi Smriti         }
33107941a88SAyushi Smriti 
33207941a88SAyushi Smriti         BMCWEB_LOG_DEBUG << key << " : " << value;
33307941a88SAyushi Smriti         if (key == "EventFormatType")
33407941a88SAyushi Smriti         {
33507941a88SAyushi Smriti             formatType = value;
33607941a88SAyushi Smriti         }
33707941a88SAyushi Smriti         else if (key == "MessageId")
33807941a88SAyushi Smriti         {
33907941a88SAyushi Smriti             messageIds.push_back(value);
34007941a88SAyushi Smriti         }
34107941a88SAyushi Smriti         else if (key == "RegistryPrefix")
34207941a88SAyushi Smriti         {
34307941a88SAyushi Smriti             registryPrefixes.push_back(value);
34407941a88SAyushi Smriti         }
34507941a88SAyushi Smriti         else if (key == "MetricReportDefinition")
34607941a88SAyushi Smriti         {
34707941a88SAyushi Smriti             metricReportDefinitions.push_back(value);
34807941a88SAyushi Smriti         }
34907941a88SAyushi Smriti         else
35007941a88SAyushi Smriti         {
35107941a88SAyushi Smriti             BMCWEB_LOG_ERROR << "Invalid property(" << key
35207941a88SAyushi Smriti                              << ")in SSE filter query.";
35307941a88SAyushi Smriti             return false;
35407941a88SAyushi Smriti         }
35507941a88SAyushi Smriti     }
35607941a88SAyushi Smriti     return true;
35707941a88SAyushi Smriti }
35807941a88SAyushi Smriti 
359b52664e2SAppaRao Puli class Subscription
360b52664e2SAppaRao Puli {
361b52664e2SAppaRao Puli   public:
362b52664e2SAppaRao Puli     std::string id;
363b52664e2SAppaRao Puli     std::string destinationUrl;
364b52664e2SAppaRao Puli     std::string protocol;
365b52664e2SAppaRao Puli     std::string retryPolicy;
366b52664e2SAppaRao Puli     std::string customText;
367b52664e2SAppaRao Puli     std::string eventFormatType;
368b52664e2SAppaRao Puli     std::string subscriptionType;
369b52664e2SAppaRao Puli     std::vector<std::string> registryMsgIds;
370b52664e2SAppaRao Puli     std::vector<std::string> registryPrefixes;
371e56f254cSSunitha Harish     std::vector<std::string> resourceTypes;
372b52664e2SAppaRao Puli     std::vector<nlohmann::json> httpHeaders; // key-value pair
373156d6b00SAppaRao Puli     std::vector<nlohmann::json> metricReportDefinitions;
374b52664e2SAppaRao Puli 
375b52664e2SAppaRao Puli     Subscription(const Subscription&) = delete;
376b52664e2SAppaRao Puli     Subscription& operator=(const Subscription&) = delete;
377b52664e2SAppaRao Puli     Subscription(Subscription&&) = delete;
378b52664e2SAppaRao Puli     Subscription& operator=(Subscription&&) = delete;
379b52664e2SAppaRao Puli 
380b52664e2SAppaRao Puli     Subscription(const std::string& inHost, const std::string& inPort,
381b52664e2SAppaRao Puli                  const std::string& inPath, const std::string& inUriProto) :
3820b4bdd93SAppaRao Puli         eventSeqNum(1),
3830b4bdd93SAppaRao Puli         host(inHost), port(inPort), path(inPath), uriProto(inUriProto)
384b52664e2SAppaRao Puli     {
385b52664e2SAppaRao Puli         conn = std::make_shared<crow::HttpClient>(
386fe44eb0bSAyushi Smriti             crow::connections::systemBus->get_io_context(), id, host, port,
387fe44eb0bSAyushi Smriti             path);
388b52664e2SAppaRao Puli     }
3894bbf237fSAppaRao Puli 
3904bbf237fSAppaRao Puli     Subscription(const std::shared_ptr<crow::Request::Adaptor>& adaptor) :
3914bbf237fSAppaRao Puli         eventSeqNum(1)
3924bbf237fSAppaRao Puli     {
3934bbf237fSAppaRao Puli         sseConn = std::make_shared<crow::ServerSentEvents>(adaptor);
3944bbf237fSAppaRao Puli     }
3954bbf237fSAppaRao Puli 
396b52664e2SAppaRao Puli     ~Subscription()
3971214b7e7SGunnar Mills     {}
398b52664e2SAppaRao Puli 
399b52664e2SAppaRao Puli     void sendEvent(const std::string& msg)
400b52664e2SAppaRao Puli     {
4014bbf237fSAppaRao Puli         if (conn != nullptr)
4024bbf237fSAppaRao Puli         {
403b52664e2SAppaRao Puli             std::vector<std::pair<std::string, std::string>> reqHeaders;
404b52664e2SAppaRao Puli             for (const auto& header : httpHeaders)
405b52664e2SAppaRao Puli             {
406b52664e2SAppaRao Puli                 for (const auto& item : header.items())
407b52664e2SAppaRao Puli                 {
408b52664e2SAppaRao Puli                     std::string key = item.key();
409b52664e2SAppaRao Puli                     std::string val = item.value();
410b52664e2SAppaRao Puli                     reqHeaders.emplace_back(std::pair(key, val));
411b52664e2SAppaRao Puli                 }
412b52664e2SAppaRao Puli             }
413b52664e2SAppaRao Puli             conn->setHeaders(reqHeaders);
4142a5689a7SAppaRao Puli             conn->sendData(msg);
41596330b99SSunitha Harish             this->eventSeqNum++;
416b52664e2SAppaRao Puli         }
417b52664e2SAppaRao Puli 
4184bbf237fSAppaRao Puli         if (sseConn != nullptr)
4194bbf237fSAppaRao Puli         {
4204bbf237fSAppaRao Puli             sseConn->sendData(eventSeqNum, msg);
4214bbf237fSAppaRao Puli         }
4224bbf237fSAppaRao Puli     }
4234bbf237fSAppaRao Puli 
4240b4bdd93SAppaRao Puli     void sendTestEventLog()
4250b4bdd93SAppaRao Puli     {
4260b4bdd93SAppaRao Puli         nlohmann::json logEntryArray;
4270b4bdd93SAppaRao Puli         logEntryArray.push_back({});
4280b4bdd93SAppaRao Puli         nlohmann::json& logEntryJson = logEntryArray.back();
4290b4bdd93SAppaRao Puli 
4300b4bdd93SAppaRao Puli         logEntryJson = {{"EventId", "TestID"},
4310b4bdd93SAppaRao Puli                         {"EventType", "Event"},
4320b4bdd93SAppaRao Puli                         {"Severity", "OK"},
4330b4bdd93SAppaRao Puli                         {"Message", "Generated test event"},
4340b4bdd93SAppaRao Puli                         {"MessageId", "OpenBMC.0.1.TestEventLog"},
4350b4bdd93SAppaRao Puli                         {"MessageArgs", nlohmann::json::array()},
4360b4bdd93SAppaRao Puli                         {"EventTimestamp", crow::utility::dateTimeNow()},
4370b4bdd93SAppaRao Puli                         {"Context", customText}};
4380b4bdd93SAppaRao Puli 
4390b4bdd93SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
4400b4bdd93SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
4410b4bdd93SAppaRao Puli                               {"Name", "Event Log"},
4420b4bdd93SAppaRao Puli                               {"Events", logEntryArray}};
4430b4bdd93SAppaRao Puli 
4440b4bdd93SAppaRao Puli         this->sendEvent(msg.dump());
4450b4bdd93SAppaRao Puli     }
4460b4bdd93SAppaRao Puli 
4477f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
4487f4eb588SAppaRao Puli     void filterAndSendEventLogs(
4497f4eb588SAppaRao Puli         const std::vector<EventLogObjectsType>& eventRecords)
4507f4eb588SAppaRao Puli     {
4517f4eb588SAppaRao Puli         nlohmann::json logEntryArray;
4527f4eb588SAppaRao Puli         for (const EventLogObjectsType& logEntry : eventRecords)
4537f4eb588SAppaRao Puli         {
4547f4eb588SAppaRao Puli             const std::string& idStr = std::get<0>(logEntry);
4557f4eb588SAppaRao Puli             const std::string& timestamp = std::get<1>(logEntry);
4567f4eb588SAppaRao Puli             const std::string& messageID = std::get<2>(logEntry);
4577f4eb588SAppaRao Puli             const std::string& registryName = std::get<3>(logEntry);
4587f4eb588SAppaRao Puli             const std::string& messageKey = std::get<4>(logEntry);
4595e715de6SAppaRao Puli             const std::vector<std::string>& messageArgs = std::get<5>(logEntry);
4607f4eb588SAppaRao Puli 
4617f4eb588SAppaRao Puli             // If registryPrefixes list is empty, don't filter events
4627f4eb588SAppaRao Puli             // send everything.
4637f4eb588SAppaRao Puli             if (registryPrefixes.size())
4647f4eb588SAppaRao Puli             {
4657f4eb588SAppaRao Puli                 auto obj = std::find(registryPrefixes.begin(),
4667f4eb588SAppaRao Puli                                      registryPrefixes.end(), registryName);
4677f4eb588SAppaRao Puli                 if (obj == registryPrefixes.end())
4687f4eb588SAppaRao Puli                 {
4697f4eb588SAppaRao Puli                     continue;
4707f4eb588SAppaRao Puli                 }
4717f4eb588SAppaRao Puli             }
4727f4eb588SAppaRao Puli 
4737f4eb588SAppaRao Puli             // If registryMsgIds list is empty, don't filter events
4747f4eb588SAppaRao Puli             // send everything.
4757f4eb588SAppaRao Puli             if (registryMsgIds.size())
4767f4eb588SAppaRao Puli             {
4777f4eb588SAppaRao Puli                 auto obj = std::find(registryMsgIds.begin(),
4787f4eb588SAppaRao Puli                                      registryMsgIds.end(), messageKey);
4797f4eb588SAppaRao Puli                 if (obj == registryMsgIds.end())
4807f4eb588SAppaRao Puli                 {
4817f4eb588SAppaRao Puli                     continue;
4827f4eb588SAppaRao Puli                 }
4837f4eb588SAppaRao Puli             }
4847f4eb588SAppaRao Puli 
4857f4eb588SAppaRao Puli             logEntryArray.push_back({});
4867f4eb588SAppaRao Puli             nlohmann::json& bmcLogEntry = logEntryArray.back();
4877f4eb588SAppaRao Puli             if (event_log::formatEventLogEntry(idStr, messageID, messageArgs,
4887f4eb588SAppaRao Puli                                                timestamp, customText,
4897f4eb588SAppaRao Puli                                                bmcLogEntry) != 0)
4907f4eb588SAppaRao Puli             {
4917f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry failed";
4927f4eb588SAppaRao Puli                 continue;
4937f4eb588SAppaRao Puli             }
4947f4eb588SAppaRao Puli         }
4957f4eb588SAppaRao Puli 
4967f4eb588SAppaRao Puli         if (logEntryArray.size() < 1)
4977f4eb588SAppaRao Puli         {
4987f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "No log entries available to be transferred.";
4997f4eb588SAppaRao Puli             return;
5007f4eb588SAppaRao Puli         }
5017f4eb588SAppaRao Puli 
5027f4eb588SAppaRao Puli         nlohmann::json msg = {{"@odata.type", "#Event.v1_4_0.Event"},
5037f4eb588SAppaRao Puli                               {"Id", std::to_string(eventSeqNum)},
5047f4eb588SAppaRao Puli                               {"Name", "Event Log"},
5057f4eb588SAppaRao Puli                               {"Events", logEntryArray}};
5067f4eb588SAppaRao Puli 
5077f4eb588SAppaRao Puli         this->sendEvent(msg.dump());
5087f4eb588SAppaRao Puli     }
5097f4eb588SAppaRao Puli #endif
5107f4eb588SAppaRao Puli 
511156d6b00SAppaRao Puli     void filterAndSendReports(const std::string& id,
512156d6b00SAppaRao Puli                               const std::string& readingsTs,
513156d6b00SAppaRao Puli                               const ReadingsObjType& readings)
514156d6b00SAppaRao Puli     {
515156d6b00SAppaRao Puli         std::string metricReportDef =
516156d6b00SAppaRao Puli             "/redfish/v1/TelemetryService/MetricReportDefinitions/" + id;
517156d6b00SAppaRao Puli 
518156d6b00SAppaRao Puli         // Empty list means no filter. Send everything.
519156d6b00SAppaRao Puli         if (metricReportDefinitions.size())
520156d6b00SAppaRao Puli         {
521156d6b00SAppaRao Puli             if (std::find(metricReportDefinitions.begin(),
522156d6b00SAppaRao Puli                           metricReportDefinitions.end(),
523156d6b00SAppaRao Puli                           metricReportDef) == metricReportDefinitions.end())
524156d6b00SAppaRao Puli             {
525156d6b00SAppaRao Puli                 return;
526156d6b00SAppaRao Puli             }
527156d6b00SAppaRao Puli         }
528156d6b00SAppaRao Puli 
529156d6b00SAppaRao Puli         nlohmann::json metricValuesArray = nlohmann::json::array();
530156d6b00SAppaRao Puli         for (const auto& it : readings)
531156d6b00SAppaRao Puli         {
532156d6b00SAppaRao Puli             metricValuesArray.push_back({});
533156d6b00SAppaRao Puli             nlohmann::json& entry = metricValuesArray.back();
534156d6b00SAppaRao Puli 
535156d6b00SAppaRao Puli             entry = {{"MetricId", std::get<0>(it)},
536156d6b00SAppaRao Puli                      {"MetricProperty", std::get<1>(it)},
537156d6b00SAppaRao Puli                      {"MetricValue", std::to_string(std::get<2>(it))},
538156d6b00SAppaRao Puli                      {"Timestamp", std::get<3>(it)}};
539156d6b00SAppaRao Puli         }
540156d6b00SAppaRao Puli 
541156d6b00SAppaRao Puli         nlohmann::json msg = {
542156d6b00SAppaRao Puli             {"@odata.id", "/redfish/v1/TelemetryService/MetricReports/" + id},
543156d6b00SAppaRao Puli             {"@odata.type", "#MetricReport.v1_3_0.MetricReport"},
544156d6b00SAppaRao Puli             {"Id", id},
545156d6b00SAppaRao Puli             {"Name", id},
546156d6b00SAppaRao Puli             {"Timestamp", readingsTs},
547156d6b00SAppaRao Puli             {"MetricReportDefinition", {{"@odata.id", metricReportDef}}},
548156d6b00SAppaRao Puli             {"MetricValues", metricValuesArray}};
549156d6b00SAppaRao Puli 
550156d6b00SAppaRao Puli         this->sendEvent(msg.dump());
551156d6b00SAppaRao Puli     }
552156d6b00SAppaRao Puli 
553fe44eb0bSAyushi Smriti     void updateRetryConfig(const uint32_t retryAttempts,
554fe44eb0bSAyushi Smriti                            const uint32_t retryTimeoutInterval)
555fe44eb0bSAyushi Smriti     {
55662de0c68SAppaRao Puli         if (conn != nullptr)
55762de0c68SAppaRao Puli         {
558fe44eb0bSAyushi Smriti             conn->setRetryConfig(retryAttempts, retryTimeoutInterval);
559fe44eb0bSAyushi Smriti         }
56062de0c68SAppaRao Puli     }
561fe44eb0bSAyushi Smriti 
562fe44eb0bSAyushi Smriti     void updateRetryPolicy()
563fe44eb0bSAyushi Smriti     {
56462de0c68SAppaRao Puli         if (conn != nullptr)
56562de0c68SAppaRao Puli         {
566fe44eb0bSAyushi Smriti             conn->setRetryPolicy(retryPolicy);
567fe44eb0bSAyushi Smriti         }
56862de0c68SAppaRao Puli     }
569fe44eb0bSAyushi Smriti 
57096330b99SSunitha Harish     uint64_t getEventSeqNum()
57196330b99SSunitha Harish     {
57296330b99SSunitha Harish         return eventSeqNum;
57396330b99SSunitha Harish     }
57496330b99SSunitha Harish 
575b52664e2SAppaRao Puli   private:
5760b4bdd93SAppaRao Puli     uint64_t eventSeqNum;
577b52664e2SAppaRao Puli     std::string host;
578b52664e2SAppaRao Puli     std::string port;
579b52664e2SAppaRao Puli     std::string path;
580b52664e2SAppaRao Puli     std::string uriProto;
5814bbf237fSAppaRao Puli     std::shared_ptr<crow::HttpClient> conn = nullptr;
5824bbf237fSAppaRao Puli     std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr;
583b52664e2SAppaRao Puli };
584b52664e2SAppaRao Puli 
5851bf712bcSAyushi Smriti static constexpr const bool defaultEnabledState = true;
5861bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryAttempts = 3;
5871bf712bcSAyushi Smriti static constexpr const uint32_t defaultRetryInterval = 30;
5881bf712bcSAyushi Smriti static constexpr const char* defaulEventFormatType = "Event";
5891bf712bcSAyushi Smriti static constexpr const char* defaulSubscriptionType = "RedfishEvent";
5901bf712bcSAyushi Smriti static constexpr const char* defaulRetryPolicy = "TerminateAfterRetries";
5911bf712bcSAyushi Smriti 
592b52664e2SAppaRao Puli class EventServiceManager
593b52664e2SAppaRao Puli {
594b52664e2SAppaRao Puli   private:
5957d1cc387SAppaRao Puli     bool serviceEnabled;
5967d1cc387SAppaRao Puli     uint32_t retryAttempts;
5977d1cc387SAppaRao Puli     uint32_t retryTimeoutInterval;
5987d1cc387SAppaRao Puli 
599b52664e2SAppaRao Puli     EventServiceManager(const EventServiceManager&) = delete;
600b52664e2SAppaRao Puli     EventServiceManager& operator=(const EventServiceManager&) = delete;
601b52664e2SAppaRao Puli     EventServiceManager(EventServiceManager&&) = delete;
602b52664e2SAppaRao Puli     EventServiceManager& operator=(EventServiceManager&&) = delete;
603b52664e2SAppaRao Puli 
6047d1cc387SAppaRao Puli     EventServiceManager() :
60596330b99SSunitha Harish         noOfEventLogSubscribers(0), noOfMetricReportSubscribers(0), eventId(1)
606b52664e2SAppaRao Puli     {
6071bf712bcSAyushi Smriti         // Load config from persist store.
6081bf712bcSAyushi Smriti         initConfig();
609b52664e2SAppaRao Puli     }
610b52664e2SAppaRao Puli 
6117f4eb588SAppaRao Puli     std::string lastEventTStr;
6127d1cc387SAppaRao Puli     size_t noOfEventLogSubscribers;
613156d6b00SAppaRao Puli     size_t noOfMetricReportSubscribers;
614156d6b00SAppaRao Puli     std::shared_ptr<sdbusplus::bus::match::match> matchTelemetryMonitor;
615b52664e2SAppaRao Puli     boost::container::flat_map<std::string, std::shared_ptr<Subscription>>
616b52664e2SAppaRao Puli         subscriptionsMap;
617b52664e2SAppaRao Puli 
61896330b99SSunitha Harish     uint64_t eventId;
61996330b99SSunitha Harish 
620b52664e2SAppaRao Puli   public:
621b52664e2SAppaRao Puli     static EventServiceManager& getInstance()
622b52664e2SAppaRao Puli     {
623b52664e2SAppaRao Puli         static EventServiceManager handler;
624b52664e2SAppaRao Puli         return handler;
625b52664e2SAppaRao Puli     }
626b52664e2SAppaRao Puli 
6271bf712bcSAyushi Smriti     void loadDefaultConfig()
6281bf712bcSAyushi Smriti     {
6291bf712bcSAyushi Smriti         serviceEnabled = defaultEnabledState;
6301bf712bcSAyushi Smriti         retryAttempts = defaultRetryAttempts;
6311bf712bcSAyushi Smriti         retryTimeoutInterval = defaultRetryInterval;
6321bf712bcSAyushi Smriti     }
6331bf712bcSAyushi Smriti 
6341bf712bcSAyushi Smriti     void initConfig()
6351bf712bcSAyushi Smriti     {
6361bf712bcSAyushi Smriti         std::ifstream eventConfigFile(eventServiceFile);
6371bf712bcSAyushi Smriti         if (!eventConfigFile.good())
6381bf712bcSAyushi Smriti         {
6391bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService config not exist";
6401bf712bcSAyushi Smriti             loadDefaultConfig();
6411bf712bcSAyushi Smriti             return;
6421bf712bcSAyushi Smriti         }
6431bf712bcSAyushi Smriti         auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false);
6441bf712bcSAyushi Smriti         if (jsonData.is_discarded())
6451bf712bcSAyushi Smriti         {
6461bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "EventService config parse error.";
6471bf712bcSAyushi Smriti             loadDefaultConfig();
6481bf712bcSAyushi Smriti             return;
6491bf712bcSAyushi Smriti         }
6501bf712bcSAyushi Smriti 
6511bf712bcSAyushi Smriti         nlohmann::json jsonConfig;
6521bf712bcSAyushi Smriti         if (json_util::getValueFromJsonObject(jsonData, "Configuration",
6531bf712bcSAyushi Smriti                                               jsonConfig))
6541bf712bcSAyushi Smriti         {
6551bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonConfig, "ServiceEnabled",
6561bf712bcSAyushi Smriti                                                    serviceEnabled))
6571bf712bcSAyushi Smriti             {
6581bf712bcSAyushi Smriti                 serviceEnabled = defaultEnabledState;
6591bf712bcSAyushi Smriti             }
6601bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
6611bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryAttempts", retryAttempts))
6621bf712bcSAyushi Smriti             {
6631bf712bcSAyushi Smriti                 retryAttempts = defaultRetryAttempts;
6641bf712bcSAyushi Smriti             }
6651bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
6661bf712bcSAyushi Smriti                     jsonConfig, "DeliveryRetryIntervalSeconds",
6671bf712bcSAyushi Smriti                     retryTimeoutInterval))
6681bf712bcSAyushi Smriti             {
6691bf712bcSAyushi Smriti                 retryTimeoutInterval = defaultRetryInterval;
6701bf712bcSAyushi Smriti             }
6711bf712bcSAyushi Smriti         }
6721bf712bcSAyushi Smriti         else
6731bf712bcSAyushi Smriti         {
6741bf712bcSAyushi Smriti             loadDefaultConfig();
6751bf712bcSAyushi Smriti         }
6761bf712bcSAyushi Smriti 
6771bf712bcSAyushi Smriti         nlohmann::json subscriptionsList;
6781bf712bcSAyushi Smriti         if (!json_util::getValueFromJsonObject(jsonData, "Subscriptions",
6791bf712bcSAyushi Smriti                                                subscriptionsList))
6801bf712bcSAyushi Smriti         {
6811bf712bcSAyushi Smriti             BMCWEB_LOG_DEBUG << "EventService: Subscriptions not exist.";
6821bf712bcSAyushi Smriti             return;
6831bf712bcSAyushi Smriti         }
6841bf712bcSAyushi Smriti 
6851bf712bcSAyushi Smriti         for (nlohmann::json& jsonObj : subscriptionsList)
6861bf712bcSAyushi Smriti         {
6871bf712bcSAyushi Smriti             std::string protocol;
6881bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Protocol",
6891bf712bcSAyushi Smriti                                                    protocol))
6901bf712bcSAyushi Smriti             {
6911bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription Protocol exist.";
6921bf712bcSAyushi Smriti                 continue;
6931bf712bcSAyushi Smriti             }
6944bbf237fSAppaRao Puli 
6954bbf237fSAppaRao Puli             std::string subscriptionType;
6964bbf237fSAppaRao Puli             if (!json_util::getValueFromJsonObject(jsonObj, "SubscriptionType",
6974bbf237fSAppaRao Puli                                                    subscriptionType))
6984bbf237fSAppaRao Puli             {
6994bbf237fSAppaRao Puli                 subscriptionType = defaulSubscriptionType;
7004bbf237fSAppaRao Puli             }
7014bbf237fSAppaRao Puli             // SSE connections are initiated from client
7024bbf237fSAppaRao Puli             // and can't be re-established from server.
7034bbf237fSAppaRao Puli             if (subscriptionType == "SSE")
7044bbf237fSAppaRao Puli             {
7054bbf237fSAppaRao Puli                 BMCWEB_LOG_DEBUG
7064bbf237fSAppaRao Puli                     << "The subscription type is SSE, so skipping.";
7074bbf237fSAppaRao Puli                 continue;
7084bbf237fSAppaRao Puli             }
7094bbf237fSAppaRao Puli 
7101bf712bcSAyushi Smriti             std::string destination;
7111bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "Destination",
7121bf712bcSAyushi Smriti                                                    destination))
7131bf712bcSAyushi Smriti             {
7141bf712bcSAyushi Smriti                 BMCWEB_LOG_DEBUG << "Invalid subscription destination exist.";
7151bf712bcSAyushi Smriti                 continue;
7161bf712bcSAyushi Smriti             }
7171bf712bcSAyushi Smriti             std::string host;
7181bf712bcSAyushi Smriti             std::string urlProto;
7191bf712bcSAyushi Smriti             std::string port;
7201bf712bcSAyushi Smriti             std::string path;
7211bf712bcSAyushi Smriti             bool status =
7221bf712bcSAyushi Smriti                 validateAndSplitUrl(destination, urlProto, host, port, path);
7231bf712bcSAyushi Smriti 
7241bf712bcSAyushi Smriti             if (!status)
7251bf712bcSAyushi Smriti             {
7261bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR
7271bf712bcSAyushi Smriti                     << "Failed to validate and split destination url";
7281bf712bcSAyushi Smriti                 continue;
7291bf712bcSAyushi Smriti             }
7301bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue =
7311bf712bcSAyushi Smriti                 std::make_shared<Subscription>(host, port, path, urlProto);
7321bf712bcSAyushi Smriti 
7331bf712bcSAyushi Smriti             subValue->destinationUrl = destination;
7341bf712bcSAyushi Smriti             subValue->protocol = protocol;
7354bbf237fSAppaRao Puli             subValue->subscriptionType = subscriptionType;
7361bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(
7371bf712bcSAyushi Smriti                     jsonObj, "DeliveryRetryPolicy", subValue->retryPolicy))
7381bf712bcSAyushi Smriti             {
7391bf712bcSAyushi Smriti                 subValue->retryPolicy = defaulRetryPolicy;
7401bf712bcSAyushi Smriti             }
7411bf712bcSAyushi Smriti             if (!json_util::getValueFromJsonObject(jsonObj, "EventFormatType",
7421bf712bcSAyushi Smriti                                                    subValue->eventFormatType))
7431bf712bcSAyushi Smriti             {
7441bf712bcSAyushi Smriti                 subValue->eventFormatType = defaulEventFormatType;
7451bf712bcSAyushi Smriti             }
7461bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "Context",
7471bf712bcSAyushi Smriti                                               subValue->customText);
7481bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "MessageIds",
7491bf712bcSAyushi Smriti                                               subValue->registryMsgIds);
7501bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "RegistryPrefixes",
7511bf712bcSAyushi Smriti                                               subValue->registryPrefixes);
752e56f254cSSunitha Harish             json_util::getValueFromJsonObject(jsonObj, "ResourceTypes",
753e56f254cSSunitha Harish                                               subValue->resourceTypes);
7541bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(jsonObj, "HttpHeaders",
7551bf712bcSAyushi Smriti                                               subValue->httpHeaders);
7561bf712bcSAyushi Smriti             json_util::getValueFromJsonObject(
7571bf712bcSAyushi Smriti                 jsonObj, "MetricReportDefinitions",
7581bf712bcSAyushi Smriti                 subValue->metricReportDefinitions);
7591bf712bcSAyushi Smriti 
7601bf712bcSAyushi Smriti             std::string id = addSubscription(subValue, false);
7611bf712bcSAyushi Smriti             if (id.empty())
7621bf712bcSAyushi Smriti             {
7631bf712bcSAyushi Smriti                 BMCWEB_LOG_ERROR << "Failed to add subscription";
7641bf712bcSAyushi Smriti             }
7651bf712bcSAyushi Smriti         }
7661bf712bcSAyushi Smriti         return;
7671bf712bcSAyushi Smriti     }
7681bf712bcSAyushi Smriti 
769b52664e2SAppaRao Puli     void updateSubscriptionData()
770b52664e2SAppaRao Puli     {
771b52664e2SAppaRao Puli         // Persist the config and subscription data.
7721bf712bcSAyushi Smriti         nlohmann::json jsonData;
7731bf712bcSAyushi Smriti 
7741bf712bcSAyushi Smriti         nlohmann::json& configObj = jsonData["Configuration"];
7751bf712bcSAyushi Smriti         configObj["ServiceEnabled"] = serviceEnabled;
7761bf712bcSAyushi Smriti         configObj["DeliveryRetryAttempts"] = retryAttempts;
7771bf712bcSAyushi Smriti         configObj["DeliveryRetryIntervalSeconds"] = retryTimeoutInterval;
7781bf712bcSAyushi Smriti 
7791bf712bcSAyushi Smriti         nlohmann::json& subListArray = jsonData["Subscriptions"];
7801bf712bcSAyushi Smriti         subListArray = nlohmann::json::array();
7811bf712bcSAyushi Smriti 
7821bf712bcSAyushi Smriti         for (const auto& it : subscriptionsMap)
7831bf712bcSAyushi Smriti         {
7841bf712bcSAyushi Smriti             std::shared_ptr<Subscription> subValue = it.second;
7854bbf237fSAppaRao Puli             // Don't preserve SSE connections. Its initiated from
7864bbf237fSAppaRao Puli             // client side and can't be re-established from server.
7874bbf237fSAppaRao Puli             if (subValue->subscriptionType == "SSE")
7884bbf237fSAppaRao Puli             {
7894bbf237fSAppaRao Puli                 BMCWEB_LOG_DEBUG
7904bbf237fSAppaRao Puli                     << "The subscription type is SSE, so skipping.";
7914bbf237fSAppaRao Puli                 continue;
7924bbf237fSAppaRao Puli             }
7931bf712bcSAyushi Smriti 
7944bbf237fSAppaRao Puli             nlohmann::json entry;
7951bf712bcSAyushi Smriti             entry["Context"] = subValue->customText;
7961bf712bcSAyushi Smriti             entry["DeliveryRetryPolicy"] = subValue->retryPolicy;
7971bf712bcSAyushi Smriti             entry["Destination"] = subValue->destinationUrl;
7981bf712bcSAyushi Smriti             entry["EventFormatType"] = subValue->eventFormatType;
7991bf712bcSAyushi Smriti             entry["HttpHeaders"] = subValue->httpHeaders;
8001bf712bcSAyushi Smriti             entry["MessageIds"] = subValue->registryMsgIds;
8011bf712bcSAyushi Smriti             entry["Protocol"] = subValue->protocol;
8021bf712bcSAyushi Smriti             entry["RegistryPrefixes"] = subValue->registryPrefixes;
803e56f254cSSunitha Harish             entry["ResourceTypes"] = subValue->resourceTypes;
8041bf712bcSAyushi Smriti             entry["SubscriptionType"] = subValue->subscriptionType;
8051bf712bcSAyushi Smriti             entry["MetricReportDefinitions"] =
8061bf712bcSAyushi Smriti                 subValue->metricReportDefinitions;
8071bf712bcSAyushi Smriti 
8081bf712bcSAyushi Smriti             subListArray.push_back(entry);
8091bf712bcSAyushi Smriti         }
8101bf712bcSAyushi Smriti 
8111bf712bcSAyushi Smriti         const std::string tmpFile(std::string(eventServiceFile) + "_tmp");
8121bf712bcSAyushi Smriti         std::ofstream ofs(tmpFile, std::ios::out);
8131bf712bcSAyushi Smriti         const auto& writeData = jsonData.dump();
8141bf712bcSAyushi Smriti         ofs << writeData;
8151bf712bcSAyushi Smriti         ofs.close();
8161bf712bcSAyushi Smriti 
8171bf712bcSAyushi Smriti         BMCWEB_LOG_DEBUG << "EventService config updated to file.";
8181bf712bcSAyushi Smriti         if (std::rename(tmpFile.c_str(), eventServiceFile) != 0)
8191bf712bcSAyushi Smriti         {
8201bf712bcSAyushi Smriti             BMCWEB_LOG_ERROR << "Error in renaming temporary file: "
8211bf712bcSAyushi Smriti                              << tmpFile.c_str();
8221bf712bcSAyushi Smriti         }
823b52664e2SAppaRao Puli     }
824b52664e2SAppaRao Puli 
8257d1cc387SAppaRao Puli     EventServiceConfig getEventServiceConfig()
8267d1cc387SAppaRao Puli     {
8277d1cc387SAppaRao Puli         return {serviceEnabled, retryAttempts, retryTimeoutInterval};
8287d1cc387SAppaRao Puli     }
8297d1cc387SAppaRao Puli 
8307d1cc387SAppaRao Puli     void setEventServiceConfig(const EventServiceConfig& cfg)
8317d1cc387SAppaRao Puli     {
8327d1cc387SAppaRao Puli         bool updateConfig = false;
833fe44eb0bSAyushi Smriti         bool updateRetryCfg = false;
8347d1cc387SAppaRao Puli 
8357d1cc387SAppaRao Puli         if (serviceEnabled != std::get<0>(cfg))
8367d1cc387SAppaRao Puli         {
8377d1cc387SAppaRao Puli             serviceEnabled = std::get<0>(cfg);
8387d1cc387SAppaRao Puli             if (serviceEnabled && noOfMetricReportSubscribers)
8397d1cc387SAppaRao Puli             {
8407d1cc387SAppaRao Puli                 registerMetricReportSignal();
8417d1cc387SAppaRao Puli             }
8427d1cc387SAppaRao Puli             else
8437d1cc387SAppaRao Puli             {
8447d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
8457d1cc387SAppaRao Puli             }
8467d1cc387SAppaRao Puli             updateConfig = true;
8477d1cc387SAppaRao Puli         }
8487d1cc387SAppaRao Puli 
8497d1cc387SAppaRao Puli         if (retryAttempts != std::get<1>(cfg))
8507d1cc387SAppaRao Puli         {
8517d1cc387SAppaRao Puli             retryAttempts = std::get<1>(cfg);
8527d1cc387SAppaRao Puli             updateConfig = true;
853fe44eb0bSAyushi Smriti             updateRetryCfg = true;
8547d1cc387SAppaRao Puli         }
8557d1cc387SAppaRao Puli 
8567d1cc387SAppaRao Puli         if (retryTimeoutInterval != std::get<2>(cfg))
8577d1cc387SAppaRao Puli         {
8587d1cc387SAppaRao Puli             retryTimeoutInterval = std::get<2>(cfg);
8597d1cc387SAppaRao Puli             updateConfig = true;
860fe44eb0bSAyushi Smriti             updateRetryCfg = true;
8617d1cc387SAppaRao Puli         }
8627d1cc387SAppaRao Puli 
8637d1cc387SAppaRao Puli         if (updateConfig)
8647d1cc387SAppaRao Puli         {
8657d1cc387SAppaRao Puli             updateSubscriptionData();
8667d1cc387SAppaRao Puli         }
867fe44eb0bSAyushi Smriti 
868fe44eb0bSAyushi Smriti         if (updateRetryCfg)
869fe44eb0bSAyushi Smriti         {
870fe44eb0bSAyushi Smriti             // Update the changed retry config to all subscriptions
871fe44eb0bSAyushi Smriti             for (const auto& it :
872fe44eb0bSAyushi Smriti                  EventServiceManager::getInstance().subscriptionsMap)
873fe44eb0bSAyushi Smriti             {
874fe44eb0bSAyushi Smriti                 std::shared_ptr<Subscription> entry = it.second;
875fe44eb0bSAyushi Smriti                 entry->updateRetryConfig(retryAttempts, retryTimeoutInterval);
876fe44eb0bSAyushi Smriti             }
877fe44eb0bSAyushi Smriti         }
8787d1cc387SAppaRao Puli     }
8797d1cc387SAppaRao Puli 
8807d1cc387SAppaRao Puli     void updateNoOfSubscribersCount()
8817d1cc387SAppaRao Puli     {
8827d1cc387SAppaRao Puli         size_t eventLogSubCount = 0;
8837d1cc387SAppaRao Puli         size_t metricReportSubCount = 0;
8847d1cc387SAppaRao Puli         for (const auto& it : subscriptionsMap)
8857d1cc387SAppaRao Puli         {
8867d1cc387SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
8877d1cc387SAppaRao Puli             if (entry->eventFormatType == eventFormatType)
8887d1cc387SAppaRao Puli             {
8897d1cc387SAppaRao Puli                 eventLogSubCount++;
8907d1cc387SAppaRao Puli             }
8917d1cc387SAppaRao Puli             else if (entry->eventFormatType == metricReportFormatType)
8927d1cc387SAppaRao Puli             {
8937d1cc387SAppaRao Puli                 metricReportSubCount++;
8947d1cc387SAppaRao Puli             }
8957d1cc387SAppaRao Puli         }
8967d1cc387SAppaRao Puli 
8977d1cc387SAppaRao Puli         noOfEventLogSubscribers = eventLogSubCount;
8987d1cc387SAppaRao Puli         if (noOfMetricReportSubscribers != metricReportSubCount)
8997d1cc387SAppaRao Puli         {
9007d1cc387SAppaRao Puli             noOfMetricReportSubscribers = metricReportSubCount;
9017d1cc387SAppaRao Puli             if (noOfMetricReportSubscribers)
9027d1cc387SAppaRao Puli             {
9037d1cc387SAppaRao Puli                 registerMetricReportSignal();
9047d1cc387SAppaRao Puli             }
9057d1cc387SAppaRao Puli             else
9067d1cc387SAppaRao Puli             {
9077d1cc387SAppaRao Puli                 unregisterMetricReportSignal();
9087d1cc387SAppaRao Puli             }
9097d1cc387SAppaRao Puli         }
9107d1cc387SAppaRao Puli     }
9117d1cc387SAppaRao Puli 
912b52664e2SAppaRao Puli     std::shared_ptr<Subscription> getSubscription(const std::string& id)
913b52664e2SAppaRao Puli     {
914b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
915b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
916b52664e2SAppaRao Puli         {
917b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "No subscription exist with ID:" << id;
918b52664e2SAppaRao Puli             return nullptr;
919b52664e2SAppaRao Puli         }
920b52664e2SAppaRao Puli         std::shared_ptr<Subscription> subValue = obj->second;
921b52664e2SAppaRao Puli         return subValue;
922b52664e2SAppaRao Puli     }
923b52664e2SAppaRao Puli 
9241bf712bcSAyushi Smriti     std::string addSubscription(const std::shared_ptr<Subscription> subValue,
9251bf712bcSAyushi Smriti                                 const bool updateFile = true)
926b52664e2SAppaRao Puli     {
927b52664e2SAppaRao Puli         std::srand(static_cast<uint32_t>(std::time(0)));
928b52664e2SAppaRao Puli         std::string id;
929b52664e2SAppaRao Puli 
930b52664e2SAppaRao Puli         int retry = 3;
931b52664e2SAppaRao Puli         while (retry)
932b52664e2SAppaRao Puli         {
933b52664e2SAppaRao Puli             id = std::to_string(std::rand());
934b52664e2SAppaRao Puli             auto inserted = subscriptionsMap.insert(std::pair(id, subValue));
935b52664e2SAppaRao Puli             if (inserted.second)
936b52664e2SAppaRao Puli             {
937b52664e2SAppaRao Puli                 break;
938b52664e2SAppaRao Puli             }
939b52664e2SAppaRao Puli             --retry;
940b52664e2SAppaRao Puli         };
941b52664e2SAppaRao Puli 
942b52664e2SAppaRao Puli         if (retry <= 0)
943b52664e2SAppaRao Puli         {
944b52664e2SAppaRao Puli             BMCWEB_LOG_ERROR << "Failed to generate random number";
945b52664e2SAppaRao Puli             return std::string("");
946b52664e2SAppaRao Puli         }
947b52664e2SAppaRao Puli 
9487d1cc387SAppaRao Puli         updateNoOfSubscribersCount();
9491bf712bcSAyushi Smriti 
9501bf712bcSAyushi Smriti         if (updateFile)
9511bf712bcSAyushi Smriti         {
952b52664e2SAppaRao Puli             updateSubscriptionData();
9531bf712bcSAyushi Smriti         }
9547f4eb588SAppaRao Puli 
9557f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
9567f4eb588SAppaRao Puli         if (lastEventTStr.empty())
9577f4eb588SAppaRao Puli         {
9587f4eb588SAppaRao Puli             cacheLastEventTimestamp();
9597f4eb588SAppaRao Puli         }
9607f4eb588SAppaRao Puli #endif
961fe44eb0bSAyushi Smriti         // Update retry configuration.
962fe44eb0bSAyushi Smriti         subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval);
963fe44eb0bSAyushi Smriti         subValue->updateRetryPolicy();
964fe44eb0bSAyushi Smriti 
965b52664e2SAppaRao Puli         return id;
966b52664e2SAppaRao Puli     }
967b52664e2SAppaRao Puli 
968b52664e2SAppaRao Puli     bool isSubscriptionExist(const std::string& id)
969b52664e2SAppaRao Puli     {
970b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
971b52664e2SAppaRao Puli         if (obj == subscriptionsMap.end())
972b52664e2SAppaRao Puli         {
973b52664e2SAppaRao Puli             return false;
974b52664e2SAppaRao Puli         }
975b52664e2SAppaRao Puli         return true;
976b52664e2SAppaRao Puli     }
977b52664e2SAppaRao Puli 
978b52664e2SAppaRao Puli     void deleteSubscription(const std::string& id)
979b52664e2SAppaRao Puli     {
980b52664e2SAppaRao Puli         auto obj = subscriptionsMap.find(id);
981b52664e2SAppaRao Puli         if (obj != subscriptionsMap.end())
982b52664e2SAppaRao Puli         {
983b52664e2SAppaRao Puli             subscriptionsMap.erase(obj);
9847d1cc387SAppaRao Puli             updateNoOfSubscribersCount();
985b52664e2SAppaRao Puli             updateSubscriptionData();
986b52664e2SAppaRao Puli         }
987b52664e2SAppaRao Puli     }
988b52664e2SAppaRao Puli 
989b52664e2SAppaRao Puli     size_t getNumberOfSubscriptions()
990b52664e2SAppaRao Puli     {
991b52664e2SAppaRao Puli         return subscriptionsMap.size();
992b52664e2SAppaRao Puli     }
993b52664e2SAppaRao Puli 
994b52664e2SAppaRao Puli     std::vector<std::string> getAllIDs()
995b52664e2SAppaRao Puli     {
996b52664e2SAppaRao Puli         std::vector<std::string> idList;
997b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
998b52664e2SAppaRao Puli         {
999b52664e2SAppaRao Puli             idList.emplace_back(it.first);
1000b52664e2SAppaRao Puli         }
1001b52664e2SAppaRao Puli         return idList;
1002b52664e2SAppaRao Puli     }
1003b52664e2SAppaRao Puli 
1004b52664e2SAppaRao Puli     bool isDestinationExist(const std::string& destUrl)
1005b52664e2SAppaRao Puli     {
1006b52664e2SAppaRao Puli         for (const auto& it : subscriptionsMap)
1007b52664e2SAppaRao Puli         {
1008b52664e2SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
1009b52664e2SAppaRao Puli             if (entry->destinationUrl == destUrl)
1010b52664e2SAppaRao Puli             {
1011b52664e2SAppaRao Puli                 BMCWEB_LOG_ERROR << "Destination exist already" << destUrl;
1012b52664e2SAppaRao Puli                 return true;
1013b52664e2SAppaRao Puli             }
1014b52664e2SAppaRao Puli         }
1015b52664e2SAppaRao Puli         return false;
1016b52664e2SAppaRao Puli     }
10170b4bdd93SAppaRao Puli 
10180b4bdd93SAppaRao Puli     void sendTestEventLog()
10190b4bdd93SAppaRao Puli     {
10200b4bdd93SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
10210b4bdd93SAppaRao Puli         {
10220b4bdd93SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
10230b4bdd93SAppaRao Puli             entry->sendTestEventLog();
10240b4bdd93SAppaRao Puli         }
10250b4bdd93SAppaRao Puli     }
1026e9a14131SAppaRao Puli 
102796330b99SSunitha Harish     void sendEvent(const nlohmann::json& eventMessageIn,
102896330b99SSunitha Harish                    const std::string& origin, const std::string& resType)
102996330b99SSunitha Harish     {
103096330b99SSunitha Harish         nlohmann::json eventRecord = nlohmann::json::array();
103196330b99SSunitha Harish         nlohmann::json eventMessage = eventMessageIn;
103296330b99SSunitha Harish         // MemberId is 0 : since we are sending one event record.
103396330b99SSunitha Harish         uint64_t memberId = 0;
103496330b99SSunitha Harish 
103596330b99SSunitha Harish         nlohmann::json event = {
103696330b99SSunitha Harish             {"EventId", eventId},
103796330b99SSunitha Harish             {"MemberId", memberId},
103896330b99SSunitha Harish             {"EventTimestamp", crow::utility::dateTimeNow()},
103996330b99SSunitha Harish             {"OriginOfCondition", origin}};
104096330b99SSunitha Harish         for (nlohmann::json::iterator it = event.begin(); it != event.end();
104196330b99SSunitha Harish              ++it)
104296330b99SSunitha Harish         {
104396330b99SSunitha Harish             eventMessage[it.key()] = it.value();
104496330b99SSunitha Harish         }
104596330b99SSunitha Harish         eventRecord.push_back(eventMessage);
104696330b99SSunitha Harish 
104796330b99SSunitha Harish         for (const auto& it : this->subscriptionsMap)
104896330b99SSunitha Harish         {
104996330b99SSunitha Harish             std::shared_ptr<Subscription> entry = it.second;
105096330b99SSunitha Harish             bool isSubscribed = false;
105196330b99SSunitha Harish             // Search the resourceTypes list for the subscription.
105296330b99SSunitha Harish             // If resourceTypes list is empty, don't filter events
105396330b99SSunitha Harish             // send everything.
105496330b99SSunitha Harish             if (entry->resourceTypes.size())
105596330b99SSunitha Harish             {
105696330b99SSunitha Harish                 for (auto resource : entry->resourceTypes)
105796330b99SSunitha Harish                 {
105896330b99SSunitha Harish                     if (resType == resource)
105996330b99SSunitha Harish                     {
106096330b99SSunitha Harish                         BMCWEB_LOG_INFO << "ResourceType " << resource
106196330b99SSunitha Harish                                         << " found in the subscribed list";
106296330b99SSunitha Harish                         isSubscribed = true;
106396330b99SSunitha Harish                         break;
106496330b99SSunitha Harish                     }
106596330b99SSunitha Harish                 }
106696330b99SSunitha Harish             }
106796330b99SSunitha Harish             else // resourceTypes list is empty.
106896330b99SSunitha Harish             {
106996330b99SSunitha Harish                 isSubscribed = true;
107096330b99SSunitha Harish             }
107196330b99SSunitha Harish             if (isSubscribed)
107296330b99SSunitha Harish             {
107396330b99SSunitha Harish                 nlohmann::json msgJson = {
107496330b99SSunitha Harish                     {"@odata.type", "#Event.v1_4_0.Event"},
107596330b99SSunitha Harish                     {"Name", "Event Log"},
107696330b99SSunitha Harish                     {"Id", eventId},
107796330b99SSunitha Harish                     {"Events", eventRecord}};
107896330b99SSunitha Harish                 entry->sendEvent(msgJson.dump());
107996330b99SSunitha Harish                 eventId++; // increament the eventId
108096330b99SSunitha Harish             }
108196330b99SSunitha Harish             else
108296330b99SSunitha Harish             {
108396330b99SSunitha Harish                 BMCWEB_LOG_INFO << "Not subscribed to this resource";
108496330b99SSunitha Harish             }
108596330b99SSunitha Harish         }
108696330b99SSunitha Harish     }
1087*5738de59SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
1088*5738de59SAsmitha Karunanithi     void sendBroadcastMsg(const std::string& broadcastMsg)
1089*5738de59SAsmitha Karunanithi     {
1090*5738de59SAsmitha Karunanithi         for (const auto& it : this->subscriptionsMap)
1091*5738de59SAsmitha Karunanithi         {
1092*5738de59SAsmitha Karunanithi             std::shared_ptr<Subscription> entry = it.second;
1093*5738de59SAsmitha Karunanithi             nlohmann::json msgJson = {
1094*5738de59SAsmitha Karunanithi                 {"Timestamp", crow::utility::dateTimeNow()},
1095*5738de59SAsmitha Karunanithi                 {"OriginOfCondition", "/ibm/v1/HMC/BroadcastService"},
1096*5738de59SAsmitha Karunanithi                 {"Name", "Broadcast Message"},
1097*5738de59SAsmitha Karunanithi                 {"Message", broadcastMsg}};
1098*5738de59SAsmitha Karunanithi             entry->sendEvent(msgJson.dump());
1099*5738de59SAsmitha Karunanithi         }
1100*5738de59SAsmitha Karunanithi     }
1101*5738de59SAsmitha Karunanithi #endif
110296330b99SSunitha Harish 
11037f4eb588SAppaRao Puli #ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
11047f4eb588SAppaRao Puli     void cacheLastEventTimestamp()
11057f4eb588SAppaRao Puli     {
11067f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
11077f4eb588SAppaRao Puli         if (!logStream.good())
11087f4eb588SAppaRao Puli         {
11097f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed \n";
11107f4eb588SAppaRao Puli             return;
11117f4eb588SAppaRao Puli         }
11127f4eb588SAppaRao Puli         std::string logEntry;
11137f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
11147f4eb588SAppaRao Puli         {
11157f4eb588SAppaRao Puli             size_t space = logEntry.find_first_of(" ");
11167f4eb588SAppaRao Puli             if (space == std::string::npos)
11177f4eb588SAppaRao Puli             {
11187f4eb588SAppaRao Puli                 // Shouldn't enter here but lets skip it.
11197f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Invalid log entry found.";
11207f4eb588SAppaRao Puli                 continue;
11217f4eb588SAppaRao Puli             }
11227f4eb588SAppaRao Puli             lastEventTStr = logEntry.substr(0, space);
11237f4eb588SAppaRao Puli         }
11247f4eb588SAppaRao Puli         BMCWEB_LOG_DEBUG << "Last Event time stamp set: " << lastEventTStr;
11257f4eb588SAppaRao Puli     }
11267f4eb588SAppaRao Puli 
11277f4eb588SAppaRao Puli     void readEventLogsFromFile()
11287f4eb588SAppaRao Puli     {
11297f4eb588SAppaRao Puli         if (!serviceEnabled || !noOfEventLogSubscribers)
11307f4eb588SAppaRao Puli         {
11317f4eb588SAppaRao Puli             BMCWEB_LOG_DEBUG << "EventService disabled or no Subscriptions.";
11327f4eb588SAppaRao Puli             return;
11337f4eb588SAppaRao Puli         }
11347f4eb588SAppaRao Puli         std::ifstream logStream(redfishEventLogFile);
11357f4eb588SAppaRao Puli         if (!logStream.good())
11367f4eb588SAppaRao Puli         {
11377f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << " Redfish log file open failed";
11387f4eb588SAppaRao Puli             return;
11397f4eb588SAppaRao Puli         }
11407f4eb588SAppaRao Puli 
11417f4eb588SAppaRao Puli         std::vector<EventLogObjectsType> eventRecords;
11427f4eb588SAppaRao Puli 
11437f4eb588SAppaRao Puli         bool startLogCollection = false;
11447f4eb588SAppaRao Puli         bool firstEntry = true;
11457f4eb588SAppaRao Puli 
11467f4eb588SAppaRao Puli         std::string logEntry;
11477f4eb588SAppaRao Puli         while (std::getline(logStream, logEntry))
11487f4eb588SAppaRao Puli         {
11497f4eb588SAppaRao Puli             if (!startLogCollection)
11507f4eb588SAppaRao Puli             {
11517f4eb588SAppaRao Puli                 if (boost::starts_with(logEntry, lastEventTStr))
11527f4eb588SAppaRao Puli                 {
11537f4eb588SAppaRao Puli                     startLogCollection = true;
11547f4eb588SAppaRao Puli                 }
11557f4eb588SAppaRao Puli                 continue;
11567f4eb588SAppaRao Puli             }
11577f4eb588SAppaRao Puli 
11587f4eb588SAppaRao Puli             std::string idStr;
11597f4eb588SAppaRao Puli             if (!event_log::getUniqueEntryID(logEntry, idStr, firstEntry))
11607f4eb588SAppaRao Puli             {
11617f4eb588SAppaRao Puli                 continue;
11627f4eb588SAppaRao Puli             }
11637f4eb588SAppaRao Puli             firstEntry = false;
11647f4eb588SAppaRao Puli 
11657f4eb588SAppaRao Puli             std::string timestamp;
11667f4eb588SAppaRao Puli             std::string messageID;
11675e715de6SAppaRao Puli             std::vector<std::string> messageArgs;
11687f4eb588SAppaRao Puli             if (event_log::getEventLogParams(logEntry, timestamp, messageID,
11697f4eb588SAppaRao Puli                                              messageArgs) != 0)
11707f4eb588SAppaRao Puli             {
11717f4eb588SAppaRao Puli                 BMCWEB_LOG_DEBUG << "Read eventLog entry params failed";
11727f4eb588SAppaRao Puli                 continue;
11737f4eb588SAppaRao Puli             }
11747f4eb588SAppaRao Puli 
11757f4eb588SAppaRao Puli             std::string registryName;
11767f4eb588SAppaRao Puli             std::string messageKey;
11777f4eb588SAppaRao Puli             event_log::getRegistryAndMessageKey(messageID, registryName,
11787f4eb588SAppaRao Puli                                                 messageKey);
11797f4eb588SAppaRao Puli             if (registryName.empty() || messageKey.empty())
11807f4eb588SAppaRao Puli             {
11817f4eb588SAppaRao Puli                 continue;
11827f4eb588SAppaRao Puli             }
11837f4eb588SAppaRao Puli 
11847f4eb588SAppaRao Puli             lastEventTStr = timestamp;
11857f4eb588SAppaRao Puli             eventRecords.emplace_back(idStr, timestamp, messageID, registryName,
11867f4eb588SAppaRao Puli                                       messageKey, messageArgs);
11877f4eb588SAppaRao Puli         }
11887f4eb588SAppaRao Puli 
11897f4eb588SAppaRao Puli         for (const auto& it : this->subscriptionsMap)
11907f4eb588SAppaRao Puli         {
11917f4eb588SAppaRao Puli             std::shared_ptr<Subscription> entry = it.second;
11927f4eb588SAppaRao Puli             if (entry->eventFormatType == "Event")
11937f4eb588SAppaRao Puli             {
11947f4eb588SAppaRao Puli                 entry->filterAndSendEventLogs(eventRecords);
11957f4eb588SAppaRao Puli             }
11967f4eb588SAppaRao Puli         }
11977f4eb588SAppaRao Puli     }
11987f4eb588SAppaRao Puli 
11997f4eb588SAppaRao Puli     static void watchRedfishEventLogFile()
12007f4eb588SAppaRao Puli     {
12017f4eb588SAppaRao Puli         if (inotifyConn == nullptr)
12027f4eb588SAppaRao Puli         {
12037f4eb588SAppaRao Puli             return;
12047f4eb588SAppaRao Puli         }
12057f4eb588SAppaRao Puli 
12067f4eb588SAppaRao Puli         static std::array<char, 1024> readBuffer;
12077f4eb588SAppaRao Puli 
12087f4eb588SAppaRao Puli         inotifyConn->async_read_some(
12097f4eb588SAppaRao Puli             boost::asio::buffer(readBuffer),
12107f4eb588SAppaRao Puli             [&](const boost::system::error_code& ec,
12117f4eb588SAppaRao Puli                 const std::size_t& bytesTransferred) {
12127f4eb588SAppaRao Puli                 if (ec)
12137f4eb588SAppaRao Puli                 {
12147f4eb588SAppaRao Puli                     BMCWEB_LOG_ERROR << "Callback Error: " << ec.message();
12157f4eb588SAppaRao Puli                     return;
12167f4eb588SAppaRao Puli                 }
12177f4eb588SAppaRao Puli                 std::size_t index = 0;
1218b792cc56SAppaRao Puli                 while ((index + iEventSize) <= bytesTransferred)
12197f4eb588SAppaRao Puli                 {
12207f4eb588SAppaRao Puli                     struct inotify_event event;
1221b792cc56SAppaRao Puli                     std::memcpy(&event, &readBuffer[index], iEventSize);
1222b792cc56SAppaRao Puli                     if (event.wd == dirWatchDesc)
1223b792cc56SAppaRao Puli                     {
1224b792cc56SAppaRao Puli                         if ((event.len == 0) ||
1225b792cc56SAppaRao Puli                             (index + iEventSize + event.len > bytesTransferred))
1226b792cc56SAppaRao Puli                         {
1227b792cc56SAppaRao Puli                             index += (iEventSize + event.len);
1228b792cc56SAppaRao Puli                             continue;
1229b792cc56SAppaRao Puli                         }
1230b792cc56SAppaRao Puli 
1231b792cc56SAppaRao Puli                         std::string fileName(&readBuffer[index + iEventSize],
1232b792cc56SAppaRao Puli                                              event.len);
1233b792cc56SAppaRao Puli                         if (std::strcmp(fileName.c_str(), "redfish") != 0)
1234b792cc56SAppaRao Puli                         {
1235b792cc56SAppaRao Puli                             index += (iEventSize + event.len);
1236b792cc56SAppaRao Puli                             continue;
1237b792cc56SAppaRao Puli                         }
1238b792cc56SAppaRao Puli 
1239b792cc56SAppaRao Puli                         BMCWEB_LOG_DEBUG
1240b792cc56SAppaRao Puli                             << "Redfish log file created/deleted. event.name: "
1241b792cc56SAppaRao Puli                             << fileName;
1242b792cc56SAppaRao Puli                         if (event.mask == IN_CREATE)
1243b792cc56SAppaRao Puli                         {
1244b792cc56SAppaRao Puli                             if (fileWatchDesc != -1)
1245b792cc56SAppaRao Puli                             {
1246b792cc56SAppaRao Puli                                 BMCWEB_LOG_DEBUG
1247b792cc56SAppaRao Puli                                     << "Redfish log file is already on "
1248b792cc56SAppaRao Puli                                        "inotify_add_watch.";
1249b792cc56SAppaRao Puli                                 return;
1250b792cc56SAppaRao Puli                             }
1251b792cc56SAppaRao Puli 
1252b792cc56SAppaRao Puli                             fileWatchDesc = inotify_add_watch(
1253b792cc56SAppaRao Puli                                 inotifyFd, redfishEventLogFile, IN_MODIFY);
1254b792cc56SAppaRao Puli                             if (fileWatchDesc == -1)
1255b792cc56SAppaRao Puli                             {
1256b792cc56SAppaRao Puli                                 BMCWEB_LOG_ERROR
1257b792cc56SAppaRao Puli                                     << "inotify_add_watch failed for "
1258b792cc56SAppaRao Puli                                        "redfish log file.";
1259b792cc56SAppaRao Puli                                 return;
1260b792cc56SAppaRao Puli                             }
1261b792cc56SAppaRao Puli 
1262b792cc56SAppaRao Puli                             EventServiceManager::getInstance()
1263b792cc56SAppaRao Puli                                 .cacheLastEventTimestamp();
1264b792cc56SAppaRao Puli                             EventServiceManager::getInstance()
1265b792cc56SAppaRao Puli                                 .readEventLogsFromFile();
1266b792cc56SAppaRao Puli                         }
1267b792cc56SAppaRao Puli                         else if ((event.mask == IN_DELETE) ||
1268b792cc56SAppaRao Puli                                  (event.mask == IN_MOVED_TO))
1269b792cc56SAppaRao Puli                         {
1270b792cc56SAppaRao Puli                             if (fileWatchDesc != -1)
1271b792cc56SAppaRao Puli                             {
1272b792cc56SAppaRao Puli                                 inotify_rm_watch(inotifyFd, fileWatchDesc);
1273b792cc56SAppaRao Puli                                 fileWatchDesc = -1;
1274b792cc56SAppaRao Puli                             }
1275b792cc56SAppaRao Puli                         }
1276b792cc56SAppaRao Puli                     }
1277b792cc56SAppaRao Puli                     else if (event.wd == fileWatchDesc)
1278b792cc56SAppaRao Puli                     {
1279b792cc56SAppaRao Puli                         if (event.mask == IN_MODIFY)
12807f4eb588SAppaRao Puli                         {
12817f4eb588SAppaRao Puli                             EventServiceManager::getInstance()
12827f4eb588SAppaRao Puli                                 .readEventLogsFromFile();
12837f4eb588SAppaRao Puli                         }
1284b792cc56SAppaRao Puli                     }
1285b792cc56SAppaRao Puli                     index += (iEventSize + event.len);
12867f4eb588SAppaRao Puli                 }
12877f4eb588SAppaRao Puli 
12887f4eb588SAppaRao Puli                 watchRedfishEventLogFile();
12897f4eb588SAppaRao Puli             });
12907f4eb588SAppaRao Puli     }
12917f4eb588SAppaRao Puli 
12927f4eb588SAppaRao Puli     static int startEventLogMonitor(boost::asio::io_context& ioc)
12937f4eb588SAppaRao Puli     {
12947f4eb588SAppaRao Puli         inotifyConn =
12957f4eb588SAppaRao Puli             std::make_shared<boost::asio::posix::stream_descriptor>(ioc);
1296b792cc56SAppaRao Puli         inotifyFd = inotify_init1(IN_NONBLOCK);
1297b792cc56SAppaRao Puli         if (inotifyFd == -1)
12987f4eb588SAppaRao Puli         {
12997f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR << "inotify_init1 failed.";
13007f4eb588SAppaRao Puli             return -1;
13017f4eb588SAppaRao Puli         }
1302b792cc56SAppaRao Puli 
1303b792cc56SAppaRao Puli         // Add watch on directory to handle redfish event log file
1304b792cc56SAppaRao Puli         // create/delete.
1305b792cc56SAppaRao Puli         dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir,
1306b792cc56SAppaRao Puli                                          IN_CREATE | IN_MOVED_TO | IN_DELETE);
1307b792cc56SAppaRao Puli         if (dirWatchDesc == -1)
13087f4eb588SAppaRao Puli         {
13097f4eb588SAppaRao Puli             BMCWEB_LOG_ERROR
1310b792cc56SAppaRao Puli                 << "inotify_add_watch failed for event log directory.";
13117f4eb588SAppaRao Puli             return -1;
13127f4eb588SAppaRao Puli         }
13137f4eb588SAppaRao Puli 
1314b792cc56SAppaRao Puli         // Watch redfish event log file for modifications.
1315b792cc56SAppaRao Puli         fileWatchDesc =
1316b792cc56SAppaRao Puli             inotify_add_watch(inotifyFd, redfishEventLogFile, IN_MODIFY);
1317b792cc56SAppaRao Puli         if (fileWatchDesc == -1)
1318b792cc56SAppaRao Puli         {
1319b792cc56SAppaRao Puli             BMCWEB_LOG_ERROR
1320b792cc56SAppaRao Puli                 << "inotify_add_watch failed for redfish log file.";
1321b792cc56SAppaRao Puli             // Don't return error if file not exist.
1322b792cc56SAppaRao Puli             // Watch on directory will handle create/delete of file.
1323b792cc56SAppaRao Puli         }
1324b792cc56SAppaRao Puli 
13257f4eb588SAppaRao Puli         // monitor redfish event log file
1326b792cc56SAppaRao Puli         inotifyConn->assign(inotifyFd);
13277f4eb588SAppaRao Puli         watchRedfishEventLogFile();
13287f4eb588SAppaRao Puli 
13297f4eb588SAppaRao Puli         return 0;
13307f4eb588SAppaRao Puli     }
13317f4eb588SAppaRao Puli 
13327f4eb588SAppaRao Puli #endif
13337f4eb588SAppaRao Puli 
1334156d6b00SAppaRao Puli     void getMetricReading(const std::string& service,
1335156d6b00SAppaRao Puli                           const std::string& objPath, const std::string& intf)
1336156d6b00SAppaRao Puli     {
1337156d6b00SAppaRao Puli         std::size_t found = objPath.find_last_of("/");
1338156d6b00SAppaRao Puli         if (found == std::string::npos)
1339156d6b00SAppaRao Puli         {
1340156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid objPath received";
1341156d6b00SAppaRao Puli             return;
1342156d6b00SAppaRao Puli         }
1343156d6b00SAppaRao Puli 
1344156d6b00SAppaRao Puli         std::string idStr = objPath.substr(found + 1);
1345156d6b00SAppaRao Puli         if (idStr.empty())
1346156d6b00SAppaRao Puli         {
1347156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Invalid ID in objPath";
1348156d6b00SAppaRao Puli             return;
1349156d6b00SAppaRao Puli         }
1350156d6b00SAppaRao Puli 
1351156d6b00SAppaRao Puli         crow::connections::systemBus->async_method_call(
1352156d6b00SAppaRao Puli             [idStr{std::move(idStr)}](
1353156d6b00SAppaRao Puli                 const boost::system::error_code ec,
1354156d6b00SAppaRao Puli                 boost::container::flat_map<
1355156d6b00SAppaRao Puli                     std::string, std::variant<std::string, ReadingsObjType>>&
1356156d6b00SAppaRao Puli                     resp) {
1357156d6b00SAppaRao Puli                 if (ec)
1358156d6b00SAppaRao Puli                 {
1359156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG
1360156d6b00SAppaRao Puli                         << "D-Bus call failed to GetAll metric readings.";
1361156d6b00SAppaRao Puli                     return;
1362156d6b00SAppaRao Puli                 }
1363156d6b00SAppaRao Puli 
1364156d6b00SAppaRao Puli                 const std::string* timestampPtr =
1365156d6b00SAppaRao Puli                     std::get_if<std::string>(&resp["Timestamp"]);
1366156d6b00SAppaRao Puli                 if (!timestampPtr)
1367156d6b00SAppaRao Puli                 {
1368156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get timestamp.";
1369156d6b00SAppaRao Puli                     return;
1370156d6b00SAppaRao Puli                 }
1371156d6b00SAppaRao Puli 
1372156d6b00SAppaRao Puli                 ReadingsObjType* readingsPtr =
1373156d6b00SAppaRao Puli                     std::get_if<ReadingsObjType>(&resp["Readings"]);
1374156d6b00SAppaRao Puli                 if (!readingsPtr)
1375156d6b00SAppaRao Puli                 {
1376156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "Failed to Get Readings property.";
1377156d6b00SAppaRao Puli                     return;
1378156d6b00SAppaRao Puli                 }
1379156d6b00SAppaRao Puli 
1380156d6b00SAppaRao Puli                 if (!readingsPtr->size())
1381156d6b00SAppaRao Puli                 {
1382156d6b00SAppaRao Puli                     BMCWEB_LOG_DEBUG << "No metrics report to be transferred";
1383156d6b00SAppaRao Puli                     return;
1384156d6b00SAppaRao Puli                 }
1385156d6b00SAppaRao Puli 
1386156d6b00SAppaRao Puli                 for (const auto& it :
1387156d6b00SAppaRao Puli                      EventServiceManager::getInstance().subscriptionsMap)
1388156d6b00SAppaRao Puli                 {
1389156d6b00SAppaRao Puli                     std::shared_ptr<Subscription> entry = it.second;
1390156d6b00SAppaRao Puli                     if (entry->eventFormatType == metricReportFormatType)
1391156d6b00SAppaRao Puli                     {
1392156d6b00SAppaRao Puli                         entry->filterAndSendReports(idStr, *timestampPtr,
1393156d6b00SAppaRao Puli                                                     *readingsPtr);
1394156d6b00SAppaRao Puli                     }
1395156d6b00SAppaRao Puli                 }
1396156d6b00SAppaRao Puli             },
1397156d6b00SAppaRao Puli             service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
1398156d6b00SAppaRao Puli             intf);
1399156d6b00SAppaRao Puli     }
1400156d6b00SAppaRao Puli 
1401156d6b00SAppaRao Puli     void unregisterMetricReportSignal()
1402156d6b00SAppaRao Puli     {
14037d1cc387SAppaRao Puli         if (matchTelemetryMonitor)
14047d1cc387SAppaRao Puli         {
1405156d6b00SAppaRao Puli             BMCWEB_LOG_DEBUG << "Metrics report signal - Unregister";
1406156d6b00SAppaRao Puli             matchTelemetryMonitor.reset();
1407156d6b00SAppaRao Puli             matchTelemetryMonitor = nullptr;
1408156d6b00SAppaRao Puli         }
14097d1cc387SAppaRao Puli     }
1410156d6b00SAppaRao Puli 
1411156d6b00SAppaRao Puli     void registerMetricReportSignal()
1412156d6b00SAppaRao Puli     {
14137d1cc387SAppaRao Puli         if (!serviceEnabled || matchTelemetryMonitor)
1414156d6b00SAppaRao Puli         {
14157d1cc387SAppaRao Puli             BMCWEB_LOG_DEBUG << "Not registering metric report signal.";
1416156d6b00SAppaRao Puli             return;
1417156d6b00SAppaRao Puli         }
1418156d6b00SAppaRao Puli 
1419156d6b00SAppaRao Puli         BMCWEB_LOG_DEBUG << "Metrics report signal - Register";
1420156d6b00SAppaRao Puli         std::string matchStr(
1421156d6b00SAppaRao Puli             "type='signal',member='ReportUpdate', "
1422156d6b00SAppaRao Puli             "interface='xyz.openbmc_project.MonitoringService.Report'");
1423156d6b00SAppaRao Puli 
1424156d6b00SAppaRao Puli         matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match::match>(
1425156d6b00SAppaRao Puli             *crow::connections::systemBus, matchStr,
1426156d6b00SAppaRao Puli             [this](sdbusplus::message::message& msg) {
1427156d6b00SAppaRao Puli                 if (msg.is_method_error())
1428156d6b00SAppaRao Puli                 {
1429156d6b00SAppaRao Puli                     BMCWEB_LOG_ERROR << "TelemetryMonitor Signal error";
1430156d6b00SAppaRao Puli                     return;
1431156d6b00SAppaRao Puli                 }
1432156d6b00SAppaRao Puli 
1433156d6b00SAppaRao Puli                 std::string service = msg.get_sender();
1434156d6b00SAppaRao Puli                 std::string objPath = msg.get_path();
1435156d6b00SAppaRao Puli                 std::string intf = msg.get_interface();
1436156d6b00SAppaRao Puli                 getMetricReading(service, objPath, intf);
1437156d6b00SAppaRao Puli             });
1438156d6b00SAppaRao Puli     }
14391bf712bcSAyushi Smriti 
14401bf712bcSAyushi Smriti     bool validateAndSplitUrl(const std::string& destUrl, std::string& urlProto,
14411bf712bcSAyushi Smriti                              std::string& host, std::string& port,
14421bf712bcSAyushi Smriti                              std::string& path)
14431bf712bcSAyushi Smriti     {
14441bf712bcSAyushi Smriti         // Validate URL using regex expression
14451bf712bcSAyushi Smriti         // Format: <protocol>://<host>:<port>/<path>
14461bf712bcSAyushi Smriti         // protocol: http/https
14471bf712bcSAyushi Smriti         const std::regex urlRegex(
14481bf712bcSAyushi Smriti             "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
14491bf712bcSAyushi Smriti             "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
14501bf712bcSAyushi Smriti         std::cmatch match;
14511bf712bcSAyushi Smriti         if (!std::regex_match(destUrl.c_str(), match, urlRegex))
14521bf712bcSAyushi Smriti         {
14531bf712bcSAyushi Smriti             BMCWEB_LOG_INFO << "Dest. url did not match ";
14541bf712bcSAyushi Smriti             return false;
14551bf712bcSAyushi Smriti         }
14561bf712bcSAyushi Smriti 
14571bf712bcSAyushi Smriti         urlProto = std::string(match[1].first, match[1].second);
14581bf712bcSAyushi Smriti         if (urlProto == "http")
14591bf712bcSAyushi Smriti         {
14601bf712bcSAyushi Smriti #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
14611bf712bcSAyushi Smriti             return false;
14621bf712bcSAyushi Smriti #endif
14631bf712bcSAyushi Smriti         }
14641bf712bcSAyushi Smriti 
14651bf712bcSAyushi Smriti         host = std::string(match[2].first, match[2].second);
14661bf712bcSAyushi Smriti         port = std::string(match[3].first, match[3].second);
14671bf712bcSAyushi Smriti         path = std::string(match[4].first, match[4].second);
14681bf712bcSAyushi Smriti         if (port.empty())
14691bf712bcSAyushi Smriti         {
14701bf712bcSAyushi Smriti             if (urlProto == "http")
14711bf712bcSAyushi Smriti             {
14721bf712bcSAyushi Smriti                 port = "80";
14731bf712bcSAyushi Smriti             }
14741bf712bcSAyushi Smriti             else
14751bf712bcSAyushi Smriti             {
14761bf712bcSAyushi Smriti                 port = "443";
14771bf712bcSAyushi Smriti             }
14781bf712bcSAyushi Smriti         }
14791bf712bcSAyushi Smriti         if (path.empty())
14801bf712bcSAyushi Smriti         {
14811bf712bcSAyushi Smriti             path = "/";
14821bf712bcSAyushi Smriti         }
14831bf712bcSAyushi Smriti         return true;
14841bf712bcSAyushi Smriti     }
14857f4eb588SAppaRao Puli }; // namespace redfish
1486b52664e2SAppaRao Puli 
1487b52664e2SAppaRao Puli } // namespace redfish
1488