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()) { |
| 373 | for (const flatbuffers::String *n : *c->destination_nodes()) { |
| 374 | CHECK(GetNode(&result.message(), n->string_view()) != nullptr) |
| 375 | << ": Channel " << FlatbufferToJson(c) |
| 376 | << " has an unknown \"destination_nodes\" " << n->string_view(); |
| 377 | |
| 378 | CHECK_NE(n->string_view(), c->source_node()->string_view()) |
| 379 | << ": Channel " << FlatbufferToJson(c) |
| 380 | << " is forwarding data to itself"; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 389 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 390 | const std::string_view path) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 391 | // We only want to read a file once. So track the visited files in a set. |
| 392 | absl::btree_set<std::string> visited_paths; |
| 393 | return MergeConfiguration(ReadConfig(path, &visited_paths)); |
| 394 | } |
| 395 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 396 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 397 | std::string_view type, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 398 | std::string_view application_name, const Node *node) { |
| 399 | const std::string_view original_name = name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 400 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 401 | << "\" }"; |
| 402 | |
| 403 | // First handle application specific maps. Only do this if we have a matching |
| 404 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 405 | if (config->has_applications()) { |
| 406 | auto application_iterator = std::lower_bound( |
| 407 | config->applications()->cbegin(), config->applications()->cend(), |
| 408 | application_name, CompareApplications); |
| 409 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 410 | EqualsApplications(*application_iterator, application_name)) { |
| 411 | if (application_iterator->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 412 | HandleMaps(application_iterator->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 418 | if (config->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 419 | HandleMaps(config->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 422 | if (original_name != name) { |
| 423 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 424 | << type << "\" }"; |
| 425 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 426 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 427 | // Then look for the channel. |
| 428 | auto channel_iterator = |
| 429 | std::lower_bound(config->channels()->cbegin(), |
| 430 | config->channels()->cend(), |
| 431 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 432 | |
| 433 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 434 | if (channel_iterator != config->channels()->cend() && |
| 435 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 436 | if (VLOG_IS_ON(2)) { |
| 437 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 438 | } else if (VLOG_IS_ON(1)) { |
| 439 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 440 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 441 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 442 | } else { |
| 443 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 444 | << type << "\" }"; |
| 445 | return nullptr; |
| 446 | } |
| 447 | } |
| 448 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 449 | std::string CleanedChannelToString(const Channel *channel) { |
| 450 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 451 | cleaned_channel.mutable_message()->clear_schema(); |
| 452 | return FlatbufferToJson(cleaned_channel); |
| 453 | } |
| 454 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 456 | const Flatbuffer<Configuration> &config, |
| 457 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 458 | flatbuffers::FlatBufferBuilder fbb; |
| 459 | fbb.ForceDefaults(1); |
| 460 | |
| 461 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 462 | channels_offset; |
| 463 | if (config.message().has_channels()) { |
| 464 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 465 | for (const Channel *c : *config.message().channels()) { |
| 466 | flatbuffers::FlatBufferBuilder channel_fbb; |
| 467 | channel_fbb.ForceDefaults(1); |
| 468 | |
| 469 | // Search for a schema with a matching type. |
| 470 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
| 471 | for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) { |
| 472 | if (schema.message().root_table() != nullptr) { |
| 473 | if (schema.message().root_table()->name()->string_view() == |
| 474 | c->type()->string_view()) { |
| 475 | found_schema = &schema; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | CHECK(found_schema != nullptr) |
| 481 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 482 | |
| 483 | // The following is wasteful, but works. |
| 484 | // |
| 485 | // Copy it into a Channel object by creating an object with only the |
| 486 | // schema populated and merge that into the current channel. |
| 487 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 488 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 489 | &channel_fbb); |
| 490 | Channel::Builder channel_builder(channel_fbb); |
| 491 | channel_builder.add_schema(schema_offset); |
| 492 | channel_fbb.Finish(channel_builder.Finish()); |
| 493 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 494 | channel_fbb.Release()); |
| 495 | |
| 496 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 497 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 498 | |
| 499 | channel_offsets.emplace_back( |
| 500 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 501 | } |
| 502 | channels_offset = fbb.CreateVector(channel_offsets); |
| 503 | } |
| 504 | |
| 505 | // Copy the applications and maps unmodified. |
| 506 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 507 | applications_offset; |
| 508 | { |
| 509 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
| 510 | if (config.message().has_applications()) { |
| 511 | for (const Application *a : *config.message().applications()) { |
| 512 | applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb)); |
| 513 | } |
| 514 | } |
| 515 | applications_offset = fbb.CreateVector(applications_offsets); |
| 516 | } |
| 517 | |
| 518 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 519 | maps_offset; |
| 520 | { |
| 521 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 522 | if (config.message().has_maps()) { |
| 523 | for (const Map *m : *config.message().maps()) { |
| 524 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 525 | } |
| 526 | maps_offset = fbb.CreateVector(map_offsets); |
| 527 | } |
| 528 | } |
| 529 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 530 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 531 | nodes_offset; |
| 532 | { |
| 533 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 534 | if (config.message().has_nodes()) { |
| 535 | for (const Node *n : *config.message().nodes()) { |
| 536 | node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb)); |
| 537 | } |
| 538 | nodes_offset = fbb.CreateVector(node_offsets); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 543 | ConfigurationBuilder configuration_builder(fbb); |
| 544 | if (config.message().has_channels()) { |
| 545 | configuration_builder.add_channels(channels_offset); |
| 546 | } |
| 547 | if (config.message().has_maps()) { |
| 548 | configuration_builder.add_maps(maps_offset); |
| 549 | } |
| 550 | if (config.message().has_applications()) { |
| 551 | configuration_builder.add_applications(applications_offset); |
| 552 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 553 | if (config.message().has_nodes()) { |
| 554 | configuration_builder.add_nodes(nodes_offset); |
| 555 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 556 | |
| 557 | fbb.Finish(configuration_builder.Finish()); |
| 558 | return fbb.Release(); |
| 559 | } |
| 560 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 561 | const Node *GetNodeFromHostname(const Configuration *config, |
| 562 | std::string_view hostname) { |
| 563 | for (const Node *node : *config->nodes()) { |
| 564 | if (node->hostname()->string_view() == hostname) { |
| 565 | return node; |
| 566 | } |
| 567 | } |
| 568 | return nullptr; |
| 569 | } |
| 570 | const Node *GetMyNode(const Configuration *config) { |
| 571 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 572 | ? FLAGS_override_hostname |
| 573 | : network::GetHostname(); |
| 574 | const Node *node = GetNodeFromHostname(config, hostname); |
| 575 | if (node != nullptr) return node; |
| 576 | |
| 577 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 578 | << ". Consider using --override_hostname if hostname detection " |
| 579 | "is wrong."; |
| 580 | return nullptr; |
| 581 | } |
| 582 | |
| 583 | const Node *GetNode(const Configuration *config, std::string_view name) { |
| 584 | for (const Node *node : *config->nodes()) { |
| 585 | if (node->name()->string_view() == name) { |
| 586 | return node; |
| 587 | } |
| 588 | } |
| 589 | return nullptr; |
| 590 | } |
| 591 | |
| 592 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 593 | if (node == nullptr) { |
| 594 | return true; |
| 595 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 596 | return (channel->source_node()->string_view() == node->name()->string_view()); |
| 597 | } |
| 598 | |
| 599 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 600 | if (node == nullptr) { |
| 601 | return true; |
| 602 | } |
| 603 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 604 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 605 | return true; |
| 606 | } |
| 607 | |
| 608 | if (!channel->has_destination_nodes()) { |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | for (const flatbuffers::String *s : *channel->destination_nodes()) { |
| 613 | if (s->string_view() == node->name()->string_view()) { |
| 614 | return true; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return false; |
| 619 | } |
| 620 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 621 | } // namespace configuration |
| 622 | } // namespace aos |