xref: /openbmc/entity-manager/src/entity_manager/configuration.hpp (revision f7252577794639be8f22da52ec09c621de44dcda)
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* hostConfigurationDirectory = SYSCONF_DIR "configurations";
10 constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
11 constexpr const char* currentConfiguration = "/var/configuration/system.json";
12 constexpr const char* schemaDirectory = PACKAGE_DIR "schemas";
13 
14 class Configuration
15 {
16   public:
17     explicit Configuration();
18     std::unordered_set<std::string> probeInterfaces;
19     std::vector<nlohmann::json> configurations;
20 
21   private:
22     void loadConfigurations();
23     void filterProbeInterfaces();
24 };
25 
26 bool writeJsonFiles(const nlohmann::json& systemConfiguration);
27 
28 template <typename JsonType>
setJsonFromPointer(const std::string & ptrStr,const JsonType & value,nlohmann::json & systemConfiguration)29 bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
30                         nlohmann::json& systemConfiguration)
31 {
32     try
33     {
34         nlohmann::json::json_pointer ptr(ptrStr);
35         nlohmann::json& ref = systemConfiguration[ptr];
36         ref = value;
37         return true;
38     }
39     catch (const std::out_of_range&)
40     {
41         return false;
42     }
43 }
44 
45 void deriveNewConfiguration(const nlohmann::json& oldConfiguration,
46                             nlohmann::json& newConfiguration);
47 
48 bool validateJson(const nlohmann::json& schemaFile,
49                   const nlohmann::json& input);
50