blob: 43e44f2a74f68e20b6e3adc6478255612c087eac [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 Schuha81454b2020-05-12 19:58:36 -070015#include "absl/strings/str_cat.h"
Austin Schuhcb108412019-10-13 16:09:54 -070016#include "aos/configuration_generated.h"
17#include "aos/flatbuffer_merge.h"
18#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080019#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070020#include "aos/unique_malloc_ptr.h"
21#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080022#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070023#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080024
25DEFINE_string(
26 override_hostname, "",
27 "If set, this forces the hostname of this node to be the provided "
28 "hostname.");
Brian Silverman66f079a2013-08-26 16:24:30 -070029
30namespace aos {
Austin Schuhcb108412019-10-13 16:09:54 -070031
Austin Schuh40485ed2019-10-26 21:51:44 -070032// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070033// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070034bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
35 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070036 int name_compare = lhs.message().name()->string_view().compare(
37 rhs.message().name()->string_view());
38 if (name_compare == 0) {
39 return lhs.message().type()->string_view() <
40 rhs.message().type()->string_view();
41 } else if (name_compare < 0) {
42 return true;
43 } else {
44 return false;
45 }
46}
47
Austin Schuh40485ed2019-10-26 21:51:44 -070048bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
49 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070050 return lhs.message().name()->string_view() ==
51 rhs.message().name()->string_view() &&
52 lhs.message().type()->string_view() ==
53 rhs.message().type()->string_view();
54}
55
Austin Schuh40485ed2019-10-26 21:51:44 -070056bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
57 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070058 return lhs.message().name()->string_view() ==
59 rhs.message().name()->string_view();
60}
61
Austin Schuh40485ed2019-10-26 21:51:44 -070062bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
63 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070064 return lhs.message().name()->string_view() <
65 rhs.message().name()->string_view();
66}
67
Austin Schuh217a9782019-12-21 23:02:50 -080068bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
69 const FlatbufferDetachedBuffer<Node> &rhs) {
70 return lhs.message().name()->string_view() ==
71 rhs.message().name()->string_view();
72}
73
74bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
75 const FlatbufferDetachedBuffer<Node> &rhs) {
76 return lhs.message().name()->string_view() <
77 rhs.message().name()->string_view();
78}
79
Brian Silverman66f079a2013-08-26 16:24:30 -070080namespace configuration {
81namespace {
82
Austin Schuhcb108412019-10-13 16:09:54 -070083// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -070084std::string_view ExtractFolder(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,
Austin Schuhf1fff282020-03-28 16:57:32 -0700181 ::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 Schuhf1fff282020-03-28 16:57:32 -0700198 std::string *name, std::string_view type, const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700199 // For the same reason we merge configs in reverse order, we want to process
200 // maps in reverse order. That lets the outer config overwrite channels from
201 // the inner configs.
202 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800203 if (!i->has_match() || !i->match()->has_name()) {
204 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700205 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800206 if (!i->has_rename() || !i->rename()->has_name()) {
207 continue;
208 }
209
210 // Handle normal maps (now that we know that match and rename are filled
211 // out).
Austin Schuhf1fff282020-03-28 16:57:32 -0700212 const std::string_view match_name = i->match()->name()->string_view();
213 if (match_name != *name) {
214 if (match_name.back() == '*' &&
215 std::string_view(*name).substr(
216 0, std::min(name->size(), match_name.size() - 1)) ==
217 match_name.substr(0, match_name.size() - 1)) {
218 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
219 } else {
220 continue;
221 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800222 }
223
224 // Handle type specific maps.
225 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
226 continue;
227 }
228
Austin Schuhf1fff282020-03-28 16:57:32 -0700229 // Now handle node specific maps.
Austin Schuhbca6cf02019-12-22 17:28:34 -0800230 if (node != nullptr && i->match()->has_source_node() &&
231 i->match()->source_node()->string_view() !=
232 node->name()->string_view()) {
233 continue;
234 }
235
Austin Schuhf1fff282020-03-28 16:57:32 -0700236 std::string new_name(i->rename()->name()->string_view());
237 if (match_name.back() == '*') {
238 new_name += std::string(name->substr(match_name.size() - 1));
239 }
240 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
241 *name = std::move(new_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700242 }
243}
244
245} // namespace
246
Austin Schuh40485ed2019-10-26 21:51:44 -0700247FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700248 const Flatbuffer<Configuration> &config) {
Austin Schuh40485ed2019-10-26 21:51:44 -0700249 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700250 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700251 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700252
Austin Schuh40485ed2019-10-26 21:51:44 -0700253 if (config.message().has_channels()) {
254 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700255 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700256 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700257 continue;
258 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700259 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700260 continue;
261 }
262
Austin Schuh40485ed2019-10-26 21:51:44 -0700263 // Attempt to insert the channel.
264 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700265 if (!result.second) {
266 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700267 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700268 }
269 }
270 }
271
272 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700273 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700274 if (config.message().has_applications()) {
275 for (const Application *a : *config.message().applications()) {
276 if (!a->has_name()) {
277 continue;
278 }
279
280 auto result = applications.insert(CopyFlatBuffer(a));
281 if (!result.second) {
282 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
283 }
284 }
285 }
286
Austin Schuh217a9782019-12-21 23:02:50 -0800287 // Now repeat this for the node list.
288 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
289 if (config.message().has_nodes()) {
290 for (const Node *n : *config.message().nodes()) {
291 if (!n->has_name()) {
292 continue;
293 }
294
295 auto result = nodes.insert(CopyFlatBuffer(n));
296 if (!result.second) {
297 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
298 }
299 }
300 }
301
Austin Schuhcb108412019-10-13 16:09:54 -0700302 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800303 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700304
305 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700306 // Channels
307 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
308 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700309 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700310 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
311 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700312 channel_offsets.emplace_back(CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700313 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700314 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700315 }
316
317 // Applications
318 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
319 applications_offset;
320 {
321 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700322 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700323 applications_offsets.emplace_back(
324 CopyFlatBuffer<Application>(&a.message(), &fbb));
325 }
326 applications_offset = fbb.CreateVector(applications_offsets);
327 }
328
329 // Just copy the maps
330 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
331 maps_offset;
332 {
333 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
334 if (config.message().has_maps()) {
335 for (const Map *m : *config.message().maps()) {
336 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
337 }
338 maps_offset = fbb.CreateVector(map_offsets);
339 }
340 }
341
Austin Schuh217a9782019-12-21 23:02:50 -0800342 // Nodes
343 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
344 nodes_offset;
345 {
346 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
347 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
348 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
349 }
350 nodes_offset = fbb.CreateVector(node_offsets);
351 }
352
Austin Schuhcb108412019-10-13 16:09:54 -0700353 // And then build a Configuration with them all.
354 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700355 configuration_builder.add_channels(channels_offset);
Austin Schuhcb108412019-10-13 16:09:54 -0700356 if (config.message().has_maps()) {
357 configuration_builder.add_maps(maps_offset);
358 }
Austin Schuh217a9782019-12-21 23:02:50 -0800359 if (config.message().has_applications()) {
360 configuration_builder.add_applications(applications_offset);
361 }
362 if (config.message().has_nodes()) {
363 configuration_builder.add_nodes(nodes_offset);
364 }
Austin Schuhcb108412019-10-13 16:09:54 -0700365
366 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800367
368 // Now, validate that if there is a node list, every channel has a source
369 // node.
370 FlatbufferDetachedBuffer<Configuration> result(fbb.Release());
371
372 // Check that if there is a node list, all the source nodes are filled out and
373 // valid, and all the destination nodes are valid (and not the source). This
374 // is a basic consistency check.
Austin Schuhf1fff282020-03-28 16:57:32 -0700375 if (result.message().has_channels()) {
376 for (const Channel *c : *result.message().channels()) {
377 if (c->name()->string_view().back() == '/') {
378 LOG(FATAL) << "Channel names can't end with '/'";
379 }
380 if(c->name()->string_view().find("//")!= std::string_view::npos) {
381 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
382 << ", can't use //.";
383 }
384 for (const char data : c->name()->string_view()) {
385 if (data >= '0' && data <= '9') {
386 continue;
387 }
388 if (data >= 'a' && data <= 'z') {
389 continue;
390 }
391 if (data >= 'A' && data <= 'Z') {
392 continue;
393 }
394 if (data == '-' || data == '_' || data == '/') {
395 continue;
396 }
397 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
398 << ", can only use [-a-zA-Z0-9_/]";
399 }
400 }
401 }
402
403 if (result.message().has_nodes() && result.message().has_channels()) {
404 for (const Channel *c : *result.message().channels()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800405 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
406 << " is missing \"source_node\"";
407 CHECK(GetNode(&result.message(), c->source_node()->string_view()) !=
408 nullptr)
409 << ": Channel " << FlatbufferToJson(c)
410 << " has an unknown \"source_node\"";
411
412 if (c->has_destination_nodes()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800413 for (const Connection *connection : *c->destination_nodes()) {
414 CHECK(connection->has_name());
415 CHECK(GetNode(&result.message(), connection->name()->string_view()) !=
416 nullptr)
Austin Schuh217a9782019-12-21 23:02:50 -0800417 << ": Channel " << FlatbufferToJson(c)
Austin Schuh719946b2019-12-28 14:51:01 -0800418 << " has an unknown \"destination_nodes\" "
419 << connection->name()->string_view();
Austin Schuh217a9782019-12-21 23:02:50 -0800420
Austin Schuh719946b2019-12-28 14:51:01 -0800421 switch (connection->timestamp_logger()) {
422 case LoggerConfig::LOCAL_LOGGER:
423 case LoggerConfig::NOT_LOGGED:
Austin Schuhda40e472020-03-28 15:15:29 -0700424 CHECK(!connection->has_timestamp_logger_nodes());
Austin Schuh719946b2019-12-28 14:51:01 -0800425 break;
426 case LoggerConfig::REMOTE_LOGGER:
427 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700428 CHECK(connection->has_timestamp_logger_nodes());
429 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
430 for (const flatbuffers::String *timestamp_logger_node :
431 *connection->timestamp_logger_nodes()) {
432 CHECK(GetNode(&result.message(),
433 timestamp_logger_node->string_view()) != nullptr)
434 << ": Channel " << FlatbufferToJson(c)
435 << " has an unknown \"timestamp_logger_node\""
436 << connection->name()->string_view();
437 }
Austin Schuh719946b2019-12-28 14:51:01 -0800438 break;
439 }
440
441 CHECK_NE(connection->name()->string_view(),
442 c->source_node()->string_view())
Austin Schuh217a9782019-12-21 23:02:50 -0800443 << ": Channel " << FlatbufferToJson(c)
444 << " is forwarding data to itself";
445 }
446 }
447 }
448 }
449
450 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700451}
452
Austin Schuh40485ed2019-10-26 21:51:44 -0700453FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800454 const std::string_view path) {
Austin Schuhcb108412019-10-13 16:09:54 -0700455 // We only want to read a file once. So track the visited files in a set.
456 absl::btree_set<std::string> visited_paths;
457 return MergeConfiguration(ReadConfig(path, &visited_paths));
458}
459
Austin Schuh8d6cea82020-02-28 12:17:16 -0800460FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
461 const Configuration *config, std::string_view json) {
462 FlatbufferDetachedBuffer<Configuration> addition =
463 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
464
465 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
466}
467
James Kuszmaul3ae42262019-11-08 12:33:41 -0800468const Channel *GetChannel(const Configuration *config, std::string_view name,
469 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800470 std::string_view application_name, const Node *node) {
471 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700472 std::string mutable_name;
Austin Schuhcb108412019-10-13 16:09:54 -0700473 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
474 << "\" }";
475
476 // First handle application specific maps. Only do this if we have a matching
477 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700478 if (config->has_applications()) {
479 auto application_iterator = std::lower_bound(
480 config->applications()->cbegin(), config->applications()->cend(),
481 application_name, CompareApplications);
482 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700483 EqualsApplications(*application_iterator, application_name)) {
484 if (application_iterator->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700485 mutable_name = std::string(name);
486 HandleMaps(application_iterator->maps(), &mutable_name, type, node);
487 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700488 }
489 }
490 }
491
492 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700493 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700494 mutable_name = std::string(name);
495 HandleMaps(config->maps(), &mutable_name, type, node);
496 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700497 }
498
Austin Schuhbca6cf02019-12-22 17:28:34 -0800499 if (original_name != name) {
500 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
501 << type << "\" }";
502 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700503
Austin Schuh40485ed2019-10-26 21:51:44 -0700504 // Then look for the channel.
505 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700506 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700507 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700508
509 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700510 if (channel_iterator != config->channels()->cend() &&
511 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800512 if (VLOG_IS_ON(2)) {
513 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
514 } else if (VLOG_IS_ON(1)) {
515 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
516 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700517 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700518 } else {
519 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
520 << type << "\" }";
521 return nullptr;
522 }
523}
524
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800525size_t ChannelIndex(const Configuration *configuration,
526 const Channel *channel) {
527 CHECK(configuration->channels() != nullptr) << ": No channels";
528
529 auto c = std::find(configuration->channels()->begin(),
530 configuration->channels()->end(), channel);
531 CHECK(c != configuration->channels()->end())
532 << ": Channel pointer not found in configuration()->channels()";
533
534 return std::distance(configuration->channels()->begin(), c);
535}
536
Austin Schuhbca6cf02019-12-22 17:28:34 -0800537std::string CleanedChannelToString(const Channel *channel) {
538 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
539 cleaned_channel.mutable_message()->clear_schema();
540 return FlatbufferToJson(cleaned_channel);
541}
542
Austin Schuha81454b2020-05-12 19:58:36 -0700543std::string StrippedChannelToString(const Channel *channel) {
544 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
545 "\", \"type\": \"", channel->type()->string_view(),
546 "\" }");
547}
548
Alex Perrycb7da4b2019-08-28 19:35:56 -0700549FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
550 const Flatbuffer<Configuration> &config,
551 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
552 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800553 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554
555 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
556 channels_offset;
557 if (config.message().has_channels()) {
558 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
559 for (const Channel *c : *config.message().channels()) {
560 flatbuffers::FlatBufferBuilder channel_fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800561 channel_fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700562
563 // Search for a schema with a matching type.
564 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
Austin Schuhf1fff282020-03-28 16:57:32 -0700565 for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700566 if (schema.message().root_table() != nullptr) {
567 if (schema.message().root_table()->name()->string_view() ==
568 c->type()->string_view()) {
569 found_schema = &schema;
570 }
571 }
572 }
573
574 CHECK(found_schema != nullptr)
575 << ": Failed to find schema for " << FlatbufferToJson(c);
576
577 // The following is wasteful, but works.
578 //
579 // Copy it into a Channel object by creating an object with only the
580 // schema populated and merge that into the current channel.
581 flatbuffers::Offset<reflection::Schema> schema_offset =
582 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
583 &channel_fbb);
584 Channel::Builder channel_builder(channel_fbb);
585 channel_builder.add_schema(schema_offset);
586 channel_fbb.Finish(channel_builder.Finish());
587 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
588 channel_fbb.Release());
589
590 FlatbufferDetachedBuffer<Channel> merged_channel(
591 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
592
593 channel_offsets.emplace_back(
594 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
595 }
596 channels_offset = fbb.CreateVector(channel_offsets);
597 }
598
599 // Copy the applications and maps unmodified.
600 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
601 applications_offset;
602 {
603 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
604 if (config.message().has_applications()) {
605 for (const Application *a : *config.message().applications()) {
606 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
607 }
608 }
609 applications_offset = fbb.CreateVector(applications_offsets);
610 }
611
612 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
613 maps_offset;
614 {
615 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
616 if (config.message().has_maps()) {
617 for (const Map *m : *config.message().maps()) {
618 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
619 }
620 maps_offset = fbb.CreateVector(map_offsets);
621 }
622 }
623
Austin Schuh217a9782019-12-21 23:02:50 -0800624 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
625 nodes_offset;
626 {
627 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
628 if (config.message().has_nodes()) {
629 for (const Node *n : *config.message().nodes()) {
630 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
631 }
632 nodes_offset = fbb.CreateVector(node_offsets);
633 }
634 }
635
636 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700637 ConfigurationBuilder configuration_builder(fbb);
638 if (config.message().has_channels()) {
639 configuration_builder.add_channels(channels_offset);
640 }
641 if (config.message().has_maps()) {
642 configuration_builder.add_maps(maps_offset);
643 }
644 if (config.message().has_applications()) {
645 configuration_builder.add_applications(applications_offset);
646 }
Austin Schuh217a9782019-12-21 23:02:50 -0800647 if (config.message().has_nodes()) {
648 configuration_builder.add_nodes(nodes_offset);
649 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700650
651 fbb.Finish(configuration_builder.Finish());
652 return fbb.Release();
653}
654
Austin Schuh217a9782019-12-21 23:02:50 -0800655const Node *GetNodeFromHostname(const Configuration *config,
656 std::string_view hostname) {
657 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800658 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800659 return node;
660 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800661 if (node->has_hostnames()) {
662 for (const auto &candidate : *node->hostnames()) {
663 if (candidate->string_view() == hostname) {
664 return node;
665 }
666 }
667 }
Austin Schuh217a9782019-12-21 23:02:50 -0800668 }
669 return nullptr;
670}
Austin Schuhac0771c2020-01-07 18:36:30 -0800671
Austin Schuh217a9782019-12-21 23:02:50 -0800672const Node *GetMyNode(const Configuration *config) {
673 const std::string hostname = (FLAGS_override_hostname.size() > 0)
674 ? FLAGS_override_hostname
675 : network::GetHostname();
676 const Node *node = GetNodeFromHostname(config, hostname);
677 if (node != nullptr) return node;
678
679 LOG(FATAL) << "Unknown node for host: " << hostname
680 << ". Consider using --override_hostname if hostname detection "
681 "is wrong.";
682 return nullptr;
683}
684
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800685const Node *GetNode(const Configuration *config, const Node *node) {
686 if (!MultiNode(config)) {
687 CHECK(node == nullptr) << ": Provided a node in a single node world.";
688 return nullptr;
689 } else {
690 CHECK(node != nullptr);
691 CHECK(node->has_name());
692 return GetNode(config, node->name()->string_view());
693 }
694}
695
Austin Schuh217a9782019-12-21 23:02:50 -0800696const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800697 CHECK(config->has_nodes())
698 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800699 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800700 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800701 if (node->name()->string_view() == name) {
702 return node;
703 }
704 }
705 return nullptr;
706}
707
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800708const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
709 if (!MultiNode(config)) {
710 CHECK(node == nullptr) << ": Provided a node in a single node world.";
711 return nullptr;
712 } else {
713 const Node *config_node = GetNode(config, node);
714 if (config_node == nullptr) {
715 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
716 }
717 return config_node;
718 }
719}
720
Austin Schuh8bd96322020-02-13 21:18:22 -0800721namespace {
722int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800723 int node_index = 0;
724 for (const Node *iterated_node : *config->nodes()) {
725 if (iterated_node == node) {
726 return node_index;
727 }
728 ++node_index;
729 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800730 return -1;
731}
732} // namespace
733
734int GetNodeIndex(const Configuration *config, const Node *node) {
735 if (!MultiNode(config)) {
736 return 0;
737 }
738
739 {
740 int node_index = GetNodeIndexFromConfig(config, node);
741 if (node_index != -1) {
742 return node_index;
743 }
744 }
745
746 const Node *result = GetNode(config, node);
747 CHECK(result != nullptr);
748
749 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800750 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800751 if (node_index != -1) {
752 return node_index;
753 }
754 }
755
756 LOG(FATAL) << "Node " << FlatbufferToJson(node)
757 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800758}
759
Austin Schuh04408fc2020-02-16 21:48:54 -0800760int GetNodeIndex(const Configuration *config, std::string_view name) {
761 if (!MultiNode(config)) {
762 return 0;
763 }
764
765 {
766 int node_index = 0;
767 for (const Node *iterated_node : *config->nodes()) {
768 if (iterated_node->name()->string_view() == name) {
769 return node_index;
770 }
771 ++node_index;
772 }
773 }
774 LOG(FATAL) << "Node " << name << " not found in the configuration.";
775}
776
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800777std::vector<const Node *> GetNodes(const Configuration *config) {
778 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800779 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800780 for (const Node *node : *config->nodes()) {
781 nodes.emplace_back(node);
782 }
783 } else {
784 nodes.emplace_back(nullptr);
785 }
786 return nodes;
787}
788
Austin Schuhac0771c2020-01-07 18:36:30 -0800789bool MultiNode(const Configuration *config) { return config->has_nodes(); }
790
Austin Schuh217a9782019-12-21 23:02:50 -0800791bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800792 if (node == nullptr) {
793 return true;
794 }
Austin Schuh196a4452020-03-15 23:12:03 -0700795 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
796 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -0800797}
798
799bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800800 if (node == nullptr) {
801 return true;
802 }
803
Austin Schuh217a9782019-12-21 23:02:50 -0800804 if (channel->source_node()->string_view() == node->name()->string_view()) {
805 return true;
806 }
807
808 if (!channel->has_destination_nodes()) {
809 return false;
810 }
811
Austin Schuh719946b2019-12-28 14:51:01 -0800812 for (const Connection *connection : *channel->destination_nodes()) {
813 CHECK(connection->has_name());
814 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800815 return true;
816 }
817 }
818
819 return false;
820}
821
Austin Schuh719946b2019-12-28 14:51:01 -0800822bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700823 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800824 case LoggerConfig::LOCAL_LOGGER:
825 if (node == nullptr) {
826 // Single node world. If there is a local logger, then we want to use
827 // it.
828 return true;
829 }
830 return channel->source_node()->string_view() ==
831 node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800832 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700833 CHECK(channel->has_logger_nodes());
834 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800835
836 if (channel->source_node()->string_view() ==
837 CHECK_NOTNULL(node)->name()->string_view()) {
838 return true;
839 }
Austin Schuhda40e472020-03-28 15:15:29 -0700840
841 [[fallthrough]];
842 case LoggerConfig::REMOTE_LOGGER:
843 CHECK(channel->has_logger_nodes());
844 CHECK_GT(channel->logger_nodes()->size(), 0u);
845 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
846 if (logger_node->string_view() ==
847 CHECK_NOTNULL(node)->name()->string_view()) {
848 return true;
849 }
Austin Schuh719946b2019-12-28 14:51:01 -0800850 }
851
852 return false;
853 case LoggerConfig::NOT_LOGGED:
854 return false;
855 }
856
857 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
858}
859
860const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
861 if (!channel->has_destination_nodes()) {
862 return nullptr;
863 }
864 for (const Connection *connection : *channel->destination_nodes()) {
865 if (connection->name()->string_view() == node->name()->string_view()) {
866 return connection;
867 }
868 }
869 return nullptr;
870}
871
872bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
873 const Node *node,
874 const Node *logger_node) {
875 const Connection *connection = ConnectionToNode(channel, node);
876 if (connection == nullptr) {
877 return false;
878 }
879 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
880}
881
882bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
883 const Node *node) {
884 switch (connection->timestamp_logger()) {
885 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700886 CHECK(connection->has_timestamp_logger_nodes());
887 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800888 if (connection->name()->string_view() == node->name()->string_view()) {
889 return true;
890 }
891
Austin Schuhda40e472020-03-28 15:15:29 -0700892 [[fallthrough]];
893 case LoggerConfig::REMOTE_LOGGER:
894 CHECK(connection->has_timestamp_logger_nodes());
895 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
896 for (const flatbuffers::String *timestamp_logger_node :
897 *connection->timestamp_logger_nodes()) {
898 if (timestamp_logger_node->string_view() ==
899 node->name()->string_view()) {
900 return true;
901 }
Austin Schuh719946b2019-12-28 14:51:01 -0800902 }
903
904 return false;
905 case LoggerConfig::LOCAL_LOGGER:
906 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800907 case LoggerConfig::NOT_LOGGED:
908 return false;
909 }
910
911 LOG(FATAL) << "Unknown logger config "
912 << static_cast<int>(connection->timestamp_logger());
913}
914
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800915std::vector<std::string_view> SourceNodeNames(const Configuration *config,
916 const Node *my_node) {
917 std::set<std::string_view> result_set;
918
919 for (const Channel *channel : *config->channels()) {
920 if (channel->has_destination_nodes()) {
921 for (const Connection *connection : *channel->destination_nodes()) {
922 if (connection->name()->string_view() ==
923 my_node->name()->string_view()) {
924 result_set.insert(channel->source_node()->string_view());
925 }
926 }
927 }
928 }
929
930 std::vector<std::string_view> result;
931 for (const std::string_view source : result_set) {
932 VLOG(1) << "Found a source node of " << source;
933 result.emplace_back(source);
934 }
935 return result;
936}
937
938std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
939 const Node *my_node) {
940 std::vector<std::string_view> result;
941
942 for (const Channel *channel : *config->channels()) {
943 if (channel->has_source_node() && channel->source_node()->string_view() ==
944 my_node->name()->string_view()) {
945 if (!channel->has_destination_nodes()) continue;
946
947 if (channel->source_node()->string_view() !=
948 my_node->name()->string_view()) {
949 continue;
950 }
951
952 for (const Connection *connection : *channel->destination_nodes()) {
953 if (std::find(result.begin(), result.end(),
954 connection->name()->string_view()) == result.end()) {
955 result.emplace_back(connection->name()->string_view());
956 }
957 }
958 }
959 }
960
961 for (const std::string_view destination : result) {
962 VLOG(1) << "Found a destination node of " << destination;
963 }
964 return result;
965}
966
Brian Silverman66f079a2013-08-26 16:24:30 -0700967} // namespace configuration
968} // namespace aos