Build FRU table lazily

The FRU table is created when the PLDM daemon starts and depends on
BMC inventory collection to populate it completely. There is no D-Bus
signal or target that PLDM daemon can rely on to figure completion of
inventory. This can cause some of the inventory to not show up in the
FRU table if the inventory collection is not complete when the PLDM
daemon starts.

An easy solution to this problem is to do the FRU table creation
lazily. The FRU table will be created when the FRU commands or Get PDR
command is handled the first time. The entity association PDR's are
created when the FRU table is built. So Get PDR commands expects the
building of the FRU table as a pre condition.

Tested:

Ensured FRU table is created when the GetFRURecordTableMetadata,
GetFRURecordTable and GetPDR command is handled by the PLDM daemon the
first time. Next time already built table is returned for the FRU
commands.

Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
Change-Id: I0deb723f30a30a667d0e80c9f9f6aced5ab23a67
diff --git a/libpldmresponder/fru.hpp b/libpldmresponder/fru.hpp
index e23cd48..2597dc4 100644
--- a/libpldmresponder/fru.hpp
+++ b/libpldmresponder/fru.hpp
@@ -49,15 +49,19 @@
         sizeof(struct pldm_fru_record_data_format) -
         sizeof(struct pldm_fru_record_tlv);
 
-    /** @brief The FRU table is populated by processing the D-Bus inventory
-     *         namespace, based on the config files for FRU. The configPath is
-     *         consumed to build the FruParser object.
+    /** @brief Constructor for FruImpl, the configPath is consumed to build the
+     *         FruParser object.
      *
      *  @param[in] configPath - path to the directory containing config files
-     * for PLDM FRU
+     *                          for PLDM FRU
+     *  @param[in] pdrRepo - opaque pointer to PDR repository
+     *  @param[in] entityTree - opaque pointer to the entity association tree
      */
     FruImpl(const std::string& configPath, pldm_pdr* pdrRepo,
-            pldm_entity_association_tree* entityTree);
+            pldm_entity_association_tree* entityTree) :
+        parser(configPath),
+        pdrRepo(pdrRepo), entityTree(entityTree)
+    {}
 
     /** @brief Total length of the FRU table in bytes, this excludes the pad
      *         bytes and the checksum.
@@ -102,6 +106,12 @@
      */
     void getFRUTable(Response& response);
 
+    /** @brief FRU table is built by processing the D-Bus inventory namespace
+     *         based on the config files for FRU. The table is populated based
+     *         on the isBuilt flag.
+     */
+    void buildFRUTable();
+
   private:
     uint16_t nextRSI()
     {
@@ -113,7 +123,9 @@
     uint8_t padBytes = 0;
     std::vector<uint8_t> table;
     uint32_t checksum = 0;
+    bool isBuilt = false;
 
+    fru_parser::FruParser parser;
     pldm_pdr* pdrRepo;
     pldm_entity_association_tree* entityTree;
 
@@ -175,6 +187,14 @@
      */
     Response getFRURecordTable(const pldm_msg* request, size_t payloadLength);
 
+    /** @brief Build FRU table is bnot already built
+     *
+     */
+    void buildFRUTable()
+    {
+        impl.buildFRUTable();
+    }
+
   private:
     FruImpl impl;
 };