Implement VPD recollection

This commit implements functionality to recollect VPD for a hardware
by triggering VPD parser for that hardware in case there is a
replacement.

Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
Change-Id: Ia0d377b554299faac3b46a4dc8dd96f964f07bd2
diff --git a/vpd-manager/server.cpp b/vpd-manager/server.cpp
index 8291ecd..7ca72f6 100644
--- a/vpd-manager/server.cpp
+++ b/vpd-manager/server.cpp
@@ -4,10 +4,10 @@
 #include <map>
 #include <sdbusplus/exception.hpp>
 #include <sdbusplus/sdbus.hpp>
+#include <sdbusplus/sdbuspp_support/server.hpp>
 #include <sdbusplus/server.hpp>
 #include <string>
 #include <tuple>
-#include <variant>
 #include <xyz/openbmc_project/Common/error.hpp>
 
 namespace sdbusplus
@@ -30,55 +30,36 @@
 int Manager::_callback_WriteKeyword(sd_bus_message* msg, void* context,
                                     sd_bus_error* error)
 {
+    auto o = static_cast<Manager*>(context);
+
     try
     {
-        auto m = message::message(msg);
-#if 1
-        {
-            auto tbus = m.get_bus();
-            sdbusplus::server::transaction::Transaction t(tbus, m);
-            sdbusplus::server::transaction::set_id(
-                std::hash<sdbusplus::server::transaction::Transaction>{}(t));
-        }
-#endif
-
-        sdbusplus::message::object_path path{};
-        std::string record{};
-        std::string keyword{};
-        std::vector<uint8_t> value{};
-
-        m.read(path, record, keyword, value);
-
-        auto o = static_cast<Manager*>(context);
-        o->writeKeyword(path, record, keyword, value);
-
-        auto reply = m.new_method_return();
-        // No data to append on reply.
-
-        reply.method_return();
-    }
-    catch (sdbusplus::internal_exception_t& e)
-    {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return sdbusplus::sdbuspp::method_callback(
+            msg, o->_intf, error,
+            std::function([=](sdbusplus::message::object_path&& path,
+                              std::string&& record, std::string&& keyword,
+                              std::vector<uint8_t>&& value) {
+                return o->writeKeyword(path, record, keyword, value);
+            }));
     }
     catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::PathNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::RecordNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::KeywordNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
 
-    return true;
+    return 0;
 }
 
 namespace details
@@ -97,49 +78,32 @@
                                                        void* context,
                                                        sd_bus_error* error)
 {
+    auto o = static_cast<Manager*>(context);
+
     try
     {
-        auto m = message::message(msg);
-#if 1
-        {
-            auto tbus = m.get_bus();
-            sdbusplus::server::transaction::Transaction t(tbus, m);
-            sdbusplus::server::transaction::set_id(
-                std::hash<sdbusplus::server::transaction::Transaction>{}(t));
-        }
-#endif
-
-        std::string locationCode{};
-        uint16_t nodeNumber{};
-
-        m.read(locationCode, nodeNumber);
-
-        auto o = static_cast<Manager*>(context);
-        auto r = o->getFRUsByUnexpandedLocationCode(locationCode, nodeNumber);
-
-        auto reply = m.new_method_return();
-        reply.append(std::move(r));
-
-        reply.method_return();
-    }
-    catch (sdbusplus::internal_exception_t& e)
-    {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return sdbusplus::sdbuspp::method_callback(
+            msg, o->_intf, error,
+            std::function(
+                [=](std::string&& locationCode, uint16_t&& nodeNumber) {
+                    return o->getFRUsByUnexpandedLocationCode(locationCode,
+                                                              nodeNumber);
+                }));
     }
     catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::LocationNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::NodeNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
 
-    return true;
+    return 0;
 }
 
 namespace details
