blob: f064b41b56aaad3a97dfc2361406e0f2cfa7f675 [file] [log] [blame]
Jason Lingc893f432020-10-24 19:31:44 -07001#pragma once
2
Jason Linge82d7392020-11-19 13:27:02 -08003#include "fs.hpp"
William A. Kennington IIIcc7f3852021-01-21 19:01:56 -08004#include "handler_config.hpp"
William A. Kennington IIIbec23182021-01-21 18:10:52 -08005#include "status.hpp"
Jason Lingc893f432020-10-24 19:31:44 -07006
7#include <nlohmann/json.hpp>
8
Willy Tuc2779352022-06-03 12:31:46 -07009#include <array>
Jason Lingc893f432020-10-24 19:31:44 -070010#include <memory>
11#include <string>
12#include <vector>
13
14namespace ipmi_flash
15{
Jason Linge82d7392020-11-19 13:27:02 -080016/**
17 * build a systemd file triggerable action from json data
18 */
Jason Lingc893f432020-10-24 19:31:44 -070019std::unique_ptr<TriggerableActionInterface>
20 buildFileSystemd(const nlohmann::json& data);
21
Jason Linge82d7392020-11-19 13:27:02 -080022/**
23 * build a systemd triggerable action from json data
24 */
Jason Lingc893f432020-10-24 19:31:44 -070025std::unique_ptr<TriggerableActionInterface>
26 buildSystemd(const nlohmann::json& data);
27
Willy Tuc2779352022-06-03 12:31:46 -070028constexpr std::array defaultConfigPaths = {
29 "/usr/share/phosphor-ipmi-flash",
30 "/run/phosphor-ipmi-flash",
31};
William A. Kennington IIId0dc7232021-01-28 21:25:04 -080032
Jason Lingc893f432020-10-24 19:31:44 -070033/* HandlersBuilderIfc is a helper class that builds Handlers from the json files
34 * found within a specified directory.
35 * The child class that inherits from HandlersBuilderIfc should implement
36 * buildHandlersConfigs to perform json validation and parsing.
37 *
38 */
39template <typename T>
William A. Kennington III63fd9322020-12-22 15:04:21 -080040struct HandlersBuilderIfc
Jason Lingc893f432020-10-24 19:31:44 -070041{
William A. Kennington III63fd9322020-12-22 15:04:21 -080042 virtual ~HandlersBuilderIfc() = default;
43
Jason Lingc893f432020-10-24 19:31:44 -070044 /**
William A. Kennington IIId0dc7232021-01-28 21:25:04 -080045 * Builds configurations from the default set of paths used by the blob
46 * handler.
47 */
48 std::vector<HandlerConfig<T>> buildHandlerConfigsFromDefaultPaths()
49 {
50 std::vector<HandlerConfig<T>> ret;
51 for (auto path : defaultConfigPaths)
52 {
53 auto tmp = buildHandlerConfigs(path);
54 std::move(tmp.begin(), tmp.end(), std::back_inserter(ret));
55 }
56 return ret;
57 }
58
59 /**
Jason Lingc893f432020-10-24 19:31:44 -070060 * Given a folder of json configs, build the configurations.
61 *
62 * @param[in] directory - the directory to search (recurisvely).
63 * @return list of HandlerConfig objects.
64 */
William A. Kennington IIId0dc7232021-01-28 21:25:04 -080065 std::vector<HandlerConfig<T>> buildHandlerConfigs(const char* directory)
Jason Linge82d7392020-11-19 13:27:02 -080066 {
Jason Linge82d7392020-11-19 13:27:02 -080067 std::vector<HandlerConfig<T>> output;
68
69 std::vector<std::string> jsonPaths = GetJsonList(directory);
70 for (const auto& path : jsonPaths)
71 {
72 std::ifstream jsonFile(path);
73 if (!jsonFile.is_open())
74 {
75 std::fprintf(stderr, "Unable to open json file: %s\n",
76 path.c_str());
77 continue;
78 }
79
80 auto data = nlohmann::json::parse(jsonFile, nullptr, false);
81 if (data.is_discarded())
82 {
83 std::fprintf(stderr, "Parsing json failed: %s\n", path.c_str());
84 continue;
85 }
86
87 std::vector<HandlerConfig<T>> configs = buildHandlerFromJson(data);
88 std::move(configs.begin(), configs.end(),
89 std::back_inserter(output));
90 }
91 return output;
92 };
William A. Kennington III63fd9322020-12-22 15:04:21 -080093
Jason Lingc893f432020-10-24 19:31:44 -070094 /**
95 * Given a list of handlers as json data, construct the appropriate
96 * HandlerConfig objects. This method is meant to be called per json
97 * configuration file found.
98 *
99 * The list will only contain validly build HandlerConfig objects. Any
100 * invalid configuration is skipped. The hope is that the BMC firmware
101 * update configuration will never be invalid, but if another aspect is
102 * invalid, it can be fixed with a BMC firmware update once the bug is
103 * identified.
104 *
105 * This code does not validate that the blob specified is unique, that
106 * should be handled at a higher layer.
107 *
108 * @param[in] data - json data from a json file.
109 * @return list of HandlerConfig objects.
110 */
111 virtual std::vector<HandlerConfig<T>>
112 buildHandlerFromJson(const nlohmann::json& data) = 0;
113};
114} // namespace ipmi_flash