1 /*
2 // Copyright (c) 2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 #include "dbus_utility.hpp"
18 #include "error_messages.hpp"
19 #include "event_service_store.hpp"
20 #include "filter_expr_executor.hpp"
21 #include "generated/enums/event.hpp"
22 #include "generated/enums/log_entry.hpp"
23 #include "http_client.hpp"
24 #include "metric_report.hpp"
25 #include "ossl_random.hpp"
26 #include "persistent_data.hpp"
27 #include "registries.hpp"
28 #include "registries_selector.hpp"
29 #include "str_utility.hpp"
30 #include "utility.hpp"
31 #include "utils/json_utils.hpp"
32 #include "utils/time_utils.hpp"
33 
34 #include <sys/inotify.h>
35 
36 #include <boost/asio/io_context.hpp>
37 #include <boost/circular_buffer.hpp>
38 #include <boost/container/flat_map.hpp>
39 #include <boost/url/format.hpp>
40 #include <boost/url/url_view_base.hpp>
41 #include <sdbusplus/bus/match.hpp>
42 
43 #include <algorithm>
44 #include <cstdlib>
45 #include <ctime>
46 #include <fstream>
47 #include <memory>
48 #include <ranges>
49 #include <span>
50 
51 namespace redfish
52 {
53 
54 static constexpr const char* eventFormatType = "Event";
55 static constexpr const char* metricReportFormatType = "MetricReport";
56 
57 static constexpr const char* subscriptionTypeSSE = "SSE";
58 static constexpr const char* eventServiceFile =
59     "/var/lib/bmcweb/eventservice_config.json";
60 
61 static constexpr const uint8_t maxNoOfSubscriptions = 20;
62 static constexpr const uint8_t maxNoOfSSESubscriptions = 10;
63 
64 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
65 static std::optional<boost::asio::posix::stream_descriptor> inotifyConn;
66 static constexpr const char* redfishEventLogDir = "/var/log";
67 static constexpr const char* redfishEventLogFile = "/var/log/redfish";
68 static constexpr const size_t iEventSize = sizeof(inotify_event);
69 
70 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
71 static int inotifyFd = -1;
72 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
73 static int dirWatchDesc = -1;
74 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
75 static int fileWatchDesc = -1;
76 struct EventLogObjectsType
77 {
78     std::string id;
79     std::string timestamp;
80     std::string messageId;
81     std::vector<std::string> messageArgs;
82 };
83 
84 namespace registries
85 {
86 static const Message*
87     getMsgFromRegistry(const std::string& messageKey,
88                        const std::span<const MessageEntry>& registry)
89 {
90     std::span<const MessageEntry>::iterator messageIt = std::ranges::find_if(
91         registry, [&messageKey](const MessageEntry& messageEntry) {
92         return messageKey == messageEntry.first;
93     });
94     if (messageIt != registry.end())
95     {
96         return &messageIt->second;
97     }
98 
99     return nullptr;
100 }
101 
102 static const Message* formatMessage(std::string_view messageID)
103 {
104     // Redfish MessageIds are in the form
105     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
106     // the right Message
107     std::vector<std::string> fields;
108     fields.reserve(4);
109 
110     bmcweb::split(fields, messageID, '.');
111     if (fields.size() != 4)
112     {
113         return nullptr;
114     }
115     const std::string& registryName = fields[0];
116     const std::string& messageKey = fields[3];
117 
118     // Find the right registry and check it for the MessageKey
119     return getMsgFromRegistry(messageKey, getRegistryFromPrefix(registryName));
120 }
121 } // namespace registries
122 
123 namespace event_log
124 {
125 inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID)
126 {
127     static time_t prevTs = 0;
128     static int index = 0;
129 
130     // Get the entry timestamp
131     std::time_t curTs = 0;
132     std::tm timeStruct = {};
133     std::istringstream entryStream(logEntry);
134     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
135     {
136         curTs = std::mktime(&timeStruct);
137         if (curTs == -1)
138         {
139             return false;
140         }
141     }
142     // If the timestamp isn't unique, increment the index
143     index = (curTs == prevTs) ? index + 1 : 0;
144 
145     // Save the timestamp
146     prevTs = curTs;
147 
148     entryID = std::to_string(curTs);
149     if (index > 0)
150     {
151         entryID += "_" + std::to_string(index);
152     }
153     return true;
154 }
155 
156 inline int getEventLogParams(const std::string& logEntry,
157                              std::string& timestamp, std::string& messageID,
158                              std::vector<std::string>& messageArgs)
159 {
160     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
161     // First get the Timestamp
162     size_t space = logEntry.find_first_of(' ');
163     if (space == std::string::npos)
164     {
165         return -EINVAL;
166     }
167     timestamp = logEntry.substr(0, space);
168     // Then get the log contents
169     size_t entryStart = logEntry.find_first_not_of(' ', space);
170     if (entryStart == std::string::npos)
171     {
172         return -EINVAL;
173     }
174     std::string_view entry(logEntry);
175     entry.remove_prefix(entryStart);
176     // Use split to separate the entry into its fields
177     std::vector<std::string> logEntryFields;
178     bmcweb::split(logEntryFields, entry, ',');
179     // We need at least a MessageId to be valid
180     if (logEntryFields.empty())
181     {
182         return -EINVAL;
183     }
184     messageID = logEntryFields[0];
185 
186     // Get the MessageArgs from the log if there are any
187     if (logEntryFields.size() > 1)
188     {
189         const std::string& messageArgsStart = logEntryFields[1];
190         // If the first string is empty, assume there are no MessageArgs
191         if (!messageArgsStart.empty())
192         {
193             messageArgs.assign(logEntryFields.begin() + 1,
194                                logEntryFields.end());
195         }
196     }
197 
198     return 0;
199 }
200 
201 inline void getRegistryAndMessageKey(const std::string& messageID,
202                                      std::string& registryName,
203                                      std::string& messageKey)
204 {
205     // Redfish MessageIds are in the form
206     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
207     // the right Message
208     std::vector<std::string> fields;
209     fields.reserve(4);
210     bmcweb::split(fields, messageID, '.');
211     if (fields.size() == 4)
212     {
213         registryName = fields[0];
214         messageKey = fields[3];
215     }
216 }
217 
218 inline int formatEventLogEntry(const std::string& logEntryID,
219                                const std::string& messageID,
220                                const std::span<std::string_view> messageArgs,
221                                std::string timestamp,
222                                const std::string& customText,
223                                nlohmann::json::object_t& logEntryJson)
224 {
225     // Get the Message from the MessageRegistry
226     const registries::Message* message = registries::formatMessage(messageID);
227 
228     if (message == nullptr)
229     {
230         return -1;
231     }
232 
233     std::string msg = redfish::registries::fillMessageArgs(messageArgs,
234                                                            message->message);
235     if (msg.empty())
236     {
237         return -1;
238     }
239 
240     // Get the Created time from the timestamp. The log timestamp is in
241     // RFC3339 format which matches the Redfish format except for the
242     // fractional seconds between the '.' and the '+', so just remove them.
243     std::size_t dot = timestamp.find_first_of('.');
244     std::size_t plus = timestamp.find_first_of('+', dot);
245     if (dot != std::string::npos && plus != std::string::npos)
246     {
247         timestamp.erase(dot, plus - dot);
248     }
249 
250     // Fill in the log entry with the gathered data
251     logEntryJson["EventId"] = logEntryID;
252 
253     logEntryJson["Severity"] = message->messageSeverity;
254     logEntryJson["Message"] = std::move(msg);
255     logEntryJson["MessageId"] = messageID;
256     logEntryJson["MessageArgs"] = messageArgs;
257     logEntryJson["EventTimestamp"] = std::move(timestamp);
258     logEntryJson["Context"] = customText;
259     return 0;
260 }
261 
262 } // namespace event_log
263 
264 class Subscription : public persistent_data::UserSubscription
265 {
266   public:
267     Subscription(const Subscription&) = delete;
268     Subscription& operator=(const Subscription&) = delete;
269     Subscription(Subscription&&) = delete;
270     Subscription& operator=(Subscription&&) = delete;
271 
272     Subscription(const boost::urls::url_view_base& url,
273                  boost::asio::io_context& ioc) :
274         policy(std::make_shared<crow::ConnectionPolicy>())
275     {
276         destinationUrl = url;
277         client.emplace(ioc, policy);
278         // Subscription constructor
279         policy->invalidResp = retryRespHandler;
280     }
281 
282     explicit Subscription(crow::sse_socket::Connection& connIn) :
283         sseConn(&connIn)
284     {}
285 
286     ~Subscription() = default;
287 
288     bool sendEvent(std::string&& msg)
289     {
290         persistent_data::EventServiceConfig eventServiceConfig =
291             persistent_data::EventServiceStore::getInstance()
292                 .getEventServiceConfig();
293         if (!eventServiceConfig.enabled)
294         {
295             return false;
296         }
297 
298         if (client)
299         {
300             client->sendData(
301                 std::move(msg), destinationUrl,
302                 static_cast<ensuressl::VerifyCertificate>(verifyCertificate),
303                 httpHeaders, boost::beast::http::verb::post);
304             return true;
305         }
306 
307         if (sseConn != nullptr)
308         {
309             eventSeqNum++;
310             sseConn->sendEvent(std::to_string(eventSeqNum), msg);
311         }
312         return true;
313     }
314 
315     bool eventMatchesFilter(const nlohmann::json::object_t& eventMessage,
316                             std::string_view resType)
317     {
318         // If resourceTypes list is empty, assume all
319         if (!resourceTypes.empty())
320         {
321             // Search the resourceTypes list for the subscription.
322             auto resourceTypeIndex = std::ranges::find_if(
323                 resourceTypes, [resType](const std::string& rtEntry) {
324                 return rtEntry == resType;
325             });
326             if (resourceTypeIndex == resourceTypes.end())
327             {
328                 BMCWEB_LOG_DEBUG("Not subscribed to this resource");
329                 return false;
330             }
331             BMCWEB_LOG_DEBUG("ResourceType {} found in the subscribed list",
332                              resType);
333         }
334 
335         // If registryMsgIds list is empty, assume all
336         if (!registryMsgIds.empty())
337         {
338             auto eventJson = eventMessage.find("MessageId");
339             if (eventJson == eventMessage.end())
340             {
341                 return false;
342             }
343 
344             const std::string* messageId =
345                 eventJson->second.get_ptr<const std::string*>();
346             if (messageId == nullptr)
347             {
348                 BMCWEB_LOG_ERROR("EventType wasn't a string???");
349                 return false;
350             }
351 
352             std::string registry;
353             std::string messageKey;
354             event_log::getRegistryAndMessageKey(*messageId, registry,
355                                                 messageKey);
356 
357             auto obj = std::ranges::find(registryMsgIds, registry);
358             if (obj == registryMsgIds.end())
359             {
360                 return false;
361             }
362         }
363 
364         if (filter)
365         {
366             if (!memberMatches(eventMessage, *filter))
367             {
368                 BMCWEB_LOG_DEBUG("Filter didn't match");
369                 return false;
370             }
371         }
372 
373         return true;
374     }
375 
376     bool sendTestEventLog()
377     {
378         nlohmann::json::array_t logEntryArray;
379         nlohmann::json& logEntryJson = logEntryArray.emplace_back();
380 
381         logEntryJson["EventId"] = "TestID";
382         logEntryJson["Severity"] = log_entry::EventSeverity::OK;
383         logEntryJson["Message"] = "Generated test event";
384         logEntryJson["MessageId"] = "OpenBMC.0.2.TestEventLog";
385         logEntryJson["MessageArgs"] = nlohmann::json::array();
386         logEntryJson["EventTimestamp"] =
387             redfish::time_utils::getDateTimeOffsetNow().first;
388         logEntryJson["Context"] = customText;
389 
390         nlohmann::json msg;
391         msg["@odata.type"] = "#Event.v1_4_0.Event";
392         msg["Id"] = std::to_string(eventSeqNum);
393         msg["Name"] = "Event Log";
394         msg["Events"] = logEntryArray;
395 
396         std::string strMsg = msg.dump(2, ' ', true,
397                                       nlohmann::json::error_handler_t::replace);
398         return sendEvent(std::move(strMsg));
399     }
400 
401     void filterAndSendEventLogs(
402         const std::vector<EventLogObjectsType>& eventRecords)
403     {
404         nlohmann::json::array_t logEntryArray;
405         for (const EventLogObjectsType& logEntry : eventRecords)
406         {
407             std::vector<std::string_view> messageArgsView(
408                 logEntry.messageArgs.begin(), logEntry.messageArgs.end());
409 
410             nlohmann::json::object_t bmcLogEntry;
411             if (event_log::formatEventLogEntry(
412                     logEntry.id, logEntry.messageId, messageArgsView,
413                     logEntry.timestamp, customText, bmcLogEntry) != 0)
414             {
415                 BMCWEB_LOG_DEBUG("Read eventLog entry failed");
416                 continue;
417             }
418 
419             if (!eventMatchesFilter(bmcLogEntry, ""))
420             {
421                 continue;
422             }
423 
424             logEntryArray.emplace_back(std::move(bmcLogEntry));
425         }
426 
427         if (logEntryArray.empty())
428         {
429             BMCWEB_LOG_DEBUG("No log entries available to be transferred.");
430             return;
431         }
432 
433         nlohmann::json msg;
434         msg["@odata.type"] = "#Event.v1_4_0.Event";
435         msg["Id"] = std::to_string(eventSeqNum);
436         msg["Name"] = "Event Log";
437         msg["Events"] = std::move(logEntryArray);
438         std::string strMsg = msg.dump(2, ' ', true,
439                                       nlohmann::json::error_handler_t::replace);
440         sendEvent(std::move(strMsg));
441         eventSeqNum++;
442     }
443 
444     void filterAndSendReports(const std::string& reportId,
445                               const telemetry::TimestampReadings& var)
446     {
447         boost::urls::url mrdUri = boost::urls::format(
448             "/redfish/v1/TelemetryService/MetricReportDefinitions/{}",
449             reportId);
450 
451         // Empty list means no filter. Send everything.
452         if (!metricReportDefinitions.empty())
453         {
454             if (std::ranges::find(metricReportDefinitions, mrdUri.buffer()) ==
455                 metricReportDefinitions.end())
456             {
457                 return;
458             }
459         }
460 
461         nlohmann::json msg;
462         if (!telemetry::fillReport(msg, reportId, var))
463         {
464             BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus "
465                              "Report with id {}",
466                              reportId);
467             return;
468         }
469 
470         // Context is set by user during Event subscription and it must be
471         // set for MetricReport response.
472         if (!customText.empty())
473         {
474             msg["Context"] = customText;
475         }
476 
477         std::string strMsg = msg.dump(2, ' ', true,
478                                       nlohmann::json::error_handler_t::replace);
479         sendEvent(std::move(strMsg));
480     }
481 
482     void updateRetryConfig(uint32_t retryAttempts,
483                            uint32_t retryTimeoutInterval)
484     {
485         if (policy == nullptr)
486         {
487             BMCWEB_LOG_DEBUG("Retry policy was nullptr, ignoring set");
488             return;
489         }
490         policy->maxRetryAttempts = retryAttempts;
491         policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval);
492     }
493 
494     uint64_t getEventSeqNum() const
495     {
496         return eventSeqNum;
497     }
498 
499     void setSubscriptionId(const std::string& id2)
500     {
501         BMCWEB_LOG_DEBUG("Subscription ID: {}", id2);
502         subId = id2;
503     }
504 
505     std::string getSubscriptionId()
506     {
507         return subId;
508     }
509 
510     bool matchSseId(const crow::sse_socket::Connection& thisConn)
511     {
512         return &thisConn == sseConn;
513     }
514 
515     // Check used to indicate what response codes are valid as part of our retry
516     // policy.  2XX is considered acceptable
517     static boost::system::error_code retryRespHandler(unsigned int respCode)
518     {
519         BMCWEB_LOG_DEBUG(
520             "Checking response code validity for SubscriptionEvent");
521         if ((respCode < 200) || (respCode >= 300))
522         {
523             return boost::system::errc::make_error_code(
524                 boost::system::errc::result_out_of_range);
525         }
526 
527         // Return 0 if the response code is valid
528         return boost::system::errc::make_error_code(
529             boost::system::errc::success);
530     }
531 
532   private:
533     std::string subId;
534     uint64_t eventSeqNum = 1;
535     boost::urls::url host;
536     std::shared_ptr<crow::ConnectionPolicy> policy;
537     crow::sse_socket::Connection* sseConn = nullptr;
538 
539     std::optional<crow::HttpClient> client;
540 
541   public:
542     std::optional<filter_ast::LogicalAnd> filter;
543 };
544 
545 class EventServiceManager
546 {
547   private:
548     bool serviceEnabled = false;
549     uint32_t retryAttempts = 0;
550     uint32_t retryTimeoutInterval = 0;
551 
552     std::streampos redfishLogFilePosition{0};
553     size_t noOfEventLogSubscribers{0};
554     size_t noOfMetricReportSubscribers{0};
555     std::shared_ptr<sdbusplus::bus::match_t> matchTelemetryMonitor;
556     boost::container::flat_map<std::string, std::shared_ptr<Subscription>>
557         subscriptionsMap;
558 
559     uint64_t eventId{1};
560 
561     struct Event
562     {
563         std::string id;
564         nlohmann::json message;
565     };
566 
567     constexpr static size_t maxMessages = 200;
568     boost::circular_buffer<Event> messages{maxMessages};
569 
570     boost::asio::io_context& ioc;
571 
572   public:
573     EventServiceManager(const EventServiceManager&) = delete;
574     EventServiceManager& operator=(const EventServiceManager&) = delete;
575     EventServiceManager(EventServiceManager&&) = delete;
576     EventServiceManager& operator=(EventServiceManager&&) = delete;
577     ~EventServiceManager() = default;
578 
579     explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn)
580     {
581         // Load config from persist store.
582         initConfig();
583     }
584 
585     static EventServiceManager&
586         getInstance(boost::asio::io_context* ioc = nullptr)
587     {
588         static EventServiceManager handler(*ioc);
589         return handler;
590     }
591 
592     void initConfig()
593     {
594         loadOldBehavior();
595 
596         persistent_data::EventServiceConfig eventServiceConfig =
597             persistent_data::EventServiceStore::getInstance()
598                 .getEventServiceConfig();
599 
600         serviceEnabled = eventServiceConfig.enabled;
601         retryAttempts = eventServiceConfig.retryAttempts;
602         retryTimeoutInterval = eventServiceConfig.retryTimeoutInterval;
603 
604         for (const auto& it : persistent_data::EventServiceStore::getInstance()
605                                   .subscriptionsConfigMap)
606         {
607             std::shared_ptr<persistent_data::UserSubscription> newSub =
608                 it.second;
609 
610             boost::system::result<boost::urls::url> url =
611                 boost::urls::parse_absolute_uri(newSub->destinationUrl);
612 
613             if (!url)
614             {
615                 BMCWEB_LOG_ERROR(
616                     "Failed to validate and split destination url");
617                 continue;
618             }
619             std::shared_ptr<Subscription> subValue =
620                 std::make_shared<Subscription>(*url, ioc);
621 
622             subValue->id = newSub->id;
623             subValue->destinationUrl = newSub->destinationUrl;
624             subValue->protocol = newSub->protocol;
625             subValue->verifyCertificate = newSub->verifyCertificate;
626             subValue->retryPolicy = newSub->retryPolicy;
627             subValue->customText = newSub->customText;
628             subValue->eventFormatType = newSub->eventFormatType;
629             subValue->subscriptionType = newSub->subscriptionType;
630             subValue->registryMsgIds = newSub->registryMsgIds;
631             subValue->registryPrefixes = newSub->registryPrefixes;
632             subValue->resourceTypes = newSub->resourceTypes;
633             subValue->httpHeaders = newSub->httpHeaders;
634             subValue->metricReportDefinitions = newSub->metricReportDefinitions;
635 
636             if (subValue->id.empty())
637             {
638                 BMCWEB_LOG_ERROR("Failed to add subscription");
639             }
640             subscriptionsMap.insert(std::pair(subValue->id, subValue));
641 
642             updateNoOfSubscribersCount();
643 
644             if constexpr (!BMCWEB_REDFISH_DBUS_LOG)
645             {
646                 cacheRedfishLogFile();
647             }
648 
649             // Update retry configuration.
650             subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval);
651         }
652     }
653 
654     static void loadOldBehavior()
655     {
656         std::ifstream eventConfigFile(eventServiceFile);
657         if (!eventConfigFile.good())
658         {
659             BMCWEB_LOG_DEBUG("Old eventService config not exist");
660             return;
661         }
662         auto jsonData = nlohmann::json::parse(eventConfigFile, nullptr, false);
663         if (jsonData.is_discarded())
664         {
665             BMCWEB_LOG_ERROR("Old eventService config parse error.");
666             return;
667         }
668 
669         const nlohmann::json::object_t* obj =
670             jsonData.get_ptr<const nlohmann::json::object_t*>();
671         for (const auto& item : *obj)
672         {
673             if (item.first == "Configuration")
674             {
675                 persistent_data::EventServiceStore::getInstance()
676                     .getEventServiceConfig()
677                     .fromJson(item.second);
678             }
679             else if (item.first == "Subscriptions")
680             {
681                 for (const auto& elem : item.second)
682                 {
683                     std::shared_ptr<persistent_data::UserSubscription>
684                         newSubscription =
685                             persistent_data::UserSubscription::fromJson(elem,
686                                                                         true);
687                     if (newSubscription == nullptr)
688                     {
689                         BMCWEB_LOG_ERROR("Problem reading subscription "
690                                          "from old persistent store");
691                         continue;
692                     }
693 
694                     std::uniform_int_distribution<uint32_t> dist(0);
695                     bmcweb::OpenSSLGenerator gen;
696 
697                     std::string id;
698 
699                     int retry = 3;
700                     while (retry != 0)
701                     {
702                         id = std::to_string(dist(gen));
703                         if (gen.error())
704                         {
705                             retry = 0;
706                             break;
707                         }
708                         newSubscription->id = id;
709                         auto inserted =
710                             persistent_data::EventServiceStore::getInstance()
711                                 .subscriptionsConfigMap.insert(
712                                     std::pair(id, newSubscription));
713                         if (inserted.second)
714                         {
715                             break;
716                         }
717                         --retry;
718                     }
719 
720                     if (retry <= 0)
721                     {
722                         BMCWEB_LOG_ERROR(
723                             "Failed to generate random number from old "
724                             "persistent store");
725                         continue;
726                     }
727                 }
728             }
729 
730             persistent_data::getConfig().writeData();
731             std::error_code ec;
732             std::filesystem::remove(eventServiceFile, ec);
733             if (ec)
734             {
735                 BMCWEB_LOG_DEBUG(
736                     "Failed to remove old event service file.  Ignoring");
737             }
738             else
739             {
740                 BMCWEB_LOG_DEBUG("Remove old eventservice config");
741             }
742         }
743     }
744 
745     void updateSubscriptionData() const
746     {
747         persistent_data::EventServiceStore::getInstance()
748             .eventServiceConfig.enabled = serviceEnabled;
749         persistent_data::EventServiceStore::getInstance()
750             .eventServiceConfig.retryAttempts = retryAttempts;
751         persistent_data::EventServiceStore::getInstance()
752             .eventServiceConfig.retryTimeoutInterval = retryTimeoutInterval;
753 
754         persistent_data::getConfig().writeData();
755     }
756 
757     void setEventServiceConfig(const persistent_data::EventServiceConfig& cfg)
758     {
759         bool updateConfig = false;
760         bool updateRetryCfg = false;
761 
762         if (serviceEnabled != cfg.enabled)
763         {
764             serviceEnabled = cfg.enabled;
765             if (serviceEnabled && noOfMetricReportSubscribers != 0U)
766             {
767                 registerMetricReportSignal();
768             }
769             else
770             {
771                 unregisterMetricReportSignal();
772             }
773             updateConfig = true;
774         }
775 
776         if (retryAttempts != cfg.retryAttempts)
777         {
778             retryAttempts = cfg.retryAttempts;
779             updateConfig = true;
780             updateRetryCfg = true;
781         }
782 
783         if (retryTimeoutInterval != cfg.retryTimeoutInterval)
784         {
785             retryTimeoutInterval = cfg.retryTimeoutInterval;
786             updateConfig = true;
787             updateRetryCfg = true;
788         }
789 
790         if (updateConfig)
791         {
792             updateSubscriptionData();
793         }
794 
795         if (updateRetryCfg)
796         {
797             // Update the changed retry config to all subscriptions
798             for (const auto& it :
799                  EventServiceManager::getInstance().subscriptionsMap)
800             {
801                 Subscription& entry = *it.second;
802                 entry.updateRetryConfig(retryAttempts, retryTimeoutInterval);
803             }
804         }
805     }
806 
807     void updateNoOfSubscribersCount()
808     {
809         size_t eventLogSubCount = 0;
810         size_t metricReportSubCount = 0;
811         for (const auto& it : subscriptionsMap)
812         {
813             std::shared_ptr<Subscription> entry = it.second;
814             if (entry->eventFormatType == eventFormatType)
815             {
816                 eventLogSubCount++;
817             }
818             else if (entry->eventFormatType == metricReportFormatType)
819             {
820                 metricReportSubCount++;
821             }
822         }
823 
824         noOfEventLogSubscribers = eventLogSubCount;
825         if (noOfMetricReportSubscribers != metricReportSubCount)
826         {
827             noOfMetricReportSubscribers = metricReportSubCount;
828             if (noOfMetricReportSubscribers != 0U)
829             {
830                 registerMetricReportSignal();
831             }
832             else
833             {
834                 unregisterMetricReportSignal();
835             }
836         }
837     }
838 
839     std::shared_ptr<Subscription> getSubscription(const std::string& id)
840     {
841         auto obj = subscriptionsMap.find(id);
842         if (obj == subscriptionsMap.end())
843         {
844             BMCWEB_LOG_ERROR("No subscription exist with ID:{}", id);
845             return nullptr;
846         }
847         std::shared_ptr<Subscription> subValue = obj->second;
848         return subValue;
849     }
850 
851     std::string
852         addSubscriptionInternal(const std::shared_ptr<Subscription>& subValue)
853     {
854         std::uniform_int_distribution<uint32_t> dist(0);
855         bmcweb::OpenSSLGenerator gen;
856 
857         std::string id;
858 
859         int retry = 3;
860         while (retry != 0)
861         {
862             id = std::to_string(dist(gen));
863             if (gen.error())
864             {
865                 retry = 0;
866                 break;
867             }
868             auto inserted = subscriptionsMap.insert(std::pair(id, subValue));
869             if (inserted.second)
870             {
871                 break;
872             }
873             --retry;
874         }
875 
876         if (retry <= 0)
877         {
878             BMCWEB_LOG_ERROR("Failed to generate random number");
879             return "";
880         }
881 
882         std::shared_ptr<persistent_data::UserSubscription> newSub =
883             std::make_shared<persistent_data::UserSubscription>();
884         newSub->id = id;
885         newSub->destinationUrl = subValue->destinationUrl;
886         newSub->protocol = subValue->protocol;
887         newSub->retryPolicy = subValue->retryPolicy;
888         newSub->customText = subValue->customText;
889         newSub->eventFormatType = subValue->eventFormatType;
890         newSub->subscriptionType = subValue->subscriptionType;
891         newSub->registryMsgIds = subValue->registryMsgIds;
892         newSub->registryPrefixes = subValue->registryPrefixes;
893         newSub->resourceTypes = subValue->resourceTypes;
894         newSub->httpHeaders = subValue->httpHeaders;
895         newSub->metricReportDefinitions = subValue->metricReportDefinitions;
896         persistent_data::EventServiceStore::getInstance()
897             .subscriptionsConfigMap.emplace(newSub->id, newSub);
898 
899         updateNoOfSubscribersCount();
900 
901         if constexpr (!BMCWEB_REDFISH_DBUS_LOG)
902         {
903             if (redfishLogFilePosition != 0)
904             {
905                 cacheRedfishLogFile();
906             }
907         }
908         // Update retry configuration.
909         subValue->updateRetryConfig(retryAttempts, retryTimeoutInterval);
910 
911         // Set Subscription ID for back trace
912         subValue->setSubscriptionId(id);
913 
914         return id;
915     }
916 
917     std::string
918         addSSESubscription(const std::shared_ptr<Subscription>& subValue,
919                            std::string_view lastEventId)
920     {
921         std::string id = addSubscriptionInternal(subValue);
922 
923         if (!lastEventId.empty())
924         {
925             BMCWEB_LOG_INFO("Attempting to find message for last id {}",
926                             lastEventId);
927             boost::circular_buffer<Event>::iterator lastEvent =
928                 std::find_if(messages.begin(), messages.end(),
929                              [&lastEventId](const Event& event) {
930                 return event.id == lastEventId;
931             });
932             // Can't find a matching ID
933             if (lastEvent == messages.end())
934             {
935                 nlohmann::json msg = messages::eventBufferExceeded();
936                 // If the buffer overloaded, send all messages.
937                 subValue->sendEvent(msg);
938                 lastEvent = messages.begin();
939             }
940             else
941             {
942                 // Skip the last event the user already has
943                 lastEvent++;
944             }
945 
946             for (boost::circular_buffer<Event>::const_iterator event =
947                      lastEvent;
948                  lastEvent != messages.end(); lastEvent++)
949             {
950                 subValue->sendEvent(event->message);
951             }
952         }
953         return id;
954     }
955 
956     std::string
957         addPushSubscription(const std::shared_ptr<Subscription>& subValue)
958     {
959         std::string id = addSubscriptionInternal(subValue);
960 
961         updateSubscriptionData();
962         return id;
963     }
964 
965     bool isSubscriptionExist(const std::string& id)
966     {
967         auto obj = subscriptionsMap.find(id);
968         return obj != subscriptionsMap.end();
969     }
970 
971     void deleteSubscription(const std::string& id)
972     {
973         auto obj = subscriptionsMap.find(id);
974         if (obj != subscriptionsMap.end())
975         {
976             subscriptionsMap.erase(obj);
977             auto obj2 = persistent_data::EventServiceStore::getInstance()
978                             .subscriptionsConfigMap.find(id);
979             persistent_data::EventServiceStore::getInstance()
980                 .subscriptionsConfigMap.erase(obj2);
981             updateNoOfSubscribersCount();
982             updateSubscriptionData();
983         }
984     }
985 
986     void deleteSseSubscription(const crow::sse_socket::Connection& thisConn)
987     {
988         for (auto it = subscriptionsMap.begin(); it != subscriptionsMap.end();)
989         {
990             std::shared_ptr<Subscription> entry = it->second;
991             bool entryIsThisConn = entry->matchSseId(thisConn);
992             if (entryIsThisConn)
993             {
994                 persistent_data::EventServiceStore::getInstance()
995                     .subscriptionsConfigMap.erase(
996                         it->second->getSubscriptionId());
997                 it = subscriptionsMap.erase(it);
998                 return;
999             }
1000             it++;
1001         }
1002     }
1003 
1004     size_t getNumberOfSubscriptions() const
1005     {
1006         return subscriptionsMap.size();
1007     }
1008 
1009     size_t getNumberOfSSESubscriptions() const
1010     {
1011         auto size = std::ranges::count_if(
1012             subscriptionsMap,
1013             [](const std::pair<std::string, std::shared_ptr<Subscription>>&
1014                    entry) {
1015             return (entry.second->subscriptionType == subscriptionTypeSSE);
1016         });
1017         return static_cast<size_t>(size);
1018     }
1019 
1020     std::vector<std::string> getAllIDs()
1021     {
1022         std::vector<std::string> idList;
1023         for (const auto& it : subscriptionsMap)
1024         {
1025             idList.emplace_back(it.first);
1026         }
1027         return idList;
1028     }
1029 
1030     bool sendTestEventLog()
1031     {
1032         for (const auto& it : subscriptionsMap)
1033         {
1034             std::shared_ptr<Subscription> entry = it.second;
1035             if (!entry->sendTestEventLog())
1036             {
1037                 return false;
1038             }
1039         }
1040         return true;
1041     }
1042 
1043     void sendEvent(nlohmann::json::object_t eventMessage,
1044                    std::string_view origin, std::string_view resourceType)
1045     {
1046         eventMessage["EventId"] = eventId;
1047 
1048         eventMessage["EventTimestamp"] =
1049             redfish::time_utils::getDateTimeOffsetNow().first;
1050         eventMessage["OriginOfCondition"] = origin;
1051 
1052         // MemberId is 0 : since we are sending one event record.
1053         eventMessage["MemberId"] = 0;
1054 
1055         messages.push_back(Event(std::to_string(eventId), eventMessage));
1056 
1057         for (auto& it : subscriptionsMap)
1058         {
1059             std::shared_ptr<Subscription>& entry = it.second;
1060             if (!entry->eventMatchesFilter(eventMessage, resourceType))
1061             {
1062                 BMCWEB_LOG_DEBUG("Filter didn't match");
1063                 continue;
1064             }
1065 
1066             nlohmann::json::array_t eventRecord;
1067             eventRecord.emplace_back(eventMessage);
1068 
1069             nlohmann::json msgJson;
1070 
1071             msgJson["@odata.type"] = "#Event.v1_4_0.Event";
1072             msgJson["Name"] = "Event Log";
1073             msgJson["Id"] = eventId;
1074             msgJson["Events"] = std::move(eventRecord);
1075 
1076             std::string strMsg = msgJson.dump(
1077                 2, ' ', true, nlohmann::json::error_handler_t::replace);
1078             entry->sendEvent(std::move(strMsg));
1079             eventId++; // increment the eventId
1080         }
1081     }
1082 
1083     void resetRedfishFilePosition()
1084     {
1085         // Control would be here when Redfish file is created.
1086         // Reset File Position as new file is created
1087         redfishLogFilePosition = 0;
1088     }
1089 
1090     void cacheRedfishLogFile()
1091     {
1092         // Open the redfish file and read till the last record.
1093 
1094         std::ifstream logStream(redfishEventLogFile);
1095         if (!logStream.good())
1096         {
1097             BMCWEB_LOG_ERROR(" Redfish log file open failed ");
1098             return;
1099         }
1100         std::string logEntry;
1101         while (std::getline(logStream, logEntry))
1102         {
1103             redfishLogFilePosition = logStream.tellg();
1104         }
1105     }
1106 
1107     void readEventLogsFromFile()
1108     {
1109         std::ifstream logStream(redfishEventLogFile);
1110         if (!logStream.good())
1111         {
1112             BMCWEB_LOG_ERROR(" Redfish log file open failed");
1113             return;
1114         }
1115 
1116         std::vector<EventLogObjectsType> eventRecords;
1117 
1118         std::string logEntry;
1119 
1120         // Get the read pointer to the next log to be read.
1121         logStream.seekg(redfishLogFilePosition);
1122 
1123         while (std::getline(logStream, logEntry))
1124         {
1125             // Update Pointer position
1126             redfishLogFilePosition = logStream.tellg();
1127 
1128             std::string idStr;
1129             if (!event_log::getUniqueEntryID(logEntry, idStr))
1130             {
1131                 continue;
1132             }
1133 
1134             if (!serviceEnabled || noOfEventLogSubscribers == 0)
1135             {
1136                 // If Service is not enabled, no need to compute
1137                 // the remaining items below.
1138                 // But, Loop must continue to keep track of Timestamp
1139                 continue;
1140             }
1141 
1142             std::string timestamp;
1143             std::string messageID;
1144             std::vector<std::string> messageArgs;
1145             if (event_log::getEventLogParams(logEntry, timestamp, messageID,
1146                                              messageArgs) != 0)
1147             {
1148                 BMCWEB_LOG_DEBUG("Read eventLog entry params failed");
1149                 continue;
1150             }
1151 
1152             eventRecords.emplace_back(idStr, timestamp, messageID, messageArgs);
1153         }
1154 
1155         if (!serviceEnabled || noOfEventLogSubscribers == 0)
1156         {
1157             BMCWEB_LOG_DEBUG("EventService disabled or no Subscriptions.");
1158             return;
1159         }
1160 
1161         if (eventRecords.empty())
1162         {
1163             // No Records to send
1164             BMCWEB_LOG_DEBUG("No log entries available to be transferred.");
1165             return;
1166         }
1167 
1168         for (const auto& it : subscriptionsMap)
1169         {
1170             std::shared_ptr<Subscription> entry = it.second;
1171             if (entry->eventFormatType == "Event")
1172             {
1173                 entry->filterAndSendEventLogs(eventRecords);
1174             }
1175         }
1176     }
1177 
1178     static void watchRedfishEventLogFile()
1179     {
1180         if (!inotifyConn)
1181         {
1182             return;
1183         }
1184 
1185         static std::array<char, 1024> readBuffer;
1186 
1187         inotifyConn->async_read_some(boost::asio::buffer(readBuffer),
1188                                      [&](const boost::system::error_code& ec,
1189                                          const std::size_t& bytesTransferred) {
1190             if (ec == boost::asio::error::operation_aborted)
1191             {
1192                 BMCWEB_LOG_DEBUG("Inotify was canceled (shutdown?)");
1193                 return;
1194             }
1195             if (ec)
1196             {
1197                 BMCWEB_LOG_ERROR("Callback Error: {}", ec.message());
1198                 return;
1199             }
1200             std::size_t index = 0;
1201             while ((index + iEventSize) <= bytesTransferred)
1202             {
1203                 struct inotify_event event
1204                 {};
1205                 std::memcpy(&event, &readBuffer[index], iEventSize);
1206                 if (event.wd == dirWatchDesc)
1207                 {
1208                     if ((event.len == 0) ||
1209                         (index + iEventSize + event.len > bytesTransferred))
1210                     {
1211                         index += (iEventSize + event.len);
1212                         continue;
1213                     }
1214 
1215                     std::string fileName(&readBuffer[index + iEventSize]);
1216                     if (fileName != "redfish")
1217                     {
1218                         index += (iEventSize + event.len);
1219                         continue;
1220                     }
1221 
1222                     BMCWEB_LOG_DEBUG(
1223                         "Redfish log file created/deleted. event.name: {}",
1224                         fileName);
1225                     if (event.mask == IN_CREATE)
1226                     {
1227                         if (fileWatchDesc != -1)
1228                         {
1229                             BMCWEB_LOG_DEBUG(
1230                                 "Remove and Add inotify watcher on "
1231                                 "redfish event log file");
1232                             // Remove existing inotify watcher and add
1233                             // with new redfish event log file.
1234                             inotify_rm_watch(inotifyFd, fileWatchDesc);
1235                             fileWatchDesc = -1;
1236                         }
1237 
1238                         fileWatchDesc = inotify_add_watch(
1239                             inotifyFd, redfishEventLogFile, IN_MODIFY);
1240                         if (fileWatchDesc == -1)
1241                         {
1242                             BMCWEB_LOG_ERROR("inotify_add_watch failed for "
1243                                              "redfish log file.");
1244                             return;
1245                         }
1246 
1247                         EventServiceManager::getInstance()
1248                             .resetRedfishFilePosition();
1249                         EventServiceManager::getInstance()
1250                             .readEventLogsFromFile();
1251                     }
1252                     else if ((event.mask == IN_DELETE) ||
1253                              (event.mask == IN_MOVED_TO))
1254                     {
1255                         if (fileWatchDesc != -1)
1256                         {
1257                             inotify_rm_watch(inotifyFd, fileWatchDesc);
1258                             fileWatchDesc = -1;
1259                         }
1260                     }
1261                 }
1262                 else if (event.wd == fileWatchDesc)
1263                 {
1264                     if (event.mask == IN_MODIFY)
1265                     {
1266                         EventServiceManager::getInstance()
1267                             .readEventLogsFromFile();
1268                     }
1269                 }
1270                 index += (iEventSize + event.len);
1271             }
1272 
1273             watchRedfishEventLogFile();
1274         });
1275     }
1276 
1277     static int startEventLogMonitor(boost::asio::io_context& ioc)
1278     {
1279         inotifyConn.emplace(ioc);
1280         inotifyFd = inotify_init1(IN_NONBLOCK);
1281         if (inotifyFd == -1)
1282         {
1283             BMCWEB_LOG_ERROR("inotify_init1 failed.");
1284             return -1;
1285         }
1286 
1287         // Add watch on directory to handle redfish event log file
1288         // create/delete.
1289         dirWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogDir,
1290                                          IN_CREATE | IN_MOVED_TO | IN_DELETE);
1291         if (dirWatchDesc == -1)
1292         {
1293             BMCWEB_LOG_ERROR(
1294                 "inotify_add_watch failed for event log directory.");
1295             return -1;
1296         }
1297 
1298         // Watch redfish event log file for modifications.
1299         fileWatchDesc = inotify_add_watch(inotifyFd, redfishEventLogFile,
1300                                           IN_MODIFY);
1301         if (fileWatchDesc == -1)
1302         {
1303             BMCWEB_LOG_ERROR("inotify_add_watch failed for redfish log file.");
1304             // Don't return error if file not exist.
1305             // Watch on directory will handle create/delete of file.
1306         }
1307 
1308         // monitor redfish event log file
1309         inotifyConn->assign(inotifyFd);
1310         watchRedfishEventLogFile();
1311 
1312         return 0;
1313     }
1314 
1315     static void stopEventLogMonitor()
1316     {
1317         inotifyConn.reset();
1318     }
1319 
1320     static void getReadingsForReport(sdbusplus::message_t& msg)
1321     {
1322         if (msg.is_method_error())
1323         {
1324             BMCWEB_LOG_ERROR("TelemetryMonitor Signal error");
1325             return;
1326         }
1327 
1328         sdbusplus::message::object_path path(msg.get_path());
1329         std::string id = path.filename();
1330         if (id.empty())
1331         {
1332             BMCWEB_LOG_ERROR("Failed to get Id from path");
1333             return;
1334         }
1335 
1336         std::string interface;
1337         dbus::utility::DBusPropertiesMap props;
1338         std::vector<std::string> invalidProps;
1339         msg.read(interface, props, invalidProps);
1340 
1341         auto found = std::ranges::find_if(
1342             props, [](const auto& x) { return x.first == "Readings"; });
1343         if (found == props.end())
1344         {
1345             BMCWEB_LOG_INFO("Failed to get Readings from Report properties");
1346             return;
1347         }
1348 
1349         const telemetry::TimestampReadings* readings =
1350             std::get_if<telemetry::TimestampReadings>(&found->second);
1351         if (readings == nullptr)
1352         {
1353             BMCWEB_LOG_INFO("Failed to get Readings from Report properties");
1354             return;
1355         }
1356 
1357         for (const auto& it :
1358              EventServiceManager::getInstance().subscriptionsMap)
1359         {
1360             Subscription& entry = *it.second;
1361             if (entry.eventFormatType == metricReportFormatType)
1362             {
1363                 entry.filterAndSendReports(id, *readings);
1364             }
1365         }
1366     }
1367 
1368     void unregisterMetricReportSignal()
1369     {
1370         if (matchTelemetryMonitor)
1371         {
1372             BMCWEB_LOG_DEBUG("Metrics report signal - Unregister");
1373             matchTelemetryMonitor.reset();
1374             matchTelemetryMonitor = nullptr;
1375         }
1376     }
1377 
1378     void registerMetricReportSignal()
1379     {
1380         if (!serviceEnabled || matchTelemetryMonitor)
1381         {
1382             BMCWEB_LOG_DEBUG("Not registering metric report signal.");
1383             return;
1384         }
1385 
1386         BMCWEB_LOG_DEBUG("Metrics report signal - Register");
1387         std::string matchStr = "type='signal',member='PropertiesChanged',"
1388                                "interface='org.freedesktop.DBus.Properties',"
1389                                "arg0=xyz.openbmc_project.Telemetry.Report";
1390 
1391         matchTelemetryMonitor = std::make_shared<sdbusplus::bus::match_t>(
1392             *crow::connections::systemBus, matchStr, getReadingsForReport);
1393     }
1394 };
1395 
1396 } // namespace redfish
1397