blob: 0fcfdde7f22591f122f5aa9878d0660a502d3b44 [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_FAST_SANITY_SERVICE_FRU_SERVICE_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_FAST_SANITY_SERVICE_FRU_SERVICE_H_
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/span.h"
#include "grpcpp/security/auth_context.h"
#include "grpcpp/server_context.h"
#include "grpcpp/support/server_callback.h"
#include "grpcpp/support/status.h"
#include "nlohmann/json_fwd.hpp"
#include "fru_component_model.pb.h"
#include "fru_service.grpc.pb.h"
#include "app.hpp"
#include "http_request.hpp"
#include "zatar/bmcweb_cert_provider.h"
namespace milotic_fast_sanity {
#ifdef INTERNAL_GRPC_GEN
using FruService = ::milotic_fast_sanity::grpc_gen::FruService;
#else
using FruService = ::milotic_fast_sanity::FruService;
#endif
struct FruComponents {
absl::Mutex mutex;
absl::flat_hash_map<std::string, FruComponent> devpath_to_fru_component
ABSL_GUARDED_BY(mutex);
void AddFruComponent(FruComponent &&fru_component) {
absl::MutexLock lock(&mutex);
devpath_to_fru_component[fru_component.primary_identifier().value()] =
std::move(fru_component);
}
};
struct FruServiceOptions {
bool enable_authorization = true;
::milotic::redfish::BmcWebCertProvider *cert_provider = nullptr;
};
struct OngoingDevpathExtractionRequestCount {
absl::Mutex mutex;
uint32_t ongoing_devpath_extraction_request_count ABSL_GUARDED_BY(mutex);
explicit OngoingDevpathExtractionRequestCount(
uint32_t ongoing_devpath_extraction_request_count)
: ongoing_devpath_extraction_request_count(
ongoing_devpath_extraction_request_count) {}
void DecreaseOngoingDevpathExtractionRequestCount() {
absl::MutexLock lock(&mutex);
--ongoing_devpath_extraction_request_count;
}
};
class FruServiceImpl : public FruService::CallbackService {
public:
explicit FruServiceImpl(App *app, const FruServiceOptions &options);
::grpc::ServerUnaryReactor *GetAllFruInfo(
::grpc::CallbackServerContext *context,
const GetAllFruInfoRequest *request,
GetAllFruInfoResponse *response) override;
void GenerateMatchedDevpathsFromRedfishPaths(
::grpc::ServerUnaryReactor &reactor, const GetAllFruInfoRequest &request,
GetAllFruInfoResponse &response);
::grpc::Status RequestIsAuthenticated(
const ::grpc::AuthContext *auth_context) const;
static absl::StatusOr<std::vector<std::string>> GetAllChassisUrls(
const nlohmann::json &json);
static absl::StatusOr<crow::Request> CreateRedfishRequest(
absl::string_view url, boost::beast::http::verb method);
static void SendGetAllFruInfoResponse(
::grpc::ServerUnaryReactor &reactor, GetAllFruInfoResponse &response,
const std::shared_ptr<FruComponents> &fru_components);
static bool IsLocationTypeSupported(const nlohmann::json &location_type_json);
static absl::Status IsLocationInfoValid(const nlohmann::json &fru);
static absl::StatusOr<std::string> ExtractRootChassisLocationCodeFromJson(
const nlohmann::json &json);
static FruComponent::Status ExtractFruStatus(const nlohmann::json &fru);
static std::string ExtractFruModelName(const nlohmann::json &fru);
static FruComponent BuildFruComponent(absl::string_view devpath,
absl::string_view model_name,
FruComponent::Status status,
ComponentType component_type);
static absl::StatusOr<FruComponent> ExtractFruComponentFromJson(
const nlohmann::json &json, absl::string_view root_chassis_location_code);
static void AppendGeneratedFruComponentsFromRedfishObject(
const nlohmann::json &json, absl::string_view root_chassis_location_code,
const std::shared_ptr<FruComponents> &fru_components);
protected:
void RequestRedfishAndExtractDevpaths(
::grpc::ServerUnaryReactor &reactor, const GetAllFruInfoRequest &request,
GetAllFruInfoResponse &response,
absl::string_view root_chassis_location_code,
absl::Span<const std::string> chassis_urls);
App *app_;
const bool has_trust_bundle_ = false;
};
} // namespace milotic_fast_sanity
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_FAST_SANITY_SERVICE_FRU_SERVICE_H_