fw_update: Introduce FirmwareInventory

Spec reference: [1]

Description:
FirmwareInventory is a class that manages all of the D-Bus
utilities for the firmware update functionality of each entry,
FirmwareInventoryManager is used to manage multiple FirmwareInventory
instances.

Flow of the routine when one or more firmware device is added:
1. When one or more firmware device is added, the InventoryManager
   instance will try to create a FirmwareInventory entry for the added
   device, to accomplish this, it will need to fetch the necessary
   information from various source (e.g. EM, device descriptors).

2. After the information is gathered, the InventoryManager will try to
   obtain a Name for each firmware devices, to fit the different
   platform condition, the name will be reference by the order below:
    1. From Entity Manager (EM), if the endpoint has a specific EM
       configuration with "Name" property listed.
    2. From device descriptors, if the device descriptor contains a
       vendor defined descriptor with "OpenBMC.Name" titled.
    3. Spawn a default one, named "Firmware_Device_<Endpoint ID>".

3. The InventoryManager will then invoke
   `FirmwareInventoryManager::createFirmwareInventory` to create a
   firmware inventory entry.

Properties managed by firmware Inventory:
1. Version
   Version object that represents the firmware version
2. Association
   Association object that represents the associations for the
   firmware

The object path pattern of the firmware inventory entry is:
`/xyz/openbmc_project/software/<BoardName>_<SoftwareName>_<SoftwareID>`

Where:
- `<BoardName>` represents the board the device is on.
- `<SoftwareName>` is the name of the firmware device obtained from the
  InventoryManager.
- `<SoftwareID>` is a 4-byte random number to help consumer
  distinguish from the new/old objects of a inventory item.
  For example:
  `server_board_slot_1_VR_2603`
  The new Activation object and its related interfaces needs to
  resides on a new objectPath, hence the softwareId is appended to
  the path.

Test results:
 - Build passed
 - Successfully create firmware inventory entries
   on Yosemite V4

[1]: https://github.com/openbmc/docs/blob/master/designs/code-update.md

Change-Id: Idebd7d013c82c60f08309a1860d5de1deeb3829a
Signed-off-by: Unive Tien <unive.tien.wiwynn@gmail.com>
Signed-off-by: Carter Chen <carter.chen.wiwynn@gmail.com>
diff --git a/fw-update/inventory_manager.cpp b/fw-update/inventory_manager.cpp
index 38e8392..6ad9f39 100644
--- a/fw-update/inventory_manager.cpp
+++ b/fw-update/inventory_manager.cpp
@@ -15,10 +15,11 @@
 {
 namespace fw_update
 {
-void InventoryManager::discoverFDs(const std::vector<mctp_eid_t>& eids)
+void InventoryManager::discoverFDs(const MctpInfos& mctpInfos)
 {
-    for (const auto& eid : eids)
+    for (const auto& mctpInfo : mctpInfos)
     {
+        auto eid = std::get<pldm::eid>(mctpInfo);
         try
         {
             sendQueryDeviceIdentifiersRequest(eid);
@@ -32,6 +33,19 @@
     }
 }
 
+void InventoryManager::removeFDs(const MctpInfos& mctpInfos)
+{
+    for (const auto& mctpInfo : mctpInfos)
+    {
+        auto eid = std::get<pldm::eid>(mctpInfo);
+        firmwareDeviceNameMap.erase(eid);
+        descriptorMap.erase(eid);
+        downstreamDescriptorMap.erase(eid);
+        componentInfoMap.erase(eid);
+        firmwareInventoryManager.deleteFirmwareEntry(eid);
+    }
+}
+
 void InventoryManager::sendQueryDeviceIdentifiersRequest(mctp_eid_t eid)
 {
     auto instanceId = instanceIdDb.next(eid);
@@ -161,7 +175,8 @@
         deviceIdentifiersLen -= nextDescriptorOffset;
     }
 
-    descriptorMap.emplace(eid, std::move(descriptors));
+    obtainFirmwareDeviceName(eid, descriptors);
+    descriptorMap.insert_or_assign(eid, std::move(descriptors));
 
     // Send GetFirmwareParameters request
     sendGetFirmwareParametersRequest(eid);
@@ -544,6 +559,25 @@
     }
 }
 
+void InventoryManager::obtainFirmwareDeviceName(pldm::eid eid,
+                                                const Descriptors& descriptors)
+{
+    auto firmwareDeviceName =
+        obtainDeviceNameFromConfigurations(configurations, eid);
+
+    if (!firmwareDeviceName)
+    {
+        firmwareDeviceName = obtainDeviceNameFromDescriptors(descriptors);
+    }
+
+    if (!firmwareDeviceName)
+    {
+        firmwareDeviceName = std::format("Firmware_Device_{}", eid);
+    }
+
+    firmwareDeviceNameMap.insert_or_assign(eid, *firmwareDeviceName);
+}
+
 void InventoryManager::sendGetFirmwareParametersRequest(mctp_eid_t eid)
 {
     auto instanceId = instanceIdDb.next(eid);
@@ -642,7 +676,62 @@
         compParamTableLen -= sizeof(pldm_component_parameter_entry) +
                              activeCompVerStr.length + pendingCompVerStr.length;
     }
-    componentInfoMap.emplace(eid, std::move(componentInfo));
+
+    if (firmwareDeviceNameMap.contains(eid))
+    {
+        firmwareInventoryManager.createFirmwareEntry(
+            SoftwareIdentifier(eid, 0), firmwareDeviceNameMap.at(eid),
+            utils::toString(activeCompImageSetVerStr), descriptorMap[eid],
+            componentInfo);
+    }
+    else
+    {
+        error("Firmware device name not found for endpoint ID {EID}", "EID",
+              eid);
+    }
+
+    componentInfoMap.insert_or_assign(eid, std::move(componentInfo));
+}
+
+std::optional<SoftwareName> obtainDeviceNameFromConfigurations(
+    const Configurations& configurations, pldm::eid eid)
+{
+    for (const auto& [_, mctpInfo] : configurations)
+    {
+        if (std::get<pldm::eid>(mctpInfo) == eid)
+        {
+            auto nameOption = std::get<std::optional<std::string>>(mctpInfo);
+            if (nameOption)
+            {
+                return *nameOption;
+            }
+            break;
+        }
+    }
+    return std::nullopt;
+}
+
+std::optional<SoftwareName> obtainDeviceNameFromDescriptors(
+    const Descriptors& descriptors)
+{
+    for (const auto& [descriptorType, descriptorData] : descriptors)
+    {
+        if (descriptorType == PLDM_FWUP_VENDOR_DEFINED)
+        {
+            auto vendorInfo =
+                std::get<VendorDefinedDescriptorInfo>(descriptorData);
+            auto title = std::get<VendorDefinedDescriptorTitle>(vendorInfo);
+            if (title == "OpenBMC.Name")
+            {
+                auto deviceNameData =
+                    std::get<VendorDefinedDescriptorData>(vendorInfo);
+                return SoftwareName{
+                    reinterpret_cast<char*>(deviceNameData.data()),
+                    deviceNameData.size()};
+            }
+        }
+    }
+    return std::nullopt;
 }
 
 } // namespace fw_update