blob: 3cd94d449b5dff6c8807860d55803d2b52a6e257 [file] [log] [blame]
Christopher Meisbdaa6b22025-04-02 10:49:02 +02001#pragma once
2
3#include <nlohmann/json.hpp>
4
Christopher Meisf7252572025-06-11 13:22:05 +02005#include <unordered_set>
6#include <vector>
Christopher Meisbdaa6b22025-04-02 10:49:02 +02007
Christopher Meisbdaa6b22025-04-02 10:49:02 +02008constexpr const char* globalSchema = "global.json";
Christopher Meisbdaa6b22025-04-02 10:49:02 +02009constexpr const char* currentConfiguration = "/var/configuration/system.json";
Alexander Hansenddc8eb62025-04-25 18:08:51 +020010constexpr const char* schemaDirectory = PACKAGE_DIR "schemas";
Christopher Meisbdaa6b22025-04-02 10:49:02 +020011
Christopher Meisf7252572025-06-11 13:22:05 +020012class Configuration
13{
14 public:
Alexander Hansen8290ca42025-08-04 15:27:22 +020015 explicit Configuration(
16 const std::vector<std::filesystem::path>& configurationDirectories);
Christopher Meisf7252572025-06-11 13:22:05 +020017 std::unordered_set<std::string> probeInterfaces;
18 std::vector<nlohmann::json> configurations;
Christopher Meisbdaa6b22025-04-02 10:49:02 +020019
Alexander Hansen8290ca42025-08-04 15:27:22 +020020 protected:
Christopher Meisf7252572025-06-11 13:22:05 +020021 void loadConfigurations();
22 void filterProbeInterfaces();
Alexander Hansen8290ca42025-08-04 15:27:22 +020023
24 private:
25 std::vector<std::filesystem::path> configurationDirectories;
Christopher Meisf7252572025-06-11 13:22:05 +020026};
27
28bool writeJsonFiles(const nlohmann::json& systemConfiguration);
Christopher Meisbdaa6b22025-04-02 10:49:02 +020029
30template <typename JsonType>
31bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
32 nlohmann::json& systemConfiguration)
33{
34 try
35 {
36 nlohmann::json::json_pointer ptr(ptrStr);
37 nlohmann::json& ref = systemConfiguration[ptr];
38 ref = value;
39 return true;
40 }
41 catch (const std::out_of_range&)
42 {
43 return false;
44 }
45}
46
47void deriveNewConfiguration(const nlohmann::json& oldConfiguration,
48 nlohmann::json& newConfiguration);
49
50bool validateJson(const nlohmann::json& schemaFile,
51 const nlohmann::json& input);