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