blob: 2d4b867e744bc9527159cf7a0e92fbdb5a4f74b8 [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 Schuhc9e10ec2020-01-26 16:08:28 -0800473size_t ChannelIndex(const Configuration *configuration,
474 const Channel *channel) {
475 CHECK(configuration->channels() != nullptr) << ": No channels";
476
477 auto c = std::find(configuration->channels()->begin(),
478 configuration->channels()->end(), channel);
479 CHECK(c != configuration->channels()->end())
480 << ": Channel pointer not found in configuration()->channels()";
481
482 return std::distance(configuration->channels()->begin(), c);
483}
484
Austin Schuhbca6cf02019-12-22 17:28:34 -0800485std::string CleanedChannelToString(const Channel *channel) {
486 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
487 cleaned_channel.mutable_message()->clear_schema();
488 return FlatbufferToJson(cleaned_channel);
489}
490
Alex Perrycb7da4b2019-08-28 19:35:56 -0700491FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
492 const Flatbuffer<Configuration> &config,
493 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
494 flatbuffers::FlatBufferBuilder fbb;
495 fbb.ForceDefaults(1);
496
497 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
498 channels_offset;
499 if (config.message().has_channels()) {
500 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
501 for (const Channel *c : *config.message().channels()) {
502 flatbuffers::FlatBufferBuilder channel_fbb;
503 channel_fbb.ForceDefaults(1);
504
505 // Search for a schema with a matching type.
506 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
507 for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) {
508 if (schema.message().root_table() != nullptr) {
509 if (schema.message().root_table()->name()->string_view() ==
510 c->type()->string_view()) {
511 found_schema = &schema;
512 }
513 }
514 }
515
516 CHECK(found_schema != nullptr)
517 << ": Failed to find schema for " << FlatbufferToJson(c);
518
519 // The following is wasteful, but works.
520 //
521 // Copy it into a Channel object by creating an object with only the
522 // schema populated and merge that into the current channel.
523 flatbuffers::Offset<reflection::Schema> schema_offset =
524 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
525 &channel_fbb);
526 Channel::Builder channel_builder(channel_fbb);
527 channel_builder.add_schema(schema_offset);
528 channel_fbb.Finish(channel_builder.Finish());
529 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
530 channel_fbb.Release());
531
532 FlatbufferDetachedBuffer<Channel> merged_channel(
533 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
534
535 channel_offsets.emplace_back(
536 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
537 }
538 channels_offset = fbb.CreateVector(channel_offsets);
539 }
540
541 // Copy the applications and maps unmodified.
542 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
543 applications_offset;
544 {
545 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
546 if (config.message().has_applications()) {
547 for (const Application *a : *config.message().applications()) {
548 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
549 }
550 }
551 applications_offset = fbb.CreateVector(applications_offsets);
552 }
553
554 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
555 maps_offset;
556 {
557 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
558 if (config.message().has_maps()) {
559 for (const Map *m : *config.message().maps()) {
560 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
561 }
562 maps_offset = fbb.CreateVector(map_offsets);
563 }
564 }
565
Austin Schuh217a9782019-12-21 23:02:50 -0800566 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
567 nodes_offset;
568 {
569 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
570 if (config.message().has_nodes()) {
571 for (const Node *n : *config.message().nodes()) {
572 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
573 }
574 nodes_offset = fbb.CreateVector(node_offsets);
575 }
576 }
577
578 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700579 ConfigurationBuilder configuration_builder(fbb);
580 if (config.message().has_channels()) {
581 configuration_builder.add_channels(channels_offset);
582 }
583 if (config.message().has_maps()) {
584 configuration_builder.add_maps(maps_offset);
585 }
586 if (config.message().has_applications()) {
587 configuration_builder.add_applications(applications_offset);
588 }
Austin Schuh217a9782019-12-21 23:02:50 -0800589 if (config.message().has_nodes()) {
590 configuration_builder.add_nodes(nodes_offset);
591 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592
593 fbb.Finish(configuration_builder.Finish());
594 return fbb.Release();
595}
596
Austin Schuh217a9782019-12-21 23:02:50 -0800597const Node *GetNodeFromHostname(const Configuration *config,
598 std::string_view hostname) {
599 for (const Node *node : *config->nodes()) {
600 if (node->hostname()->string_view() == hostname) {
601 return node;
602 }
603 }
604 return nullptr;
605}
Austin Schuhac0771c2020-01-07 18:36:30 -0800606
Austin Schuh217a9782019-12-21 23:02:50 -0800607const Node *GetMyNode(const Configuration *config) {
608 const std::string hostname = (FLAGS_override_hostname.size() > 0)
609 ? FLAGS_override_hostname
610 : network::GetHostname();
611 const Node *node = GetNodeFromHostname(config, hostname);
612 if (node != nullptr) return node;
613
614 LOG(FATAL) << "Unknown node for host: " << hostname
615 << ". Consider using --override_hostname if hostname detection "
616 "is wrong.";
617 return nullptr;
618}
619
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800620const Node *GetNode(const Configuration *config, const Node *node) {
621 if (!MultiNode(config)) {
622 CHECK(node == nullptr) << ": Provided a node in a single node world.";
623 return nullptr;
624 } else {
625 CHECK(node != nullptr);
626 CHECK(node->has_name());
627 return GetNode(config, node->name()->string_view());
628 }
629}
630
Austin Schuh217a9782019-12-21 23:02:50 -0800631const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800632 CHECK(config->has_nodes())
633 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800634 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800635 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800636 if (node->name()->string_view() == name) {
637 return node;
638 }
639 }
640 return nullptr;
641}
642
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800643const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
644 if (!MultiNode(config)) {
645 CHECK(node == nullptr) << ": Provided a node in a single node world.";
646 return nullptr;
647 } else {
648 const Node *config_node = GetNode(config, node);
649 if (config_node == nullptr) {
650 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
651 }
652 return config_node;
653 }
654}
655
Austin Schuh8bd96322020-02-13 21:18:22 -0800656namespace {
657int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800658 int node_index = 0;
659 for (const Node *iterated_node : *config->nodes()) {
660 if (iterated_node == node) {
661 return node_index;
662 }
663 ++node_index;
664 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800665 return -1;
666}
667} // namespace
668
669int GetNodeIndex(const Configuration *config, const Node *node) {
670 if (!MultiNode(config)) {
671 return 0;
672 }
673
674 {
675 int node_index = GetNodeIndexFromConfig(config, node);
676 if (node_index != -1) {
677 return node_index;
678 }
679 }
680
681 const Node *result = GetNode(config, node);
682 CHECK(result != nullptr);
683
684 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800685 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800686 if (node_index != -1) {
687 return node_index;
688 }
689 }
690
691 LOG(FATAL) << "Node " << FlatbufferToJson(node)
692 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800693}
694
Austin Schuh04408fc2020-02-16 21:48:54 -0800695int GetNodeIndex(const Configuration *config, std::string_view name) {
696 if (!MultiNode(config)) {
697 return 0;
698 }
699
700 {
701 int node_index = 0;
702 for (const Node *iterated_node : *config->nodes()) {
703 if (iterated_node->name()->string_view() == name) {
704 return node_index;
705 }
706 ++node_index;
707 }
708 }
709 LOG(FATAL) << "Node " << name << " not found in the configuration.";
710}
711
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800712std::vector<const Node *> GetNodes(const Configuration *config) {
713 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800714 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800715 for (const Node *node : *config->nodes()) {
716 nodes.emplace_back(node);
717 }
718 } else {
719 nodes.emplace_back(nullptr);
720 }
721 return nodes;
722}
723
Austin Schuhac0771c2020-01-07 18:36:30 -0800724bool MultiNode(const Configuration *config) { return config->has_nodes(); }
725
Austin Schuh217a9782019-12-21 23:02:50 -0800726bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800727 if (node == nullptr) {
728 return true;
729 }
Austin Schuh217a9782019-12-21 23:02:50 -0800730 return (channel->source_node()->string_view() == node->name()->string_view());
731}
732
733bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800734 if (node == nullptr) {
735 return true;
736 }
737
Austin Schuh217a9782019-12-21 23:02:50 -0800738 if (channel->source_node()->string_view() == node->name()->string_view()) {
739 return true;
740 }
741
742 if (!channel->has_destination_nodes()) {
743 return false;
744 }
745
Austin Schuh719946b2019-12-28 14:51:01 -0800746 for (const Connection *connection : *channel->destination_nodes()) {
747 CHECK(connection->has_name());
748 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800749 return true;
750 }
751 }
752
753 return false;
754}
755
Austin Schuh719946b2019-12-28 14:51:01 -0800756bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
757 switch(channel->logger()) {
758 case LoggerConfig::LOCAL_LOGGER:
759 if (node == nullptr) {
760 // Single node world. If there is a local logger, then we want to use
761 // it.
762 return true;
763 }
764 return channel->source_node()->string_view() ==
765 node->name()->string_view();
766 case LoggerConfig::REMOTE_LOGGER:
767 CHECK(channel->has_logger_node());
768
769 return channel->logger_node()->string_view() ==
770 CHECK_NOTNULL(node)->name()->string_view();
771 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
772 CHECK(channel->has_logger_node());
773
774 if (channel->source_node()->string_view() ==
775 CHECK_NOTNULL(node)->name()->string_view()) {
776 return true;
777 }
778 if (channel->logger_node()->string_view() == node->name()->string_view()) {
779 return true;
780 }
781
782 return false;
783 case LoggerConfig::NOT_LOGGED:
784 return false;
785 }
786
787 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
788}
789
790const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
791 if (!channel->has_destination_nodes()) {
792 return nullptr;
793 }
794 for (const Connection *connection : *channel->destination_nodes()) {
795 if (connection->name()->string_view() == node->name()->string_view()) {
796 return connection;
797 }
798 }
799 return nullptr;
800}
801
802bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
803 const Node *node,
804 const Node *logger_node) {
805 const Connection *connection = ConnectionToNode(channel, node);
806 if (connection == nullptr) {
807 return false;
808 }
809 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
810}
811
812bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
813 const Node *node) {
814 switch (connection->timestamp_logger()) {
815 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
816 CHECK(connection->has_timestamp_logger_node());
817 if (connection->name()->string_view() == node->name()->string_view()) {
818 return true;
819 }
820
821 if (connection->timestamp_logger_node()->string_view() ==
822 node->name()->string_view()) {
823 return true;
824 }
825
826 return false;
827 case LoggerConfig::LOCAL_LOGGER:
828 return connection->name()->string_view() == node->name()->string_view();
829 case LoggerConfig::REMOTE_LOGGER:
830 CHECK(connection->has_timestamp_logger_node());
831
832 return connection->timestamp_logger_node()->string_view() ==
833 node->name()->string_view();
834 case LoggerConfig::NOT_LOGGED:
835 return false;
836 }
837
838 LOG(FATAL) << "Unknown logger config "
839 << static_cast<int>(connection->timestamp_logger());
840}
841
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800842std::vector<std::string_view> SourceNodeNames(const Configuration *config,
843 const Node *my_node) {
844 std::set<std::string_view> result_set;
845
846 for (const Channel *channel : *config->channels()) {
847 if (channel->has_destination_nodes()) {
848 for (const Connection *connection : *channel->destination_nodes()) {
849 if (connection->name()->string_view() ==
850 my_node->name()->string_view()) {
851 result_set.insert(channel->source_node()->string_view());
852 }
853 }
854 }
855 }
856
857 std::vector<std::string_view> result;
858 for (const std::string_view source : result_set) {
859 VLOG(1) << "Found a source node of " << source;
860 result.emplace_back(source);
861 }
862 return result;
863}
864
865std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
866 const Node *my_node) {
867 std::vector<std::string_view> result;
868
869 for (const Channel *channel : *config->channels()) {
870 if (channel->has_source_node() && channel->source_node()->string_view() ==
871 my_node->name()->string_view()) {
872 if (!channel->has_destination_nodes()) continue;
873
874 if (channel->source_node()->string_view() !=
875 my_node->name()->string_view()) {
876 continue;
877 }
878
879 for (const Connection *connection : *channel->destination_nodes()) {
880 if (std::find(result.begin(), result.end(),
881 connection->name()->string_view()) == result.end()) {
882 result.emplace_back(connection->name()->string_view());
883 }
884 }
885 }
886 }
887
888 for (const std::string_view destination : result) {
889 VLOG(1) << "Found a destination node of " << destination;
890 }
891 return result;
892}
893
Brian Silverman66f079a2013-08-26 16:24:30 -0700894} // namespace configuration
895} // namespace aos