blob: fac8da812b43d4856f510b3326437a4169a9b65a [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Pesavento1d7e7af2015-10-10 23:54:08 +020024 */
Steve DiBenedettobb75b552014-02-08 12:12:11 -070025
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040026#ifndef NFD_DAEMON_COMMON_CONFIG_FILE_HPP
27#define NFD_DAEMON_COMMON_CONFIG_FILE_HPP
Steve DiBenedettobb75b552014-02-08 12:12:11 -070028
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "core/common.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070030
31#include <boost/property_tree/ptree.hpp>
32
33namespace nfd {
34
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040035/**
36 * \brief A configuration file section.
Junxiao Shi38b24c72017-01-05 02:59:31 +000037 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040038using ConfigSection = boost::property_tree::ptree;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070039
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040040/**
41 * \brief An optional configuration file section.
Junxiao Shi38b24c72017-01-05 02:59:31 +000042 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040043using OptionalConfigSection = boost::optional<const ConfigSection&>;
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060044
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040045/**
46 * \brief Callback to process a configuration file section.
Junxiao Shi38b24c72017-01-05 02:59:31 +000047 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040048using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun,
49 const std::string& filename)>;
Junxiao Shi38b24c72017-01-05 02:59:31 +000050
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040051/**
52 * \brief Callback to process a configuration file section without a #ConfigSectionHandler.
Junxiao Shi38b24c72017-01-05 02:59:31 +000053 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040054using UnknownConfigSectionHandler = std::function<void(const std::string& filename,
55 const std::string& sectionName,
56 const ConfigSection& section,
57 bool isDryRun)>;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070058
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040059/**
60 * \brief Configuration file parsing utility.
Junxiao Shi0cc125c2016-08-25 21:50:04 +000061 */
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060062class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070063{
64public:
Steve DiBenedettobb75b552014-02-08 12:12:11 -070065 class Error : public std::runtime_error
66 {
67 public:
Davide Pesavento19779d82019-02-14 13:40:04 -050068 using std::runtime_error::runtime_error;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070069 };
70
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020071 explicit
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060072 ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
73
Junxiao Shi0cc125c2016-08-25 21:50:04 +000074public: // unknown section handlers
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060075 static void
76 throwErrorOnUnknownSection(const std::string& filename,
77 const std::string& sectionName,
78 const ConfigSection& section,
79 bool isDryRun);
80
81 static void
82 ignoreUnknownSection(const std::string& filename,
83 const std::string& sectionName,
84 const ConfigSection& section,
85 bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070086
Junxiao Shi0cc125c2016-08-25 21:50:04 +000087public: // parse helpers
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040088 /** \brief Parse a config option that can be either "yes" or "no".
Junxiao Shi0cc125c2016-08-25 21:50:04 +000089 * \retval true "yes"
90 * \retval false "no"
91 * \throw Error the value is neither "yes" nor "no"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070092 */
93 static bool
Junxiao Shi0cc125c2016-08-25 21:50:04 +000094 parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
95
96 static bool
97 parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
98 {
99 return parseYesNo(option.second, option.first, sectionName);
100 }
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200102 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400103 * \brief Parse a numeric (integral or floating point) config option.
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000104 * \tparam T an arithmetic type
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200105 *
106 * \return the numeric value of the parsed option
107 * \throw Error the value cannot be converted to the specified type
108 */
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000109 template<typename T>
110 static T
111 parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
112 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400113 static_assert(std::is_arithmetic_v<T>);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200114
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400115 auto value = node.get_value_optional<T>();
Eric Newberrycb27d912020-04-08 19:38:18 -0700116 // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489
117 if (value &&
118 (std::is_signed<T>() || node.get_value<std::string>().find("-") == std::string::npos)) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000119 return *value;
120 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500121 NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
122 "' for option '" + key + "' in section '" + sectionName + "'"));
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000123 }
124
Junxiao Shia6286a92021-02-23 06:43:52 -0700125 template<typename T>
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000126 static T
127 parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
128 {
129 return parseNumber<T>(option.second, option.first, sectionName);
130 }
131
Junxiao Shia6286a92021-02-23 06:43:52 -0700132 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400133 * \brief Check that a value is within the inclusive range [min, max].
Junxiao Shia6286a92021-02-23 06:43:52 -0700134 * \throw Error the value is out of the acceptable range
135 */
136 template<typename T>
137 static void
138 checkRange(T value, T min, T max, const std::string& key, const std::string& sectionName)
139 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400140 static_assert(std::is_integral_v<T>);
Junxiao Shia6286a92021-02-23 06:43:52 -0700141
142 if (value < min || value > max) {
143 NDN_THROW(Error("Invalid value '" + to_string(value) + "' for option '" + key +
144 "' in section '" + sectionName + "': out of acceptable range [" +
145 to_string(min) + ", " + to_string(max) + "]"));
146 }
147 }
148
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000149public: // setup and parsing
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400150 /// \brief Setup notification of configuration file sections.
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700151 void
152 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600153 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700154
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700155 /**
156 * \param filename file to parse
157 * \param isDryRun true if performing a dry run of configuration, false otherwise
158 * \throws ConfigFile::Error if file not found
159 * \throws ConfigFile::Error if parse error
160 */
161 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600162 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700163
164 /**
165 * \param input configuration (as a string) to parse
166 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800167 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700168 * \throws ConfigFile::Error if file not found
169 * \throws ConfigFile::Error if parse error
170 */
171 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600172 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700173
174 /**
175 * \param input stream to parse
176 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800177 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700178 * \throws ConfigFile::Error if parse error
179 */
180 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600181 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700182
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800183 /**
184 * \param config ConfigSection that needs to be processed
185 * \param isDryRun true if performing a dry run of configuration, false otherwise
186 * \param filename logical filename of the config file, can appear in error messages
187 * \throws ConfigFile::Error if parse error
188 */
189 void
190 parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
191
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700192private:
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700193 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200194 process(bool isDryRun, const std::string& filename) const;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700195
196private:
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600197 UnknownConfigSectionHandler m_unknownSectionCallback;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200198 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700199 ConfigSection m_global;
200};
201
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200202} // namespace nfd
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700203
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400204#endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP