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