1 #pragma once 2 3 #include <nlohmann/json.hpp> 4 5 #include <unordered_set> 6 #include <vector> 7 8 constexpr const char* globalSchema = "global.json"; 9 constexpr const char* currentConfiguration = "/var/configuration/system.json"; 10 constexpr const char* schemaDirectory = PACKAGE_DIR "schemas"; 11 12 class Configuration 13 { 14 public: 15 explicit Configuration( 16 const std::vector<std::filesystem::path>& configurationDirectories); 17 std::unordered_set<std::string> probeInterfaces; 18 std::vector<nlohmann::json> configurations; 19 20 protected: 21 void loadConfigurations(); 22 void filterProbeInterfaces(); 23 24 private: 25 std::vector<std::filesystem::path> configurationDirectories; 26 }; 27 28 bool writeJsonFiles(const nlohmann::json& systemConfiguration); 29 30 template <typename JsonType> setJsonFromPointer(const std::string & ptrStr,const JsonType & value,nlohmann::json & systemConfiguration)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 47 void deriveNewConfiguration(const nlohmann::json& oldConfiguration, 48 nlohmann::json& newConfiguration); 49 50 bool validateJson(const nlohmann::json& schemaFile, 51 const nlohmann::json& input); 52