70 {
71 bool success = !
brokers.empty();
72
73#include "bridge-schema.json.h"
74
75 if (!success) {
76 try {
77 const nlohmann::json bridgeJsonSchema = nlohmann::json::parse(bridgeJsonSchemaString);
78
79 if (!fileName.empty()) {
80 std::ifstream bridgeConfigJsonFile(fileName);
81
82 if (bridgeConfigJsonFile.is_open()) {
83 VLOG(1) << "Bridge config JSON: " << fileName;
84
85 try {
86 nlohmann::json bridgesConfigJson;
87
88 bridgeConfigJsonFile >> bridgesConfigJson;
89
90 try {
91 const nlohmann::json_schema::json_validator
validator(bridgeJsonSchema);
92
93 try {
95
96 try {
97 bridgesConfigJson = bridgesConfigJson.patch(defaultPatch);
98
99 for (const nlohmann::json& bridgeConfigJson : bridgesConfigJson["bridges"]) {
101 bridgeConfigJson["name"], bridgeConfigJson["prefix"], bridgeConfigJson["disabled"]);
102
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"],
108 topicJson["qos"]);
109 }
110 }
111
112 const nlohmann::json& mqtt = brokerConfigJson["mqtt"];
113 const nlohmann::json& network = brokerConfigJson["network"];
114
115 const std::string fullInstanceName =
116 bridge.getName() + "+" + network["instance_name"].get<std::string>();
117
118 brokers.emplace(fullInstanceName,
119 Broker(bridge,
120 brokerConfigJson["session_store"],
121 fullInstanceName,
122 network["protocol"],
123 network["encryption"],
124 network["transport"],
125 network[network["protocol"]],
126 mqtt["client_id"],
127 mqtt["keep_alive"],
128 mqtt["clean_session"],
129 mqtt["will_topic"],
130 mqtt["will_message"],
131 mqtt["will_qos"],
132 mqtt["will_retain"],
133 mqtt["username"],
134 mqtt["password"],
135 mqtt["loop_prevention"],
136 brokerConfigJson["prefix"],
137 brokerConfigJson["disabled"],
138 topics));
139 }
140 }
141
142 success = true;
143 } catch (const std::exception& e) {
144 VLOG(1) << " Patching JSON with default patch failed:\n" << defaultPatch.dump(4);
145 VLOG(1) << " " << e.what();
146 }
147 } catch (const std::exception& e) {
148 VLOG(1) << " Validating JSON failed:\n" << bridgesConfigJson.dump(4);
149 VLOG(1) << " " << e.what();
150 }
151 } catch (const std::exception& e) {
152 VLOG(1) << " Setting root json mapping schema failed:\n" << bridgeJsonSchema.dump(4);
153 VLOG(1) << " " << e.what();
154 }
155 } catch (const std::exception& e) {
156 VLOG(1) << " JSON map file parsing failed:" << e.what() << " at " << bridgeConfigJsonFile.tellg();
157 }
158
159 bridgeConfigJsonFile.close();
160 } else {
161 VLOG(1) << "BridgeJsonConfig: " << fileName << " not found";
162 }
163 } else {
164
165 }
166 } catch (const std::exception& e) {
167 VLOG(1) << "Parsing schema failed: " << e.what();
168 VLOG(1) << bridgeJsonSchemaString;
169 }
170 } else {
171 VLOG(1) << "MappingFile already loaded and validated";
172 }
173
174 return success;
175 }
nlohmann::json_schema::json_validator validator
std::list< Bridge > bridgeList
json validate(const json &) const