Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_COMMON_CONFIG_FILE_HPP |
| 27 | #define NFD_DAEMON_COMMON_CONFIG_FILE_HPP |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 29 | #include "core/common.hpp" |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 30 | |
| 31 | #include <boost/property_tree/ptree.hpp> |
| 32 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 33 | #include <functional> |
| 34 | #include <map> |
| 35 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 36 | namespace nfd { |
| 37 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 38 | /** |
| 39 | * \brief A configuration file section. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 40 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 41 | using ConfigSection = boost::property_tree::ptree; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 42 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 43 | /** |
| 44 | * \brief An optional configuration file section. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 45 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 46 | using OptionalConfigSection = boost::optional<const ConfigSection&>; |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 47 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 48 | /** |
| 49 | * \brief Callback to process a configuration file section. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 50 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 51 | using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun, |
| 52 | const std::string& filename)>; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 53 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 54 | /** |
| 55 | * \brief Callback to process a configuration file section without a #ConfigSectionHandler. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 56 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 57 | using UnknownConfigSectionHandler = std::function<void(const std::string& filename, |
| 58 | const std::string& sectionName, |
| 59 | const ConfigSection& section, |
| 60 | bool isDryRun)>; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 61 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 62 | /** |
| 63 | * \brief Configuration file parsing utility. |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 64 | */ |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 65 | class ConfigFile : noncopyable |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 66 | { |
| 67 | public: |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 68 | class Error : public std::runtime_error |
| 69 | { |
| 70 | public: |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 71 | using std::runtime_error::runtime_error; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 74 | explicit |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 75 | ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection); |
| 76 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 77 | public: // unknown section handlers |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 78 | static void |
| 79 | throwErrorOnUnknownSection(const std::string& filename, |
| 80 | const std::string& sectionName, |
| 81 | const ConfigSection& section, |
| 82 | bool isDryRun); |
| 83 | |
| 84 | static void |
| 85 | ignoreUnknownSection(const std::string& filename, |
| 86 | const std::string& sectionName, |
| 87 | const ConfigSection& section, |
| 88 | bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 90 | public: // parse helpers |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 91 | /** \brief Parse a config option that can be either "yes" or "no". |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 92 | * \retval true "yes" |
| 93 | * \retval false "no" |
| 94 | * \throw Error the value is neither "yes" nor "no" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 95 | */ |
| 96 | static bool |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 97 | parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName); |
| 98 | |
| 99 | static bool |
| 100 | parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName) |
| 101 | { |
| 102 | return parseYesNo(option.second, option.first, sectionName); |
| 103 | } |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 104 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 105 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 106 | * \brief Parse a numeric (integral or floating point) config option. |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 107 | * \tparam T an arithmetic type |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 108 | * |
| 109 | * \return the numeric value of the parsed option |
| 110 | * \throw Error the value cannot be converted to the specified type |
| 111 | */ |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 112 | template<typename T> |
| 113 | static T |
| 114 | parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName) |
| 115 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 116 | static_assert(std::is_arithmetic_v<T>); |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 117 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 118 | auto value = node.get_value_optional<T>(); |
Eric Newberry | cb27d91 | 2020-04-08 19:38:18 -0700 | [diff] [blame] | 119 | // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489 |
| 120 | if (value && |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 121 | (std::is_signed_v<T> || node.get_value<std::string>().find("-") == std::string::npos)) { |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 122 | return *value; |
| 123 | } |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 124 | NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() + |
| 125 | "' for option '" + key + "' in section '" + sectionName + "'")); |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 128 | template<typename T> |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 129 | static T |
| 130 | parseNumber(const ConfigSection::value_type& option, const std::string& sectionName) |
| 131 | { |
| 132 | return parseNumber<T>(option.second, option.first, sectionName); |
| 133 | } |
| 134 | |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 135 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 136 | * \brief Check that a value is within the inclusive range [min, max]. |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 137 | * \throw Error the value is out of the acceptable range |
| 138 | */ |
| 139 | template<typename T> |
| 140 | static void |
| 141 | checkRange(T value, T min, T max, const std::string& key, const std::string& sectionName) |
| 142 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 143 | static_assert(std::is_integral_v<T>); |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 144 | |
| 145 | if (value < min || value > max) { |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 146 | NDN_THROW(Error("Invalid value '" + std::to_string(value) + "' for option '" + key + |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 147 | "' in section '" + sectionName + "': out of acceptable range [" + |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 148 | std::to_string(min) + ", " + std::to_string(max) + "]")); |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Junxiao Shi | 0cc125c | 2016-08-25 21:50:04 +0000 | [diff] [blame] | 152 | public: // setup and parsing |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 153 | /// \brief Setup notification of configuration file sections. |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 154 | void |
| 155 | addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 156 | ConfigSectionHandler subscriber); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 157 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 158 | /** |
| 159 | * \param filename file to parse |
| 160 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 161 | * \throws ConfigFile::Error if file not found |
| 162 | * \throws ConfigFile::Error if parse error |
| 163 | */ |
| 164 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 165 | parse(const std::string& filename, bool isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * \param input configuration (as a string) to parse |
| 169 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 170 | * \param filename logical filename of the config file, can appear in error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 171 | * \throws ConfigFile::Error if file not found |
| 172 | * \throws ConfigFile::Error if parse error |
| 173 | */ |
| 174 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 175 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * \param input stream to parse |
| 179 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 180 | * \param filename logical filename of the config file, can appear in error messages |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 181 | * \throws ConfigFile::Error if parse error |
| 182 | */ |
| 183 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 184 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 186 | /** |
| 187 | * \param config ConfigSection that needs to be processed |
| 188 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 189 | * \param filename logical filename of the config file, can appear in error messages |
| 190 | * \throws ConfigFile::Error if parse error |
| 191 | */ |
| 192 | void |
| 193 | parse(const ConfigSection& config, bool isDryRun, const std::string& filename); |
| 194 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 195 | private: |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 196 | void |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 197 | process(bool isDryRun, const std::string& filename) const; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 198 | |
| 199 | private: |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 200 | UnknownConfigSectionHandler m_unknownSectionCallback; |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 201 | std::map<std::string, ConfigSectionHandler> m_subscriptions; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 202 | ConfigSection m_global; |
| 203 | }; |
| 204 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 205 | } // namespace nfd |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 206 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 207 | #endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP |