xref: /openbmc/entity-manager/src/entity_manager/configuration.hpp (revision bc0b05bea374093673a94cfe3b8d455a72f3b9ad)
1 #pragma once
2 
3 #include <nlohmann/json.hpp>
4 
5 #include <unordered_set>
6 #include <vector>
7 
8 constexpr const char* currentConfiguration = "/var/configuration/system.json";
9 
10 class Configuration
11 {
12   public:
13     explicit Configuration(
14         const std::vector<std::filesystem::path>& configurationDirectories,
15         const std::filesystem::path& schemaDirectory);
16     std::unordered_set<std::string> probeInterfaces;
17     std::vector<nlohmann::json> configurations;
18 
19     const std::filesystem::path schemaDirectory;
20 
21   protected:
22     void loadConfigurations();
23     void filterProbeInterfaces();
24 
25   private:
26     std::vector<std::filesystem::path> configurationDirectories;
27 };
28 
29 bool writeJsonFiles(const nlohmann::json& systemConfiguration);
30 
31 template <typename JsonType>
setJsonFromPointer(const std::string & ptrStr,const JsonType & value,nlohmann::json & systemConfiguration)32 bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
33                         nlohmann::json& systemConfiguration)
34 {
35     try
36     {
37         nlohmann::json::json_pointer ptr(ptrStr);
38         nlohmann::json& ref = systemConfiguration[ptr];
39         ref = value;
40         return true;
41     }
42     catch (const std::out_of_range&)
43     {
44         return false;
45     }
46 }
47 
48 void deriveNewConfiguration(const nlohmann::json& oldConfiguration,
49                             nlohmann::json& newConfiguration);
50 
51 bool validateJson(const nlohmann::json& schemaFile,
52                   const nlohmann::json& input);
53