John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #include "aos/configuration.h" |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 2 | |
| 3 | #include <string.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <arpa/inet.h> |
| 8 | #include <ifaddrs.h> |
| 9 | #include <unistd.h> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 10 | #include <string_view> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 11 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 12 | #include "absl/container/btree_set.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 13 | #include "aos/configuration_generated.h" |
| 14 | #include "aos/flatbuffer_merge.h" |
| 15 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 16 | #include "aos/network/team_number.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 17 | #include "aos/unique_malloc_ptr.h" |
| 18 | #include "aos/util/file.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 19 | #include "gflags/gflags.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 20 | #include "glog/logging.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 21 | |
| 22 | DEFINE_string( |
| 23 | override_hostname, "", |
| 24 | "If set, this forces the hostname of this node to be the provided " |
| 25 | "hostname."); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 26 | |
| 27 | namespace aos { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 28 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 29 | // Define the compare and equal operators for Channel and Application so we can |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 30 | // insert them in the btree below. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 31 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 32 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 33 | int name_compare = lhs.message().name()->string_view().compare( |
| 34 | rhs.message().name()->string_view()); |
| 35 | if (name_compare == 0) { |
| 36 | return lhs.message().type()->string_view() < |
| 37 | rhs.message().type()->string_view(); |
| 38 | } else if (name_compare < 0) { |
| 39 | return true; |
| 40 | } else { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 45 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 46 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 47 | return lhs.message().name()->string_view() == |
| 48 | rhs.message().name()->string_view() && |
| 49 | lhs.message().type()->string_view() == |
| 50 | rhs.message().type()->string_view(); |
| 51 | } |
| 52 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 53 | bool operator==(const FlatbufferDetachedBuffer<Application> &lhs, |
| 54 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 55 | return lhs.message().name()->string_view() == |
| 56 | rhs.message().name()->string_view(); |
| 57 | } |
| 58 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 59 | bool operator<(const FlatbufferDetachedBuffer<Application> &lhs, |
| 60 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 61 | return lhs.message().name()->string_view() < |
| 62 | rhs.message().name()->string_view(); |
| 63 | } |
| 64 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 65 | bool operator==(const FlatbufferDetachedBuffer<Node> &lhs, |
| 66 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 67 | return lhs.message().name()->string_view() == |
| 68 | rhs.message().name()->string_view(); |
| 69 | } |
| 70 | |
| 71 | bool operator<(const FlatbufferDetachedBuffer<Node> &lhs, |
| 72 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 73 | return lhs.message().name()->string_view() < |
| 74 | rhs.message().name()->string_view(); |
| 75 | } |
| 76 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 77 | namespace configuration { |
| 78 | namespace { |
| 79 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 80 | // Extracts the folder part of a path. Returns ./ if there is no path. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 81 | std::string_view ExtractFolder( |
| 82 | const std::string_view filename) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 83 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 84 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 85 | return last_slash_pos == std::string_view::npos |
| 86 | ? std::string_view("./") |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 87 | : filename.substr(0, last_slash_pos + 1); |
| 88 | } |
| 89 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 90 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 91 | const std::string_view path, absl::btree_set<std::string> *visited_paths) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer( |
| 93 | util::ReadFileToStringOrDie(path), ConfigurationTypeTable()); |
| 94 | |
| 95 | CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file"; |
| 96 | |
| 97 | FlatbufferDetachedBuffer<Configuration> config(std::move(buffer)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 98 | // Depth first. Take the following example: |
| 99 | // |
| 100 | // config1.json: |
| 101 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 102 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 103 | // { |
| 104 | // "name": "/foo", |
| 105 | // "type": ".aos.bar", |
| 106 | // "max_size": 5 |
| 107 | // } |
| 108 | // ], |
| 109 | // "imports": [ |
| 110 | // "config2.json", |
| 111 | // ] |
| 112 | // } |
| 113 | // |
| 114 | // config2.json: |
| 115 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 116 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 117 | // { |
| 118 | // "name": "/foo", |
| 119 | // "type": ".aos.bar", |
| 120 | // "max_size": 7 |
| 121 | // } |
| 122 | // ], |
| 123 | // } |
| 124 | // |
| 125 | // We want the main config (config1.json) to be able to override the imported |
| 126 | // config. That means that it needs to be merged into the imported configs, |
| 127 | // not the other way around. |
| 128 | |
| 129 | // Track that we have seen this file before recursing. |
| 130 | visited_paths->insert(::std::string(path)); |
| 131 | |
| 132 | if (config.message().has_imports()) { |
| 133 | // Capture the imports. |
| 134 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v = |
| 135 | config.message().imports(); |
| 136 | |
| 137 | // And then wipe them. This gets GCed when we merge later. |
| 138 | config.mutable_message()->clear_imports(); |
| 139 | |
| 140 | // Start with an empty configuration to merge into. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 141 | FlatbufferDetachedBuffer<Configuration> merged_config = |
| 142 | FlatbufferDetachedBuffer<Configuration>::Empty(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 143 | |
| 144 | const ::std::string folder(ExtractFolder(path)); |
| 145 | |
| 146 | for (const flatbuffers::String *str : *v) { |
| 147 | const ::std::string included_config = folder + str->c_str(); |
| 148 | // Abort on any paths we have already seen. |
| 149 | CHECK(visited_paths->find(included_config) == visited_paths->end()) |
| 150 | << ": Found duplicate file " << included_config << " while reading " |
| 151 | << path; |
| 152 | |
| 153 | // And them merge everything in. |
| 154 | merged_config = MergeFlatBuffers( |
| 155 | merged_config, ReadConfig(included_config, visited_paths)); |
| 156 | } |
| 157 | |
| 158 | // Finally, merge this file in. |
| 159 | config = MergeFlatBuffers(merged_config, config); |
| 160 | } |
| 161 | return config; |
| 162 | } |
| 163 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 164 | // Compares (c < p) a channel, and a name, type tuple. |
| 165 | bool CompareChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 166 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 167 | int name_compare = c->name()->string_view().compare(p.first); |
| 168 | if (name_compare == 0) { |
| 169 | return c->type()->string_view() < p.second; |
| 170 | } else if (name_compare < 0) { |
| 171 | return true; |
| 172 | } else { |
| 173 | return false; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | // Compares for equality (c == p) a channel, and a name, type tuple. |
| 178 | bool EqualsChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 179 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 180 | return c->name()->string_view() == p.first && |
| 181 | c->type()->string_view() == p.second; |
| 182 | } |
| 183 | |
| 184 | // Compares (c < p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 185 | bool CompareApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 186 | return a->name()->string_view() < name; |
| 187 | }; |
| 188 | |
| 189 | // Compares for equality (c == p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 190 | bool EqualsApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | return a->name()->string_view() == name; |
| 192 | } |
| 193 | |
| 194 | // Maps name for the provided maps. Modifies name. |
| 195 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 196 | std::string_view *name, std::string_view type, |
| 197 | const Node *node) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 198 | // For the same reason we merge configs in reverse order, we want to process |
| 199 | // maps in reverse order. That lets the outer config overwrite channels from |
| 200 | // the inner configs. |
| 201 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 202 | if (!i->has_match() || !i->match()->has_name()) { |
| 203 | continue; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 204 | } |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 205 | if (!i->has_rename() || !i->rename()->has_name()) { |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | // Handle normal maps (now that we know that match and rename are filled |
| 210 | // out). |
| 211 | if (i->match()->name()->string_view() != *name) { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | // Handle type specific maps. |
| 216 | if (i->match()->has_type() && i->match()->type()->string_view() != type) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | if (node != nullptr && i->match()->has_source_node() && |
| 221 | i->match()->source_node()->string_view() != |
| 222 | node->name()->string_view()) { |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | VLOG(1) << "Renamed \"" << *name << "\" to \"" |
| 227 | << i->rename()->name()->string_view() << "\""; |
| 228 | *name = i->rename()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | } // namespace |
| 233 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 234 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 235 | const Flatbuffer<Configuration> &config) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 236 | // Store all the channels in a sorted set. This lets us track channels we |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 237 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 238 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 239 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 240 | if (config.message().has_channels()) { |
| 241 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 242 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 243 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 244 | continue; |
| 245 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 246 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 247 | continue; |
| 248 | } |
| 249 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 250 | // Attempt to insert the channel. |
| 251 | auto result = channels.insert(CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 252 | if (!result.second) { |
| 253 | // Already there, so merge the new table into the original. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 254 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 260 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 261 | if (config.message().has_applications()) { |
| 262 | for (const Application *a : *config.message().applications()) { |
| 263 | if (!a->has_name()) { |
| 264 | continue; |
| 265 | } |
| 266 | |
| 267 | auto result = applications.insert(CopyFlatBuffer(a)); |
| 268 | if (!result.second) { |
| 269 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a)); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 274 | // Now repeat this for the node list. |
| 275 | absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes; |
| 276 | if (config.message().has_nodes()) { |
| 277 | for (const Node *n : *config.message().nodes()) { |
| 278 | if (!n->has_name()) { |
| 279 | continue; |
| 280 | } |
| 281 | |
| 282 | auto result = nodes.insert(CopyFlatBuffer(n)); |
| 283 | if (!result.second) { |
| 284 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n)); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 289 | flatbuffers::FlatBufferBuilder fbb; |
| 290 | fbb.ForceDefaults(1); |
| 291 | |
| 292 | // Start by building the vectors. They need to come before the final table. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 293 | // Channels |
| 294 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 295 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 296 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 297 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 298 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
| 299 | channel_offsets.emplace_back( |
| 300 | CopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 301 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 302 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Applications |
| 306 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 307 | applications_offset; |
| 308 | { |
| 309 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 310 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 311 | applications_offsets.emplace_back( |
| 312 | CopyFlatBuffer<Application>(&a.message(), &fbb)); |
| 313 | } |
| 314 | applications_offset = fbb.CreateVector(applications_offsets); |
| 315 | } |
| 316 | |
| 317 | // Just copy the maps |
| 318 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 319 | maps_offset; |
| 320 | { |
| 321 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 322 | if (config.message().has_maps()) { |
| 323 | for (const Map *m : *config.message().maps()) { |
| 324 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 325 | } |
| 326 | maps_offset = fbb.CreateVector(map_offsets); |
| 327 | } |
| 328 | } |
| 329 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 330 | // Nodes |
| 331 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 332 | nodes_offset; |
| 333 | { |
| 334 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 335 | for (const FlatbufferDetachedBuffer<Node> &n : nodes) { |
| 336 | node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb)); |
| 337 | } |
| 338 | nodes_offset = fbb.CreateVector(node_offsets); |
| 339 | } |
| 340 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 341 | // And then build a Configuration with them all. |
| 342 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 343 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 344 | if (config.message().has_maps()) { |
| 345 | configuration_builder.add_maps(maps_offset); |
| 346 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 347 | if (config.message().has_applications()) { |
| 348 | configuration_builder.add_applications(applications_offset); |
| 349 | } |
| 350 | if (config.message().has_nodes()) { |
| 351 | configuration_builder.add_nodes(nodes_offset); |
| 352 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 353 | |
| 354 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 355 | |
| 356 | // Now, validate that if there is a node list, every channel has a source |
| 357 | // node. |
| 358 | FlatbufferDetachedBuffer<Configuration> result(fbb.Release()); |
| 359 | |
| 360 | // Check that if there is a node list, all the source nodes are filled out and |
| 361 | // valid, and all the destination nodes are valid (and not the source). This |
| 362 | // is a basic consistency check. |
| 363 | if (result.message().has_nodes()) { |
| 364 | for (const Channel *c : *config.message().channels()) { |
| 365 | CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c) |
| 366 | << " is missing \"source_node\""; |
| 367 | CHECK(GetNode(&result.message(), c->source_node()->string_view()) != |
| 368 | nullptr) |
| 369 | << ": Channel " << FlatbufferToJson(c) |
| 370 | << " has an unknown \"source_node\""; |
| 371 | |
| 372 | if (c->has_destination_nodes()) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 373 | for (const Connection *connection : *c->destination_nodes()) { |
| 374 | CHECK(connection->has_name()); |
| 375 | CHECK(GetNode(&result.message(), connection->name()->string_view()) != |
| 376 | nullptr) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 377 | << ": Channel " << FlatbufferToJson(c) |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 378 | << " has an unknown \"destination_nodes\" " |
| 379 | << connection->name()->string_view(); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 380 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 381 | switch (connection->timestamp_logger()) { |
| 382 | case LoggerConfig::LOCAL_LOGGER: |
| 383 | case LoggerConfig::NOT_LOGGED: |
| 384 | CHECK(!connection->has_timestamp_logger_node()); |
| 385 | break; |
| 386 | case LoggerConfig::REMOTE_LOGGER: |
| 387 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 388 | CHECK(connection->has_timestamp_logger_node()); |
| 389 | CHECK( |
| 390 | GetNode(&result.message(), |
| 391 | connection->timestamp_logger_node()->string_view()) != |
| 392 | nullptr) |
| 393 | << ": Channel " << FlatbufferToJson(c) |
| 394 | << " has an unknown \"timestamp_logger_node\"" |
| 395 | << connection->name()->string_view(); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | CHECK_NE(connection->name()->string_view(), |
| 400 | c->source_node()->string_view()) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 401 | << ": Channel " << FlatbufferToJson(c) |
| 402 | << " is forwarding data to itself"; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 411 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 412 | const std::string_view path) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 413 | // We only want to read a file once. So track the visited files in a set. |
| 414 | absl::btree_set<std::string> visited_paths; |
| 415 | return MergeConfiguration(ReadConfig(path, &visited_paths)); |
| 416 | } |
| 417 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 418 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 419 | std::string_view type, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 420 | std::string_view application_name, const Node *node) { |
| 421 | const std::string_view original_name = name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 422 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 423 | << "\" }"; |
| 424 | |
| 425 | // First handle application specific maps. Only do this if we have a matching |
| 426 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 427 | if (config->has_applications()) { |
| 428 | auto application_iterator = std::lower_bound( |
| 429 | config->applications()->cbegin(), config->applications()->cend(), |
| 430 | application_name, CompareApplications); |
| 431 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 432 | EqualsApplications(*application_iterator, application_name)) { |
| 433 | if (application_iterator->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 434 | HandleMaps(application_iterator->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 440 | if (config->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 441 | HandleMaps(config->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 444 | if (original_name != name) { |
| 445 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 446 | << type << "\" }"; |
| 447 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 448 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 449 | // Then look for the channel. |
| 450 | auto channel_iterator = |
| 451 | std::lower_bound(config->channels()->cbegin(), |
| 452 | config->channels()->cend(), |
| 453 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 454 | |
| 455 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 456 | if (channel_iterator != config->channels()->cend() && |
| 457 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 458 | if (VLOG_IS_ON(2)) { |
| 459 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 460 | } else if (VLOG_IS_ON(1)) { |
| 461 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 462 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 463 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 464 | } else { |
| 465 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 466 | << type << "\" }"; |
| 467 | return nullptr; |
| 468 | } |
| 469 | } |
| 470 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 471 | std::string CleanedChannelToString(const Channel *channel) { |
| 472 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 473 | cleaned_channel.mutable_message()->clear_schema(); |
| 474 | return FlatbufferToJson(cleaned_channel); |
| 475 | } |
| 476 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 477 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 478 | const Flatbuffer<Configuration> &config, |
| 479 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 480 | flatbuffers::FlatBufferBuilder fbb; |
| 481 | fbb.ForceDefaults(1); |
| 482 | |
| 483 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 484 | channels_offset; |
| 485 | if (config.message().has_channels()) { |
| 486 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 487 | for (const Channel *c : *config.message().channels()) { |
| 488 | flatbuffers::FlatBufferBuilder channel_fbb; |
| 489 | channel_fbb.ForceDefaults(1); |
| 490 | |
| 491 | // Search for a schema with a matching type. |
| 492 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
| 493 | for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) { |
| 494 | if (schema.message().root_table() != nullptr) { |
| 495 | if (schema.message().root_table()->name()->string_view() == |
| 496 | c->type()->string_view()) { |
| 497 | found_schema = &schema; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | CHECK(found_schema != nullptr) |
| 503 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 504 | |
| 505 | // The following is wasteful, but works. |
| 506 | // |
| 507 | // Copy it into a Channel object by creating an object with only the |
| 508 | // schema populated and merge that into the current channel. |
| 509 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 510 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 511 | &channel_fbb); |
| 512 | Channel::Builder channel_builder(channel_fbb); |
| 513 | channel_builder.add_schema(schema_offset); |
| 514 | channel_fbb.Finish(channel_builder.Finish()); |
| 515 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 516 | channel_fbb.Release()); |
| 517 | |
| 518 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 519 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 520 | |
| 521 | channel_offsets.emplace_back( |
| 522 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 523 | } |
| 524 | channels_offset = fbb.CreateVector(channel_offsets); |
| 525 | } |
| 526 | |
| 527 | // Copy the applications and maps unmodified. |
| 528 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 529 | applications_offset; |
| 530 | { |
| 531 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
| 532 | if (config.message().has_applications()) { |
| 533 | for (const Application *a : *config.message().applications()) { |
| 534 | applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb)); |
| 535 | } |
| 536 | } |
| 537 | applications_offset = fbb.CreateVector(applications_offsets); |
| 538 | } |
| 539 | |
| 540 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 541 | maps_offset; |
| 542 | { |
| 543 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 544 | if (config.message().has_maps()) { |
| 545 | for (const Map *m : *config.message().maps()) { |
| 546 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 547 | } |
| 548 | maps_offset = fbb.CreateVector(map_offsets); |
| 549 | } |
| 550 | } |
| 551 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 552 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 553 | nodes_offset; |
| 554 | { |
| 555 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 556 | if (config.message().has_nodes()) { |
| 557 | for (const Node *n : *config.message().nodes()) { |
| 558 | node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb)); |
| 559 | } |
| 560 | nodes_offset = fbb.CreateVector(node_offsets); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 565 | ConfigurationBuilder configuration_builder(fbb); |
| 566 | if (config.message().has_channels()) { |
| 567 | configuration_builder.add_channels(channels_offset); |
| 568 | } |
| 569 | if (config.message().has_maps()) { |
| 570 | configuration_builder.add_maps(maps_offset); |
| 571 | } |
| 572 | if (config.message().has_applications()) { |
| 573 | configuration_builder.add_applications(applications_offset); |
| 574 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 575 | if (config.message().has_nodes()) { |
| 576 | configuration_builder.add_nodes(nodes_offset); |
| 577 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 578 | |
| 579 | fbb.Finish(configuration_builder.Finish()); |
| 580 | return fbb.Release(); |
| 581 | } |
| 582 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 583 | const Node *GetNodeFromHostname(const Configuration *config, |
| 584 | std::string_view hostname) { |
| 585 | for (const Node *node : *config->nodes()) { |
| 586 | if (node->hostname()->string_view() == hostname) { |
| 587 | return node; |
| 588 | } |
| 589 | } |
| 590 | return nullptr; |
| 591 | } |
| 592 | const Node *GetMyNode(const Configuration *config) { |
| 593 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 594 | ? FLAGS_override_hostname |
| 595 | : network::GetHostname(); |
| 596 | const Node *node = GetNodeFromHostname(config, hostname); |
| 597 | if (node != nullptr) return node; |
| 598 | |
| 599 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 600 | << ". Consider using --override_hostname if hostname detection " |
| 601 | "is wrong."; |
| 602 | return nullptr; |
| 603 | } |
| 604 | |
| 605 | const Node *GetNode(const Configuration *config, std::string_view name) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 606 | CHECK(config->has_nodes()) |
| 607 | << ": Asking for a node from a single node configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 608 | for (const Node *node : *config->nodes()) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 609 | CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 610 | if (node->name()->string_view() == name) { |
| 611 | return node; |
| 612 | } |
| 613 | } |
| 614 | return nullptr; |
| 615 | } |
| 616 | |
| 617 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 618 | if (node == nullptr) { |
| 619 | return true; |
| 620 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 621 | return (channel->source_node()->string_view() == node->name()->string_view()); |
| 622 | } |
| 623 | |
| 624 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 625 | if (node == nullptr) { |
| 626 | return true; |
| 627 | } |
| 628 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 629 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 630 | return true; |
| 631 | } |
| 632 | |
| 633 | if (!channel->has_destination_nodes()) { |
| 634 | return false; |
| 635 | } |
| 636 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 637 | for (const Connection *connection : *channel->destination_nodes()) { |
| 638 | CHECK(connection->has_name()); |
| 639 | if (connection->name()->string_view() == node->name()->string_view()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 640 | return true; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return false; |
| 645 | } |
| 646 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 647 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) { |
| 648 | switch(channel->logger()) { |
| 649 | case LoggerConfig::LOCAL_LOGGER: |
| 650 | if (node == nullptr) { |
| 651 | // Single node world. If there is a local logger, then we want to use |
| 652 | // it. |
| 653 | return true; |
| 654 | } |
| 655 | return channel->source_node()->string_view() == |
| 656 | node->name()->string_view(); |
| 657 | case LoggerConfig::REMOTE_LOGGER: |
| 658 | CHECK(channel->has_logger_node()); |
| 659 | |
| 660 | return channel->logger_node()->string_view() == |
| 661 | CHECK_NOTNULL(node)->name()->string_view(); |
| 662 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 663 | CHECK(channel->has_logger_node()); |
| 664 | |
| 665 | if (channel->source_node()->string_view() == |
| 666 | CHECK_NOTNULL(node)->name()->string_view()) { |
| 667 | return true; |
| 668 | } |
| 669 | if (channel->logger_node()->string_view() == node->name()->string_view()) { |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | return false; |
| 674 | case LoggerConfig::NOT_LOGGED: |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger()); |
| 679 | } |
| 680 | |
| 681 | const Connection *ConnectionToNode(const Channel *channel, const Node *node) { |
| 682 | if (!channel->has_destination_nodes()) { |
| 683 | return nullptr; |
| 684 | } |
| 685 | for (const Connection *connection : *channel->destination_nodes()) { |
| 686 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 687 | return connection; |
| 688 | } |
| 689 | } |
| 690 | return nullptr; |
| 691 | } |
| 692 | |
| 693 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 694 | const Node *node, |
| 695 | const Node *logger_node) { |
| 696 | const Connection *connection = ConnectionToNode(channel, node); |
| 697 | if (connection == nullptr) { |
| 698 | return false; |
| 699 | } |
| 700 | return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node); |
| 701 | } |
| 702 | |
| 703 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 704 | const Node *node) { |
| 705 | switch (connection->timestamp_logger()) { |
| 706 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 707 | CHECK(connection->has_timestamp_logger_node()); |
| 708 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | if (connection->timestamp_logger_node()->string_view() == |
| 713 | node->name()->string_view()) { |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | return false; |
| 718 | case LoggerConfig::LOCAL_LOGGER: |
| 719 | return connection->name()->string_view() == node->name()->string_view(); |
| 720 | case LoggerConfig::REMOTE_LOGGER: |
| 721 | CHECK(connection->has_timestamp_logger_node()); |
| 722 | |
| 723 | return connection->timestamp_logger_node()->string_view() == |
| 724 | node->name()->string_view(); |
| 725 | case LoggerConfig::NOT_LOGGED: |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | LOG(FATAL) << "Unknown logger config " |
| 730 | << static_cast<int>(connection->timestamp_logger()); |
| 731 | } |
| 732 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 733 | } // namespace configuration |
| 734 | } // namespace aos |