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