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.cpp b/libpldmresponder/fru.cpp
index a9c8787..ce470ab 100644
--- a/libpldmresponder/fru.cpp
+++ b/libpldmresponder/fru.cpp
@@ -17,12 +17,13 @@
 namespace responder
 {
 
-FruImpl::FruImpl(const std::string& configPath, pldm_pdr* pdrRepo,
-                 pldm_entity_association_tree* entityTree) :
-    pdrRepo(pdrRepo),
-    entityTree(entityTree)
+void FruImpl::buildFRUTable()
 {
-    fru_parser::FruParser handle(configPath);
+
+    if (isBuilt)
+    {
+        return;
+    }
 
     fru_parser::DBusLookupInfo dbusInfo;
     // Read the all the inventory D-Bus objects
@@ -31,7 +32,7 @@
 
     try
     {
-        dbusInfo = handle.inventoryLookup();
+        dbusInfo = parser.inventoryLookup();
         auto method = bus.new_method_call(
             std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(),
             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
@@ -61,7 +62,7 @@
                 try
                 {
                     pldm_entity entity{};
-                    entity.entity_type = handle.getEntityType(interface.first);
+                    entity.entity_type = parser.getEntityType(interface.first);
                     pldm_entity_node* parent = nullptr;
                     auto parentObj = pldm::utils::findParent(object.first.str);
                     // To add a FRU to the entity association tree, we need to
@@ -90,7 +91,7 @@
                         PLDM_ENTITY_ASSOCIAION_PHYSICAL);
                     objToEntityNode[object.first.str] = node;
 
-                    auto recordInfos = handle.getRecordInfo(interface.first);
+                    auto recordInfos = parser.getRecordInfo(interface.first);
                     populateRecords(interfaces, recordInfos, entity);
                     break;
                 }
@@ -115,6 +116,7 @@
         // Calculate the checksum
         checksum = crc32(table.data(), table.size());
     }
+    isBuilt = true;
 }
 
 void FruImpl::populateRecords(
@@ -208,6 +210,9 @@
 Response Handler::getFRURecordTableMetadata(const pldm_msg* request,
                                             size_t /*payloadLength*/)
 {
+    // FRU table is built lazily, build if not done.
+    buildFRUTable();
+
     constexpr uint8_t major = 0x01;
     constexpr uint8_t minor = 0x00;
     constexpr uint32_t maxSize = 0xFFFFFFFF;
@@ -232,6 +237,9 @@
 Response Handler::getFRURecordTable(const pldm_msg* request,
                                     size_t payloadLength)
 {
+    // FRU table is built lazily, build if not done.
+    buildFRUTable();
+
     if (payloadLength != PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES)
     {
         return ccOnlyResponse(request, PLDM_ERROR_INVALID_LENGTH);