blob: 7bc8f73c989df5218808a9adbab2a2592ae3b3cc [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_STORE_STORE_IMPL_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_STORE_STORE_IMPL_H_
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "absl/log/log.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "nlohmann/json.hpp"
#include "tlbmc/collector/collector.h"
#include "tlbmc/collector/fru_collector.h"
#include "tlbmc/collector/sensor_collector.h"
#include "tlbmc/configs/entity_config.h"
#include "topology_config.pb.h"
#include "tlbmc/hal/fru_scanner.h"
#include "tlbmc/hal/sysfs/i2c.h"
#include "fru.pb.h"
#include "sensor.pb.h"
#include "tlbmc/scheduler/scheduler.h"
#include "tlbmc/sensors/sensor.h"
#include "tlbmc/store/factory.h"
#include "tlbmc/store/store.h"
#include "router_interface.h"
namespace milotic_tlbmc {
// Houses all the collectors.
// Extend this struct to add support for new collectors.
struct AllCollectors {
std::unique_ptr<SensorCollector> sensor_collector = nullptr;
std::unique_ptr<FruCollector> fru_collector = nullptr;
};
// Implementation of the Store interface.
// The store creates and owns all the telemetry collectors and is responsible
// for facilitating access to the data collected by the collectors.
//
// This class is thread-safe.
class StoreImpl : public Store {
public:
struct Options {
std::vector<FruScanner*> fru_scanners;
I2cSysfs* i2c_sysfs = nullptr;
std::string config_location = "/etc/tlbmc";
std::unique_ptr<EntityConfigReader> entity_config_reader =
std::make_unique<EntityConfigReaderImpl>();
std::unique_ptr<CollectorFactory> collector_factory =
std::make_unique<CollectorFactoryImpl>();
std::optional<int> override_sensor_sampling_interval_ms = std::nullopt;
std::optional<int> override_store_snapshot_interval_ms = std::nullopt;
// TODO(rahulkpr): Add options for the store as needed.
};
static absl::StatusOr<std::unique_ptr<StoreImpl>> Create(
const Options& options);
~StoreImpl() override;
// StoreImpl is neither copyable nor movable.
StoreImpl(const StoreImpl&) = delete;
StoreImpl& operator=(const StoreImpl&) = delete;
StoreImpl(StoreImpl&&) = delete;
StoreImpl& operator=(StoreImpl&&) = delete;
absl::Status ConfigureCollection(const Collector::Config& config,
Collector::Type type) const override;
// Returns the list of sensor names contained by the given Config name.
std::vector<std::string> GetAllSensorKeysByConfigName(
const std::string& board_config_name) const override;
// Returns all the sensors.
std::vector<std::shared_ptr<const Sensor>> GetAllSensors() const override;
// Returns the sensor for the given sensor key.
std::shared_ptr<const Sensor> GetSensorBySensorKey(
const std::string& sensor_key) const override;
// Returns the sensor for the given board config name and given sensor
// key.
std::shared_ptr<const Sensor> GetSensorByConfigNameAndSensorKey(
const std::string& board_config_name,
const std::string& sensor_key) const override;
absl::StatusOr<const Fru*> GetFru(absl::string_view key) const override;
absl::StatusOr<const TopologyConfigNode*> GetFruTopology(
absl::string_view config_name) const override;
absl::StatusOr<const TopologyConfig*> GetTopologyConfig() const override;
absl::StatusOr<std::vector<std::string>> GetAllConfigNames() const override;
absl::StatusOr<std::string> GetConfigNameByFruKey(
absl::string_view fru_key) const override;
absl::StatusOr<std::string> GetFruKeyByConfigName(
absl::string_view config_name) const override;
absl::StatusOr<const FruTable*> GetAllFrus() const override;
// TODO(rahulkpr): Add APIs that can be used to model Redfish resource based
// on data from the store. For example, find devpath for a given fru or sensor
// key, find parent of a given fru or sensor key, etc.
nlohmann::json ToJson() const override;
std::string ToString() const override { return ToJson().dump(); }
void SetSmartRouter(::crow::RouterInterface* smart_router) override;
nlohmann::json GetSchedulerStats() const override;
Metrics GetMetrics() const override;
private:
explicit StoreImpl(AllCollectors all_collectors,
std::shared_ptr<EntityConfig> entity_config,
absl::Duration store_snapshot_interval,
Metrics metrics_at_bootup);
// StoreImpl owns all telemetry collectors.
// Collectors are thread-safe.
AllCollectors all_collectors_;
std::shared_ptr<EntityConfig> entity_config_;
// Scheduler for store level tasks.
// Note: Each collector has its own scheduler.
std::unique_ptr<TaskScheduler> task_scheduler_;
// Metrics for the store.
// Note: Const for now, will consider making it mutable if we need to update
// metrics during runtime.
const Metrics metrics_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_STORE_STORE_IMPL_H_