blob: d6d276ef164f4c1bd9235168741403e616e5e3a3 [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;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800292 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700293
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.
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800365 if (result.message().has_nodes() && config.message().has_channels()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800366 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
Austin Schuh8d6cea82020-02-28 12:17:16 -0800420FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
421 const Configuration *config, std::string_view json) {
422 FlatbufferDetachedBuffer<Configuration> addition =
423 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
424
425 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
426}
427
James Kuszmaul3ae42262019-11-08 12:33:41 -0800428const Channel *GetChannel(const Configuration *config, std::string_view name,
429 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800430 std::string_view application_name, const Node *node) {
431 const std::string_view original_name = name;
Austin Schuhcb108412019-10-13 16:09:54 -0700432 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
433 << "\" }";
434
435 // First handle application specific maps. Only do this if we have a matching
436 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700437 if (config->has_applications()) {
438 auto application_iterator = std::lower_bound(
439 config->applications()->cbegin(), config->applications()->cend(),
440 application_name, CompareApplications);
441 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700442 EqualsApplications(*application_iterator, application_name)) {
443 if (application_iterator->has_maps()) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800444 HandleMaps(application_iterator->maps(), &name, type, node);
Austin Schuhcb108412019-10-13 16:09:54 -0700445 }
446 }
447 }
448
449 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700450 if (config->has_maps()) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800451 HandleMaps(config->maps(), &name, type, node);
Austin Schuhcb108412019-10-13 16:09:54 -0700452 }
453
Austin Schuhbca6cf02019-12-22 17:28:34 -0800454 if (original_name != name) {
455 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
456 << type << "\" }";
457 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700458
Austin Schuh40485ed2019-10-26 21:51:44 -0700459 // Then look for the channel.
460 auto channel_iterator =
461 std::lower_bound(config->channels()->cbegin(),
462 config->channels()->cend(),
463 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700464
465 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700466 if (channel_iterator != config->channels()->cend() &&
467 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800468 if (VLOG_IS_ON(2)) {
469 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
470 } else if (VLOG_IS_ON(1)) {
471 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
472 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700473 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700474 } else {
475 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
476 << type << "\" }";
477 return nullptr;
478 }
479}
480
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800481size_t ChannelIndex(const Configuration *configuration,
482 const Channel *channel) {
483 CHECK(configuration->channels() != nullptr) << ": No channels";
484
485 auto c = std::find(configuration->channels()->begin(),
486 configuration->channels()->end(), channel);
487 CHECK(c != configuration->channels()->end())
488 << ": Channel pointer not found in configuration()->channels()";
489
490 return std::distance(configuration->channels()->begin(), c);
491}
492
Austin Schuhbca6cf02019-12-22 17:28:34 -0800493std::string CleanedChannelToString(const Channel *channel) {
494 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
495 cleaned_channel.mutable_message()->clear_schema();
496 return FlatbufferToJson(cleaned_channel);
497}
498
Alex Perrycb7da4b2019-08-28 19:35:56 -0700499FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
500 const Flatbuffer<Configuration> &config,
501 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
502 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800503 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700504
505 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
506 channels_offset;
507 if (config.message().has_channels()) {
508 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
509 for (const Channel *c : *config.message().channels()) {
510 flatbuffers::FlatBufferBuilder channel_fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800511 channel_fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512
513 // Search for a schema with a matching type.
514 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
515 for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) {
516 if (schema.message().root_table() != nullptr) {
517 if (schema.message().root_table()->name()->string_view() ==
518 c->type()->string_view()) {
519 found_schema = &schema;
520 }
521 }
522 }
523
524 CHECK(found_schema != nullptr)
525 << ": Failed to find schema for " << FlatbufferToJson(c);
526
527 // The following is wasteful, but works.
528 //
529 // Copy it into a Channel object by creating an object with only the
530 // schema populated and merge that into the current channel.
531 flatbuffers::Offset<reflection::Schema> schema_offset =
532 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
533 &channel_fbb);
534 Channel::Builder channel_builder(channel_fbb);
535 channel_builder.add_schema(schema_offset);
536 channel_fbb.Finish(channel_builder.Finish());
537 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
538 channel_fbb.Release());
539
540 FlatbufferDetachedBuffer<Channel> merged_channel(
541 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
542
543 channel_offsets.emplace_back(
544 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
545 }
546 channels_offset = fbb.CreateVector(channel_offsets);
547 }
548
549 // Copy the applications and maps unmodified.
550 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
551 applications_offset;
552 {
553 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
554 if (config.message().has_applications()) {
555 for (const Application *a : *config.message().applications()) {
556 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
557 }
558 }
559 applications_offset = fbb.CreateVector(applications_offsets);
560 }
561
562 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
563 maps_offset;
564 {
565 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
566 if (config.message().has_maps()) {
567 for (const Map *m : *config.message().maps()) {
568 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
569 }
570 maps_offset = fbb.CreateVector(map_offsets);
571 }
572 }
573
Austin Schuh217a9782019-12-21 23:02:50 -0800574 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
575 nodes_offset;
576 {
577 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
578 if (config.message().has_nodes()) {
579 for (const Node *n : *config.message().nodes()) {
580 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
581 }
582 nodes_offset = fbb.CreateVector(node_offsets);
583 }
584 }
585
586 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 ConfigurationBuilder configuration_builder(fbb);
588 if (config.message().has_channels()) {
589 configuration_builder.add_channels(channels_offset);
590 }
591 if (config.message().has_maps()) {
592 configuration_builder.add_maps(maps_offset);
593 }
594 if (config.message().has_applications()) {
595 configuration_builder.add_applications(applications_offset);
596 }
Austin Schuh217a9782019-12-21 23:02:50 -0800597 if (config.message().has_nodes()) {
598 configuration_builder.add_nodes(nodes_offset);
599 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700600
601 fbb.Finish(configuration_builder.Finish());
602 return fbb.Release();
603}
604
Austin Schuh217a9782019-12-21 23:02:50 -0800605const Node *GetNodeFromHostname(const Configuration *config,
606 std::string_view hostname) {
607 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800608 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800609 return node;
610 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800611 if (node->has_hostnames()) {
612 for (const auto &candidate : *node->hostnames()) {
613 if (candidate->string_view() == hostname) {
614 return node;
615 }
616 }
617 }
Austin Schuh217a9782019-12-21 23:02:50 -0800618 }
619 return nullptr;
620}
Austin Schuhac0771c2020-01-07 18:36:30 -0800621
Austin Schuh217a9782019-12-21 23:02:50 -0800622const Node *GetMyNode(const Configuration *config) {
623 const std::string hostname = (FLAGS_override_hostname.size() > 0)
624 ? FLAGS_override_hostname
625 : network::GetHostname();
626 const Node *node = GetNodeFromHostname(config, hostname);
627 if (node != nullptr) return node;
628
629 LOG(FATAL) << "Unknown node for host: " << hostname
630 << ". Consider using --override_hostname if hostname detection "
631 "is wrong.";
632 return nullptr;
633}
634
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800635const Node *GetNode(const Configuration *config, const Node *node) {
636 if (!MultiNode(config)) {
637 CHECK(node == nullptr) << ": Provided a node in a single node world.";
638 return nullptr;
639 } else {
640 CHECK(node != nullptr);
641 CHECK(node->has_name());
642 return GetNode(config, node->name()->string_view());
643 }
644}
645
Austin Schuh217a9782019-12-21 23:02:50 -0800646const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800647 CHECK(config->has_nodes())
648 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800649 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800650 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800651 if (node->name()->string_view() == name) {
652 return node;
653 }
654 }
655 return nullptr;
656}
657
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800658const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
659 if (!MultiNode(config)) {
660 CHECK(node == nullptr) << ": Provided a node in a single node world.";
661 return nullptr;
662 } else {
663 const Node *config_node = GetNode(config, node);
664 if (config_node == nullptr) {
665 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
666 }
667 return config_node;
668 }
669}
670
Austin Schuh8bd96322020-02-13 21:18:22 -0800671namespace {
672int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800673 int node_index = 0;
674 for (const Node *iterated_node : *config->nodes()) {
675 if (iterated_node == node) {
676 return node_index;
677 }
678 ++node_index;
679 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800680 return -1;
681}
682} // namespace
683
684int GetNodeIndex(const Configuration *config, const Node *node) {
685 if (!MultiNode(config)) {
686 return 0;
687 }
688
689 {
690 int node_index = GetNodeIndexFromConfig(config, node);
691 if (node_index != -1) {
692 return node_index;
693 }
694 }
695
696 const Node *result = GetNode(config, node);
697 CHECK(result != nullptr);
698
699 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800700 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800701 if (node_index != -1) {
702 return node_index;
703 }
704 }
705
706 LOG(FATAL) << "Node " << FlatbufferToJson(node)
707 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800708}
709
Austin Schuh04408fc2020-02-16 21:48:54 -0800710int GetNodeIndex(const Configuration *config, std::string_view name) {
711 if (!MultiNode(config)) {
712 return 0;
713 }
714
715 {
716 int node_index = 0;
717 for (const Node *iterated_node : *config->nodes()) {
718 if (iterated_node->name()->string_view() == name) {
719 return node_index;
720 }
721 ++node_index;
722 }
723 }
724 LOG(FATAL) << "Node " << name << " not found in the configuration.";
725}
726
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800727std::vector<const Node *> GetNodes(const Configuration *config) {
728 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800729 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800730 for (const Node *node : *config->nodes()) {
731 nodes.emplace_back(node);
732 }
733 } else {
734 nodes.emplace_back(nullptr);
735 }
736 return nodes;
737}
738
Austin Schuhac0771c2020-01-07 18:36:30 -0800739bool MultiNode(const Configuration *config) { return config->has_nodes(); }
740
Austin Schuh217a9782019-12-21 23:02:50 -0800741bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800742 if (node == nullptr) {
743 return true;
744 }
Austin Schuh217a9782019-12-21 23:02:50 -0800745 return (channel->source_node()->string_view() == node->name()->string_view());
746}
747
748bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800749 if (node == nullptr) {
750 return true;
751 }
752
Austin Schuh217a9782019-12-21 23:02:50 -0800753 if (channel->source_node()->string_view() == node->name()->string_view()) {
754 return true;
755 }
756
757 if (!channel->has_destination_nodes()) {
758 return false;
759 }
760
Austin Schuh719946b2019-12-28 14:51:01 -0800761 for (const Connection *connection : *channel->destination_nodes()) {
762 CHECK(connection->has_name());
763 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800764 return true;
765 }
766 }
767
768 return false;
769}
770
Austin Schuh719946b2019-12-28 14:51:01 -0800771bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
772 switch(channel->logger()) {
773 case LoggerConfig::LOCAL_LOGGER:
774 if (node == nullptr) {
775 // Single node world. If there is a local logger, then we want to use
776 // it.
777 return true;
778 }
779 return channel->source_node()->string_view() ==
780 node->name()->string_view();
781 case LoggerConfig::REMOTE_LOGGER:
782 CHECK(channel->has_logger_node());
783
784 return channel->logger_node()->string_view() ==
785 CHECK_NOTNULL(node)->name()->string_view();
786 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
787 CHECK(channel->has_logger_node());
788
789 if (channel->source_node()->string_view() ==
790 CHECK_NOTNULL(node)->name()->string_view()) {
791 return true;
792 }
793 if (channel->logger_node()->string_view() == node->name()->string_view()) {
794 return true;
795 }
796
797 return false;
798 case LoggerConfig::NOT_LOGGED:
799 return false;
800 }
801
802 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
803}
804
805const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
806 if (!channel->has_destination_nodes()) {
807 return nullptr;
808 }
809 for (const Connection *connection : *channel->destination_nodes()) {
810 if (connection->name()->string_view() == node->name()->string_view()) {
811 return connection;
812 }
813 }
814 return nullptr;
815}
816
817bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
818 const Node *node,
819 const Node *logger_node) {
820 const Connection *connection = ConnectionToNode(channel, node);
821 if (connection == nullptr) {
822 return false;
823 }
824 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
825}
826
827bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
828 const Node *node) {
829 switch (connection->timestamp_logger()) {
830 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
831 CHECK(connection->has_timestamp_logger_node());
832 if (connection->name()->string_view() == node->name()->string_view()) {
833 return true;
834 }
835
836 if (connection->timestamp_logger_node()->string_view() ==
837 node->name()->string_view()) {
838 return true;
839 }
840
841 return false;
842 case LoggerConfig::LOCAL_LOGGER:
843 return connection->name()->string_view() == node->name()->string_view();
844 case LoggerConfig::REMOTE_LOGGER:
845 CHECK(connection->has_timestamp_logger_node());
846
847 return connection->timestamp_logger_node()->string_view() ==
848 node->name()->string_view();
849 case LoggerConfig::NOT_LOGGED:
850 return false;
851 }
852
853 LOG(FATAL) << "Unknown logger config "
854 << static_cast<int>(connection->timestamp_logger());
855}
856
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800857std::vector<std::string_view> SourceNodeNames(const Configuration *config,
858 const Node *my_node) {
859 std::set<std::string_view> result_set;
860
861 for (const Channel *channel : *config->channels()) {
862 if (channel->has_destination_nodes()) {
863 for (const Connection *connection : *channel->destination_nodes()) {
864 if (connection->name()->string_view() ==
865 my_node->name()->string_view()) {
866 result_set.insert(channel->source_node()->string_view());
867 }
868 }
869 }
870 }
871
872 std::vector<std::string_view> result;
873 for (const std::string_view source : result_set) {
874 VLOG(1) << "Found a source node of " << source;
875 result.emplace_back(source);
876 }
877 return result;
878}
879
880std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
881 const Node *my_node) {
882 std::vector<std::string_view> result;
883
884 for (const Channel *channel : *config->channels()) {
885 if (channel->has_source_node() && channel->source_node()->string_view() ==
886 my_node->name()->string_view()) {
887 if (!channel->has_destination_nodes()) continue;
888
889 if (channel->source_node()->string_view() !=
890 my_node->name()->string_view()) {
891 continue;
892 }
893
894 for (const Connection *connection : *channel->destination_nodes()) {
895 if (std::find(result.begin(), result.end(),
896 connection->name()->string_view()) == result.end()) {
897 result.emplace_back(connection->name()->string_view());
898 }
899 }
900 }
901 }
902
903 for (const std::string_view destination : result) {
904 VLOG(1) << "Found a destination node of " << destination;
905 }
906 return result;
907}
908
Brian Silverman66f079a2013-08-26 16:24:30 -0700909} // namespace configuration
910} // namespace aos