PEL: Add SRC callouts from message registry

When an SRC is being built for a new PEL, it will now also add callouts
from the message registry JSON for that error.

This commit only adds support for callouts of maintenance procedures or
symbolic FRUs.  A future commit will add support for calling out a
regular piece of hardware

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I78c0d3440c748f21145d452742e3aa682f26003b
diff --git a/extensions/openpower-pels/data_interface.hpp b/extensions/openpower-pels/data_interface.hpp
index 5b6ff94..6a3e326 100644
--- a/extensions/openpower-pels/data_interface.hpp
+++ b/extensions/openpower-pels/data_interface.hpp
@@ -226,6 +226,18 @@
                                     std::string& ccin,
                                     std::string& serialNumber) const = 0;
 
+    /**
+     * @brief Gets the system type from Entity Manager
+     *
+     * @param[in] std::string - The system type string
+     */
+    virtual std::string getSystemType() const
+    {
+        // TODO, not implemented by entity manager yet, but adding now
+        // so it can be mocked.
+        return _systemType;
+    }
+
   protected:
     /**
      * @brief Sets the host on/off state and runs any
@@ -326,6 +338,11 @@
      * @brief The motherboard CCIN
      */
     std::string _motherboardCCIN;
+
+    /**
+     * @brief The system type
+     */
+    std::string _systemType;
 };
 
 /**
diff --git a/extensions/openpower-pels/src.cpp b/extensions/openpower-pels/src.cpp
index 5310947..0899417 100644
--- a/extensions/openpower-pels/src.cpp
+++ b/extensions/openpower-pels/src.cpp
@@ -120,7 +120,7 @@
 
     _asciiString = std::make_unique<src::AsciiString>(regEntry);
 
-    addCallouts(additionalData, dataIface);
+    addCallouts(regEntry, additionalData, dataIface);
 
     _size = baseSRCSize;
     _size += _callouts ? _callouts->flattenedSize() : 0;
@@ -502,7 +502,8 @@
     return ps;
 }
 
-void SRC::addCallouts(const AdditionalData& additionalData,
+void SRC::addCallouts(const message::Entry& regEntry,
+                      const AdditionalData& additionalData,
                       const DataInterfaceBase& dataIface)
 {
     auto item = additionalData.getValue("CALLOUT_INVENTORY_PATH");
@@ -512,6 +513,11 @@
     }
 
     // TODO: CALLOUT_DEVICE_PATH
+
+    if (regEntry.callouts)
+    {
+        addRegistryCallouts(regEntry, additionalData, dataIface);
+    }
 }
 
 void SRC::addInventoryCallout(const std::string& inventoryPath,
@@ -543,8 +549,79 @@
     }
 
     _callouts->addCallout(std::move(callout));
+}
 
-} // namespace pels
+void SRC::addRegistryCallouts(const message::Entry& regEntry,
+                              const AdditionalData& additionalData,
+                              const DataInterfaceBase& dataIface)
+{
+    try
+    {
+        auto systemType = dataIface.getSystemType();
+
+        auto regCallouts = message::Registry::getCallouts(
+            regEntry.callouts.value(), systemType, additionalData);
+
+        for (const auto& regCallout : regCallouts)
+        {
+            addRegistryCallout(regCallout, dataIface);
+        }
+    }
+    catch (std::exception& e)
+    {
+        log<level::ERR>("Error parsing PEL message registry callout JSON",
+                        entry("ERROR=%s", e.what()));
+    }
+}
+
+void SRC::addRegistryCallout(const message::RegistryCallout& regCallout,
+                             const DataInterfaceBase& dataIface)
+{
+    std::unique_ptr<src::Callout> callout;
+
+    // TODO: expand this location code.
+    auto locCode = regCallout.locCode;
+
+    // Via the PEL values table, get the priority enum.
+    // The schema will have validated the priority was a valid value.
+    auto priorityIt =
+        pv::findByName(regCallout.priority, pv::calloutPriorityValues);
+    assert(priorityIt != pv::calloutPriorityValues.end());
+    auto priority =
+        static_cast<CalloutPriority>(std::get<pv::fieldValuePos>(*priorityIt));
+
+    if (!regCallout.procedure.empty())
+    {
+        // Procedure callout
+        callout =
+            std::make_unique<src::Callout>(priority, regCallout.procedure);
+    }
+    else if (!regCallout.symbolicFRU.empty())
+    {
+        // Symbolic FRU callout
+        callout = std::make_unique<src::Callout>(
+            priority, regCallout.symbolicFRU, locCode, false);
+    }
+    else if (!regCallout.symbolicFRUTrusted.empty())
+    {
+        // Symbolic FRU with trusted location code callout
+
+        // The registry wants it to be trusted, but that requires a valid
+        // location code for it to actually be.
+        callout = std::make_unique<src::Callout>(
+            priority, regCallout.symbolicFRUTrusted, locCode, !locCode.empty());
+    }
+    else
+    {
+        // TODO: HW callouts
+    }
+
+    if (callout)
+    {
+        createCalloutsObject();
+        _callouts->addCallout(std::move(callout));
+    }
+}
 
 } // namespace pels
 } // namespace openpower
diff --git a/extensions/openpower-pels/src.hpp b/extensions/openpower-pels/src.hpp
index a9fecf8..52a973c 100644
--- a/extensions/openpower-pels/src.hpp
+++ b/extensions/openpower-pels/src.hpp
@@ -345,10 +345,12 @@
      * The callout sources are the AdditionalData event log property
      * and the message registry JSON.
      *
-     * @param[in] additionalData - the AdditionalData values
+     * @param[in] regEntry - The message registry entry for the error
+     * @param[in] additionalData - The AdditionalData values
      * @param[in] dataIface - The DataInterface object
      */
-    void addCallouts(const AdditionalData& additionalData,
+    void addCallouts(const message::Entry& regEntry,
+                     const AdditionalData& additionalData,
                      const DataInterfaceBase& dataIface);
 
     /**
@@ -361,6 +363,26 @@
                              const DataInterfaceBase& dataIface);
 
     /**
+     * @brief Adds FRU callouts based on the registry entry JSON
+     *       for this error.
+     * @param[in] regEntry - The message registry entry for the error
+     * @param[in] additionalData - The AdditionalData values
+     * @param[in] dataIface - The DataInterface object
+     */
+    void addRegistryCallouts(const message::Entry& regEntry,
+                             const AdditionalData& additionalData,
+                             const DataInterfaceBase& dataIface);
+
+    /**
+     * @brief Adds a single FRU callout from the message registry.
+     *
+     * @param[in] callout - The registry callout structure
+     * @param[in] dataIface - The DataInterface object
+     */
+    void addRegistryCallout(const message::RegistryCallout& callout,
+                            const DataInterfaceBase& dataIface);
+
+    /**
      * @brief Creates the Callouts object _callouts
      *        so that callouts can be added to it.
      */