Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Jason Ling | e82d739 | 2020-11-19 13:27:02 -0800 | [diff] [blame] | 3 | #include "fs.hpp" |
William A. Kennington III | cc7f385 | 2021-01-21 19:01:56 -0800 | [diff] [blame] | 4 | #include "handler_config.hpp" |
William A. Kennington III | bec2318 | 2021-01-21 18:10:52 -0800 | [diff] [blame] | 5 | #include "status.hpp" |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 6 | |
| 7 | #include <nlohmann/json.hpp> |
| 8 | |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | namespace ipmi_flash |
| 14 | { |
Jason Ling | e82d739 | 2020-11-19 13:27:02 -0800 | [diff] [blame] | 15 | /** |
| 16 | * build a systemd file triggerable action from json data |
| 17 | */ |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 18 | std::unique_ptr<TriggerableActionInterface> |
| 19 | buildFileSystemd(const nlohmann::json& data); |
| 20 | |
Jason Ling | e82d739 | 2020-11-19 13:27:02 -0800 | [diff] [blame] | 21 | /** |
| 22 | * build a systemd triggerable action from json data |
| 23 | */ |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 24 | std::unique_ptr<TriggerableActionInterface> |
| 25 | buildSystemd(const nlohmann::json& data); |
| 26 | |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 27 | /* HandlersBuilderIfc is a helper class that builds Handlers from the json files |
| 28 | * found within a specified directory. |
| 29 | * The child class that inherits from HandlersBuilderIfc should implement |
| 30 | * buildHandlersConfigs to perform json validation and parsing. |
| 31 | * |
| 32 | */ |
| 33 | template <typename T> |
William A. Kennington III | 63fd932 | 2020-12-22 15:04:21 -0800 | [diff] [blame] | 34 | struct HandlersBuilderIfc |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 35 | { |
William A. Kennington III | 63fd932 | 2020-12-22 15:04:21 -0800 | [diff] [blame] | 36 | virtual ~HandlersBuilderIfc() = default; |
| 37 | |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Given a folder of json configs, build the configurations. |
| 40 | * |
| 41 | * @param[in] directory - the directory to search (recurisvely). |
| 42 | * @return list of HandlerConfig objects. |
| 43 | */ |
| 44 | std::vector<HandlerConfig<T>> |
Jason Ling | e82d739 | 2020-11-19 13:27:02 -0800 | [diff] [blame] | 45 | buildHandlerConfigs(const std::string& directory) |
| 46 | { |
| 47 | |
| 48 | std::vector<HandlerConfig<T>> output; |
| 49 | |
| 50 | std::vector<std::string> jsonPaths = GetJsonList(directory); |
| 51 | for (const auto& path : jsonPaths) |
| 52 | { |
| 53 | std::ifstream jsonFile(path); |
| 54 | if (!jsonFile.is_open()) |
| 55 | { |
| 56 | std::fprintf(stderr, "Unable to open json file: %s\n", |
| 57 | path.c_str()); |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | auto data = nlohmann::json::parse(jsonFile, nullptr, false); |
| 62 | if (data.is_discarded()) |
| 63 | { |
| 64 | std::fprintf(stderr, "Parsing json failed: %s\n", path.c_str()); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | std::vector<HandlerConfig<T>> configs = buildHandlerFromJson(data); |
| 69 | std::move(configs.begin(), configs.end(), |
| 70 | std::back_inserter(output)); |
| 71 | } |
| 72 | return output; |
| 73 | }; |
William A. Kennington III | 63fd932 | 2020-12-22 15:04:21 -0800 | [diff] [blame] | 74 | |
Jason Ling | c893f43 | 2020-10-24 19:31:44 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Given a list of handlers as json data, construct the appropriate |
| 77 | * HandlerConfig objects. This method is meant to be called per json |
| 78 | * configuration file found. |
| 79 | * |
| 80 | * The list will only contain validly build HandlerConfig objects. Any |
| 81 | * invalid configuration is skipped. The hope is that the BMC firmware |
| 82 | * update configuration will never be invalid, but if another aspect is |
| 83 | * invalid, it can be fixed with a BMC firmware update once the bug is |
| 84 | * identified. |
| 85 | * |
| 86 | * This code does not validate that the blob specified is unique, that |
| 87 | * should be handled at a higher layer. |
| 88 | * |
| 89 | * @param[in] data - json data from a json file. |
| 90 | * @return list of HandlerConfig objects. |
| 91 | */ |
| 92 | virtual std::vector<HandlerConfig<T>> |
| 93 | buildHandlerFromJson(const nlohmann::json& data) = 0; |
| 94 | }; |
| 95 | } // namespace ipmi_flash |