blob: b0ff973e0040038290f6985e63de635b28d48799 [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
Brian Silverman66f079a2013-08-26 16:24:30 -070025namespace aos {
Austin Schuhcb108412019-10-13 16:09:54 -070026
Austin Schuh40485ed2019-10-26 21:51:44 -070027// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070028// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070029bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
30 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070031 int name_compare = lhs.message().name()->string_view().compare(
32 rhs.message().name()->string_view());
33 if (name_compare == 0) {
34 return lhs.message().type()->string_view() <
35 rhs.message().type()->string_view();
36 } else if (name_compare < 0) {
37 return true;
38 } else {
39 return false;
40 }
41}
42
Austin Schuh40485ed2019-10-26 21:51:44 -070043bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
44 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070045 return lhs.message().name()->string_view() ==
46 rhs.message().name()->string_view() &&
47 lhs.message().type()->string_view() ==
48 rhs.message().type()->string_view();
49}
50
Austin Schuh40485ed2019-10-26 21:51:44 -070051bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
52 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070053 return lhs.message().name()->string_view() ==
54 rhs.message().name()->string_view();
55}
56
Austin Schuh40485ed2019-10-26 21:51:44 -070057bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
58 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070059 return lhs.message().name()->string_view() <
60 rhs.message().name()->string_view();
61}
62
Austin Schuh217a9782019-12-21 23:02:50 -080063bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
64 const FlatbufferDetachedBuffer<Node> &rhs) {
65 return lhs.message().name()->string_view() ==
66 rhs.message().name()->string_view();
67}
68
69bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
70 const FlatbufferDetachedBuffer<Node> &rhs) {
71 return lhs.message().name()->string_view() <
72 rhs.message().name()->string_view();
73}
74
Brian Silverman66f079a2013-08-26 16:24:30 -070075namespace configuration {
76namespace {
77
Austin Schuhcb108412019-10-13 16:09:54 -070078// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -070079std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -070080 auto last_slash_pos = filename.find_last_of("/\\");
81
James Kuszmaul3ae42262019-11-08 12:33:41 -080082 return last_slash_pos == std::string_view::npos
83 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -070084 : filename.substr(0, last_slash_pos + 1);
85}
86
Austin Schuh40485ed2019-10-26 21:51:44 -070087FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -080088 const std::string_view path, absl::btree_set<std::string> *visited_paths) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
90 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
91
92 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file";
93
94 FlatbufferDetachedBuffer<Configuration> config(std::move(buffer));
Austin Schuhcb108412019-10-13 16:09:54 -070095 // Depth first. Take the following example:
96 //
97 // config1.json:
98 // {
Austin Schuh40485ed2019-10-26 21:51:44 -070099 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700100 // {
101 // "name": "/foo",
102 // "type": ".aos.bar",
103 // "max_size": 5
104 // }
105 // ],
106 // "imports": [
107 // "config2.json",
108 // ]
109 // }
110 //
111 // config2.json:
112 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700113 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700114 // {
115 // "name": "/foo",
116 // "type": ".aos.bar",
117 // "max_size": 7
118 // }
119 // ],
120 // }
121 //
122 // We want the main config (config1.json) to be able to override the imported
123 // config. That means that it needs to be merged into the imported configs,
124 // not the other way around.
125
126 // Track that we have seen this file before recursing.
127 visited_paths->insert(::std::string(path));
128
129 if (config.message().has_imports()) {
130 // Capture the imports.
131 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
132 config.message().imports();
133
134 // And then wipe them. This gets GCed when we merge later.
135 config.mutable_message()->clear_imports();
136
137 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700138 FlatbufferDetachedBuffer<Configuration> merged_config =
139 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700140
141 const ::std::string folder(ExtractFolder(path));
142
143 for (const flatbuffers::String *str : *v) {
144 const ::std::string included_config = folder + str->c_str();
145 // Abort on any paths we have already seen.
146 CHECK(visited_paths->find(included_config) == visited_paths->end())
147 << ": Found duplicate file " << included_config << " while reading "
148 << path;
149
150 // And them merge everything in.
151 merged_config = MergeFlatBuffers(
152 merged_config, ReadConfig(included_config, visited_paths));
153 }
154
155 // Finally, merge this file in.
156 config = MergeFlatBuffers(merged_config, config);
157 }
158 return config;
159}
160
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161// Compares (c < p) a channel, and a name, type tuple.
162bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800163 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700164 int name_compare = c->name()->string_view().compare(p.first);
165 if (name_compare == 0) {
166 return c->type()->string_view() < p.second;
167 } else if (name_compare < 0) {
168 return true;
169 } else {
170 return false;
171 }
172};
173
174// Compares for equality (c == p) a channel, and a name, type tuple.
175bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700176 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700177 return c->name()->string_view() == p.first &&
178 c->type()->string_view() == p.second;
179}
180
181// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800182bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700183 return a->name()->string_view() < name;
184};
185
186// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800187bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 return a->name()->string_view() == name;
189}
190
191// Maps name for the provided maps. Modifies name.
192void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
Austin Schuhf1fff282020-03-28 16:57:32 -0700193 std::string *name, std::string_view type, const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700194 // For the same reason we merge configs in reverse order, we want to process
195 // maps in reverse order. That lets the outer config overwrite channels from
196 // the inner configs.
197 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800198 if (!i->has_match() || !i->match()->has_name()) {
199 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700200 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800201 if (!i->has_rename() || !i->rename()->has_name()) {
202 continue;
203 }
204
205 // Handle normal maps (now that we know that match and rename are filled
206 // out).
Austin Schuhf1fff282020-03-28 16:57:32 -0700207 const std::string_view match_name = i->match()->name()->string_view();
208 if (match_name != *name) {
209 if (match_name.back() == '*' &&
210 std::string_view(*name).substr(
211 0, std::min(name->size(), match_name.size() - 1)) ==
212 match_name.substr(0, match_name.size() - 1)) {
213 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
214 } else {
215 continue;
216 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800217 }
218
219 // Handle type specific maps.
220 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
221 continue;
222 }
223
Austin Schuhf1fff282020-03-28 16:57:32 -0700224 // Now handle node specific maps.
Austin Schuhbca6cf02019-12-22 17:28:34 -0800225 if (node != nullptr && i->match()->has_source_node() &&
226 i->match()->source_node()->string_view() !=
227 node->name()->string_view()) {
228 continue;
229 }
230
Austin Schuhf1fff282020-03-28 16:57:32 -0700231 std::string new_name(i->rename()->name()->string_view());
232 if (match_name.back() == '*') {
233 new_name += std::string(name->substr(match_name.size() - 1));
234 }
235 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
236 *name = std::move(new_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700237 }
238}
239
240} // namespace
241
Austin Schuh40485ed2019-10-26 21:51:44 -0700242FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700243 const Flatbuffer<Configuration> &config) {
Austin Schuh40485ed2019-10-26 21:51:44 -0700244 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700245 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700246 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700247
Austin Schuh40485ed2019-10-26 21:51:44 -0700248 if (config.message().has_channels()) {
249 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700250 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700251 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700252 continue;
253 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700254 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700255 continue;
256 }
257
Austin Schuh40485ed2019-10-26 21:51:44 -0700258 // Attempt to insert the channel.
259 auto result = channels.insert(CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700260 if (!result.second) {
261 // Already there, so merge the new table into the original.
Austin Schuh40485ed2019-10-26 21:51:44 -0700262 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700263 }
264 }
265 }
266
267 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700268 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700269 if (config.message().has_applications()) {
270 for (const Application *a : *config.message().applications()) {
271 if (!a->has_name()) {
272 continue;
273 }
274
275 auto result = applications.insert(CopyFlatBuffer(a));
276 if (!result.second) {
277 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a));
278 }
279 }
280 }
281
Austin Schuh217a9782019-12-21 23:02:50 -0800282 // Now repeat this for the node list.
283 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
284 if (config.message().has_nodes()) {
285 for (const Node *n : *config.message().nodes()) {
286 if (!n->has_name()) {
287 continue;
288 }
289
290 auto result = nodes.insert(CopyFlatBuffer(n));
291 if (!result.second) {
292 *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n));
293 }
294 }
295 }
296
Austin Schuhcb108412019-10-13 16:09:54 -0700297 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800298 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700299
300 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700301 // Channels
302 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
303 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700304 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700305 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
306 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700307 channel_offsets.emplace_back(CopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700308 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700309 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700310 }
311
312 // Applications
313 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
314 applications_offset;
315 {
316 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700317 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700318 applications_offsets.emplace_back(
319 CopyFlatBuffer<Application>(&a.message(), &fbb));
320 }
321 applications_offset = fbb.CreateVector(applications_offsets);
322 }
323
324 // Just copy the maps
325 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
326 maps_offset;
327 {
328 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
329 if (config.message().has_maps()) {
330 for (const Map *m : *config.message().maps()) {
331 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
332 }
333 maps_offset = fbb.CreateVector(map_offsets);
334 }
335 }
336
Austin Schuh217a9782019-12-21 23:02:50 -0800337 // Nodes
338 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
339 nodes_offset;
340 {
341 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
342 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
343 node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb));
344 }
345 nodes_offset = fbb.CreateVector(node_offsets);
346 }
347
Austin Schuhcb108412019-10-13 16:09:54 -0700348 // And then build a Configuration with them all.
349 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700350 configuration_builder.add_channels(channels_offset);
Austin Schuhcb108412019-10-13 16:09:54 -0700351 if (config.message().has_maps()) {
352 configuration_builder.add_maps(maps_offset);
353 }
Austin Schuh217a9782019-12-21 23:02:50 -0800354 if (config.message().has_applications()) {
355 configuration_builder.add_applications(applications_offset);
356 }
357 if (config.message().has_nodes()) {
358 configuration_builder.add_nodes(nodes_offset);
359 }
Austin Schuhcb108412019-10-13 16:09:54 -0700360
361 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800362
363 // Now, validate that if there is a node list, every channel has a source
364 // node.
365 FlatbufferDetachedBuffer<Configuration> result(fbb.Release());
366
367 // Check that if there is a node list, all the source nodes are filled out and
368 // valid, and all the destination nodes are valid (and not the source). This
369 // is a basic consistency check.
Austin Schuhf1fff282020-03-28 16:57:32 -0700370 if (result.message().has_channels()) {
371 for (const Channel *c : *result.message().channels()) {
372 if (c->name()->string_view().back() == '/') {
373 LOG(FATAL) << "Channel names can't end with '/'";
374 }
375 if(c->name()->string_view().find("//")!= std::string_view::npos) {
376 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
377 << ", can't use //.";
378 }
379 for (const char data : c->name()->string_view()) {
380 if (data >= '0' && data <= '9') {
381 continue;
382 }
383 if (data >= 'a' && data <= 'z') {
384 continue;
385 }
386 if (data >= 'A' && data <= 'Z') {
387 continue;
388 }
389 if (data == '-' || data == '_' || data == '/') {
390 continue;
391 }
392 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
393 << ", can only use [-a-zA-Z0-9_/]";
394 }
395 }
396 }
397
398 if (result.message().has_nodes() && result.message().has_channels()) {
399 for (const Channel *c : *result.message().channels()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800400 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
401 << " is missing \"source_node\"";
402 CHECK(GetNode(&result.message(), c->source_node()->string_view()) !=
403 nullptr)
404 << ": Channel " << FlatbufferToJson(c)
405 << " has an unknown \"source_node\"";
406
407 if (c->has_destination_nodes()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800408 for (const Connection *connection : *c->destination_nodes()) {
409 CHECK(connection->has_name());
410 CHECK(GetNode(&result.message(), connection->name()->string_view()) !=
411 nullptr)
Austin Schuh217a9782019-12-21 23:02:50 -0800412 << ": Channel " << FlatbufferToJson(c)
Austin Schuh719946b2019-12-28 14:51:01 -0800413 << " has an unknown \"destination_nodes\" "
414 << connection->name()->string_view();
Austin Schuh217a9782019-12-21 23:02:50 -0800415
Austin Schuh719946b2019-12-28 14:51:01 -0800416 switch (connection->timestamp_logger()) {
417 case LoggerConfig::LOCAL_LOGGER:
418 case LoggerConfig::NOT_LOGGED:
Austin Schuhda40e472020-03-28 15:15:29 -0700419 CHECK(!connection->has_timestamp_logger_nodes());
Austin Schuh719946b2019-12-28 14:51:01 -0800420 break;
421 case LoggerConfig::REMOTE_LOGGER:
422 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700423 CHECK(connection->has_timestamp_logger_nodes());
424 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
425 for (const flatbuffers::String *timestamp_logger_node :
426 *connection->timestamp_logger_nodes()) {
427 CHECK(GetNode(&result.message(),
428 timestamp_logger_node->string_view()) != nullptr)
429 << ": Channel " << FlatbufferToJson(c)
430 << " has an unknown \"timestamp_logger_node\""
431 << connection->name()->string_view();
432 }
Austin Schuh719946b2019-12-28 14:51:01 -0800433 break;
434 }
435
436 CHECK_NE(connection->name()->string_view(),
437 c->source_node()->string_view())
Austin Schuh217a9782019-12-21 23:02:50 -0800438 << ": Channel " << FlatbufferToJson(c)
439 << " is forwarding data to itself";
440 }
441 }
442 }
443 }
444
445 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700446}
447
Austin Schuh40485ed2019-10-26 21:51:44 -0700448FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -0800449 const std::string_view path) {
Austin Schuhcb108412019-10-13 16:09:54 -0700450 // We only want to read a file once. So track the visited files in a set.
451 absl::btree_set<std::string> visited_paths;
452 return MergeConfiguration(ReadConfig(path, &visited_paths));
453}
454
Austin Schuh8d6cea82020-02-28 12:17:16 -0800455FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Brian Silverman24f5aa82020-06-23 16:21:28 -0700456 const Configuration *config, const Flatbuffer<Configuration> &addition) {
457 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
458}
459
460FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Austin Schuh8d6cea82020-02-28 12:17:16 -0800461 const Configuration *config, std::string_view json) {
462 FlatbufferDetachedBuffer<Configuration> addition =
463 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
464
Brian Silverman24f5aa82020-06-23 16:21:28 -0700465 return MergeWithConfig(config, addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -0800466}
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