blob: cb52d09789d93580d47025fe7545e318c39981fd [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
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008#include <cstdint>
James Kuszmaul3ae42262019-11-08 12:33:41 -08009#include <string_view>
10
Austin Schuhb8075812020-10-19 09:36:49 -070011#include "aos/configuration_generated.h" // IWYU pragma: export
Austin Schuhcb108412019-10-13 16:09:54 -070012#include "aos/flatbuffers.h"
13
Brian Silverman66f079a2013-08-26 16:24:30 -070014namespace aos {
15
16// Holds global configuration data. All of the functions are safe to call
17// from wherever.
18namespace configuration {
19
Austin Schuhcb108412019-10-13 16:09:54 -070020// Reads a json configuration. This includes all imports and merges. Note:
Milind Upadhyay17098ba2022-04-15 22:18:50 -070021// duplicate imports or invalid paths will result in a FATAL.
Austin Schuh40485ed2019-10-26 21:51:44 -070022FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -070023 const std::string_view path,
24 const std::vector<std::string_view> &extra_import_paths = {});
Austin Schuhcb108412019-10-13 16:09:54 -070025
Milind Upadhyay17098ba2022-04-15 22:18:50 -070026// Reads a json configuration. This includes all imports and merges. Returns
27// std::nullopt on duplicate imports or invalid paths.
28std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig(
29 const std::string_view path,
30 const std::vector<std::string_view> &extra_import_paths = {});
31
Alex Perrycb7da4b2019-08-28 19:35:56 -070032// Sorts and merges entries in a config.
33FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
34 const Flatbuffer<Configuration> &config);
35
36// Adds schema definitions to a sorted and merged config from the provided
37// schema list.
38FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
39 const Flatbuffer<Configuration> &config,
Austin Schuh0de30f32020-12-06 12:44:28 -080040 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas);
Alex Perrycb7da4b2019-08-28 19:35:56 -070041
Austin Schuh8d6cea82020-02-28 12:17:16 -080042// Merges a configuration json snippet into the provided configuration and
43// returns the modified config.
44FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
45 const Configuration *config, std::string_view json);
Brian Silverman24f5aa82020-06-23 16:21:28 -070046FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
47 const Configuration *config, const Flatbuffer<Configuration> &addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -080048
Austin Schuhc5fa6d92022-02-25 14:36:28 -080049// Adds the list of schemas to the provide aos_config.json. This should mostly
50// be used for testing and in conjunction with MergeWithConfig.
Austin Schuha9df9ad2021-06-16 14:49:39 -070051FlatbufferDetachedBuffer<aos::Configuration> AddSchema(
52 std::string_view json,
53 const std::vector<FlatbufferVector<reflection::Schema>> &schemas);
54
Austin Schuh681a2472020-12-31 23:55:40 -080055// Returns the resolved Channel for a name, type, and application name. Returns
Austin Schuh40485ed2019-10-26 21:51:44 -070056// nullptr if none is found.
Austin Schuhcb108412019-10-13 16:09:54 -070057//
58// If the application name is empty, it is ignored. Maps are processed in
59// reverse order, and application specific first.
James Kuszmaul71a81932020-12-15 21:08:01 -080060//
61// The config should already be fully merged and sorted (as produced by
62// MergeConfiguration() or any of the associated functions).
Austin Schuhbca6cf02019-12-22 17:28:34 -080063const Channel *GetChannel(const Configuration *config,
64 const std::string_view name,
65 const std::string_view type,
66 const std::string_view application_name,
Austin Schuh0de30f32020-12-06 12:44:28 -080067 const Node *node, bool quiet = false);
Austin Schuhbca6cf02019-12-22 17:28:34 -080068inline const Channel *GetChannel(const Flatbuffer<Configuration> &config,
69 const std::string_view name,
70 const std::string_view type,
71 const std::string_view application_name,
72 const Node *node) {
73 return GetChannel(&config.message(), name, type, application_name, node);
Austin Schuh40485ed2019-10-26 21:51:44 -070074}
Austin Schuh2f8fd752020-09-01 22:38:28 -070075template <typename T>
76inline const Channel *GetChannel(const Configuration *config,
77 const std::string_view name,
78 const std::string_view application_name,
79 const Node *node) {
80 return GetChannel(config, name, T::GetFullyQualifiedName(), application_name,
81 node);
82}
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080083// Convenience wrapper for getting a channel from a specified config if you
84// already have the name/type in a Channel object--this is useful if you Channel
85// object you have does not point to memory within config.
86inline const Channel *GetChannel(const Configuration *config,
87 const Channel *channel,
88 const std::string_view application_name,
89 const Node *node) {
90 return GetChannel(config, channel->name()->string_view(),
91 channel->type()->string_view(), application_name, node);
92}
Austin Schuhcb108412019-10-13 16:09:54 -070093
Austin Schuhc9e10ec2020-01-26 16:08:28 -080094// Returns the channel index (or dies) of channel in the provided config.
95size_t ChannelIndex(const Configuration *config, const Channel *channel);
96
Austin Schuh217a9782019-12-21 23:02:50 -080097// Returns the Node out of the config with the matching name, or nullptr if it
98// can't be found.
99const Node *GetNode(const Configuration *config, std::string_view name);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800100const Node *GetNode(const Configuration *config, const Node *node);
Austin Schuh0ca1fd32020-12-18 22:53:05 -0800101// Returns the node with the provided index. This works in both a single and
102// multi-node world.
103const Node *GetNode(const Configuration *config, size_t node_index);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800104// Returns a matching node, or nullptr if the provided node is nullptr and we
105// are in a single node world.
106const Node *GetNodeOrDie(const Configuration *config, const Node *node);
Austin Schuh217a9782019-12-21 23:02:50 -0800107// Returns the Node out of the configuration which matches our hostname.
108// CHECKs if it can't be found.
109const Node *GetMyNode(const Configuration *config);
110const Node *GetNodeFromHostname(const Configuration *config,
111 std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800113// Returns a vector of the nodes in the config. (nullptr is considered the node
114// in a single node world.)
115std::vector<const Node *> GetNodes(const Configuration *config);
116
Austin Schuh65465332020-11-05 17:36:53 -0800117// Returns a vector of the nodes in the config with the provided tag. If this
118// is a single-node world, we assume that all tags match.
119std::vector<const Node *> GetNodesWithTag(const Configuration *config,
120 std::string_view tag);
121
Brian Silverman631b6262021-11-10 12:25:08 -0800122// Returns whether the given node has the provided tag. If this is a single-node
123// world, we assume that all tags match.
124bool NodeHasTag(const Node *node, std::string_view tag);
125
Austin Schuh8bd96322020-02-13 21:18:22 -0800126// Returns the node index for a node. Note: will be faster if node is a pointer
127// to a node in config, but is not required.
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800128int GetNodeIndex(const Configuration *config, const Node *node);
Austin Schuh04408fc2020-02-16 21:48:54 -0800129int GetNodeIndex(const Configuration *config, std::string_view name);
Austin Schuh681a2472020-12-31 23:55:40 -0800130// Returns the number of nodes.
131size_t NodesCount(const Configuration *config);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800132
Austin Schuhac0771c2020-01-07 18:36:30 -0800133// Returns true if we are running in a multinode configuration.
134bool MultiNode(const Configuration *config);
135
Austin Schuh217a9782019-12-21 23:02:50 -0800136// Returns true if the provided channel is sendable on the provided node.
137bool ChannelIsSendableOnNode(const Channel *channel, const Node *node);
138// Returns true if the provided channel is able to be watched or fetched on the
139// provided node.
140bool ChannelIsReadableOnNode(const Channel *channel, const Node *node);
141
Austin Schuh719946b2019-12-28 14:51:01 -0800142// Returns true if the message is supposed to be logged on this node.
143bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node);
Austin Schuh2bb80e02021-03-20 21:46:17 -0700144bool ChannelMessageIsLoggedOnNode(const Channel *channel,
145 std::string_view node_name);
Austin Schuh719946b2019-12-28 14:51:01 -0800146
Austin Schuh58646e22021-08-23 23:51:46 -0700147// Returns the number of connections.
148size_t ConnectionCount(const Channel *channel);
149
Austin Schuh719946b2019-12-28 14:51:01 -0800150const Connection *ConnectionToNode(const Channel *channel, const Node *node);
151// Returns true if the delivery timestamps are supposed to be logged on this
152// node.
153bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
154 const Node *node,
155 const Node *logger_node);
156bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
157 const Node *node);
158
Austin Schuhbca6cf02019-12-22 17:28:34 -0800159// Prints a channel to json, but without the schema.
160std::string CleanedChannelToString(const Channel *channel);
Austin Schuha81454b2020-05-12 19:58:36 -0700161// Prints out a channel to json, but only with the name and type.
162std::string StrippedChannelToString(const Channel *channel);
Austin Schuhbca6cf02019-12-22 17:28:34 -0800163
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800164// Returns the node names that this node should be forwarding to.
165std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
166 const Node *my_node);
167
168// Returns the node names that this node should be receiving messages from.
169std::vector<std::string_view> SourceNodeNames(const Configuration *config,
170 const Node *my_node);
171
Austin Schuhfc7b6a02021-07-12 21:19:07 -0700172// Returns the source node index for each channel in a config.
173std::vector<size_t> SourceNodeIndex(const Configuration *config);
174
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700175// Returns the all nodes that are logging timestamps on our node.
176std::vector<const Node *> TimestampNodes(const Configuration *config,
177 const Node *my_node);
178
Austin Schuhd2e2f6a2021-02-07 20:46:16 -0800179// Returns the application for the provided node and name if it exists, or
180// nullptr if it does not exist on this node.
181const Application *GetApplication(const Configuration *config,
182 const Node *my_node,
183 std::string_view application_name);
184
Austin Schuhc41fa3c2021-10-16 14:35:35 -0700185// Returns true if the provided application should start on the provided node.
186bool ApplicationShouldStart(const Configuration *config, const Node *my_node,
187 const Application *application);
188
Austin Schuh217a9782019-12-21 23:02:50 -0800189// TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
Brian Silverman66f079a2013-08-26 16:24:30 -0700190
Brian Silverman66f079a2013-08-26 16:24:30 -0700191} // namespace configuration
Alex Perrycb7da4b2019-08-28 19:35:56 -0700192
193// Compare and equality operators for Channel. Note: these only check the name
194// and type for equality.
195bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
196 const FlatbufferDetachedBuffer<Channel> &rhs);
197bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
198 const FlatbufferDetachedBuffer<Channel> &rhs);
Brian Silverman66f079a2013-08-26 16:24:30 -0700199} // namespace aos
200
John Park398c74a2018-10-20 21:17:39 -0700201#endif // AOS_CONFIGURATION_H_