entity-manager: extract postBoardToDBus function
Extract postBoardToDBus from postToDbus function.
The function was really huge and not suitable for further development.
Extracting the loop body is a logical choice and already removes one
level of indentation.
What's been changed:
- extract the function
- change one 'continue' statement to a 'return' statement, preserving
the original control flow.
Tested: on Tyan S8030
Checked that `busctl tree` looks as before
```
`- /xyz
`- /xyz/openbmc_project
|- /xyz/openbmc_project/EntityManager
`- /xyz/openbmc_project/inventory
`- /xyz/openbmc_project/inventory/system
|- /xyz/openbmc_project/inventory/system/board
| `- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/CPU0_Power_Consumption
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/CPU0_Temp
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/GARBO_SENSOR
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_18_RUN
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_CORE_RUN
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_MEM_ABCD
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_MEM_EFGH
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/P0_VDD_SOC_RUN
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VBAT_33
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_12_RUN
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_33_DUAL
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_33_RUN
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_5_DUAL
| |- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VDD_5_RUN
| `- /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/X550
|- /xyz/openbmc_project/inventory/system/chassis
| `- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/Fan_Control
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_1_Ignore
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_2_Ignore
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_FRU_1
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/NIC_FRU_2
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/SYS_FAN_1
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/SYS_FAN_2
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/SYS_FAN_3
| |- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/Temperature_Control
| `- /xyz/openbmc_project/inventory/system/chassis/MBX_1_57_Chassis/Zone0
`- /xyz/openbmc_project/inventory/system/powersupply
|- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0
| |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0/PSU0
| |- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0/PSU0_ADDR
| `- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_0/PSU0_FRU
`- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1
|- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1/PSU1
|- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1/PSU1_ADDR
`- /xyz/openbmc_project/inventory/system/powersupply/Supermicro_PWS_920P_SQ_1/PSU1_FRU
```
Checked some individual object paths, also appear same as before.
```
busctl introspect xyz.openbmc_project.EntityManager /xyz/openbmc_project/inventory/system/board/Tyan_S8030_Baseboard/VBAT_33
```
output:
```
...
xyz.openbmc_project.Configuration.ADC interface - - -
.Index property t 10 emits-change
.Name property s "VBAT_33" emits-change
.PowerState property s "Always" emits-change
.ScaleFactor property d 0.3333 emits-change
.Type property s "ADC" emits-change
xyz.openbmc_project.Configuration.ADC.Thresholds0 interface - - -
.Delete method - - -
.Direction property s "greater than" emits-change
.Name property s "upper critical" emits-change
.Severity property d 1 emits-change
.Value property d 3.507 emits-change
xyz.openbmc_project.Configuration.ADC.Thresholds1 interface - - -
.Delete method - - -
.Direction property s "less than" emits-change
.Name property s "lower critical" emits-change
.Severity property d 1 emits-change
.Value property d 2.688 emits-change
```
Change-Id: I518d8d03e8a4f24c45deb12f9dc2069523dee987
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/entity_manager.cpp b/src/entity_manager/entity_manager.cpp
index d17a6bb..7734abc 100644
--- a/src/entity_manager/entity_manager.cpp
+++ b/src/entity_manager/entity_manager.cpp
@@ -84,228 +84,7 @@
// iterate through boards
for (const auto& [boardId, boardConfig] : newConfiguration.items())
{
- std::string boardName = boardConfig["Name"];
- std::string boardNameOrig = boardConfig["Name"];
- std::string jsonPointerPath = "/" + boardId;
- // loop through newConfiguration, but use values from system
- // configuration to be able to modify via dbus later
- auto boardValues = systemConfiguration[boardId];
- auto findBoardType = boardValues.find("Type");
- std::string boardType;
- if (findBoardType != boardValues.end() &&
- findBoardType->type() == nlohmann::json::value_t::string)
- {
- boardType = findBoardType->get<std::string>();
- std::regex_replace(boardType.begin(), boardType.begin(),
- boardType.end(), illegalDbusMemberRegex, "_");
- }
- else
- {
- std::cerr << "Unable to find type for " << boardName
- << " reverting to Chassis.\n";
- boardType = "Chassis";
- }
- std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
-
- std::regex_replace(boardName.begin(), boardName.begin(),
- boardName.end(), illegalDbusMemberRegex, "_");
- std::string boardPath = "/xyz/openbmc_project/inventory/system/";
- boardPath += boardtypeLower;
- boardPath += "/";
- boardPath += boardName;
-
- std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
- dbus_interface.createInterface(objServer, boardPath,
- "xyz.openbmc_project.Inventory.Item",
- boardName);
-
- std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
- dbus_interface.createInterface(
- objServer, boardPath,
- "xyz.openbmc_project.Inventory.Item." + boardType,
- boardNameOrig);
-
- dbus_interface.createAddObjectMethod(
- io, jsonPointerPath, boardPath, systemConfiguration, objServer,
- boardNameOrig);
-
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
- objServer);
- jsonPointerPath += "/";
- // iterate through board properties
- for (const auto& [propName, propValue] : boardValues.items())
- {
- if (propValue.type() == nlohmann::json::value_t::object)
- {
- std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
- dbus_interface.createInterface(objServer, boardPath,
- propName, boardNameOrig);
-
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration, jsonPointerPath + propName, iface,
- propValue, objServer);
- }
- }
-
- auto exposes = boardValues.find("Exposes");
- if (exposes == boardValues.end())
- {
- continue;
- }
- // iterate through exposes
- jsonPointerPath += "Exposes/";
-
- // store the board level pointer so we can modify it on the way down
- std::string jsonPointerPathBoard = jsonPointerPath;
- size_t exposesIndex = -1;
- for (auto& item : *exposes)
- {
- exposesIndex++;
- jsonPointerPath = jsonPointerPathBoard;
- jsonPointerPath += std::to_string(exposesIndex);
-
- auto findName = item.find("Name");
- if (findName == item.end())
- {
- std::cerr << "cannot find name in field " << item << "\n";
- continue;
- }
- auto findStatus = item.find("Status");
- // if status is not found it is assumed to be status = 'okay'
- if (findStatus != item.end())
- {
- if (*findStatus == "disabled")
- {
- continue;
- }
- }
- auto findType = item.find("Type");
- std::string itemType;
- if (findType != item.end())
- {
- itemType = findType->get<std::string>();
- std::regex_replace(itemType.begin(), itemType.begin(),
- itemType.end(), illegalDbusPathRegex, "_");
- }
- else
- {
- itemType = "unknown";
- }
- std::string itemName = findName->get<std::string>();
- std::regex_replace(itemName.begin(), itemName.begin(),
- itemName.end(), illegalDbusMemberRegex, "_");
- std::string ifacePath = boardPath;
- ifacePath += "/";
- ifacePath += itemName;
-
- if (itemType == "BMC")
- {
- std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
- dbus_interface.createInterface(
- objServer, ifacePath,
- "xyz.openbmc_project.Inventory.Item.Bmc",
- boardNameOrig);
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration, jsonPointerPath, bmcIface, item,
- objServer, getPermission(itemType));
- }
- else if (itemType == "System")
- {
- std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
- dbus_interface.createInterface(
- objServer, ifacePath,
- "xyz.openbmc_project.Inventory.Item.System",
- boardNameOrig);
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration, jsonPointerPath, systemIface, item,
- objServer, getPermission(itemType));
- }
-
- for (const auto& [name, config] : item.items())
- {
- jsonPointerPath = jsonPointerPathBoard;
- jsonPointerPath.append(std::to_string(exposesIndex))
- .append("/")
- .append(name);
- if (config.type() == nlohmann::json::value_t::object)
- {
- std::string ifaceName =
- "xyz.openbmc_project.Configuration.";
- ifaceName.append(itemType).append(".").append(name);
-
- std::shared_ptr<sdbusplus::asio::dbus_interface>
- objectIface = dbus_interface.createInterface(
- objServer, ifacePath, ifaceName, boardNameOrig);
-
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration, jsonPointerPath, objectIface,
- config, objServer, getPermission(name));
- }
- else if (config.type() == nlohmann::json::value_t::array)
- {
- size_t index = 0;
- if (config.empty())
- {
- continue;
- }
- bool isLegal = true;
- auto type = config[0].type();
- if (type != nlohmann::json::value_t::object)
- {
- continue;
- }
-
- // verify legal json
- for (const auto& arrayItem : config)
- {
- if (arrayItem.type() != type)
- {
- isLegal = false;
- break;
- }
- }
- if (!isLegal)
- {
- std::cerr << "dbus format error" << config << "\n";
- break;
- }
-
- for (auto& arrayItem : config)
- {
- std::string ifaceName =
- "xyz.openbmc_project.Configuration.";
- ifaceName.append(itemType).append(".").append(name);
- ifaceName.append(std::to_string(index));
-
- std::shared_ptr<sdbusplus::asio::dbus_interface>
- objectIface = dbus_interface.createInterface(
- objServer, ifacePath, ifaceName, boardNameOrig);
-
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration,
- jsonPointerPath + "/" + std::to_string(index),
- objectIface, arrayItem, objServer,
- getPermission(name));
- index++;
- }
- }
- }
-
- std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
- dbus_interface.createInterface(
- objServer, ifacePath,
- "xyz.openbmc_project.Configuration." + itemType,
- boardNameOrig);
-
- dbus_interface::populateInterfaceFromJson(
- io, systemConfiguration, jsonPointerPath, itemIface, item,
- objServer, getPermission(itemType));
-
- topology.addBoard(boardPath, boardType, boardNameOrig, item);
- }
-
- newBoards.emplace(boardPath, boardNameOrig);
+ postBoardToDBus(boardId, boardConfig, newBoards);
}
for (const auto& [assocPath, assocPropValue] :
@@ -326,6 +105,228 @@
}
}
+void EntityManager::postBoardToDBus(
+ const std::string& boardId, const nlohmann::json& boardConfig,
+ std::map<std::string, std::string>& newBoards)
+{
+ std::string boardName = boardConfig["Name"];
+ std::string boardNameOrig = boardConfig["Name"];
+ std::string jsonPointerPath = "/" + boardId;
+ // loop through newConfiguration, but use values from system
+ // configuration to be able to modify via dbus later
+ auto boardValues = systemConfiguration[boardId];
+ auto findBoardType = boardValues.find("Type");
+ std::string boardType;
+ if (findBoardType != boardValues.end() &&
+ findBoardType->type() == nlohmann::json::value_t::string)
+ {
+ boardType = findBoardType->get<std::string>();
+ std::regex_replace(boardType.begin(), boardType.begin(),
+ boardType.end(), illegalDbusMemberRegex, "_");
+ }
+ else
+ {
+ std::cerr << "Unable to find type for " << boardName
+ << " reverting to Chassis.\n";
+ boardType = "Chassis";
+ }
+ std::string boardtypeLower = boost::algorithm::to_lower_copy(boardType);
+
+ std::regex_replace(boardName.begin(), boardName.begin(), boardName.end(),
+ illegalDbusMemberRegex, "_");
+ std::string boardPath = "/xyz/openbmc_project/inventory/system/";
+ boardPath += boardtypeLower;
+ boardPath += "/";
+ boardPath += boardName;
+
+ std::shared_ptr<sdbusplus::asio::dbus_interface> inventoryIface =
+ dbus_interface.createInterface(objServer, boardPath,
+ "xyz.openbmc_project.Inventory.Item",
+ boardName);
+
+ std::shared_ptr<sdbusplus::asio::dbus_interface> boardIface =
+ dbus_interface.createInterface(
+ objServer, boardPath,
+ "xyz.openbmc_project.Inventory.Item." + boardType, boardNameOrig);
+
+ dbus_interface.createAddObjectMethod(
+ io, jsonPointerPath, boardPath, systemConfiguration, objServer,
+ boardNameOrig);
+
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration, jsonPointerPath, boardIface, boardValues,
+ objServer);
+ jsonPointerPath += "/";
+ // iterate through board properties
+ for (const auto& [propName, propValue] : boardValues.items())
+ {
+ if (propValue.type() == nlohmann::json::value_t::object)
+ {
+ std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
+ dbus_interface.createInterface(objServer, boardPath, propName,
+ boardNameOrig);
+
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration, jsonPointerPath + propName, iface,
+ propValue, objServer);
+ }
+ }
+
+ auto exposes = boardValues.find("Exposes");
+ if (exposes == boardValues.end())
+ {
+ return;
+ }
+ // iterate through exposes
+ jsonPointerPath += "Exposes/";
+
+ // store the board level pointer so we can modify it on the way down
+ std::string jsonPointerPathBoard = jsonPointerPath;
+ size_t exposesIndex = -1;
+ for (auto& item : *exposes)
+ {
+ exposesIndex++;
+ jsonPointerPath = jsonPointerPathBoard;
+ jsonPointerPath += std::to_string(exposesIndex);
+
+ auto findName = item.find("Name");
+ if (findName == item.end())
+ {
+ std::cerr << "cannot find name in field " << item << "\n";
+ continue;
+ }
+ auto findStatus = item.find("Status");
+ // if status is not found it is assumed to be status = 'okay'
+ if (findStatus != item.end())
+ {
+ if (*findStatus == "disabled")
+ {
+ continue;
+ }
+ }
+ auto findType = item.find("Type");
+ std::string itemType;
+ if (findType != item.end())
+ {
+ itemType = findType->get<std::string>();
+ std::regex_replace(itemType.begin(), itemType.begin(),
+ itemType.end(), illegalDbusPathRegex, "_");
+ }
+ else
+ {
+ itemType = "unknown";
+ }
+ std::string itemName = findName->get<std::string>();
+ std::regex_replace(itemName.begin(), itemName.begin(), itemName.end(),
+ illegalDbusMemberRegex, "_");
+ std::string ifacePath = boardPath;
+ ifacePath += "/";
+ ifacePath += itemName;
+
+ if (itemType == "BMC")
+ {
+ std::shared_ptr<sdbusplus::asio::dbus_interface> bmcIface =
+ dbus_interface.createInterface(
+ objServer, ifacePath,
+ "xyz.openbmc_project.Inventory.Item.Bmc", boardNameOrig);
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration, jsonPointerPath, bmcIface, item,
+ objServer, getPermission(itemType));
+ }
+ else if (itemType == "System")
+ {
+ std::shared_ptr<sdbusplus::asio::dbus_interface> systemIface =
+ dbus_interface.createInterface(
+ objServer, ifacePath,
+ "xyz.openbmc_project.Inventory.Item.System", boardNameOrig);
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration, jsonPointerPath, systemIface, item,
+ objServer, getPermission(itemType));
+ }
+
+ for (const auto& [name, config] : item.items())
+ {
+ jsonPointerPath = jsonPointerPathBoard;
+ jsonPointerPath.append(std::to_string(exposesIndex))
+ .append("/")
+ .append(name);
+ if (config.type() == nlohmann::json::value_t::object)
+ {
+ std::string ifaceName = "xyz.openbmc_project.Configuration.";
+ ifaceName.append(itemType).append(".").append(name);
+
+ std::shared_ptr<sdbusplus::asio::dbus_interface> objectIface =
+ dbus_interface.createInterface(objServer, ifacePath,
+ ifaceName, boardNameOrig);
+
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration, jsonPointerPath, objectIface,
+ config, objServer, getPermission(name));
+ }
+ else if (config.type() == nlohmann::json::value_t::array)
+ {
+ size_t index = 0;
+ if (config.empty())
+ {
+ continue;
+ }
+ bool isLegal = true;
+ auto type = config[0].type();
+ if (type != nlohmann::json::value_t::object)
+ {
+ continue;
+ }
+
+ // verify legal json
+ for (const auto& arrayItem : config)
+ {
+ if (arrayItem.type() != type)
+ {
+ isLegal = false;
+ break;
+ }
+ }
+ if (!isLegal)
+ {
+ std::cerr << "dbus format error" << config << "\n";
+ break;
+ }
+
+ for (auto& arrayItem : config)
+ {
+ std::string ifaceName =
+ "xyz.openbmc_project.Configuration.";
+ ifaceName.append(itemType).append(".").append(name);
+ ifaceName.append(std::to_string(index));
+
+ std::shared_ptr<sdbusplus::asio::dbus_interface>
+ objectIface = dbus_interface.createInterface(
+ objServer, ifacePath, ifaceName, boardNameOrig);
+
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration,
+ jsonPointerPath + "/" + std::to_string(index),
+ objectIface, arrayItem, objServer, getPermission(name));
+ index++;
+ }
+ }
+ }
+
+ std::shared_ptr<sdbusplus::asio::dbus_interface> itemIface =
+ dbus_interface.createInterface(
+ objServer, ifacePath,
+ "xyz.openbmc_project.Configuration." + itemType, boardNameOrig);
+
+ dbus_interface::populateInterfaceFromJson(
+ io, systemConfiguration, jsonPointerPath, itemIface, item,
+ objServer, getPermission(itemType));
+
+ topology.addBoard(boardPath, boardType, boardNameOrig, item);
+ }
+
+ newBoards.emplace(boardPath, boardNameOrig);
+}
+
static bool deviceRequiresPowerOn(const nlohmann::json& entity)
{
auto powerState = entity.find("PowerState");
diff --git a/src/entity_manager/entity_manager.hpp b/src/entity_manager/entity_manager.hpp
index d2bce67..70436f5 100644
--- a/src/entity_manager/entity_manager.hpp
+++ b/src/entity_manager/entity_manager.hpp
@@ -51,6 +51,10 @@
boost::asio::steady_timer& timer,
nlohmann::json newConfiguration);
void postToDbus(const nlohmann::json& newConfiguration);
+ void postBoardToDBus(const std::string& boardId,
+ const nlohmann::json& boardConfig,
+ std::map<std::string, std::string>& newBoards);
+
void pruneConfiguration(bool powerOff, const std::string& name,
const nlohmann::json& device);