xref: /openbmc/entity-manager/src/entity_manager/configuration.hpp (revision fc9e7fda2bd21c5cc9afca2cef8af0f19b28dc08)
1*fc9e7fdaSChristopher Meis #pragma once
2*fc9e7fdaSChristopher Meis 
3*fc9e7fdaSChristopher Meis #include <nlohmann/json.hpp>
4*fc9e7fdaSChristopher Meis 
5*fc9e7fdaSChristopher Meis #include <list>
6*fc9e7fdaSChristopher Meis #include <set>
7*fc9e7fdaSChristopher Meis 
8*fc9e7fdaSChristopher Meis namespace configuration
9*fc9e7fdaSChristopher Meis {
10*fc9e7fdaSChristopher Meis constexpr const char* globalSchema = "global.json";
11*fc9e7fdaSChristopher Meis constexpr const char* hostConfigurationDirectory = SYSCONF_DIR "configurations";
12*fc9e7fdaSChristopher Meis constexpr const char* configurationDirectory = PACKAGE_DIR "configurations";
13*fc9e7fdaSChristopher Meis constexpr const char* currentConfiguration = "/var/configuration/system.json";
14*fc9e7fdaSChristopher Meis constexpr const char* schemaDirectory = PACKAGE_DIR "configurations/schemas";
15*fc9e7fdaSChristopher Meis 
16*fc9e7fdaSChristopher Meis bool writeJsonFiles(const nlohmann::json& systemConfiguration);
17*fc9e7fdaSChristopher Meis 
18*fc9e7fdaSChristopher Meis bool loadConfigurations(std::list<nlohmann::json>& configurations);
19*fc9e7fdaSChristopher Meis 
20*fc9e7fdaSChristopher Meis template <typename JsonType>
setJsonFromPointer(const std::string & ptrStr,const JsonType & value,nlohmann::json & systemConfiguration)21*fc9e7fdaSChristopher Meis bool setJsonFromPointer(const std::string& ptrStr, const JsonType& value,
22*fc9e7fdaSChristopher Meis                         nlohmann::json& systemConfiguration)
23*fc9e7fdaSChristopher Meis {
24*fc9e7fdaSChristopher Meis     try
25*fc9e7fdaSChristopher Meis     {
26*fc9e7fdaSChristopher Meis         nlohmann::json::json_pointer ptr(ptrStr);
27*fc9e7fdaSChristopher Meis         nlohmann::json& ref = systemConfiguration[ptr];
28*fc9e7fdaSChristopher Meis         ref = value;
29*fc9e7fdaSChristopher Meis         return true;
30*fc9e7fdaSChristopher Meis     }
31*fc9e7fdaSChristopher Meis     catch (const std::out_of_range&)
32*fc9e7fdaSChristopher Meis     {
33*fc9e7fdaSChristopher Meis         return false;
34*fc9e7fdaSChristopher Meis     }
35*fc9e7fdaSChristopher Meis }
36*fc9e7fdaSChristopher Meis 
37*fc9e7fdaSChristopher Meis void deriveNewConfiguration(const nlohmann::json& oldConfiguration,
38*fc9e7fdaSChristopher Meis                             nlohmann::json& newConfiguration);
39*fc9e7fdaSChristopher Meis 
40*fc9e7fdaSChristopher Meis bool validateJson(const nlohmann::json& schemaFile,
41*fc9e7fdaSChristopher Meis                   const nlohmann::json& input);
42*fc9e7fdaSChristopher Meis 
43*fc9e7fdaSChristopher Meis std::set<std::string> getProbeInterfaces();
44*fc9e7fdaSChristopher Meis 
45*fc9e7fdaSChristopher Meis } // namespace configuration
46