blob: 4d434cf82de8e2adbe4ed31f6d4eff6a75b1868a [file] [edit]
#include "tlbmc/collector/pcie_collector.h"
#include <memory>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "nlohmann/json.hpp"
#include "tlbmc/collector/gpio_collector.h"
#include "pcie_config.pb.h"
#include "tlbmc/redfish/data/stable_id.h"
#include "stable_id.pb.h"
namespace milotic_tlbmc {
absl::StatusOr<std::unique_ptr<PcieCollector>> PcieCollector::Create(
const Params& params) {
if (params.gpio_collector == nullptr) {
return absl::InvalidArgumentError("GpioCollector is required");
}
return absl::WrapUnique(new PcieCollector(params));
}
absl::StatusOr<bool> PcieCollector::GetPresence(
const nlohmann::json& redfish_location) const {
for (const PcieConfig& config : pcie_configs_.pcie_configs()) {
if (MatchStableId(redfish_location, config.pcie_slot_id())) {
return gpio_collector_->IsActive(config.presence_gpio_pin_name());
}
}
return absl::NotFoundError(absl::StrCat("PCIe slot not found for location: ",
redfish_location.dump()));
}
nlohmann::json PcieCollector::ToJson() const {
nlohmann::json::array_t pcies;
for (const PcieConfig& config : pcie_configs_.pcie_configs()) {
nlohmann::json pcie_json;
pcie_json["PresenceGpioPinName"] = config.presence_gpio_pin_name();
absl::StatusOr<bool> presence =
gpio_collector_->IsActive(config.presence_gpio_pin_name());
if (presence.ok()) {
pcie_json["Presence"] = *presence;
} else {
pcie_json["Presence"] = nullptr;
}
nlohmann::json stable_id_json;
nlohmann::json part_location_json;
part_location_json["ServiceLabel"] = config.pcie_slot_id().service_label();
part_location_json["LocationType"] = GetRedfishStringFromPartLocationType(
config.pcie_slot_id().location_type());
stable_id_json["PartLocation"] = part_location_json;
stable_id_json["PartLocationContext"] =
config.pcie_slot_id().part_location_context();
nlohmann::json oem_google_json;
oem_google_json["EmbeddedLocationContext"] =
config.pcie_slot_id().embedded_location_context();
oem_google_json["Devpath"] = config.pcie_slot_id().machine_local_devpath();
stable_id_json["Oem"]["Google"] = oem_google_json;
pcie_json["StableId"] = stable_id_json;
pcies.push_back(pcie_json);
}
return pcies;
}
nlohmann::json EmptyPcieCollector::ToJson() const {
return nlohmann::json::parse("{\"Warning\": \"EmptyPcieCollector used.\"}");
}
} // namespace milotic_tlbmc