70 bool BridgeStore::loadAndValidate(
const std::string& fileName) {
71 bool success = !
brokers.empty();
73#include "bridge-schema.json.h"
77 const nlohmann::json bridgeJsonSchema =
nlohmann::json::parse(bridgeJsonSchemaString);
79 if (!fileName.empty()) {
80 std::ifstream bridgeConfigJsonFile(fileName);
82 if (bridgeConfigJsonFile.is_open()) {
83 VLOG(1) <<
"Bridge config JSON: " << fileName;
88 bridgeConfigJsonFile >> bridgesConfigJson;
97 bridgesConfigJson = bridgesConfigJson.patch(defaultPatch);
99 for (
const nlohmann::json& bridgeConfigJson : bridgesConfigJson[
"bridges"]) {
100 Bridge& bridge = bridgeList.emplace_back(
101 bridgeConfigJson[
"name"], bridgeConfigJson[
"prefix"], bridgeConfigJson[
"disabled"]);
103 for (
const nlohmann::json& brokerConfigJson : bridgeConfigJson[
"brokers"]) {
104 std::list<iot::mqtt::Topic> topics;
105 for (
const nlohmann::json& topicJson : brokerConfigJson[
"topics"]) {
106 if (!topicJson[
"topic"].get<std::string>().empty()) {
107 topics.emplace_back(topicJson[
"topic"],
112 const nlohmann::json& mqtt = brokerConfigJson[
"mqtt"];
113 const nlohmann::json& network = brokerConfigJson[
"network"];
115 const std::string fullInstanceName =
116 bridge.getName() +
"+" + network[
"instance_name"].get<std::string>();
118 brokers.emplace(fullInstanceName,
120 brokerConfigJson[
"session_store"],
123 network[
"encryption"],
124 network[
"transport"],
125 network[network[
"protocol"]],
128 mqtt[
"clean_session"],
130 mqtt[
"will_message"],
135 mqtt[
"loop_prevention"],
136 brokerConfigJson[
"prefix"],
137 brokerConfigJson[
"disabled"],
143 }
catch (
const std::exception& e) {
144 VLOG(1) <<
" Patching JSON with default patch failed:\n" << defaultPatch.dump(4);
145 VLOG(1) <<
" " << e.what();
147 }
catch (
const std::exception& e) {
148 VLOG(1) <<
" Validating JSON failed:\n" << bridgesConfigJson.dump(4);
149 VLOG(1) <<
" " << e.what();
151 }
catch (
const std::exception& e) {
152 VLOG(1) <<
" Setting root json mapping schema failed:\n" << bridgeJsonSchema.dump(4);
153 VLOG(1) <<
" " << e.what();
155 }
catch (
const std::exception& e) {
156 VLOG(1) <<
" JSON map file parsing failed:" << e.what() <<
" at " << bridgeConfigJsonFile.tellg();
159 bridgeConfigJsonFile.close();
161 VLOG(1) <<
"BridgeJsonConfig: " << fileName <<
" not found";
166 }
catch (
const std::exception& e) {
167 VLOG(1) <<
"Parsing schema failed: " << e.what();
168 VLOG(1) << bridgeJsonSchemaString;
171 VLOG(1) <<
"MappingFile already loaded and validated";