blob: 095add57d48f1cc91b86e6826ea41c4259d609f8 [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
3#include <string.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07004#include <stdlib.h>
5#include <sys/types.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8#include <ifaddrs.h>
9#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010
11#include <set>
James Kuszmaul3ae42262019-11-08 12:33:41 -080012#include <string_view>
Brian Silverman66f079a2013-08-26 16:24:30 -070013
Austin Schuhcb108412019-10-13 16:09:54 -070014#include "absl/container/btree_set.h"
Austin Schuhcb108412019-10-13 16:09:54 -070015#include "aos/configuration_generated.h"
16#include "aos/flatbuffer_merge.h"
17#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080018#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070019#include "aos/unique_malloc_ptr.h"
20#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080021#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070022#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080023
24DEFINE_string(
25 override_hostname, "",
26 "If set, this forces the hostname of this node to be the provided "
27 "hostname.");
Brian Silverman66f079a2013-08-26 16:24:30 -070028
29namespace aos {
Austin Schuhcb108412019-10-13 16:09:54 -070030
Austin Schuh40485ed2019-10-26 21:51:44 -070031// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070032// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070033bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
34 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070035 int name_compare = lhs.message().name()->string_view().compare(
36 rhs.message().name()->string_view());
37 if (name_compare == 0) {
38 return lhs.message().type()->string_view() <
39 rhs.message().type()->string_view();
40 } else if (name_compare < 0) {
41 return true;
42 } else {
43 return false;
44 }
45}
46
Austin Schuh40485ed2019-10-26 21:51:44 -070047bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
48 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070049 return lhs.message().name()->string_view() ==
50 rhs.message().name()->string_view() &&
51 lhs.message().type()->string_view() ==
52 rhs.message().type()->string_view();
53}
54
Austin Schuh40485ed2019-10-26 21:51:44 -070055bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
56 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070057 return lhs.message().name()->string_view() ==
58 rhs.message().name()->string_view();
59}
60
Austin Schuh40485ed2019-10-26 21:51:44 -070061bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
62 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070063 return lhs.message().name()->string_view() <
64 rhs.message().name()->string_view();
65}
66
Austin Schuh217a9782019-12-21 23:02:50 -080067bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
68 const FlatbufferDetachedBuffer<Node> &rhs) {
69 return lhs.message().name()->string_view() ==
70 rhs.message().name()->string_view();
71}
72
73bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
74 const FlatbufferDetachedBuffer<Node> &rhs) {
75 return lhs.message().name()->string_view() <
76 rhs.message().name()->string_view();
77}
78
Brian Silverman66f079a2013-08-26 16:24:30 -070079namespace configuration {
80namespace {
81
Austin Schuhcb108412019-10-13 16:09:54 -070082// Extracts the folder part of a path. Returns ./ if there is no path.
James Kuszmaul3ae42262019-11-08 12:33:41 -080083std::string_view ExtractFolder(
84 const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -070085 auto last_slash_pos = filename.find_last_of("/\\");
86
James Kuszmaul3ae42262019-11-08 12:33:41 -080087 return last_slash_pos == std::string_view::npos
88 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -070089 : filename.substr(0, last_slash_pos + 1);
90}
91
Austin Schuh40485ed2019-10-26 21:51:44 -070092FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -080093 const std::string_view path, absl::btree_set<std::string> *visited_paths) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070094 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
95 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
96
97 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file";
98
99 FlatbufferDetachedBuffer<Configuration> config(std::move(buffer));
Austin Schuhcb108412019-10-13 16:09:54 -0700100 // Depth first. Take the following example:
101 //
102 // config1.json:
103 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700104 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700105 // {
106 // "name": "/foo",
107 // "type": ".aos.bar",
108 // "max_size": 5
109 // }
110 // ],
111 // "imports": [
112 // "config2.json",
113 // ]
114 // }
115 //
116 // config2.json:
117 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700118 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700119 // {
120 // "name": "/foo",
121 // "type": ".aos.bar",
122 // "max_size": 7
123 // }
124 // ],
125 // }
126 //
127 // We want the main config (config1.json) to be able to override the imported
128 // config. That means that it needs to be merged into the imported configs,
129 // not the other way around.
130
131 // Track that we have seen this file before recursing.
132 visited_paths->insert(::std::string(path));
133
134 if (config.message().has_imports()) {
135 // Capture the imports.
136 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
137 config.message().imports();
138
139 // And then wipe them. This gets GCed when we merge later.
140 config.mutable_message()->clear_imports();
141
142 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700143 FlatbufferDetachedBuffer<Configuration> merged_config =
144 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700145
146 const ::std::string folder(ExtractFolder(path));
147
148 for (const flatbuffers::String *str : *v) {
149 const ::std::string included_config = folder + str->c_str();
150 // Abort on any paths we have already seen.
151 CHECK(visited_paths->find(included_config) == visited_paths->end())
152 << ": Found duplicate file " << included_config << " while reading "
153 << path;
154
155 // And them merge everything in.
156 merged_config = MergeFlatBuffers(
157 merged_config, ReadConfig(included_config, visited_paths));
158 }
159
160 // Finally, merge this file in.
161 config = MergeFlatBuffers(merged_config, config);
162 }
163 return config;
164}
165
Alex Perrycb7da4b2019-08-28 19:35:56 -0700166// Compares (c < p) a channel, and a name, type tuple.
167bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800168 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700169 int name_compare = c->name()->string_view().compare(p.first);
170 if (name_compare == 0) {
171 return c->type()->string_view() < p.second;
172 } else if (name_compare < 0) {
173 return true;
174 } else {
175 return false;
176 }
177};
178
179// Compares for equality (c == p) a channel, and a name, type tuple.
180bool EqualsChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800181 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 return c->name()->string_view() == p.first &&
183 c->type()->string_view() == p.second;
184}
185
186// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800187bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 return a->name()->string_view() < name;
189};
190
191// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800192bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 return a->name()->string_view() == name;
194}
195
196// Maps name for the provided maps. Modifies name.
197void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800198 std::string_view *name, std::string_view type,
199 const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 // For the same reason we merge configs in reverse order, we want to process
201 // maps in reverse order. That lets the outer config overwrite channels from
202 // the inner configs.
203 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800204 if (!i->has_match() || !i->match()->has_name()) {
205 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700206 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800207 if (!i->has_rename() || !i->rename()->has_name()) {
208 continue;
209 }
210
211 // Handle normal maps (now that we know that match and rename are filled
212 // out).
213 if (i->match()->name()->string_view() != *name) {
214 continue;
215 }
216
217 // Handle type specific maps.
218 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
219 continue;
220 }
221
222 if (node != nullptr && i->match()->has_source_node() &&
223 i->match()->source_node()->string_view() !=
224 node->name()->string_view()) {
225 continue;
226 }
227
228 VLOG(1) << "Renamed \"" << *name << "\" to \""
229 << i->rename()->name()->string_view() << "\"";
230 *name = i->rename()->name()->string_view();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700231 }
232}
233
234} // namespace
235
Austin Schuh40485ed2019-10-26 21:51:44 -0700236FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700237 const Flatbuffer<Configuration> &config) {
Austin Schuh40485ed2019-10-26 21:51:44 -0700238 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700239 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700240 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700241
Austin Schuh40485ed2019-10-26 21:51:44 -0700242 if (config.message().has_channels()) {
243 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700244 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700245 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700246 continue;
247 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700248 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700249 continue;
250 }
251
Austin Schuh40485ed2019-10-26 21:51:44 -0700252 // Attempt to insert the channel.
253 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700254 if (!result.second) {
255 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700256 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700257 }
258 }
259 }
260
261 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700262 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700263 if (config.message().has_applications()) {
264 for (const Application *a : *config.message().applications()) {
265 if (!a->has_name()) {
266 continue;
267 }
268
269 auto result = applications.insert(CopyFlatBuffer(a));
270 if (!result.second) {
271 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
272 }
273 }
274 }
275
Austin Schuh217a9782019-12-21 23:02:50 -0800276 // Now repeat this for the node list.
277 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
278 if (config.message().has_nodes()) {
279 for (const Node *n : *config.message().nodes()) {
280 if (!n->has_name()) {
281 continue;
282 }
283
284 auto result = nodes.insert(CopyFlatBuffer(n));
285 if (!result.second) {
286 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
287 }
288 }
289 }
290
Austin Schuhcb108412019-10-13 16:09:54 -0700291 flatbuffers::FlatBufferBuilder fbb;
292 fbb.ForceDefaults(1);
293
294 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700295 // Channels
296 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
297 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700298 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700299 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
300 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
301 channel_offsets.emplace_back(
302 CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700303 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700304 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700305 }
306
307 // Applications
308 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
309 applications_offset;
310 {
311 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700312 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700313 applications_offsets.emplace_back(
314 CopyFlatBuffer<Application>(&a.message(), &fbb));
315 }
316 applications_offset = fbb.CreateVector(applications_offsets);
317 }
318
319 // Just copy the maps
320 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
321 maps_offset;
322 {
323 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
324 if (config.message().has_maps()) {
325 for (const Map *m : *config.message().maps()) {
326 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
327 }
328 maps_offset = fbb.CreateVector(map_offsets);
329 }
330 }
331
Austin Schuh217a9782019-12-21 23:02:50 -0800332 // Nodes
333 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
334 nodes_offset;
335 {
336 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
337 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
338 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
339 }
340 nodes_offset = fbb.CreateVector(node_offsets);
341 }
342
Austin Schuhcb108412019-10-13 16:09:54 -0700343 // And then build a Configuration with them all.
344 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700345 configuration_builder.add_channels(channels_offset);
Austin Schuhcb108412019-10-13 16:09:54 -0700346 if (config.message().has_maps()) {
347 configuration_builder.add_maps(maps_offset);
348 }
Austin Schuh217a9782019-12-21 23:02:50 -0800349 if (config.message().has_applications()) {
350 configuration_builder.add_applications(applications_offset);
351 }
352 if (config.message().has_nodes()) {
353 configuration_builder.add_nodes(nodes_offset);
354 }
Austin Schuhcb108412019-10-13 16:09:54 -0700355
356 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800357
358 // Now, validate that if there is a node list, every channel has a source
359 // node.
360 FlatbufferDetachedBuffer<Configuration> result(fbb.Release());
361
362 // Check that if there is a node list, all the source nodes are filled out and
363 // valid, and all the destination nodes are valid (and not the source). This
364 // is a basic consistency check.
365 if (result.message().has_nodes()) {
366 for (const Channel *c : *config.message().channels()) {
367 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
368 << " is missing \"source_node\"";
369 CHECK(GetNode(&result.message(), c->source_node()->string_view()) !=
370 nullptr)
371 << ": Channel " << FlatbufferToJson(c)
372 << " has an unknown \"source_node\"";
373
374 if (c->has_destination_nodes()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800375 for (const Connection *connection : *c->destination_nodes()) {
376 CHECK(connection->has_name());
377 CHECK(GetNode(&result.message(), connection->name()->string_view()) !=
378 nullptr)
Austin Schuh217a9782019-12-21 23:02:50 -0800379 << ": Channel " << FlatbufferToJson(c)
Austin Schuh719946b2019-12-28 14:51:01 -0800380 << " has an unknown \"destination_nodes\" "
381 << connection->name()->string_view();
Austin Schuh217a9782019-12-21 23:02:50 -0800382
Austin Schuh719946b2019-12-28 14:51:01 -0800383 switch (connection->timestamp_logger()) {
384 case LoggerConfig::LOCAL_LOGGER:
385 case LoggerConfig::NOT_LOGGED:
386 CHECK(!connection->has_timestamp_logger_node());
387 break;
388 case LoggerConfig::REMOTE_LOGGER:
389 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
390 CHECK(connection->has_timestamp_logger_node());
391 CHECK(
392 GetNode(&result.message(),
393 connection->timestamp_logger_node()->string_view()) !=
394 nullptr)
395 << ": Channel " << FlatbufferToJson(c)
396 << " has an unknown \"timestamp_logger_node\""
397 << connection->name()->string_view();
398 break;
399 }
400
401 CHECK_NE(connection->name()->string_view(),
402 c->source_node()->string_view())
Austin Schuh217a9782019-12-21 23:02:50 -0800403 << ": Channel " << FlatbufferToJson(c)
404 << " is forwarding data to itself";
405 }
406 }
407 }
408 }
409
410 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700411}
412
Austin Schuh40485ed2019-10-26 21:51:44 -0700413FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800414 const std::string_view path) {
Austin Schuhcb108412019-10-13 16:09:54 -0700415 // We only want to read a file once. So track the visited files in a set.
416 absl::btree_set<std::string> visited_paths;
417 return MergeConfiguration(ReadConfig(path, &visited_paths));
418}
419
James Kuszmaul3ae42262019-11-08 12:33:41 -0800420const Channel *GetChannel(const Configuration *config, std::string_view name,
421 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800422 std::string_view application_name, const Node *node) {
423 const std::string_view original_name = name;
Austin Schuhcb108412019-10-13 16:09:54 -0700424 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
425 << "\" }";
426
427 // First handle application specific maps. Only do this if we have a matching
428 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700429 if (config->has_applications()) {
430 auto application_iterator = std::lower_bound(
431 config->applications()->cbegin(), config->applications()->cend(),
432 application_name, CompareApplications);
433 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700434 EqualsApplications(*application_iterator, application_name)) {
435 if (application_iterator->has_maps()) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800436 HandleMaps(application_iterator->maps(), &name, type, node);
Austin Schuhcb108412019-10-13 16:09:54 -0700437 }
438 }
439 }
440
441 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700442 if (config->has_maps()) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800443 HandleMaps(config->maps(), &name, type, node);
Austin Schuhcb108412019-10-13 16:09:54 -0700444 }
445
Austin Schuhbca6cf02019-12-22 17:28:34 -0800446 if (original_name != name) {
447 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
448 << type << "\" }";
449 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700450
Austin Schuh40485ed2019-10-26 21:51:44 -0700451 // Then look for the channel.
452 auto channel_iterator =
453 std::lower_bound(config->channels()->cbegin(),
454 config->channels()->cend(),
455 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700456
457 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700458 if (channel_iterator != config->channels()->cend() &&
459 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800460 if (VLOG_IS_ON(2)) {
461 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
462 } else if (VLOG_IS_ON(1)) {
463 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
464 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700465 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700466 } else {
467 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
468 << type << "\" }";
469 return nullptr;
470 }
471}
472
Austin Schuhbca6cf02019-12-22 17:28:34 -0800473std::string CleanedChannelToString(const Channel *channel) {
474 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
475 cleaned_channel.mutable_message()->clear_schema();
476 return FlatbufferToJson(cleaned_channel);
477}
478
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
480 const Flatbuffer<Configuration> &config,
481 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
482 flatbuffers::FlatBufferBuilder fbb;
483 fbb.ForceDefaults(1);
484
485 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
486 channels_offset;
487 if (config.message().has_channels()) {
488 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
489 for (const Channel *c : *config.message().channels()) {
490 flatbuffers::FlatBufferBuilder channel_fbb;
491 channel_fbb.ForceDefaults(1);
492
493 // Search for a schema with a matching type.
494 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
495 for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) {
496 if (schema.message().root_table() != nullptr) {
497 if (schema.message().root_table()->name()->string_view() ==
498 c->type()->string_view()) {
499 found_schema = &schema;
500 }
501 }
502 }
503
504 CHECK(found_schema != nullptr)
505 << ": Failed to find schema for " << FlatbufferToJson(c);
506
507 // The following is wasteful, but works.
508 //
509 // Copy it into a Channel object by creating an object with only the
510 // schema populated and merge that into the current channel.
511 flatbuffers::Offset<reflection::Schema> schema_offset =
512 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
513 &channel_fbb);
514 Channel::Builder channel_builder(channel_fbb);
515 channel_builder.add_schema(schema_offset);
516 channel_fbb.Finish(channel_builder.Finish());
517 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
518 channel_fbb.Release());
519
520 FlatbufferDetachedBuffer<Channel> merged_channel(
521 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
522
523 channel_offsets.emplace_back(
524 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
525 }
526 channels_offset = fbb.CreateVector(channel_offsets);
527 }
528
529 // Copy the applications and maps unmodified.
530 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
531 applications_offset;
532 {
533 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
534 if (config.message().has_applications()) {
535 for (const Application *a : *config.message().applications()) {
536 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
537 }
538 }
539 applications_offset = fbb.CreateVector(applications_offsets);
540 }
541
542 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
543 maps_offset;
544 {
545 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
546 if (config.message().has_maps()) {
547 for (const Map *m : *config.message().maps()) {
548 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
549 }
550 maps_offset = fbb.CreateVector(map_offsets);
551 }
552 }
553
Austin Schuh217a9782019-12-21 23:02:50 -0800554 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
555 nodes_offset;
556 {
557 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
558 if (config.message().has_nodes()) {
559 for (const Node *n : *config.message().nodes()) {
560 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
561 }
562 nodes_offset = fbb.CreateVector(node_offsets);
563 }
564 }
565
566 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567 ConfigurationBuilder configuration_builder(fbb);
568 if (config.message().has_channels()) {
569 configuration_builder.add_channels(channels_offset);
570 }
571 if (config.message().has_maps()) {
572 configuration_builder.add_maps(maps_offset);
573 }
574 if (config.message().has_applications()) {
575 configuration_builder.add_applications(applications_offset);
576 }
Austin Schuh217a9782019-12-21 23:02:50 -0800577 if (config.message().has_nodes()) {
578 configuration_builder.add_nodes(nodes_offset);
579 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700580
581 fbb.Finish(configuration_builder.Finish());
582 return fbb.Release();
583}
584
Austin Schuh217a9782019-12-21 23:02:50 -0800585const Node *GetNodeFromHostname(const Configuration *config,
586 std::string_view hostname) {
587 for (const Node *node : *config->nodes()) {
588 if (node->hostname()->string_view() == hostname) {
589 return node;
590 }
591 }
592 return nullptr;
593}
Austin Schuhac0771c2020-01-07 18:36:30 -0800594
Austin Schuh217a9782019-12-21 23:02:50 -0800595const Node *GetMyNode(const Configuration *config) {
596 const std::string hostname = (FLAGS_override_hostname.size() > 0)
597 ? FLAGS_override_hostname
598 : network::GetHostname();
599 const Node *node = GetNodeFromHostname(config, hostname);
600 if (node != nullptr) return node;
601
602 LOG(FATAL) << "Unknown node for host: " << hostname
603 << ". Consider using --override_hostname if hostname detection "
604 "is wrong.";
605 return nullptr;
606}
607
608const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800609 CHECK(config->has_nodes())
610 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800611 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800612 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800613 if (node->name()->string_view() == name) {
614 return node;
615 }
616 }
617 return nullptr;
618}
619
Austin Schuhac0771c2020-01-07 18:36:30 -0800620bool MultiNode(const Configuration *config) { return config->has_nodes(); }
621
Austin Schuh217a9782019-12-21 23:02:50 -0800622bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800623 if (node == nullptr) {
624 return true;
625 }
Austin Schuh217a9782019-12-21 23:02:50 -0800626 return (channel->source_node()->string_view() == node->name()->string_view());
627}
628
629bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800630 if (node == nullptr) {
631 return true;
632 }
633
Austin Schuh217a9782019-12-21 23:02:50 -0800634 if (channel->source_node()->string_view() == node->name()->string_view()) {
635 return true;
636 }
637
638 if (!channel->has_destination_nodes()) {
639 return false;
640 }
641
Austin Schuh719946b2019-12-28 14:51:01 -0800642 for (const Connection *connection : *channel->destination_nodes()) {
643 CHECK(connection->has_name());
644 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800645 return true;
646 }
647 }
648
649 return false;
650}
651
Austin Schuh719946b2019-12-28 14:51:01 -0800652bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
653 switch(channel->logger()) {
654 case LoggerConfig::LOCAL_LOGGER:
655 if (node == nullptr) {
656 // Single node world. If there is a local logger, then we want to use
657 // it.
658 return true;
659 }
660 return channel->source_node()->string_view() ==
661 node->name()->string_view();
662 case LoggerConfig::REMOTE_LOGGER:
663 CHECK(channel->has_logger_node());
664
665 return channel->logger_node()->string_view() ==
666 CHECK_NOTNULL(node)->name()->string_view();
667 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
668 CHECK(channel->has_logger_node());
669
670 if (channel->source_node()->string_view() ==
671 CHECK_NOTNULL(node)->name()->string_view()) {
672 return true;
673 }
674 if (channel->logger_node()->string_view() == node->name()->string_view()) {
675 return true;
676 }
677
678 return false;
679 case LoggerConfig::NOT_LOGGED:
680 return false;
681 }
682
683 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
684}
685
686const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
687 if (!channel->has_destination_nodes()) {
688 return nullptr;
689 }
690 for (const Connection *connection : *channel->destination_nodes()) {
691 if (connection->name()->string_view() == node->name()->string_view()) {
692 return connection;
693 }
694 }
695 return nullptr;
696}
697
698bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
699 const Node *node,
700 const Node *logger_node) {
701 const Connection *connection = ConnectionToNode(channel, node);
702 if (connection == nullptr) {
703 return false;
704 }
705 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
706}
707
708bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
709 const Node *node) {
710 switch (connection->timestamp_logger()) {
711 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
712 CHECK(connection->has_timestamp_logger_node());
713 if (connection->name()->string_view() == node->name()->string_view()) {
714 return true;
715 }
716
717 if (connection->timestamp_logger_node()->string_view() ==
718 node->name()->string_view()) {
719 return true;
720 }
721
722 return false;
723 case LoggerConfig::LOCAL_LOGGER:
724 return connection->name()->string_view() == node->name()->string_view();
725 case LoggerConfig::REMOTE_LOGGER:
726 CHECK(connection->has_timestamp_logger_node());
727
728 return connection->timestamp_logger_node()->string_view() ==
729 node->name()->string_view();
730 case LoggerConfig::NOT_LOGGED:
731 return false;
732 }
733
734 LOG(FATAL) << "Unknown logger config "
735 << static_cast<int>(connection->timestamp_logger());
736}
737
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800738std::vector<std::string_view> SourceNodeNames(const Configuration *config,
739 const Node *my_node) {
740 std::set<std::string_view> result_set;
741
742 for (const Channel *channel : *config->channels()) {
743 if (channel->has_destination_nodes()) {
744 for (const Connection *connection : *channel->destination_nodes()) {
745 if (connection->name()->string_view() ==
746 my_node->name()->string_view()) {
747 result_set.insert(channel->source_node()->string_view());
748 }
749 }
750 }
751 }
752
753 std::vector<std::string_view> result;
754 for (const std::string_view source : result_set) {
755 VLOG(1) << "Found a source node of " << source;
756 result.emplace_back(source);
757 }
758 return result;
759}
760
761std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
762 const Node *my_node) {
763 std::vector<std::string_view> result;
764
765 for (const Channel *channel : *config->channels()) {
766 if (channel->has_source_node() && channel->source_node()->string_view() ==
767 my_node->name()->string_view()) {
768 if (!channel->has_destination_nodes()) continue;
769
770 if (channel->source_node()->string_view() !=
771 my_node->name()->string_view()) {
772 continue;
773 }
774
775 for (const Connection *connection : *channel->destination_nodes()) {
776 if (std::find(result.begin(), result.end(),
777 connection->name()->string_view()) == result.end()) {
778 result.emplace_back(connection->name()->string_view());
779 }
780 }
781 }
782 }
783
784 for (const std::string_view destination : result) {
785 VLOG(1) << "Found a destination node of " << destination;
786 }
787 return result;
788}
789
Brian Silverman66f079a2013-08-26 16:24:30 -0700790} // namespace configuration
791} // namespace aos