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