sel: Fix assertion in custom SEL parser

The code was using assert() to make sure the custom SEL's additional
data exists.

The assumption was wrong because there could be log entries with
additional data that does not contain the metadata defined in
xyz/openbmc_project/Logging/SEL.metadata.yaml.
E.g.
 ipmid: ../git/selutility.cpp:107: void ipmi::sel::internal::constructOEMSEL(uint8_t, std::chrono::milliseconds, const additionalDataMap&, ipmi::sel::GetSELEntryResponse&): Assertion `dataIter != m.end()' failed.

The behavior is changed to:
* Check if the additional data contains the SEL.metadata;
* If yes, parse it as custom SEL;
* If no, parse it as before, and throw if there is no associated
  inventory sensor, which is the logic before the custom SEL is
  introduced.

Tested: Verify `ipmitool sel list` shows all the entries and does not
        assert when the logging entries without SEL.metadata exists.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: Icc3df27ade4fdc40cf4fe73d3716e7270a97a5f1
diff --git a/selutility.cpp b/selutility.cpp
index 9187484..11b2fb9 100644
--- a/selutility.cpp
+++ b/selutility.cpp
@@ -219,21 +219,42 @@
     std::chrono::milliseconds chronoTimeStamp(
         std::get<uint64_t>(iterTimeStamp->second));
 
-    if (iter == invSensors.end())
+    bool isFromSELLogger = false;
+    additionalDataMap m;
+
+    // The recordID are with the same offset between different types,
+    // so we are safe to set the recordID here
+    record.event.eventRecord.recordID =
+        static_cast<uint16_t>(std::get<uint32_t>(iterId->second));
+
+    iterId = entryData.find(propAdditionalData);
+    if (iterId != entryData.end())
+    {
+        // Check if it's a SEL from phosphor-sel-logger which shall contain
+        // the record ID, etc
+        const auto& addData = std::get<AdditionalData>(iterId->second);
+        m = parseAdditionalData(addData);
+        auto recordTypeIter = m.find(strRecordType);
+        if (recordTypeIter != m.end())
+        {
+            // It is a SEL from phosphor-sel-logger
+            isFromSELLogger = true;
+        }
+        else
+        {
+            // Not a SEL from phosphor-sel-logger, it shall have a valid
+            // invSensor
+            if (iter == invSensors.end())
+            {
+                log<level::ERR>("System event sensor not found");
+                elog<InternalFailure>();
+            }
+        }
+    }
+
+    if (isFromSELLogger)
     {
         // It is expected to be a custom SEL entry
-        // The recordID are with the same offset between different types,
-        // so we are safe to set the recordID here
-        record.event.eventRecord.recordID =
-            static_cast<uint16_t>(std::get<uint32_t>(iterId->second));
-        iterId = entryData.find(propAdditionalData);
-        if (iterId == entryData.end())
-        {
-            log<level::ERR>("Error finding AdditionalData");
-            elog<InternalFailure>();
-        }
-        const auto& addData = std::get<AdditionalData>(iterId->second);
-        auto m = parseAdditionalData(addData);
         auto recordType = static_cast<uint8_t>(convert(m[strRecordType]));
         auto isOEM = isRecordOEM(recordType);
         if (isOEM)
@@ -247,9 +268,6 @@
     }
     else
     {
-
-        record.event.eventRecord.recordID =
-            static_cast<uint16_t>(std::get<uint32_t>(iterId->second));
         record.event.eventRecord.timeStamp = static_cast<uint32_t>(
             std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp)
                 .count());