blob: 53773c23da0c5bf6e2096cfc04bb7f721d136779 [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"
Jason Lingc893f432020-10-24 19:31:44 -07004#include "general_systemd.hpp"
5#include "image_handler.hpp"
6
7#include <nlohmann/json.hpp>
8
9#include <memory>
10#include <string>
11#include <vector>
12
13namespace ipmi_flash
14{
Jason Linge82d7392020-11-19 13:27:02 -080015/**
16 * build a systemd file triggerable action from json data
17 */
Jason Lingc893f432020-10-24 19:31:44 -070018std::unique_ptr<TriggerableActionInterface>
19 buildFileSystemd(const nlohmann::json& data);
20
Jason Linge82d7392020-11-19 13:27:02 -080021/**
22 * build a systemd triggerable action from json data
23 */
Jason Lingc893f432020-10-24 19:31:44 -070024std::unique_ptr<TriggerableActionInterface>
25 buildSystemd(const nlohmann::json& data);
26
27/**
28 * HandlerConfig associates a blobID with an ImageHandler and a set of
29 * supported actions of type T.
30 */
31template <typename T>
32class HandlerConfig
33{
34 public:
35 HandlerConfig() = default;
36 ~HandlerConfig() = default;
37 HandlerConfig(const HandlerConfig&) = delete;
38 HandlerConfig& operator=(const HandlerConfig&) = delete;
39 HandlerConfig(HandlerConfig&&) = default;
40 HandlerConfig& operator=(HandlerConfig&&) = default;
41
42 /* A string in the form: /flash/{unique}, s.t. unique is something like,
43 * flash, ubitar, statictar, or bios
44 */
45 std::string blobId;
46
47 /* This owns a handler interface, this is typically going to be a file
48 * writer object.
49 */
50 std::unique_ptr<ImageHandlerInterface> handler;
51
52 /* specifies actions to be taken in response to certain operations on a
53 * blob.
54 * Usually required but there are exceptions; the hashBlobId doesn't have
55 * an action pack.
56 */
57 std::unique_ptr<T> actions;
58};
59
60/* HandlersBuilderIfc is a helper class that builds Handlers from the json files
61 * found within a specified directory.
62 * The child class that inherits from HandlersBuilderIfc should implement
63 * buildHandlersConfigs to perform json validation and parsing.
64 *
65 */
66template <typename T>
67class HandlersBuilderIfc
68{
69 public:
70 HandlersBuilderIfc() = default;
71 ~HandlersBuilderIfc() = default;
72 HandlersBuilderIfc(const HandlersBuilderIfc&) = delete;
73 HandlersBuilderIfc& operator=(const HandlersBuilderIfc&) = delete;
74 HandlersBuilderIfc(HandlersBuilderIfc&&) = default;
75 HandlersBuilderIfc& operator=(HandlersBuilderIfc&&) = default;
76 /**
77 * Given a folder of json configs, build the configurations.
78 *
79 * @param[in] directory - the directory to search (recurisvely).
80 * @return list of HandlerConfig objects.
81 */
82 std::vector<HandlerConfig<T>>
Jason Linge82d7392020-11-19 13:27:02 -080083 buildHandlerConfigs(const std::string& directory)
84 {
85
86 std::vector<HandlerConfig<T>> output;
87
88 std::vector<std::string> jsonPaths = GetJsonList(directory);
89 for (const auto& path : jsonPaths)
90 {
91 std::ifstream jsonFile(path);
92 if (!jsonFile.is_open())
93 {
94 std::fprintf(stderr, "Unable to open json file: %s\n",
95 path.c_str());
96 continue;
97 }
98
99 auto data = nlohmann::json::parse(jsonFile, nullptr, false);
100 if (data.is_discarded())
101 {
102 std::fprintf(stderr, "Parsing json failed: %s\n", path.c_str());
103 continue;
104 }
105
106 std::vector<HandlerConfig<T>> configs = buildHandlerFromJson(data);
107 std::move(configs.begin(), configs.end(),
108 std::back_inserter(output));
109 }
110 return output;
111 };
Jason Lingc893f432020-10-24 19:31:44 -0700112 /**
113 * Given a list of handlers as json data, construct the appropriate
114 * HandlerConfig objects. This method is meant to be called per json
115 * configuration file found.
116 *
117 * The list will only contain validly build HandlerConfig objects. Any
118 * invalid configuration is skipped. The hope is that the BMC firmware
119 * update configuration will never be invalid, but if another aspect is
120 * invalid, it can be fixed with a BMC firmware update once the bug is
121 * identified.
122 *
123 * This code does not validate that the blob specified is unique, that
124 * should be handled at a higher layer.
125 *
126 * @param[in] data - json data from a json file.
127 * @return list of HandlerConfig objects.
128 */
129 virtual std::vector<HandlerConfig<T>>
130 buildHandlerFromJson(const nlohmann::json& data) = 0;
131};
132} // namespace ipmi_flash