oem: ampere: use lg2::commit to log events

Switch to use lg2::commit method to create Redfish events instead
journal logs. The changes of events are mapped as below:
- AmpereEvent -> ReportedSELInfo
- AmpereWarning -> ReportedSELWarning
- AmpereCritical -> ReportedSELCritical
- BIOSFirmwarePanicReason: no change, it still creates journal logs.

Tested:
  1. Turn off/on the Host
     $ipmitool power <off/on>
  2. Check the Redfish events in the schema
     /redfish/v1/Systems/system/LogServices/EventLog/Entries
  3. Host's events are shown

Change-Id: Ib56354a3d7d07265c123918615d0fab7b680e6a7
Signed-off-by: Thang Tran <thuutran@amperecomputing.com>
diff --git a/oem/ampere/event/oem_event_manager.cpp b/oem/ampere/event/oem_event_manager.cpp
index efdd34a..a33726d 100644
--- a/oem/ampere/event/oem_event_manager.cpp
+++ b/oem/ampere/event/oem_event_manager.cpp
@@ -11,6 +11,8 @@
 #include <libpldm/utils.h>
 #include <systemd/sd-journal.h>
 
+#include <com/ampere/Event/ReportedSEL/event.hpp>
+#include <phosphor-logging/commit.hpp>
 #include <phosphor-logging/lg2.hpp>
 #include <xyz/openbmc_project/Logging/Entry/server.hpp>
 
@@ -27,6 +29,8 @@
 {
 namespace fs = std::filesystem;
 using namespace std::chrono;
+namespace ReportedErrorSEL = sdbusplus::error::com::ampere::event::ReportedSEL;
+namespace ReportedEventSEL = sdbusplus::event::com::ampere::event::ReportedSEL;
 
 namespace boot_stage = boot::stage;
 namespace ddr_status = ddr::status;
@@ -35,9 +39,6 @@
 namespace phy_syndrome = dimm::training_failure::phy_syndrome;
 namespace training_failure = dimm::training_failure;
 
-constexpr const char* ampereEventRegistry = "OpenBMC.0.1.AmpereEvent";
-constexpr const char* ampereWarningRegistry = "OpenBMC.0.1.AmpereWarning";
-constexpr const char* ampereCriticalRegistry = "OpenBMC.0.1.AmpereCritical";
 constexpr const char* BIOSFWPanicRegistry =
     "OpenBMC.0.1.BIOSFirmwarePanicReason";
 constexpr auto maxDIMMIdxBitNum = 24;
@@ -184,16 +185,6 @@
          std::make_pair("DIMM training failure",
                         dimmTrainingFailureSyndromeToMsgMap)}};
 
-/*
-    A map between log level and the registry used for Redfish SEL log
-    Using pldm::oem::log_level
-*/
-std::unordered_map<log_level, std::string> logLevelToRedfishMsgIdMap = {
-    {log_level::OK, ampereEventRegistry},
-    {log_level::WARNING, ampereWarningRegistry},
-    {log_level::CRITICAL, ampereCriticalRegistry},
-    {log_level::BIOSFWPANIC, BIOSFWPanicRegistry}};
-
 std::unordered_map<
     uint16_t,
     std::vector<std::pair<
@@ -225,28 +216,30 @@
                                                   uint16_t sensorId)
 {
     std::string description;
-    if (!tidToSocketNameMap.contains(tid))
-    {
-        description += "TID " + std::to_string(tid) + ": ";
-    }
-    else
-    {
-        description += tidToSocketNameMap[tid] + ": ";
-    }
 
     if (!sensorIdToStrMap.contains(sensorId))
     {
-        description += "Sensor ID " + std::to_string(sensorId) + ": ";
+        description += "Sensor ID " + std::to_string(sensorId) + " of ";
     }
     else
     {
-        description += sensorIdToStrMap[sensorId] + ": ";
+        description += "Sensor " + sensorIdToStrMap[sensorId] + " of ";
+    }
+
+    if (!tidToSocketNameMap.contains(tid))
+    {
+        description += "TID " + std::to_string(tid);
+    }
+    else
+    {
+        description += tidToSocketNameMap[tid];
     }
 
     return description;
 }
 
