PEL: Update D-Bus event sev based on PEL severity

Put in checks to ensure the D-Bus event log severity agrees with the
final PEL severity for PELs created by the BMC.  The D-bus property is
what is used in the Redfish event log, and we want to avoid cases like
having a Critical event log for an informational PEL.

This could happen in the case where the PEL message registry entry for
the event has a hardcoded or manufacturing specific severity value that
is different than the severity the D-Bus event log is first created
with.

This commit ensures that:
* If the PEL is nonError/recovered, the D-Bus severity is one of the
  non error ones.
* If the PEL isn't nonError/recovered, then the D-Bus severity also
  isn't.
* If the PEL is critical, the D-Bus severity is also critical.

It doesn't try to update the D-Bus severity for every PEL severity
because there isn't a one to one mapping - e.g. Notice, Debug, and
Informational D-Bus severities all map to a PEL severity of
nonError(informational).

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I6b0006034090e6d8e33e9f8b136ae50cce489f6e
diff --git a/extensions/openpower-pels/severity.cpp b/extensions/openpower-pels/severity.cpp
index 2c7c9df..acac0f5 100644
--- a/extensions/openpower-pels/severity.cpp
+++ b/extensions/openpower-pels/severity.cpp
@@ -15,8 +15,6 @@
  */
 #include "severity.hpp"
 
-#include "pel_types.hpp"
-
 namespace openpower
 {
 namespace pels
@@ -24,6 +22,33 @@
 
 using LogSeverity = phosphor::logging::Entry::Level;
 
+namespace
+{
+
+LogSeverity convertPELSeverityToOBMC(SeverityType pelSeverity)
+{
+    LogSeverity logSeverity = LogSeverity::Error;
+
+    const std::map<SeverityType, LogSeverity> severities{
+        {SeverityType::nonError, LogSeverity::Informational},
+        {SeverityType::recovered, LogSeverity::Informational},
+        {SeverityType::predictive, LogSeverity::Warning},
+        {SeverityType::unrecoverable, LogSeverity::Error},
+        {SeverityType::critical, LogSeverity::Critical},
+        {SeverityType::diagnostic, LogSeverity::Error},
+        {SeverityType::symptom, LogSeverity::Warning}};
+
+    auto s = severities.find(pelSeverity);
+    if (s != severities.end())
+    {
+        logSeverity = s->second;
+    }
+
+    return logSeverity;
+}
+
+} // namespace
+
 uint8_t convertOBMCSeverityToPEL(LogSeverity severity)
 {
     uint8_t pelSeverity = static_cast<uint8_t>(SeverityType::unrecoverable);
@@ -52,5 +77,41 @@
 
     return pelSeverity;
 }
+
+std::optional<LogSeverity> fixupLogSeverity(LogSeverity obmcSeverity,
+                                            SeverityType pelSeverity)
+{
+    bool isNonErrPelSev = (pelSeverity == SeverityType::nonError) ||
+                          (pelSeverity == SeverityType::recovered);
+
+    bool isNonErrObmcSev = (obmcSeverity == LogSeverity::Notice) ||
+                           (obmcSeverity == LogSeverity::Informational) ||
+                           (obmcSeverity == LogSeverity::Debug);
+
+    // If a nonError/recovered PEL, then the LogSeverity must be
+    // Notice/Informational/Debug, otherwise set it to Informational.
+    if (isNonErrPelSev && !isNonErrObmcSev)
+    {
+        return LogSeverity::Informational;
+    }
+
+    // If a Notice/Informational/Debug LogSeverity, then the PEL
+    // severity must be nonError/recovered, otherwise set it
+    // to an appropriate value based on the actual PEL severity.
+    if (isNonErrObmcSev && !isNonErrPelSev)
+    {
+        return convertPELSeverityToOBMC(pelSeverity);
+    }
+
+    // If PEL is critical, the LogSeverity should be as well.
+    if ((obmcSeverity != LogSeverity::Critical) &&
+        (pelSeverity == SeverityType::critical))
+    {
+        return LogSeverity::Critical;
+    }
+
+    return std::nullopt;
+}
+
 } // namespace pels
 } // namespace openpower