entityName: move pieces into handler object
Move different items used by the handler into the handler and enable
unit-testing by parameterizing different aspects of the code.
Tested: Only ran unit-tests (added new ones).
Change-Id: Ia3b4b5792c0ac1ae5bc6513eadfc9ee35f7a369f
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/handler.hpp b/handler.hpp
index be5f4d0..ec77657 100644
--- a/handler.hpp
+++ b/handler.hpp
@@ -1,6 +1,8 @@
#pragma once
#include <cstdint>
+#include <map>
+#include <nlohmann/json.hpp>
#include <string>
#include <tuple>
@@ -12,6 +14,8 @@
using VersionTuple =
std::tuple<std::uint8_t, std::uint8_t, std::uint8_t, std::uint8_t>;
+extern const std::string defaultConfigFile;
+
class HandlerInterface
{
public:
@@ -68,7 +72,8 @@
class Handler : public HandlerInterface
{
public:
- Handler() = default;
+ explicit Handler(const std::string& entityConfigPath = defaultConfigFile) :
+ _configFile(entityConfigPath){};
~Handler() = default;
std::tuple<std::uint8_t, std::string> getEthDetails() const override;
@@ -76,8 +81,35 @@
VersionTuple getCpldVersion(unsigned int id) const override;
void psuResetDelay(std::uint32_t delay) const override;
std::string getEntityName(std::uint8_t id, std::uint8_t instance) override;
+
+ private:
+ std::string _configFile;
+
+ bool _entityConfigParsed = false;
+
+ const std::map<uint8_t, std::string> _entityIdToName{
+ {0x03, "cpu"},
+ {0x04, "storage_device"},
+ {0x06, "system_management_module"},
+ {0x08, "memory_module"},
+ {0x0B, "add_in_card"},
+ {0x17, "system_chassis"},
+ {0x20, "memory_device"}};
+
+ nlohmann::json _entityConfig{};
};
+/**
+ * Given a type, entity instance, and a configuration, return the name.
+ *
+ * @param[in] type - the entity type
+ * @param[in] instance - the entity instance
+ * @param[in] config - the json object holding the entity mapping
+ * @return the name of the entity from the map
+ */
+std::string readNameFromConfig(const std::string& type, uint8_t instance,
+ const nlohmann::json& config);
+
extern Handler handlerImpl;
} // namespace ipmi