-void OemEventManager::sendJournalRedfish(const std::string& description,
+void OemEventManager::sendJournalRedfish(const std::string& source,
+                                         const std::string& description,
                                          log_level& logLevel)
 {
     if (description.empty())
@@ -254,15 +247,31 @@
         return;
     }
 
-    if (!logLevelToRedfishMsgIdMap.contains(logLevel))
+    switch (logLevel)
     {
-        lg2::error("Invalid {LEVEL} Description {DES}", "LEVEL", logLevel,
-                   "DES", description);
-        return;
+        case log_level::OK:
+            lg2::commit(ReportedEventSEL::ReportedSELInfo(
+                "SOURCE", source, "MESSAGE", description, "RAW_DATA", ""));
+            break;
+        case log_level::WARNING:
+            lg2::commit(ReportedErrorSEL::ReportedSELWarning(
+                "SOURCE", source, "MESSAGE", description, "RAW_DATA", ""));
+            break;
+        case log_level::CRITICAL:
+            lg2::commit(ReportedErrorSEL::ReportedSELCritical(
+                "SOURCE", source, "MESSAGE", description, "RAW_DATA", ""));
+            break;
+        case log_level::BIOSFWPANIC:
+            lg2::info("MESSAGE={DES}", "DES", description, "REDFISH_MESSAGE_ID",
+                      BIOSFWPanicRegistry, "REDFISH_MESSAGE_ARGS", description);
+            break;
+        default:
+        {
+            lg2::error("Invalid {LEVEL} Description {DES}", "LEVEL", logLevel,
+                       "DES", description);
+            return;
+        }
     }
-    auto redfishMsgId = logLevelToRedfishMsgIdMap[logLevel];
-    lg2::info("MESSAGE={DES}", "DES", description, "REDFISH_MESSAGE_ID",
-              redfishMsgId, "REDFISH_MESSAGE_ARGS", description);
 }
 
 std::string OemEventManager::dimmIdxsToString(uint32_t dimmIdxs)
@@ -290,11 +299,12 @@
     return dimmIdx;
 }
 
-void OemEventManager::handleBootOverallEvent(
-    pldm_tid_t /*tid*/, uint16_t /*sensorId*/, uint32_t presentReading)
+void OemEventManager::handleBootOverallEvent(pldm_tid_t tid, uint16_t sensorId,
+                                             uint32_t presentReading)
 {
     log_level logLevel{log_level::OK};
     std::string description;
+    std::string source;
     std::stringstream strStream;
 
     uint8_t byte0 = (presentReading & 0x000000ff);
@@ -379,8 +389,9 @@
         }
     }
 
+    source = prefixMsgStrCreation(tid, sensorId);
     // Log to Redfish event
-    sendJournalRedfish(description, logLevel);
+    sendJournalRedfish(source, description, logLevel);
 }
 
 int OemEventManager::processNumericSensorEvent(
@@ -474,12 +485,12 @@
     }
 
     std::string description;
+    std::string source = prefixMsgStrCreation(tid, sensorId);
 
     if (stateSensorToMsgMap.contains(sensorId))
     {
         log_level logLevel = log_level::OK;
 
-        description += prefixMsgStrCreation(tid, sensorId);
         auto componentMap = stateSensorToMsgMap[sensorId];
         if (sensorOffset < componentMap.size())
         {
@@ -512,7 +523,7 @@
                            std::to_string(sensorOffset);
         }
 
-        sendJournalRedfish(description, logLevel);
+        sendJournalRedfish(source, description, logLevel);
     }
     else
     {
@@ -638,6 +649,7 @@
                                              uint32_t presentReading)
 {
     std::string description;
+    std::string source;
     std::stringstream strStream;
     PCIeHotPlugEventRecord_t record{presentReading};
 
@@ -646,7 +658,7 @@
     log_level logLevel =
         (!record.bits.opStatus) ? log_level::OK : log_level::WARNING;
 
-    description += prefixMsgStrCreation(tid, sensorId);
+    source = prefixMsgStrCreation(tid, sensorId);
 
     strStream << "Segment (0x" << std::setfill('0') << std::hex << std::setw(2)
               << static_cast<uint32_t>(record.bits.segment) << "); Bus (0x"
@@ -661,7 +673,7 @@
     description += strStream.str();
 
     // Log to Redfish event
-    sendJournalRedfish(description, logLevel);
+    sendJournalRedfish(source, description, logLevel);
 }
 
 std::string OemEventManager::dimmTrainingFailureToMsg(uint32_t failureInfo)
