Add Json Config support for entity to DBus string
The entity map comprises both entity types and their corresponding
names, essential for generating unique D-Bus paths for each entity.
However, challenges arise due to variations in path generation across
companies, often dictated by differing entity types. To address this,
enabling configurability of EntityID becomes imperative, empowering
communities to tailor configurations to their specific use cases.
Testing:
Unit tests passed
Change-Id: I3981fde64bd00940caa5067a9472fd948e74cbf0
Signed-off-by: Kamalkumar Patel <kamalkumar.patel@ibm.com>
diff --git a/host-bmc/host_pdr_handler.cpp b/host-bmc/host_pdr_handler.cpp
index 71e3812..13d83a1 100644
--- a/host-bmc/host_pdr_handler.cpp
+++ b/host-bmc/host_pdr_handler.cpp
@@ -26,6 +26,7 @@
using namespace pldm::utils;
using namespace sdbusplus::bus::match::rules;
using namespace pldm::responder::pdr_utils;
+using namespace pldm::hostbmc::utils;
using Json = nlohmann::json;
namespace fs = std::filesystem;
using namespace pldm::dbus;
@@ -95,7 +96,8 @@
mctp_eid(mctp_eid), event(event), repo(repo),
stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
bmcEntityTree(bmcEntityTree), instanceIdDb(instanceIdDb), handler(handler),
- oemPlatformHandler(oemPlatformHandler)
+ oemPlatformHandler(oemPlatformHandler),
+ entityMaps(parseEntityMap(ENTITY_MAP_JSON))
{
mergedHostParents = false;
hostOffMatch = std::make_unique<sdbusplus::bus::match_t>(
@@ -628,8 +630,8 @@
}
if (!nextRecordHandle)
{
- pldm::hostbmc::utils::updateEntityAssociation(entityAssociations,
- entityTree, objPathMap);
+ updateEntityAssociation(entityAssociations, entityTree, objPathMap,
+ entityMaps);
/*received last record*/
this->parseStateSensorPDRs(stateSensorPDRs);
diff --git a/host-bmc/host_pdr_handler.hpp b/host-bmc/host_pdr_handler.hpp
index 053cfef..0755a25 100644
--- a/host-bmc/host_pdr_handler.hpp
+++ b/host-bmc/host_pdr_handler.hpp
@@ -327,6 +327,10 @@
/** @OEM platform handler */
pldm::responder::oem_platform::Handler* oemPlatformHandler;
+
+ /** @brief entityID and entity name is only loaded once
+ */
+ EntityMaps entityMaps;
};
} // namespace pldm
diff --git a/host-bmc/test/entitymap_test.json b/host-bmc/test/entitymap_test.json
new file mode 100644
index 0000000..7dc08f2
--- /dev/null
+++ b/host-bmc/test/entitymap_test.json
@@ -0,0 +1,19 @@
+{
+ "Description": {
+ "_comment": [
+ "This file serves as a crucial tool for testing JSON parsing capabilities, facilitating the population of the Entitymap by harnessing its data."
+ ]
+ },
+ "EntityTypeToDbusStringMap": {
+ "45": "chassis",
+ "60": "io_board",
+ "63": "system_management_module",
+ "64": "motherboard",
+ "67": "dcm",
+ "80": "io_module",
+ "135": "cpu",
+ "190": "socket",
+ "32813": "system",
+ "32903": "core"
+ }
+}
diff --git a/host-bmc/test/meson.build b/host-bmc/test/meson.build
index 66d75b7..fd3e8c9 100644
--- a/host-bmc/test/meson.build
+++ b/host-bmc/test/meson.build
@@ -4,6 +4,7 @@
test_sources = [
'../../common/utils.cpp',
+ '../utils.cpp',
'../custom_dbus.cpp',
'../utils.cpp'
]
diff --git a/host-bmc/test/utils_test.cpp b/host-bmc/test/utils_test.cpp
index 701f4cb..373274f 100644
--- a/host-bmc/test/utils_test.cpp
+++ b/host-bmc/test/utils_test.cpp
@@ -1,4 +1,5 @@
#include "../utils.hpp"
+#include "common/utils.hpp"
#include <libpldm/pdr.h>
@@ -9,6 +10,13 @@
namespace fs = std::filesystem;
using namespace pldm;
using namespace pldm::utils;
+using namespace pldm::hostbmc::utils;
+
+TEST(EntityAssociation, parseEntityMap)
+{
+ EntityMaps entityMaps = parseEntityMap("./entitymap_test.json");
+ EXPECT_EQ(entityMaps.size(), 10);
+}
TEST(EntityAssociation, addObjectPathEntityAssociations1)
{
@@ -80,8 +88,8 @@
l5b}};
ObjectPathMaps objPathMap;
- pldm::hostbmc::utils::updateEntityAssociation(entityAssociations, tree,
- objPathMap);
+ EntityMaps entityMaps = parseEntityMap("./entitymap_test.json");
+ updateEntityAssociation(entityAssociations, tree, objPathMap, entityMaps);
EXPECT_EQ(objPathMap.size(), retObjectMaps.size());
diff --git a/host-bmc/utils.cpp b/host-bmc/utils.cpp
index f025ada..a178d48 100644
--- a/host-bmc/utils.cpp
+++ b/host-bmc/utils.cpp
@@ -4,6 +4,7 @@
#include "utils.hpp"
+#include <cstdlib>
#include <iostream>
namespace pldm
@@ -58,7 +59,8 @@
void addObjectPathEntityAssociations(const EntityAssociations& entityAssoc,
pldm_entity_node* entity,
const fs::path& path,
- ObjectPathMaps& objPathMap)
+ ObjectPathMaps& objPathMap,
+ EntityMaps entityMaps)
{
if (entity == nullptr)
{
@@ -120,7 +122,7 @@
for (size_t i = 1; i < ev.size(); i++)
{
addObjectPathEntityAssociations(entityAssoc, ev[i], p,
- objPathMap);
+ objPathMap, entityMaps);
}
found = true;
}
@@ -131,7 +133,6 @@
std::string dbusPath =
path / fs::path{entityName +
std::to_string(node_entity.entity_instance_num)};
-
try
{
pldm::utils::DBusHandler().getService(dbusPath.c_str(), nullptr);
@@ -145,7 +146,7 @@
void updateEntityAssociation(const EntityAssociations& entityAssoc,
pldm_entity_association_tree* entityTree,
- ObjectPathMaps& objPathMap)
+ ObjectPathMaps& objPathMap, EntityMaps entityMaps)
{
std::vector<pldm_entity_node*> parentsEntity =
getParentEntites(entityAssoc);
@@ -200,9 +201,45 @@
paths.pop_back();
}
- addObjectPathEntityAssociations(entityAssoc, entity, path, objPathMap);
+ addObjectPathEntityAssociations(entityAssoc, entity, path, objPathMap,
+ entityMaps);
}
}
+
+EntityMaps parseEntityMap(const fs::path& filePath)
+{
+ const Json emptyJson{};
+ EntityMaps entityMaps{};
+ std::ifstream jsonFile(filePath);
+ auto data = Json::parse(jsonFile);
+ if (data.is_discarded())
+ {
+ error("Failed parsing of EntityMap data from json file: '{JSON_PATH}'",
+ "JSON_PATH", filePath);
+ return entityMaps;
+ }
+ auto entities = data.value("EntityTypeToDbusStringMap", emptyJson);
+ char* err;
+ try
+ {
+ std::ranges::transform(entities.items(),
+ std::inserter(entityMaps, entityMaps.begin()),
+ [&err](const auto& element) {
+ std::string key = static_cast<EntityName>(element.key());
+ return std::make_pair(strtol(key.c_str(), &err, 10),
+ static_cast<EntityName>(element.value()));
+ });
+ }
+ catch (const std::exception& e)
+ {
+ error(
+ "Failed to create entity to DBus string mapping {ERROR} and Conversion failure is '{ERR}'",
+ "ERROR", e, "ERR", err);
+ }
+
+ return entityMaps;
+}
+
} // namespace utils
} // namespace hostbmc
} // namespace pldm
diff --git a/host-bmc/utils.hpp b/host-bmc/utils.hpp
index 857d6c7..2b0e2a7 100644
--- a/host-bmc/utils.hpp
+++ b/host-bmc/utils.hpp
@@ -14,33 +14,20 @@
PHOSPHOR_LOG2_USING;
namespace pldm
{
+using Json = nlohmann::json;
+namespace fs = std::filesystem;
+using ObjectPath = fs::path;
using EntityName = std::string;
using EntityType = uint16_t;
-
using Entities = std::vector<pldm_entity_node*>;
using EntityAssociations = std::vector<Entities>;
-using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
+using ObjectPathMaps = std::map<ObjectPath, pldm_entity_node*>;
+using EntityMaps = std::map<EntityType, EntityName>;
namespace hostbmc
{
namespace utils
{
-const std::map<EntityType, EntityName> entityMaps = {
- {PLDM_ENTITY_SYSTEM_CHASSIS, "chassis"},
- {PLDM_ENTITY_BOARD, "io_board"},
- {PLDM_ENTITY_SYS_BOARD, "motherboard"},
- {PLDM_ENTITY_POWER_SUPPLY, "powersupply"},
- {PLDM_ENTITY_PROC, "cpu"},
- {PLDM_ENTITY_SYSTEM_CHASSIS | 0x8000, "system"},
- {PLDM_ENTITY_PROC_MODULE, "dcm"},
- {PLDM_ENTITY_PROC | 0x8000, "core"},
- {PLDM_ENTITY_IO_MODULE, "io_module"},
- {PLDM_ENTITY_FAN, "fan"},
- {PLDM_ENTITY_SYS_MGMT_MODULE, "system_management_module"},
- {PLDM_ENTITY_POWER_CONVERTER, "power_converter"},
- {PLDM_ENTITY_SLOT, "slot"},
- {PLDM_ENTITY_CONNECTOR, "connector"}};
-
/** @brief Vector a entity name to pldm_entity from entity association tree
* @param[in] entityAssoc - Vector of associated pldm entities
* @param[in] entityTree - entity association tree
@@ -50,7 +37,15 @@
*/
void updateEntityAssociation(const EntityAssociations& entityAssoc,
pldm_entity_association_tree* entityTree,
- ObjectPathMaps& objPathMap);
+ ObjectPathMaps& objPathMap, EntityMaps entityMaps);
+
+/** @brief Parsing entity to DBus string mapping from json file
+ *
+ * @param[in] filePath - JSON file path for parsing
+ *
+ * @return returns the entity to DBus string mapping object
+ */
+EntityMaps parseEntityMap(const fs::path& filePath);
} // namespace utils
} // namespace hostbmc