1 #pragma once 2 3 #include "external_storer_interface.hpp" 4 #include "nlohmann/json.hpp" 5 #include "notifier_dbus_handler.hpp" 6 7 #include <boost/uuid/uuid_generators.hpp> 8 9 #include <filesystem> 10 #include <string> 11 12 namespace bios_bmc_smm_error_logger 13 { 14 namespace rde 15 { 16 17 /** 18 * @brief Simple Base class for writing JSON data to files. 19 * 20 * This class allows us to unit test the ExternalStorerFileInterface 21 * functionality. 22 */ 23 class FileHandlerInterface 24 { 25 public: 26 virtual ~FileHandlerInterface() = default; 27 28 /** 29 * @brief Create a folder at the provided path. 30 * 31 * @param[in] folderPath - folder path. 32 * @return true if successful. 33 */ 34 virtual bool createFolder(const std::string& folderPath) const = 0; 35 36 /** 37 * @brief Create an index.json and write the JSON content to it. 38 * 39 * If the file already exists, this will overwrite it. 40 * 41 * @param[in] folderPath - path of the file without including the file name. 42 * @param[in] jsonPdr - PDR in nlohmann::json format. 43 * @return true if successful. 44 */ 45 virtual bool createFile(const std::string& folderPath, 46 const nlohmann::json& jsonPdr) const = 0; 47 }; 48 49 /** 50 * @brief Class for handling folder and file creation for ExternalStorer. 51 */ 52 class ExternalStorerFileWriter : public FileHandlerInterface 53 { 54 public: 55 bool createFolder(const std::string& folderPath) const override; 56 bool createFile(const std::string& folderPath, 57 const nlohmann::json& jsonPdr) const override; 58 }; 59 60 /** 61 * @brief Categories for different redfish JSON strings. 62 */ 63 enum class JsonPdrType 64 { 65 logEntry, 66 logService, 67 other 68 }; 69 70 /** 71 * @brief Class for handling ExternalStorer file operations. 72 */ 73 class ExternalStorerFileInterface : public ExternalStorerInterface 74 { 75 public: 76 /** 77 * @brief Constructor for the ExternalStorerFileInterface. 78 * 79 * @param[in] bus - bus to attach to. 80 * @param[in] rootPath - root path for creating redfish folders. 81 * Eg: "/run/bmcweb" 82 * @param[in] fileHandler - an ExternalStorerFileWriter object. This class 83 * will take the ownership of this object. 84 */ 85 ExternalStorerFileInterface( 86 sdbusplus::bus_t& bus, std::string_view rootPath, 87 std::unique_ptr<FileHandlerInterface> fileHandler); 88 89 bool publishJson(std::string_view jsonStr) override; 90 91 private: 92 std::string rootPath; 93 std::unique_ptr<FileHandlerInterface> fileHandler; 94 std::string logServiceId; 95 std::unique_ptr<CperFileNotifierHandler> cperNotifier; 96 boost::uuids::random_generator randomGen; 97 98 /** 99 * @brief Get the type of the received PDR. 100 * 101 * @param[in] jsonSchema - PDR in nlohmann::json format. 102 * @return JsonPdrType of the PDR. 103 */ 104 JsonPdrType getSchemaType(const nlohmann::json& jsonSchema) const; 105 106 /** 107 * @brief Process a LogEntry type PDR. 108 * 109 * @param[in] logEntry - PDR in nlohmann::json format. 110 * @return true if successful. 111 */ 112 bool processLogEntry(nlohmann::json& logEntry); 113 114 /** 115 * @brief Process a LogService type PDR. 116 * 117 * @param[in] logService - PDR in nlohmann::json format. 118 * @return true if successful. 119 */ 120 bool processLogService(const nlohmann::json& logService); 121 122 /** 123 * @brief Process PDRs that doesn't have a specific category. 124 * 125 * @param[in] jsonPdr - PDR in nlohmann::json format. 126 * @return true if successful. 127 */ 128 bool processOtherTypes(const nlohmann::json& jsonPdr) const; 129 130 /** 131 * @brief Create the needed folders and the index.json. 132 * 133 * @param subPath - path within the root folder. 134 * @param jsonPdr - PDR in nlohmann::json format. 135 * @return true if successful. 136 */ 137 bool createFile(const std::string& subPath, 138 const nlohmann::json& jsonPdr) const; 139 }; 140 141 } // namespace rde 142 } // namespace bios_bmc_smm_error_logger 143