blob: a34b5a80df9b1469d546787bfea54ca4e88f97af [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#ifndef AOS_CONFIGURATION_H_
2#define AOS_CONFIGURATION_H_
Brian Silverman66f079a2013-08-26 16:24:30 -07003
Brian Silverman66f079a2013-08-26 16:24:30 -07004#include <arpa/inet.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <netinet/in.h>
6#include <sys/socket.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07007
Austin Schuhfb37c612022-08-11 15:24:51 -07008#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07009#include <cstdint>
James Kuszmaul3ae42262019-11-08 12:33:41 -080010#include <string_view>
11
Austin Schuhb8075812020-10-19 09:36:49 -070012#include "aos/configuration_generated.h" // IWYU pragma: export
Austin Schuhcb108412019-10-13 16:09:54 -070013#include "aos/flatbuffers.h"
14
Brian Silverman66f079a2013-08-26 16:24:30 -070015namespace aos {
16
17// Holds global configuration data. All of the functions are safe to call
18// from wherever.
19namespace configuration {
20
Austin Schuhcb108412019-10-13 16:09:54 -070021// Reads a json configuration. This includes all imports and merges. Note:
Milind Upadhyay17098ba2022-04-15 22:18:50 -070022// duplicate imports or invalid paths will result in a FATAL.
Austin Schuh40485ed2019-10-26 21:51:44 -070023FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -070024 const std::string_view path,
25 const std::vector<std::string_view> &extra_import_paths = {});
Austin Schuhcb108412019-10-13 16:09:54 -070026
Milind Upadhyay17098ba2022-04-15 22:18:50 -070027// Reads a json configuration. This includes all imports and merges. Returns
28// std::nullopt on duplicate imports or invalid paths.
29std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig(
30 const std::string_view path,
31 const std::vector<std::string_view> &extra_import_paths = {});
32
Alex Perrycb7da4b2019-08-28 19:35:56 -070033// Sorts and merges entries in a config.
34FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
35 const Flatbuffer<Configuration> &config);
36
37// Adds schema definitions to a sorted and merged config from the provided
38// schema list.
39FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
40 const Flatbuffer<Configuration> &config,
Austin Schuh0de30f32020-12-06 12:44:28 -080041 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas);
Alex Perrycb7da4b2019-08-28 19:35:56 -070042
Austin Schuh8d6cea82020-02-28 12:17:16 -080043// Merges a configuration json snippet into the provided configuration and
44// returns the modified config.
45FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
46 const Configuration *config, std::string_view json);
Brian Silverman24f5aa82020-06-23 16:21:28 -070047FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
48 const Configuration *config, const Flatbuffer<Configuration> &addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -080049
Austin Schuhc5fa6d92022-02-25 14:36:28 -080050// Adds the list of schemas to the provide aos_config.json. This should mostly
51// be used for testing and in conjunction with MergeWithConfig.
Austin Schuha9df9ad2021-06-16 14:49:39 -070052FlatbufferDetachedBuffer<aos::Configuration> AddSchema(
53 std::string_view json,
54 const std::vector<FlatbufferVector<reflection::Schema>> &schemas);
55
Austin Schuh681a2472020-12-31 23:55:40 -080056// Returns the resolved Channel for a name, type, and application name. Returns
Austin Schuh40485ed2019-10-26 21:51:44 -070057// nullptr if none is found.
Austin Schuhcb108412019-10-13 16:09:54 -070058//
59// If the application name is empty, it is ignored. Maps are processed in
60// reverse order, and application specific first.
James Kuszmaul71a81932020-12-15 21:08:01 -080061//
62// The config should already be fully merged and sorted (as produced by
63// MergeConfiguration() or any of the associated functions).
Austin Schuhbca6cf02019-12-22 17:28:34 -080064const Channel *GetChannel(const Configuration *config,
65 const std::string_view name,
66 const std::string_view type,
67 const std::string_view application_name,
Austin Schuh0de30f32020-12-06 12:44:28 -080068 const Node *node, bool quiet = false);
Austin Schuhbca6cf02019-12-22 17:28:34 -080069inline const Channel *GetChannel(const Flatbuffer<Configuration> &config,
70 const std::string_view name,
71 const std::string_view type,
72 const std::string_view application_name,
73 const Node *node) {
74 return GetChannel(&config.message(), name, type, application_name, node);
Austin Schuh40485ed2019-10-26 21:51:44 -070075}
Austin Schuh2f8fd752020-09-01 22:38:28 -070076template <typename T>
77inline const Channel *GetChannel(const Configuration *config,
78 const std::string_view name,
79 const std::string_view application_name,
80 const Node *node) {
81 return GetChannel(config, name, T::GetFullyQualifiedName(), application_name,
82 node);
83}
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080084// Convenience wrapper for getting a channel from a specified config if you
85// already have the name/type in a Channel object--this is useful if you Channel
86// object you have does not point to memory within config.
87inline const Channel *GetChannel(const Configuration *config,
88 const Channel *channel,
89 const std::string_view application_name,
90 const Node *node) {
91 return GetChannel(config, channel->name()->string_view(),
92 channel->type()->string_view(), application_name, node);
93}
Austin Schuhcb108412019-10-13 16:09:54 -070094
James Kuszmaulc8503f32022-06-25 16:17:12 -070095// Returns a list of all the channel names that can be used to refer to the
96// specified channel on the given node/application. this allows a reverse-lookup
97// of any renames that happen.
98// Does not perform forwards-lookup first.
99std::set<std::string> GetChannelAliases(const Configuration *config,
100 std::string_view name,
101 std::string_view type,
102 const std::string_view application_name,
103 const Node *node);
104inline std::set<std::string> GetChannelAliases(
105 const Configuration *config, const Channel *channel,
106 const std::string_view application_name, const Node *node) {
107 return GetChannelAliases(config, channel->name()->string_view(),
108 channel->type()->string_view(), application_name,
109 node);
110}
111
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800112// Returns the channel index (or dies) of channel in the provided config.
113size_t ChannelIndex(const Configuration *config, const Channel *channel);
114
Austin Schuh217a9782019-12-21 23:02:50 -0800115// Returns the Node out of the config with the matching name, or nullptr if it
116// can't be found.
117const Node *GetNode(const Configuration *config, std::string_view name);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800118const Node *GetNode(const Configuration *config, const Node *node);
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800119// Returns the node with the provided index. This works in both a single and
120// multi-node world.
121const Node *GetNode(const Configuration *config, size_t node_index);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800122// Returns a matching node, or nullptr if the provided node is nullptr and we
123// are in a single node world.
124const Node *GetNodeOrDie(const Configuration *config, const Node *node);
Austin Schuh217a9782019-12-21 23:02:50 -0800125// Returns the Node out of the configuration which matches our hostname.
126// CHECKs if it can't be found.
127const Node *GetMyNode(const Configuration *config);
128const Node *GetNodeFromHostname(const Configuration *config,
129 std::string_view name);
Maxwell Gumleyf84aca32024-05-10 13:51:59 -0600130// Determine if this node exists in this configuration. This function checks to
131// make sure this pointer exists in the configuration. This function is useful
132// for determing if the node is named the same but from a different
133// configuration.
134bool IsNodeFromConfiguration(const Configuration *config, const Node *node);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700135
Austin Schuh63097262023-08-16 17:04:29 -0700136// Returns a printable name for the node. (singlenode) if we are on a single
137// node system, and the name otherwise.
138std::string_view NodeName(const Configuration *config, size_t node_index);
139
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800140// Returns a vector of the nodes in the config. (nullptr is considered the node
141// in a single node world.)
142std::vector<const Node *> GetNodes(const Configuration *config);
143
Austin Schuh65465332020-11-05 17:36:53 -0800144// Returns a vector of the nodes in the config with the provided tag. If this
145// is a single-node world, we assume that all tags match.
146std::vector<const Node *> GetNodesWithTag(const Configuration *config,
147 std::string_view tag);
148
Brian Silverman631b6262021-11-10 12:25:08 -0800149// Returns whether the given node has the provided tag. If this is a single-node
150// world, we assume that all tags match.
151bool NodeHasTag(const Node *node, std::string_view tag);
152
Austin Schuh8bd96322020-02-13 21:18:22 -0800153// Returns the node index for a node. Note: will be faster if node is a pointer
154// to a node in config, but is not required.
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800155int GetNodeIndex(const Configuration *config, const Node *node);
Austin Schuh04408fc2020-02-16 21:48:54 -0800156int GetNodeIndex(const Configuration *config, std::string_view name);
Austin Schuh681a2472020-12-31 23:55:40 -0800157// Returns the number of nodes.
158size_t NodesCount(const Configuration *config);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800159
Austin Schuhac0771c2020-01-07 18:36:30 -0800160// Returns true if we are running in a multinode configuration.
161bool MultiNode(const Configuration *config);
162
Austin Schuh217a9782019-12-21 23:02:50 -0800163// Returns true if the provided channel is sendable on the provided node.
164bool ChannelIsSendableOnNode(const Channel *channel, const Node *node);
165// Returns true if the provided channel is able to be watched or fetched on the
166// provided node.
167bool ChannelIsReadableOnNode(const Channel *channel, const Node *node);
168
James Kuszmaul24db2d32023-05-26 11:40:12 -0700169// Returns true if the provided channel is sent on the provided node and gets
170// forwarded to any other nodes.
171bool ChannelIsForwardedFromNode(const Channel *channel, const Node *node);
172
Austin Schuh719946b2019-12-28 14:51:01 -0800173// Returns true if the message is supposed to be logged on this node.
174bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node);
Austin Schuh2bb80e02021-03-20 21:46:17 -0700175bool ChannelMessageIsLoggedOnNode(const Channel *channel,
176 std::string_view node_name);
Austin Schuh719946b2019-12-28 14:51:01 -0800177
Austin Schuh58646e22021-08-23 23:51:46 -0700178// Returns the number of connections.
179size_t ConnectionCount(const Channel *channel);
180
Austin Schuh719946b2019-12-28 14:51:01 -0800181const Connection *ConnectionToNode(const Channel *channel, const Node *node);
182// Returns true if the delivery timestamps are supposed to be logged on this
183// node.
184bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
185 const Node *node,
186 const Node *logger_node);
187bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
188 const Node *node);
189
Austin Schuhbca6cf02019-12-22 17:28:34 -0800190// Prints a channel to json, but without the schema.
191std::string CleanedChannelToString(const Channel *channel);
Austin Schuha81454b2020-05-12 19:58:36 -0700192// Prints out a channel to json, but only with the name and type.
193std::string StrippedChannelToString(const Channel *channel);
Austin Schuhbca6cf02019-12-22 17:28:34 -0800194
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800195// Returns the node names that this node should be forwarding to.
196std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
197 const Node *my_node);
198
199// Returns the node names that this node should be receiving messages from.
200std::vector<std::string_view> SourceNodeNames(const Configuration *config,
201 const Node *my_node);
202
Austin Schuhfc7b6a02021-07-12 21:19:07 -0700203// Returns the source node index for each channel in a config.
204std::vector<size_t> SourceNodeIndex(const Configuration *config);
205
Austin Schuh1ccc3a12024-04-30 17:46:29 -0700206// Returns the source node for a channel.
207const Node *SourceNode(const Configuration *config, const Channel *channel);
208
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700209// Returns the all nodes that are logging timestamps on our node.
210std::vector<const Node *> TimestampNodes(const Configuration *config,
211 const Node *my_node);
212
Austin Schuhd2e2f6a2021-02-07 20:46:16 -0800213// Returns the application for the provided node and name if it exists, or
214// nullptr if it does not exist on this node.
215const Application *GetApplication(const Configuration *config,
216 const Node *my_node,
217 std::string_view application_name);
218
Austin Schuhc41fa3c2021-10-16 14:35:35 -0700219// Returns true if the provided application should start on the provided node.
220bool ApplicationShouldStart(const Configuration *config, const Node *my_node,
221 const Application *application);
222
Austin Schuhfb37c612022-08-11 15:24:51 -0700223// Returns the number of messages in the queue.
Austin Schuh83cbb1e2023-06-23 12:59:02 -0700224size_t QueueSize(const Configuration *config, const Channel *channel);
225size_t QueueSize(size_t frequency,
226 std::chrono::nanoseconds channel_storage_duration);
Austin Schuhfb37c612022-08-11 15:24:51 -0700227
228// Returns the number of scratch buffers in the queue.
229int QueueScratchBufferSize(const Channel *channel);
230
Nathan Leong307c9692022-10-08 15:25:03 -0700231// Searches through configurations for schemas that include a certain type.
232const reflection::Schema *GetSchema(const Configuration *config,
233 std::string_view schema_type);
234
235// GetSchema template
236template <typename T>
237const reflection::Schema *GetSchema(const Configuration *config) {
238 return GetSchema(config, T::GetFullyQualifiedName());
239}
240
241// Copy schema reflection into detached flatbuffer
242std::optional<FlatbufferDetachedBuffer<reflection::Schema>>
243GetSchemaDetachedBuffer(const Configuration *config,
244 std::string_view schema_type);
Brian Silverman66f079a2013-08-26 16:24:30 -0700245
Austin Schuhfff9c3a2023-06-16 18:48:23 -0700246// Returns the storage duration for a channel.
247std::chrono::nanoseconds ChannelStorageDuration(const Configuration *config,
248 const Channel *channel);
249
James Kuszmaul741a4d02023-01-05 14:59:21 -0800250// Adds the specified channel to the config and returns the new, merged, config.
251// The channel name is derived from the specified name, the type and schema from
252// the provided schema, the source node from the specified node, and all other
253// fields (e.g., frequency) will be derived from the overrides parameter.
254aos::FlatbufferDetachedBuffer<Configuration> AddChannelToConfiguration(
255 const Configuration *config, std::string_view name,
256 aos::FlatbufferVector<reflection::Schema> schema,
257 const aos::Node *source_node = nullptr, ChannelT overrides = {});
258
Maxwell Gumley8c1b87f2024-02-13 17:54:52 -0700259// Build a new configuration that only contains the channels we want to
260// include. This is useful for excluding obsolete or deprecated channels, so
261// they don't appear in the configuration when reading the log.
262FlatbufferDetachedBuffer<Configuration> GetPartialConfiguration(
263 const Configuration &configuration,
264 std::function<bool(const Channel &)> should_include_channel);
Brian Silverman66f079a2013-08-26 16:24:30 -0700265} // namespace configuration
Alex Perrycb7da4b2019-08-28 19:35:56 -0700266
267// Compare and equality operators for Channel. Note: these only check the name
268// and type for equality.
269bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
270 const FlatbufferDetachedBuffer<Channel> &rhs);
271bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
272 const FlatbufferDetachedBuffer<Channel> &rhs);
Nathan Leong307c9692022-10-08 15:25:03 -0700273
Brian Silverman66f079a2013-08-26 16:24:30 -0700274} // namespace aos
275
John Park398c74a2018-10-20 21:17:39 -0700276#endif // AOS_CONFIGURATION_H_