libpldmresponder: create PDR when the D-Bus object path exists

On a system where this D-Bus object path does not exist, it should
not create a PDR when the D-Bus object specified here is not found.

Tested: In the PDR config files, the PDR is successfully created when
the objectPath of the D-Bus exists, otherwise, skipped the current
config and the PDR is not created.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ib59fa79cc28d5712161bfb971c37c86c363bafb0
diff --git a/libpldmresponder/pdr_numeric_effecter.hpp b/libpldmresponder/pdr_numeric_effecter.hpp
index 1aa525b..402a426 100644
--- a/libpldmresponder/pdr_numeric_effecter.hpp
+++ b/libpldmresponder/pdr_numeric_effecter.hpp
@@ -24,8 +24,9 @@
  *  @param[out] repo - pdr::RepoInterface
  *
  */
-template <class Handler>
-void generateNumericEffecterPDR(const Json& json, Handler& handler,
+template <class DBusInterface, class Handler>
+void generateNumericEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
+                                Handler& handler,
                                 pdr_utils::RepoInterface& repo)
 {
     static const std::vector<Json> emptyList{};
@@ -164,6 +165,17 @@
         auto interface = dbusEntry.value("interface", "");
         auto propertyName = dbusEntry.value("property_name", "");
         auto propertyType = dbusEntry.value("property_type", "");
+
+        try
+        {
+            auto service =
+                dBusIntf.getService(objectPath.c_str(), interface.c_str());
+        }
+        catch (const std::exception& e)
+        {
+            continue;
+        }
+
         pldm::utils::DBusMapping dbusMapping{objectPath, interface,
                                              propertyName, propertyType};
         DbusMappings dbusMappings{};
diff --git a/libpldmresponder/pdr_state_effecter.hpp b/libpldmresponder/pdr_state_effecter.hpp
index 5b49a46..1391def 100644
--- a/libpldmresponder/pdr_state_effecter.hpp
+++ b/libpldmresponder/pdr_state_effecter.hpp
@@ -24,9 +24,9 @@
  *  @param[out] repo - pdr::RepoInterface
  *
  */
-template <class Handler>
-void generateStateEffecterPDR(const Json& json, Handler& handler,
-                              pdr_utils::RepoInterface& repo)
+template <class DBusInterface, class Handler>
+void generateStateEffecterPDR(const DBusInterface& dBusIntf, const Json& json,
+                              Handler& handler, pdr_utils::RepoInterface& repo)
 {
     static const std::vector<Json> emptyList{};
     auto entries = json.value("entries", emptyList);
@@ -104,6 +104,17 @@
             auto interface = dbusEntry.value("interface", "");
             auto propertyName = dbusEntry.value("property_name", "");
             auto propertyType = dbusEntry.value("property_type", "");
+
+            try
+            {
+                auto service =
+                    dBusIntf.getService(objectPath.c_str(), interface.c_str());
+            }
+            catch (const std::exception& e)
+            {
+                continue;
+            }
+
             pldm::utils::DBusMapping dbusMapping{objectPath, interface,
                                                  propertyName, propertyType};
             dbusMappings.emplace_back(std::move(dbusMapping));
diff --git a/libpldmresponder/platform.cpp b/libpldmresponder/platform.cpp
index 88ddcf8..affe52e 100644
--- a/libpldmresponder/platform.cpp
+++ b/libpldmresponder/platform.cpp
@@ -65,7 +65,8 @@
     return dbusObjMaps.at(effecterId);
 }
 
-void Handler::generate(const std::string& dir, Repo& repo)
+void Handler::generate(const pldm::utils::DBusHandler& dBusIntf,
+                       const std::string& dir, Repo& repo)
 {
     if (!fs::exists(dir))
     {
@@ -79,14 +80,18 @@
 
     const std::map<Type, generatePDR> generateHandlers = {
         {PLDM_STATE_EFFECTER_PDR,
-         [this](const auto& json, RepoInterface& repo) {
-             pdr_state_effecter::generateStateEffecterPDR<Handler>(json, *this,
-                                                                   repo);
+         [this](const DBusHandler& dBusIntf, const auto& json,
+                RepoInterface& repo) {
+             pdr_state_effecter::generateStateEffecterPDR<
+                 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
+                                                    repo);
          }},
         {PLDM_NUMERIC_EFFECTER_PDR,
-         [this](const auto& json, RepoInterface& repo) {
-             pdr_numeric_effecter::generateNumericEffecterPDR<Handler>(
-                 json, *this, repo);
+         [this](const DBusHandler& dBusIntf, const auto& json,
+                RepoInterface& repo) {
+             pdr_numeric_effecter::generateNumericEffecterPDR<
+                 pldm::utils::DBusHandler, Handler>(dBusIntf, json, *this,
+                                                    repo);
          }}};
 
     Type pdrType{};
@@ -101,7 +106,7 @@
                 for (const auto& effecter : effecterPDRs)
                 {
                     pdrType = effecter.value("pdrType", 0);
-                    generateHandlers.at(pdrType)(effecter, repo);
+                    generateHandlers.at(pdrType)(dBusIntf, effecter, repo);
                 }
             }
         }
diff --git a/libpldmresponder/platform.hpp b/libpldmresponder/platform.hpp
index ad635bd..c79a2ed 100644
--- a/libpldmresponder/platform.hpp
+++ b/libpldmresponder/platform.hpp
@@ -28,7 +28,8 @@
 using namespace pldm::responder::pdr_utils;
 
 using generatePDR =
-    std::function<void(const Json& json, pdr_utils::RepoInterface& repo)>;
+    std::function<void(const pldm::utils::DBusHandler& dBusIntf,
+                       const Json& json, pdr_utils::RepoInterface& repo)>;
 
 using EffecterId = uint16_t;
 using DbusObjMaps =
@@ -55,7 +56,8 @@
 class Handler : public CmdHandler
 {
   public:
-    Handler(const std::string& pdrJsonsDir, const std::string& eventsJsonsDir,
+    Handler(const pldm::utils::DBusHandler& dBusIntf,
+            const std::string& pdrJsonsDir, const std::string& eventsJsonsDir,
             pldm_pdr* repo, HostPDRHandler* hostPDRHandler,
             fru::Handler* fruHandler,
             const std::optional<EventMap>& addOnHandlersMap = std::nullopt) :
@@ -63,7 +65,7 @@
         hostPDRHandler(hostPDRHandler), stateSensorHandler(eventsJsonsDir),
         fruHandler(fruHandler)
     {
-        generate(pdrJsonsDir, pdrRepo);
+        generate(dBusIntf, pdrJsonsDir, pdrRepo);
 
         handlers.emplace(PLDM_GET_PDR,
                          [this](const pldm_msg* request, size_t payloadLength) {
@@ -158,10 +160,12 @@
 
     /** @brief Parse PDR JSONs and build PDR repository
      *
+     *  @param[in] dBusIntf - The interface object
      *  @param[in] dir - directory housing platform specific PDR JSON files
      *  @param[in] repo - instance of concrete implementation of Repo
      */
-    void generate(const std::string& dir, Repo& repo);
+    void generate(const pldm::utils::DBusHandler& dBusIntf,
+                  const std::string& dir, Repo& repo);
 
     /** @brief Parse PDR JSONs and build state effecter PDR repository
      *