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::bus& bus, std::string_view rootPath,
87         std::unique_ptr<FileHandlerInterface> fileHandler);
88 
89     bool publishJson(std::string_view jsonStr) override;
90 
91   private:
92     sdbusplus::bus::bus& bus;
93     std::string rootPath;
94     std::unique_ptr<FileHandlerInterface> fileHandler;
95     std::string logServiceId;
96     std::unique_ptr<CperFileNotifierHandler> cperNotifier;
97     boost::uuids::random_generator randomGen;
98 
99     /**
100      * @brief Get the type of the received PDR.
101      *
102      * @param[in] jsonSchema - PDR in nlohmann::json format.
103      * @return JsonPdrType of the PDR.
104      */
105     JsonPdrType getSchemaType(const nlohmann::json& jsonSchema) const;
106 
107     /**
108      * @brief Process a LogEntry type PDR.
109      *
110      * @param[in] logEntry - PDR in nlohmann::json format.
111      * @return true if successful.
112      */
113     bool processLogEntry(nlohmann::json& logEntry);
114 
115     /**
116      * @brief Process a LogService type PDR.
117      *
118      * @param[in] logService - PDR in nlohmann::json format.
119      * @return true if successful.
120      */
121     bool processLogService(const nlohmann::json& logService);
122 
123     /**
124      * @brief Process PDRs that doesn't have a specific category.
125      *
126      * @param[in] jsonPdr - PDR in nlohmann::json format.
127      * @return true if successful.
128      */
129     bool processOtherTypes(const nlohmann::json& jsonPdr) const;
130 
131     /**
132      * @brief Create the needed folders and the index.json.
133      *
134      * @param subPath - path within the root folder.
135      * @param jsonPdr - PDR in nlohmann::json format.
136      * @return true if successful.
137      */
138     bool createFile(const std::string& subPath,
139                     const nlohmann::json& jsonPdr) const;
140 };
141 
142 } // namespace rde
143 } // namespace bios_bmc_smm_error_logger
144