@@ -158,48 +122,30 @@
                                                      void* context,
                                                      sd_bus_error* error)
 {
+    auto o = static_cast<Manager*>(context);
+
     try
     {
-        auto m = message::message(msg);
-#if 1
-        {
-            auto tbus = m.get_bus();
-            sdbusplus::server::transaction::Transaction t(tbus, m);
-            sdbusplus::server::transaction::set_id(
-                std::hash<sdbusplus::server::transaction::Transaction>{}(t));
-        }
-#endif
-
-        std::string locationCode{};
-
-        m.read(locationCode);
-
-        auto o = static_cast<Manager*>(context);
-        auto r = o->getFRUsByExpandedLocationCode(locationCode);
-
-        auto reply = m.new_method_return();
-        reply.append(std::move(r));
-
-        reply.method_return();
-    }
-    catch (sdbusplus::internal_exception_t& e)
-    {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return sdbusplus::sdbuspp::method_callback(
+            msg, o->_intf, error,
+            std::function([=](std::string&& locationCode) {
+                return o->getFRUsByExpandedLocationCode(locationCode);
+            }));
     }
     catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::LocationNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::NodeNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
 
-    return true;
+    return 0;
 }
 
 namespace details
@@ -218,49 +164,31 @@
                                                void* context,
                                                sd_bus_error* error)
 {
+    auto o = static_cast<Manager*>(context);
+
     try
     {
-        auto m = message::message(msg);
-#if 1
-        {
-            auto tbus = m.get_bus();
-            sdbusplus::server::transaction::Transaction t(tbus, m);
-            sdbusplus::server::transaction::set_id(
-                std::hash<sdbusplus::server::transaction::Transaction>{}(t));
-        }
-#endif
-
-        std::string locationCode{};
-        uint16_t nodeNumber{};
-
-        m.read(locationCode, nodeNumber);
-
-        auto o = static_cast<Manager*>(context);
-        auto r = o->getExpandedLocationCode(locationCode, nodeNumber);
-
-        auto reply = m.new_method_return();
-        reply.append(std::move(r));
-
-        reply.method_return();
-    }
-    catch (sdbusplus::internal_exception_t& e)
-    {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return sdbusplus::sdbuspp::method_callback(
+            msg, o->_intf, error,
+            std::function(
+                [=](std::string&& locationCode, uint16_t&& nodeNumber) {
+                    return o->getExpandedLocationCode(locationCode, nodeNumber);
+                }));
     }
     catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::LocationNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
     catch (sdbusplus::com::ibm::VPD::Error::NodeNotFound& e)
     {
-        return sd_bus_error_set(error, e.name(), e.description());
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
     }
 
-    return true;
+    return 0;
 }
 
 namespace details
@@ -274,6 +202,37 @@
 } // namespace Manager
 } // namespace details
 
+int Manager::_callback_PerformVPDRecollection(sd_bus_message* msg,
+                                              void* context,
+                                              sd_bus_error* error)
+{
+    auto o = static_cast<Manager*>(context);
+
+    try
+    {
+        return sdbusplus::sdbuspp::method_callback(
+            msg, o->_intf, error,
+            std::function([=]() { return o->performVPDRecollection(); }));
+    }
+    catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e)
+    {
+        return o->_intf->sd_bus_error_set(error, e.name(), e.description());
+    }
+
+    return 0;
+}
+
+namespace details
+{
+namespace Manager
+{
+static const auto _param_PerformVPDRecollection =
+    utility::tuple_to_array(std::make_tuple('\0'));
+static const auto _return_PerformVPDRecollection =
+    utility::tuple_to_array(std::make_tuple('\0'));
+} // namespace Manager
+} // namespace details
+
 const vtable::vtable_t Manager::_vtable[] = {
     vtable::start(),
 
@@ -297,6 +256,11 @@
                    details::Manager::_param_GetExpandedLocationCode.data(),
                    details::Manager::_return_GetExpandedLocationCode.data(),
                    _callback_GetExpandedLocationCode),
+
+    vtable::method("PerformVPDRecollection",
+                   details::Manager::_param_PerformVPDRecollection.data(),
+                   details::Manager::_return_PerformVPDRecollection.data(),
+                   _callback_PerformVPDRecollection),
     vtable::end()};
 
 } // namespace server