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