blob: 55e58f17403a7810bb59951271ae0b94b2a55857 [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(
456 const Configuration *config, std::string_view json) {
457 FlatbufferDetachedBuffer<Configuration> addition =
458 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
459
460 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
461}
462
James Kuszmaul3ae42262019-11-08 12:33:41 -0800463const Channel *GetChannel(const Configuration *config, std::string_view name,
464 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800465 std::string_view application_name, const Node *node) {
466 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700467 std::string mutable_name;
Austin Schuhcb108412019-10-13 16:09:54 -0700468 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
469 << "\" }";
470
471 // First handle application specific maps. Only do this if we have a matching
472 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700473 if (config->has_applications()) {
474 auto application_iterator = std::lower_bound(
475 config->applications()->cbegin(), config->applications()->cend(),
476 application_name, CompareApplications);
477 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700478 EqualsApplications(*application_iterator, application_name)) {
479 if (application_iterator->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700480 mutable_name = std::string(name);
481 HandleMaps(application_iterator->maps(), &mutable_name, type, node);
482 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700483 }
484 }
485 }
486
487 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700488 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700489 mutable_name = std::string(name);
490 HandleMaps(config->maps(), &mutable_name, type, node);
491 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700492 }
493
Austin Schuhbca6cf02019-12-22 17:28:34 -0800494 if (original_name != name) {
495 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
496 << type << "\" }";
497 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700498
Austin Schuh40485ed2019-10-26 21:51:44 -0700499 // Then look for the channel.
500 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700501 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700502 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700503
504 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700505 if (channel_iterator != config->channels()->cend() &&
506 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800507 if (VLOG_IS_ON(2)) {
508 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
509 } else if (VLOG_IS_ON(1)) {
510 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
511 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700512 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700513 } else {
514 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
515 << type << "\" }";
516 return nullptr;
517 }
518}
519
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800520size_t ChannelIndex(const Configuration *configuration,
521 const Channel *channel) {
522 CHECK(configuration->channels() != nullptr) << ": No channels";
523
524 auto c = std::find(configuration->channels()->begin(),
525 configuration->channels()->end(), channel);
526 CHECK(c != configuration->channels()->end())
527 << ": Channel pointer not found in configuration()->channels()";
528
529 return std::distance(configuration->channels()->begin(), c);
530}
531
Austin Schuhbca6cf02019-12-22 17:28:34 -0800532std::string CleanedChannelToString(const Channel *channel) {
533 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
534 cleaned_channel.mutable_message()->clear_schema();
535 return FlatbufferToJson(cleaned_channel);
536}
537
Austin Schuha81454b2020-05-12 19:58:36 -0700538std::string StrippedChannelToString(const Channel *channel) {
539 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
540 "\", \"type\": \"", channel->type()->string_view(),
541 "\" }");
542}
543
Alex Perrycb7da4b2019-08-28 19:35:56 -0700544FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
545 const Flatbuffer<Configuration> &config,
546 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
547 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800548 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700549
550 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
551 channels_offset;
552 if (config.message().has_channels()) {
553 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
554 for (const Channel *c : *config.message().channels()) {
555 flatbuffers::FlatBufferBuilder channel_fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800556 channel_fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700557
558 // Search for a schema with a matching type.
559 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
Austin Schuhf1fff282020-03-28 16:57:32 -0700560 for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700561 if (schema.message().root_table() != nullptr) {
562 if (schema.message().root_table()->name()->string_view() ==
563 c->type()->string_view()) {
564 found_schema = &schema;
565 }
566 }
567 }
568
569 CHECK(found_schema != nullptr)
570 << ": Failed to find schema for " << FlatbufferToJson(c);
571
572 // The following is wasteful, but works.
573 //
574 // Copy it into a Channel object by creating an object with only the
575 // schema populated and merge that into the current channel.
576 flatbuffers::Offset<reflection::Schema> schema_offset =
577 CopyFlatBuffer<reflection::Schema>(&found_schema->message(),
578 &channel_fbb);
579 Channel::Builder channel_builder(channel_fbb);
580 channel_builder.add_schema(schema_offset);
581 channel_fbb.Finish(channel_builder.Finish());
582 FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer(
583 channel_fbb.Release());
584
585 FlatbufferDetachedBuffer<Channel> merged_channel(
586 MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c)));
587
588 channel_offsets.emplace_back(
589 CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb));
590 }
591 channels_offset = fbb.CreateVector(channel_offsets);
592 }
593
594 // Copy the applications and maps unmodified.
595 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
596 applications_offset;
597 {
598 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
599 if (config.message().has_applications()) {
600 for (const Application *a : *config.message().applications()) {
601 applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb));
602 }
603 }
604 applications_offset = fbb.CreateVector(applications_offsets);
605 }
606
607 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
608 maps_offset;
609 {
610 ::std::vector<flatbuffers::Offset<Map>> map_offsets;
611 if (config.message().has_maps()) {
612 for (const Map *m : *config.message().maps()) {
613 map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb));
614 }
615 maps_offset = fbb.CreateVector(map_offsets);
616 }
617 }
618
Austin Schuh217a9782019-12-21 23:02:50 -0800619 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
620 nodes_offset;
621 {
622 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
623 if (config.message().has_nodes()) {
624 for (const Node *n : *config.message().nodes()) {
625 node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb));
626 }
627 nodes_offset = fbb.CreateVector(node_offsets);
628 }
629 }
630
631 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700632 ConfigurationBuilder configuration_builder(fbb);
633 if (config.message().has_channels()) {
634 configuration_builder.add_channels(channels_offset);
635 }
636 if (config.message().has_maps()) {
637 configuration_builder.add_maps(maps_offset);
638 }
639 if (config.message().has_applications()) {
640 configuration_builder.add_applications(applications_offset);
641 }
Austin Schuh217a9782019-12-21 23:02:50 -0800642 if (config.message().has_nodes()) {
643 configuration_builder.add_nodes(nodes_offset);
644 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700645
646 fbb.Finish(configuration_builder.Finish());
647 return fbb.Release();
648}
649
Austin Schuh217a9782019-12-21 23:02:50 -0800650const Node *GetNodeFromHostname(const Configuration *config,
651 std::string_view hostname) {
652 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800653 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800654 return node;
655 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800656 if (node->has_hostnames()) {
657 for (const auto &candidate : *node->hostnames()) {
658 if (candidate->string_view() == hostname) {
659 return node;
660 }
661 }
662 }
Austin Schuh217a9782019-12-21 23:02:50 -0800663 }
664 return nullptr;
665}
Austin Schuhac0771c2020-01-07 18:36:30 -0800666
Austin Schuh217a9782019-12-21 23:02:50 -0800667const Node *GetMyNode(const Configuration *config) {
668 const std::string hostname = (FLAGS_override_hostname.size() > 0)
669 ? FLAGS_override_hostname
670 : network::GetHostname();
671 const Node *node = GetNodeFromHostname(config, hostname);
672 if (node != nullptr) return node;
673
674 LOG(FATAL) << "Unknown node for host: " << hostname
675 << ". Consider using --override_hostname if hostname detection "
676 "is wrong.";
677 return nullptr;
678}
679
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800680const Node *GetNode(const Configuration *config, const Node *node) {
681 if (!MultiNode(config)) {
682 CHECK(node == nullptr) << ": Provided a node in a single node world.";
683 return nullptr;
684 } else {
685 CHECK(node != nullptr);
686 CHECK(node->has_name());
687 return GetNode(config, node->name()->string_view());
688 }
689}
690
Austin Schuh217a9782019-12-21 23:02:50 -0800691const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800692 CHECK(config->has_nodes())
693 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800694 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800695 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800696 if (node->name()->string_view() == name) {
697 return node;
698 }
699 }
700 return nullptr;
701}
702
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800703const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
704 if (!MultiNode(config)) {
705 CHECK(node == nullptr) << ": Provided a node in a single node world.";
706 return nullptr;
707 } else {
708 const Node *config_node = GetNode(config, node);
709 if (config_node == nullptr) {
710 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
711 }
712 return config_node;
713 }
714}
715
Austin Schuh8bd96322020-02-13 21:18:22 -0800716namespace {
717int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800718 int node_index = 0;
719 for (const Node *iterated_node : *config->nodes()) {
720 if (iterated_node == node) {
721 return node_index;
722 }
723 ++node_index;
724 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800725 return -1;
726}
727} // namespace
728
729int GetNodeIndex(const Configuration *config, const Node *node) {
730 if (!MultiNode(config)) {
731 return 0;
732 }
733
734 {
735 int node_index = GetNodeIndexFromConfig(config, node);
736 if (node_index != -1) {
737 return node_index;
738 }
739 }
740
741 const Node *result = GetNode(config, node);
742 CHECK(result != nullptr);
743
744 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800745 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800746 if (node_index != -1) {
747 return node_index;
748 }
749 }
750
751 LOG(FATAL) << "Node " << FlatbufferToJson(node)
752 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800753}
754
Austin Schuh04408fc2020-02-16 21:48:54 -0800755int GetNodeIndex(const Configuration *config, std::string_view name) {
756 if (!MultiNode(config)) {
757 return 0;
758 }
759
760 {
761 int node_index = 0;
762 for (const Node *iterated_node : *config->nodes()) {
763 if (iterated_node->name()->string_view() == name) {
764 return node_index;
765 }
766 ++node_index;
767 }
768 }
769 LOG(FATAL) << "Node " << name << " not found in the configuration.";
770}
771
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800772std::vector<const Node *> GetNodes(const Configuration *config) {
773 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800774 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800775 for (const Node *node : *config->nodes()) {
776 nodes.emplace_back(node);
777 }
778 } else {
779 nodes.emplace_back(nullptr);
780 }
781 return nodes;
782}
783
Austin Schuhac0771c2020-01-07 18:36:30 -0800784bool MultiNode(const Configuration *config) { return config->has_nodes(); }
785
Austin Schuh217a9782019-12-21 23:02:50 -0800786bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800787 if (node == nullptr) {
788 return true;
789 }
Austin Schuh196a4452020-03-15 23:12:03 -0700790 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
791 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -0800792}
793
794bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800795 if (node == nullptr) {
796 return true;
797 }
798
Austin Schuh217a9782019-12-21 23:02:50 -0800799 if (channel->source_node()->string_view() == node->name()->string_view()) {
800 return true;
801 }
802
803 if (!channel->has_destination_nodes()) {
804 return false;
805 }
806
Austin Schuh719946b2019-12-28 14:51:01 -0800807 for (const Connection *connection : *channel->destination_nodes()) {
808 CHECK(connection->has_name());
809 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800810 return true;
811 }
812 }
813
814 return false;
815}
816
Austin Schuh719946b2019-12-28 14:51:01 -0800817bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700818 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800819 case LoggerConfig::LOCAL_LOGGER:
820 if (node == nullptr) {
821 // Single node world. If there is a local logger, then we want to use
822 // it.
823 return true;
824 }
825 return channel->source_node()->string_view() ==
826 node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800827 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700828 CHECK(channel->has_logger_nodes());
829 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800830
831 if (channel->source_node()->string_view() ==
832 CHECK_NOTNULL(node)->name()->string_view()) {
833 return true;
834 }
Austin Schuhda40e472020-03-28 15:15:29 -0700835
836 [[fallthrough]];
837 case LoggerConfig::REMOTE_LOGGER:
838 CHECK(channel->has_logger_nodes());
839 CHECK_GT(channel->logger_nodes()->size(), 0u);
840 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
841 if (logger_node->string_view() ==
842 CHECK_NOTNULL(node)->name()->string_view()) {
843 return true;
844 }
Austin Schuh719946b2019-12-28 14:51:01 -0800845 }
846
847 return false;
848 case LoggerConfig::NOT_LOGGED:
849 return false;
850 }
851
852 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
853}
854
855const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
856 if (!channel->has_destination_nodes()) {
857 return nullptr;
858 }
859 for (const Connection *connection : *channel->destination_nodes()) {
860 if (connection->name()->string_view() == node->name()->string_view()) {
861 return connection;
862 }
863 }
864 return nullptr;
865}
866
867bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
868 const Node *node,
869 const Node *logger_node) {
870 const Connection *connection = ConnectionToNode(channel, node);
871 if (connection == nullptr) {
872 return false;
873 }
874 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
875}
876
877bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
878 const Node *node) {
879 switch (connection->timestamp_logger()) {
880 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -0700881 CHECK(connection->has_timestamp_logger_nodes());
882 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -0800883 if (connection->name()->string_view() == node->name()->string_view()) {
884 return true;
885 }
886
Austin Schuhda40e472020-03-28 15:15:29 -0700887 [[fallthrough]];
888 case LoggerConfig::REMOTE_LOGGER:
889 CHECK(connection->has_timestamp_logger_nodes());
890 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
891 for (const flatbuffers::String *timestamp_logger_node :
892 *connection->timestamp_logger_nodes()) {
893 if (timestamp_logger_node->string_view() ==
894 node->name()->string_view()) {
895 return true;
896 }
Austin Schuh719946b2019-12-28 14:51:01 -0800897 }
898
899 return false;
900 case LoggerConfig::LOCAL_LOGGER:
901 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -0800902 case LoggerConfig::NOT_LOGGED:
903 return false;
904 }
905
906 LOG(FATAL) << "Unknown logger config "
907 << static_cast<int>(connection->timestamp_logger());
908}
909
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800910std::vector<std::string_view> SourceNodeNames(const Configuration *config,
911 const Node *my_node) {
912 std::set<std::string_view> result_set;
913
914 for (const Channel *channel : *config->channels()) {
915 if (channel->has_destination_nodes()) {
916 for (const Connection *connection : *channel->destination_nodes()) {
917 if (connection->name()->string_view() ==
918 my_node->name()->string_view()) {
919 result_set.insert(channel->source_node()->string_view());
920 }
921 }
922 }
923 }
924
925 std::vector<std::string_view> result;
926 for (const std::string_view source : result_set) {
927 VLOG(1) << "Found a source node of " << source;
928 result.emplace_back(source);
929 }
930 return result;
931}
932
933std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
934 const Node *my_node) {
935 std::vector<std::string_view> result;
936
937 for (const Channel *channel : *config->channels()) {
938 if (channel->has_source_node() && channel->source_node()->string_view() ==
939 my_node->name()->string_view()) {
940 if (!channel->has_destination_nodes()) continue;
941
942 if (channel->source_node()->string_view() !=
943 my_node->name()->string_view()) {
944 continue;
945 }
946
947 for (const Connection *connection : *channel->destination_nodes()) {
948 if (std::find(result.begin(), result.end(),
949 connection->name()->string_view()) == result.end()) {
950 result.emplace_back(connection->name()->string_view());
951 }
952 }
953 }
954 }
955
956 for (const std::string_view destination : result) {
957 VLOG(1) << "Found a destination node of " << destination;
958 }
959 return result;
960}
961
Brian Silverman66f079a2013-08-26 16:24:30 -0700962} // namespace configuration
963} // namespace aos