blob: 0a34b02f98bf52bd36c0d47c077fe5293414b21a [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
Brian Silverman66f079a2013-08-26 16:24:30 -07003#include <arpa/inet.h>
4#include <ifaddrs.h>
Austin Schuhf1fff282020-03-28 16:57:32 -07005#include <netinet/in.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/types.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07009#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.
Austin Schuhf1fff282020-03-28 16:57:32 -070083std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -070084 auto last_slash_pos = filename.find_last_of("/\\");
85
James Kuszmaul3ae42262019-11-08 12:33:41 -080086 return last_slash_pos == std::string_view::npos
87 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -070088 : filename.substr(0, last_slash_pos + 1);
89}
90
Austin Schuh40485ed2019-10-26 21:51:44 -070091FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -080092 const std::string_view path, absl::btree_set<std::string> *visited_paths) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070093 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
94 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
95
96 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file";
97
98 FlatbufferDetachedBuffer<Configuration> config(std::move(buffer));
Austin Schuhcb108412019-10-13 16:09:54 -070099 // Depth first. Take the following example:
100 //
101 // config1.json:
102 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700103 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700104 // {
105 // "name": "/foo",
106 // "type": ".aos.bar",
107 // "max_size": 5
108 // }
109 // ],
110 // "imports": [
111 // "config2.json",
112 // ]
113 // }
114 //
115 // config2.json:
116 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700117 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700118 // {
119 // "name": "/foo",
120 // "type": ".aos.bar",
121 // "max_size": 7
122 // }
123 // ],
124 // }
125 //
126 // We want the main config (config1.json) to be able to override the imported
127 // config. That means that it needs to be merged into the imported configs,
128 // not the other way around.
129
130 // Track that we have seen this file before recursing.
131 visited_paths->insert(::std::string(path));
132
133 if (config.message().has_imports()) {
134 // Capture the imports.
135 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
136 config.message().imports();
137
138 // And then wipe them. This gets GCed when we merge later.
139 config.mutable_message()->clear_imports();
140
141 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700142 FlatbufferDetachedBuffer<Configuration> merged_config =
143 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700144
145 const ::std::string folder(ExtractFolder(path));
146
147 for (const flatbuffers::String *str : *v) {
148 const ::std::string included_config = folder + str->c_str();
149 // Abort on any paths we have already seen.
150 CHECK(visited_paths->find(included_config) == visited_paths->end())
151 << ": Found duplicate file " << included_config << " while reading "
152 << path;
153
154 // And them merge everything in.
155 merged_config = MergeFlatBuffers(
156 merged_config, ReadConfig(included_config, visited_paths));
157 }
158
159 // Finally, merge this file in.
160 config = MergeFlatBuffers(merged_config, config);
161 }
162 return config;
163}
164
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165// Compares (c < p) a channel, and a name, type tuple.
166bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800167 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168 int name_compare = c->name()->string_view().compare(p.first);
169 if (name_compare == 0) {
170 return c->type()->string_view() < p.second;
171 } else if (name_compare < 0) {
172 return true;
173 } else {
174 return false;
175 }
176};
177
178// Compares for equality (c == p) a channel, and a name, type tuple.
179bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700180 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700181 return c->name()->string_view() == p.first &&
182 c->type()->string_view() == p.second;
183}
184
185// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800186bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 return a->name()->string_view() < name;
188};
189
190// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800191bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192 return a->name()->string_view() == name;
193}
194
195// Maps name for the provided maps. Modifies name.
196void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
Austin Schuhf1fff282020-03-28 16:57:32 -0700197 std::string *name, std::string_view type, const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700198 // For the same reason we merge configs in reverse order, we want to process
199 // maps in reverse order. That lets the outer config overwrite channels from
200 // the inner configs.
201 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800202 if (!i->has_match() || !i->match()->has_name()) {
203 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800205 if (!i->has_rename() || !i->rename()->has_name()) {
206 continue;
207 }
208
209 // Handle normal maps (now that we know that match and rename are filled
210 // out).
Austin Schuhf1fff282020-03-28 16:57:32 -0700211 const std::string_view match_name = i->match()->name()->string_view();
212 if (match_name != *name) {
213 if (match_name.back() == '*' &&
214 std::string_view(*name).substr(
215 0, std::min(name->size(), match_name.size() - 1)) ==
216 match_name.substr(0, match_name.size() - 1)) {
217 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
218 } else {
219 continue;
220 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800221 }
222
223 // Handle type specific maps.
224 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
225 continue;
226 }
227
Austin Schuhf1fff282020-03-28 16:57:32 -0700228 // Now handle node specific maps.
Austin Schuhbca6cf02019-12-22 17:28:34 -0800229 if (node != nullptr && i->match()->has_source_node() &&
230 i->match()->source_node()->string_view() !=
231 node->name()->string_view()) {
232 continue;
233 }
234
Austin Schuhf1fff282020-03-28 16:57:32 -0700235 std::string new_name(i->rename()->name()->string_view());
236 if (match_name.back() == '*') {
237 new_name += std::string(name->substr(match_name.size() - 1));
238 }
239 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
240 *name = std::move(new_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241 }
242}
243
244} // namespace
245
Austin Schuh40485ed2019-10-26 21:51:44 -0700246FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700247 const Flatbuffer<Configuration> &config) {
Austin Schuh40485ed2019-10-26 21:51:44 -0700248 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700249 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700250 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700251
Austin Schuh40485ed2019-10-26 21:51:44 -0700252 if (config.message().has_channels()) {
253 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700254 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700255 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700256 continue;
257 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700258 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700259 continue;
260 }
261
Austin Schuh40485ed2019-10-26 21:51:44 -0700262 // Attempt to insert the channel.
263 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700264 if (!result.second) {
265 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700266 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700267 }
268 }
269 }
270
271 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700272 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700273 if (config.message().has_applications()) {
274 for (const Application *a : *config.message().applications()) {
275 if (!a->has_name()) {
276 continue;
277 }
278
279 auto result = applications.insert(CopyFlatBuffer(a));
280 if (!result.second) {
281 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
282 }
283 }
284 }
285
Austin Schuh217a9782019-12-21 23:02:50 -0800286 // Now repeat this for the node list.
287 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
288 if (config.message().has_nodes()) {
289 for (const Node *n : *config.message().nodes()) {
290 if (!n->has_name()) {
291 continue;
292 }
293
294 auto result = nodes.insert(CopyFlatBuffer(n));
295 if (!result.second) {
296 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
297 }
298 }
299 }
300
Austin Schuhcb108412019-10-13 16:09:54 -0700301 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800302 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700303
304 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700305 // Channels
306 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
307 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700308 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700309 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
310 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700311 channel_offsets.emplace_back(CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700312 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700313 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700314 }
315
316 // Applications
317 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
318 applications_offset;
319 {
320 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700321 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700322 applications_offsets.emplace_back(
323 CopyFlatBuffer<Application>(&a.message(), &fbb));
324 }
325 applications_offset = fbb.CreateVector(applications_offsets);
326 }
327
328 // Just copy the maps
329 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
330 maps_offset;
331 {
332 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
333 if (config.message().has_maps()) {
334 for (const Map *m : *config.message().maps()) {
335 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
336 }
337 maps_offset = fbb.CreateVector(map_offsets);
338 }
339 }
340
Austin Schuh217a9782019-12-21 23:02:50 -0800341 // Nodes
342 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
343 nodes_offset;
344 {
345 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
346 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
347 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
348 }
349 nodes_offset = fbb.CreateVector(node_offsets);
350 }
351
Austin Schuhcb108412019-10-13 16:09:54 -0700352 // And then build a Configuration with them all.
353 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700354 configuration_builder.add_channels(channels_offset);
Austin Schuhcb108412019-10-13 16:09:54 -0700355 if (config.message().has_maps()) {
356 configuration_builder.add_maps(maps_offset);
357 }
Austin Schuh217a9782019-12-21 23:02:50 -0800358 if (config.message().has_applications()) {
359 configuration_builder.add_applications(applications_offset);
360 }
361 if (config.message().has_nodes()) {
362 configuration_builder.add_nodes(nodes_offset);
363 }
Austin Schuhcb108412019-10-13 16:09:54 -0700364
365 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800366
367 // Now, validate that if there is a node list, every channel has a source
368 // node.
369 FlatbufferDetachedBuffer<Configuration> result(fbb.Release());
370
371 // Check that if there is a node list, all the source nodes are filled out and
372 // valid, and all the destination nodes are valid (and not the source). This
373 // is a basic consistency check.
Austin Schuhf1fff282020-03-28 16:57:32 -0700374 if (result.message().has_channels()) {
375 for (const Channel *c : *result.message().channels()) {
376 if (c->name()->string_view().back() == '/') {
377 LOG(FATAL) << "Channel names can't end with '/'";
378 }
379 if(c->name()->string_view().find("//")!= std::string_view::npos) {
380 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
381 << ", can't use //.";
382 }
383 for (const char data : c->name()->string_view()) {
384 if (data >= '0' && data <= '9') {
385 continue;
386 }
387 if (data >= 'a' && data <= 'z') {
388 continue;
389 }
390 if (data >= 'A' && data <= 'Z') {
391 continue;
392 }
393 if (data == '-' || data == '_' || data == '/') {
394 continue;
395 }
396 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
397 << ", can only use [-a-zA-Z0-9_/]";
398 }
399 }
400 }
401
402 if (result.message().has_nodes() && result.message().has_channels()) {
403 for (const Channel *c : *result.message().channels()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800404 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
405 << " is missing \"source_node\"";
406 CHECK(GetNode(&result.message(), c->source_node()->string_view()) !=
407 nullptr)
408 << ": Channel " << FlatbufferToJson(c)
409 << " has an unknown \"source_node\"";
410
411 if (c->has_destination_nodes()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800412 for (const Connection *connection : *c->destination_nodes()) {
413 CHECK(connection->has_name());
414 CHECK(GetNode(&result.message(), connection->name()->string_view()) !=
415 nullptr)
Austin Schuh217a9782019-12-21 23:02:50 -0800416 << ": Channel " << FlatbufferToJson(c)
Austin Schuh719946b2019-12-28 14:51:01 -0800417 << " has an unknown \"destination_nodes\" "
418 << connection->name()->string_view();
Austin Schuh217a9782019-12-21 23:02:50 -0800419
Austin Schuh719946b2019-12-28 14:51:01 -0800420 switch (connection->timestamp_logger()) {
421 case LoggerConfig::LOCAL_LOGGER:
422 case LoggerConfig::NOT_LOGGED:
Austin Schuhda40e472020-03-28 15:15:29 -0700423 CHECK(!connection->has_timestamp_logger_nodes());
Austin Schuh719946b2019-12-28 14:51:01 -0800424 break;
425 case LoggerConfig::REMOTE_LOGGER:
426 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700427 CHECK(connection->has_timestamp_logger_nodes());
428 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
429 for (const flatbuffers::String *timestamp_logger_node :
430 *connection->timestamp_logger_nodes()) {
431 CHECK(GetNode(&result.message(),
432 timestamp_logger_node->string_view()) != nullptr)
433 << ": Channel " << FlatbufferToJson(c)
434 << " has an unknown \"timestamp_logger_node\""
435 << connection->name()->string_view();
436 }
Austin Schuh719946b2019-12-28 14:51:01 -0800437 break;
438 }
439
440 CHECK_NE(connection->name()->string_view(),
441 c->source_node()->string_view())
Austin Schuh217a9782019-12-21 23:02:50 -0800442 << ": Channel " << FlatbufferToJson(c)
443 << " is forwarding data to itself";
444 }
445 }
446 }
447 }
448
449 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700450}
451
Austin Schuh40485ed2019-10-26 21:51:44 -0700452FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800453 const std::string_view path) {
Austin Schuhcb108412019-10-13 16:09:54 -0700454 // We only want to read a file once. So track the visited files in a set.
455 absl::btree_set<std::string> visited_paths;
456 return MergeConfiguration(ReadConfig(path, &visited_paths));
457}
458
Austin Schuh8d6cea82020-02-28 12:17:16 -0800459FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
460 const Configuration *config, std::string_view json) {
461 FlatbufferDetachedBuffer<Configuration> addition =
462 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
463
464 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
465}
466
James Kuszmaul3ae42262019-11-08 12:33:41 -0800467const Channel *GetChannel(const Configuration *config, std::string_view name,
468 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800469 std::string_view application_name, const Node *node) {
470 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700471 std::string mutable_name;
Austin Schuhcb108412019-10-13 16:09:54 -0700472 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
473 << "\" }";
474
475 // First handle application specific maps. Only do this if we have a matching
476 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700477 if (config->has_applications()) {
478 auto application_iterator = std::lower_bound(
479 config->applications()->cbegin(), config->applications()->cend(),
480 application_name, CompareApplications);
481 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700482 EqualsApplications(*application_iterator, application_name)) {
483 if (application_iterator->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700484 mutable_name = std::string(name);
485 HandleMaps(application_iterator->maps(), &mutable_name, type, node);
486 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700487 }
488 }
489 }
490
491 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700492 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700493 mutable_name = std::string(name);
494 HandleMaps(config->maps(), &mutable_name, type, node);
495 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700496 }
497
Austin Schuhbca6cf02019-12-22 17:28:34 -0800498 if (original_name != name) {
499 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
500 << type << "\" }";
501 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700502
Austin Schuh40485ed2019-10-26 21:51:44 -0700503 // Then look for the channel.
504 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700505 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700506 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700507
508 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700509 if (channel_iterator != config->channels()->cend() &&
510 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800511 if (VLOG_IS_ON(2)) {
512 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
513 } else if (VLOG_IS_ON(1)) {
514 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
515 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700516 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700517 } else {
518 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
519 << type << "\" }";
520 return nullptr;
521 }
522}
523
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800524size_t ChannelIndex(const Configuration *configuration,
525 const Channel *channel) {
526 CHECK(configuration->channels() != nullptr) << ": No channels";
527
528 auto c = std::find(configuration->channels()->begin(),
529 configuration->channels()->end(), channel);
530 CHECK(c != configuration->channels()->end())
531 << ": Channel pointer not found in configuration()->channels()";
532
533 return std::distance(configuration->channels()->begin(), c);
534}
535
Austin Schuhbca6cf02019-12-22 17:28:34 -0800536std::string CleanedChannelToString(const Channel *channel) {
537 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
538 cleaned_channel.mutable_message()->clear_schema();
539 return FlatbufferToJson(cleaned_channel);
540}
541
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
543 const Flatbuffer<Configuration> &config,
544 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
545 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800546 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700547
548 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
549 channels_offset;
550 if (config.message().has_channels()) {
551 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
552 for (const Channel *c : *config.message().channels()) {
553 flatbuffers::FlatBufferBuilder channel_fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800554 channel_fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700555
556 // Search for a schema with a matching type.
557 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
Austin Schuhf1fff282020-03-28 16:57:32 -0700558 for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700559 if (schema.message().root_table() != nullptr) {
560 if (schema.message().root_table()->name()->string_view() ==
561 c->type()->string_view()) {
562 found_schema = &schema;
563 }
564 }
565 }
566
567 CHECK(found_schema != nullptr)
568 << ": Failed to find schema for " << FlatbufferToJson(c);
569
570 // The following is wasteful, but works.
571 //
572 // Copy it into a Channel object by creating an object with only the
573 // schema populated and merge that into the current channel.
574 flatbuffers::Offset<reflection::Schema> schema_offset =
575 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
576 &channel_fbb);
577 Channel::Builder channel_builder(channel_fbb);
578 channel_builder.add_schema(schema_offset);
579 channel_fbb.Finish(channel_builder.Finish());
580 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
581 channel_fbb.Release());
582
583 FlatbufferDetachedBuffer<Channel> merged_channel(
584 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
585
586 channel_offsets.emplace_back(
587 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
588 }
589 channels_offset = fbb.CreateVector(channel_offsets);
590 }
591
592 // Copy the applications and maps unmodified.
593 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
594 applications_offset;
595 {
596 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
597 if (config.message().has_applications()) {
598 for (const Application *a : *config.message().applications()) {
599 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
600 }
601 }
602 applications_offset = fbb.CreateVector(applications_offsets);
603 }
604
605 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
606 maps_offset;
607 {
608 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
609 if (config.message().has_maps()) {
610 for (const Map *m : *config.message().maps()) {
611 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
612 }
613 maps_offset = fbb.CreateVector(map_offsets);
614 }
615 }
616
Austin Schuh217a9782019-12-21 23:02:50 -0800617 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
618 nodes_offset;
619 {
620 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
621 if (config.message().has_nodes()) {
622 for (const Node *n : *config.message().nodes()) {
623 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
624 }
625 nodes_offset = fbb.CreateVector(node_offsets);
626 }
627 }
628
629 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700630 ConfigurationBuilder configuration_builder(fbb);
631 if (config.message().has_channels()) {
632 configuration_builder.add_channels(channels_offset);
633 }
634 if (config.message().has_maps()) {
635 configuration_builder.add_maps(maps_offset);
636 }
637 if (config.message().has_applications()) {
638 configuration_builder.add_applications(applications_offset);
639 }
Austin Schuh217a9782019-12-21 23:02:50 -0800640 if (config.message().has_nodes()) {
641 configuration_builder.add_nodes(nodes_offset);
642 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700643
644 fbb.Finish(configuration_builder.Finish());
645 return fbb.Release();
646}
647
Austin Schuh217a9782019-12-21 23:02:50 -0800648const Node *GetNodeFromHostname(const Configuration *config,
649 std::string_view hostname) {
650 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800651 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800652 return node;
653 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800654 if (node->has_hostnames()) {
655 for (const auto &candidate : *node->hostnames()) {
656 if (candidate->string_view() == hostname) {
657 return node;
658 }
659 }
660 }
Austin Schuh217a9782019-12-21 23:02:50 -0800661 }
662 return nullptr;
663}
Austin Schuhac0771c2020-01-07 18:36:30 -0800664
Austin Schuh217a9782019-12-21 23:02:50 -0800665const Node *GetMyNode(const Configuration *config) {
666 const std::string hostname = (FLAGS_override_hostname.size() > 0)
667 ? FLAGS_override_hostname
668 : network::GetHostname();
669 const Node *node = GetNodeFromHostname(config, hostname);
670 if (node != nullptr) return node;
671
672 LOG(FATAL) << "Unknown node for host: " << hostname
673 << ". Consider using --override_hostname if hostname detection "
674 "is wrong.";
675 return nullptr;
676}
677
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800678const Node *GetNode(const Configuration *config, const Node *node) {
679 if (!MultiNode(config)) {
680 CHECK(node == nullptr) << ": Provided a node in a single node world.";
681 return nullptr;
682 } else {
683 CHECK(node != nullptr);
684 CHECK(node->has_name());
685 return GetNode(config, node->name()->string_view());
686 }
687}
688
Austin Schuh217a9782019-12-21 23:02:50 -0800689const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800690 CHECK(config->has_nodes())
691 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800692 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800693 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800694 if (node->name()->string_view() == name) {
695 return node;
696 }
697 }
698 return nullptr;
699}
700
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800701const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
702 if (!MultiNode(config)) {
703 CHECK(node == nullptr) << ": Provided a node in a single node world.";
704 return nullptr;
705 } else {
706 const Node *config_node = GetNode(config, node);
707 if (config_node == nullptr) {
708 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
709 }
710 return config_node;
711 }
712}
713
Austin Schuh8bd96322020-02-13 21:18:22 -0800714namespace {
715int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800716 int node_index = 0;
717 for (const Node *iterated_node : *config->nodes()) {
718 if (iterated_node == node) {
719 return node_index;
720 }
721 ++node_index;
722 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800723 return -1;
724}
725} // namespace
726
727int GetNodeIndex(const Configuration *config, const Node *node) {
728 if (!MultiNode(config)) {
729 return 0;
730 }
731
732 {
733 int node_index = GetNodeIndexFromConfig(config, node);
734 if (node_index != -1) {
735 return node_index;
736 }
737 }
738
739 const Node *result = GetNode(config, node);
740 CHECK(result != nullptr);
741
742 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800743 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800744 if (node_index != -1) {
745 return node_index;
746 }
747 }
748
749 LOG(FATAL) << "Node " << FlatbufferToJson(node)
750 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800751}
752
Austin Schuh04408fc2020-02-16 21:48:54 -0800753int GetNodeIndex(const Configuration *config, std::string_view name) {
754 if (!MultiNode(config)) {
755 return 0;
756 }
757
758 {
759 int node_index = 0;
760 for (const Node *iterated_node : *config->nodes()) {
761 if (iterated_node->name()->string_view() == name) {
762 return node_index;
763 }
764 ++node_index;
765 }
766 }
767 LOG(FATAL) << "Node " << name << " not found in the configuration.";
768}
769
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800770std::vector<const Node *> GetNodes(const Configuration *config) {
771 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800772 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800773 for (const Node *node : *config->nodes()) {
774 nodes.emplace_back(node);
775 }
776 } else {
777 nodes.emplace_back(nullptr);
778 }
779 return nodes;
780}
781
Austin Schuhac0771c2020-01-07 18:36:30 -0800782bool MultiNode(const Configuration *config) { return config->has_nodes(); }
783
Austin Schuh217a9782019-12-21 23:02:50 -0800784bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800785 if (node == nullptr) {
786 return true;
787 }
Austin Schuh217a9782019-12-21 23:02:50 -0800788 return (channel->source_node()->string_view() == node->name()->string_view());
789}
790
791bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800792 if (node == nullptr) {
793 return true;
794 }
795
Austin Schuh217a9782019-12-21 23:02:50 -0800796 if (channel->source_node()->string_view() == node->name()->string_view()) {
797 return true;
798 }
799
800 if (!channel->has_destination_nodes()) {
801 return false;
802 }
803
Austin Schuh719946b2019-12-28 14:51:01 -0800804 for (const Connection *connection : *channel->destination_nodes()) {
805 CHECK(connection->has_name());
806 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800807 return true;
808 }
809 }
810
811 return false;
812}
813
Austin Schuh719946b2019-12-28 14:51:01 -0800814bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700815 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800816 case LoggerConfig::LOCAL_LOGGER:
817 if (node == nullptr) {
818 // Single node world. If there is a local logger, then we want to use
819 // it.
820 return true;
821 }
822 return channel->source_node()->string_view() ==
823 node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800824 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700825 CHECK(channel->has_logger_nodes());
826 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800827
828 if (channel->source_node()->string_view() ==
829 CHECK_NOTNULL(node)->name()->string_view()) {
830 return true;
831 }
Austin Schuhda40e472020-03-28 15:15:29 -0700832
833 [[fallthrough]];
834 case LoggerConfig::REMOTE_LOGGER:
835 CHECK(channel->has_logger_nodes());
836 CHECK_GT(channel->logger_nodes()->size(), 0u);
837 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
838 if (logger_node->string_view() ==
839 CHECK_NOTNULL(node)->name()->string_view()) {
840 return true;
841 }
Austin Schuh719946b2019-12-28 14:51:01 -0800842 }
843
844 return false;
845 case LoggerConfig::NOT_LOGGED:
846 return false;
847 }
848
849 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
850}
851
852const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
853 if (!channel->has_destination_nodes()) {
854 return nullptr;
855 }
856 for (const Connection *connection : *channel->destination_nodes()) {
857 if (connection->name()->string_view() == node->name()->string_view()) {
858 return connection;
859 }
860 }
861 return nullptr;
862}
863
864bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
865 const Node *node,
866 const Node *logger_node) {
867 const Connection *connection = ConnectionToNode(channel, node);
868 if (connection == nullptr) {
869 return false;
870 }
871 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
872}
873
874bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
875 const Node *node) {
876 switch (connection->timestamp_logger()) {
877 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700878 CHECK(connection->has_timestamp_logger_nodes());
879 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800880 if (connection->name()->string_view() == node->name()->string_view()) {
881 return true;
882 }
883
Austin Schuhda40e472020-03-28 15:15:29 -0700884 [[fallthrough]];
885 case LoggerConfig::REMOTE_LOGGER:
886 CHECK(connection->has_timestamp_logger_nodes());
887 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
888 for (const flatbuffers::String *timestamp_logger_node :
889 *connection->timestamp_logger_nodes()) {
890 if (timestamp_logger_node->string_view() ==
891 node->name()->string_view()) {
892 return true;
893 }
Austin Schuh719946b2019-12-28 14:51:01 -0800894 }
895
896 return false;
897 case LoggerConfig::LOCAL_LOGGER:
898 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800899 case LoggerConfig::NOT_LOGGED:
900 return false;
901 }
902
903 LOG(FATAL) << "Unknown logger config "
904 << static_cast<int>(connection->timestamp_logger());
905}
906
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800907std::vector<std::string_view> SourceNodeNames(const Configuration *config,
908 const Node *my_node) {
909 std::set<std::string_view> result_set;
910
911 for (const Channel *channel : *config->channels()) {
912 if (channel->has_destination_nodes()) {
913 for (const Connection *connection : *channel->destination_nodes()) {
914 if (connection->name()->string_view() ==
915 my_node->name()->string_view()) {
916 result_set.insert(channel->source_node()->string_view());
917 }
918 }
919 }
920 }
921
922 std::vector<std::string_view> result;
923 for (const std::string_view source : result_set) {
924 VLOG(1) << "Found a source node of " << source;
925 result.emplace_back(source);
926 }
927 return result;
928}
929
930std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
931 const Node *my_node) {
932 std::vector<std::string_view> result;
933
934 for (const Channel *channel : *config->channels()) {
935 if (channel->has_source_node() && channel->source_node()->string_view() ==
936 my_node->name()->string_view()) {
937 if (!channel->has_destination_nodes()) continue;
938
939 if (channel->source_node()->string_view() !=
940 my_node->name()->string_view()) {
941 continue;
942 }
943
944 for (const Connection *connection : *channel->destination_nodes()) {
945 if (std::find(result.begin(), result.end(),
946 connection->name()->string_view()) == result.end()) {
947 result.emplace_back(connection->name()->string_view());
948 }
949 }
950 }
951 }
952
953 for (const std::string_view destination : result) {
954 VLOG(1) << "Found a destination node of " << destination;
955 }
956 return result;
957}
958
Brian Silverman66f079a2013-08-26 16:24:30 -0700959} // namespace configuration
960} // namespace aos