@@ -717,10 +729,11 @@
 {
     log_level logLevel{log_level::WARNING};
     std::string description;
+    std::string source;
     uint8_t byte3 = (presentReading & 0xff000000) >> 24;
     uint32_t byte012 = presentReading & 0xffffff;
 
-    description += prefixMsgStrCreation(tid, sensorId);
+    source = prefixMsgStrCreation(tid, sensorId);
 
     // DIMMx_Status sensorID 4+2*index (index 0 -> maxDIMMInstantNum-1)
     auto dimmIdx = sensorIdToDIMMIdx(sensorId);
@@ -793,7 +806,7 @@
     }
 
     // Log to Redfish event
-    sendJournalRedfish(description, logLevel);
+    sendJournalRedfish(source, description, logLevel);
 }
 
 void OemEventManager::handleDDRStatusEvent(pldm_tid_t tid, uint16_t sensorId,
@@ -801,10 +814,11 @@
 {
     log_level logLevel{log_level::WARNING};
     std::string description;
+    std::string source;
     uint8_t byte3 = (presentReading & 0xff000000) >> 24;
     uint32_t byte012 = presentReading & 0xffffff;
 
-    description += prefixMsgStrCreation(tid, sensorId);
+    source = prefixMsgStrCreation(tid, sensorId);
 
     description += "DDR ";
     if (ddrStatusToMsgMap.contains(byte3))
@@ -829,7 +843,7 @@
     }
 
     // Log to Redfish event
-    sendJournalRedfish(description, logLevel);
+    sendJournalRedfish(source, description, logLevel);
 }
 
 void OemEventManager::handleVRDStatusEvent(pldm_tid_t tid, uint16_t sensorId,
@@ -837,9 +851,10 @@
 {
     log_level logLevel{log_level::WARNING};
     std::string description;
+    std::string source;
     std::stringstream strStream;
 
-    description += prefixMsgStrCreation(tid, sensorId);
+    source = prefixMsgStrCreation(tid, sensorId);
 
     VRDStatus_t status{presentReading};
 
@@ -878,16 +893,17 @@
     description += strStream.str();
 
     // Log to Redfish event
-    sendJournalRedfish(description, logLevel);
+    sendJournalRedfish(source, description, logLevel);
 }
 
 void OemEventManager::handleNumericWatchdogEvent(
     pldm_tid_t tid, uint16_t sensorId, uint32_t presentReading)
 {
     std::string description;
+    std::string source;
     log_level logLevel = log_level::CRITICAL;
 
-    description += prefixMsgStrCreation(tid, sensorId);
+    source = prefixMsgStrCreation(tid, sensorId);
 
     if (presentReading & 0x01)
     {
@@ -903,7 +919,7 @@
     }
 
     // Log to Redfish event
-    sendJournalRedfish(description, logLevel);
+    sendJournalRedfish(source, description, logLevel);
 }
 
 int OemEventManager::processOemMsgPollEvent(pldm_tid_t tid, uint16_t eventId,
diff --git a/oem/ampere/event/oem_event_manager.hpp b/oem/ampere/event/oem_event_manager.hpp
index 4b75282..3dae22f 100644
--- a/oem/ampere/event/oem_event_manager.hpp
+++ b/oem/ampere/event/oem_event_manager.hpp
@@ -306,10 +306,12 @@
 
     /** @brief Log the message into Redfish SEL.
      *
+     *  @param[in] source - the logging resource
      *  @param[in] description - the logging message
      *  @param[in] logLevel - the logging level
      */
-    void sendJournalRedfish(const std::string& description,
+    void sendJournalRedfish(const std::string& source,
+                            const std::string& description,
                             log_level& logLevel);
 
     /** @brief Convert the one-hot DIMM index byte into a string of DIMM
@@ -353,7 +355,7 @@
      *  @param[in] sensorId - Sensor ID
      *  @param[in] presentReading - the present reading of the sensor
      */
-    void handleBootOverallEvent(pldm_tid_t /*tid*/, uint16_t /*sensorId*/,
+    void handleBootOverallEvent(pldm_tid_t tid, uint16_t sensorId,
                                 uint32_t presentReading);
 
     /** @brief Handle numeric sensor event message from DIMM status sensor.