blob: 7be8306b0d816f13055c1b2ebc45563fca93f85b [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 Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050033#include <functional>
34#include <map>
35
Steve DiBenedettobb75b552014-02-08 12:12:11 -070036namespace nfd {
37
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040038/**
39 * \brief A configuration file section.
Junxiao Shi38b24c72017-01-05 02:59:31 +000040 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040041using ConfigSection = boost::property_tree::ptree;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070042
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040043/**
44 * \brief An optional configuration file section.
Junxiao Shi38b24c72017-01-05 02:59:31 +000045 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040046using OptionalConfigSection = boost::optional<const ConfigSection&>;
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060047
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040048/**
49 * \brief Callback to process a configuration file section.
Junxiao Shi38b24c72017-01-05 02:59:31 +000050 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040051using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun,
52 const std::string& filename)>;
Junxiao Shi38b24c72017-01-05 02:59:31 +000053
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040054/**
55 * \brief Callback to process a configuration file section without a #ConfigSectionHandler.
Junxiao Shi38b24c72017-01-05 02:59:31 +000056 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040057using UnknownConfigSectionHandler = std::function<void(const std::string& filename,
58 const std::string& sectionName,
59 const ConfigSection& section,
60 bool isDryRun)>;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070061
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040062/**
63 * \brief Configuration file parsing utility.
Junxiao Shi0cc125c2016-08-25 21:50:04 +000064 */
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060065class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070066{
67public:
Steve DiBenedettobb75b552014-02-08 12:12:11 -070068 class Error : public std::runtime_error
69 {
70 public:
Davide Pesavento19779d82019-02-14 13:40:04 -050071 using std::runtime_error::runtime_error;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070072 };
73
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020074 explicit
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060075 ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
76
Junxiao Shi0cc125c2016-08-25 21:50:04 +000077public: // unknown section handlers
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060078 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 DiBenedettobb75b552014-02-08 12:12:11 -070089
Junxiao Shi0cc125c2016-08-25 21:50:04 +000090public: // parse helpers
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /** \brief Parse a config option that can be either "yes" or "no".
Junxiao Shi0cc125c2016-08-25 21:50:04 +000092 * \retval true "yes"
93 * \retval false "no"
94 * \throw Error the value is neither "yes" nor "no"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070095 */
96 static bool
Junxiao Shi0cc125c2016-08-25 21:50:04 +000097 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 Li698f4fe2015-08-19 16:30:16 -0700104
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200105 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400106 * \brief Parse a numeric (integral or floating point) config option.
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000107 * \tparam T an arithmetic type
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200108 *
109 * \return the numeric value of the parsed option
110 * \throw Error the value cannot be converted to the specified type
111 */
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000112 template<typename T>
113 static T
114 parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
115 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400116 static_assert(std::is_arithmetic_v<T>);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200117
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400118 auto value = node.get_value_optional<T>();
Eric Newberrycb27d912020-04-08 19:38:18 -0700119 // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489
120 if (value &&
Davide Pesavento0a05f7a2023-10-16 20:28:06 -0400121 (std::is_signed_v<T> || node.get_value<std::string>().find("-") == std::string::npos)) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000122 return *value;
123 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500124 NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
125 "' for option '" + key + "' in section '" + sectionName + "'"));
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000126 }
127
Junxiao Shia6286a92021-02-23 06:43:52 -0700128 template<typename T>
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000129 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 Shia6286a92021-02-23 06:43:52 -0700135 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400136 * \brief Check that a value is within the inclusive range [min, max].
Junxiao Shia6286a92021-02-23 06:43:52 -0700137 * \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 Pesaventob7bfcb92022-05-22 23:55:23 -0400143 static_assert(std::is_integral_v<T>);
Junxiao Shia6286a92021-02-23 06:43:52 -0700144
145 if (value < min || value > max) {
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500146 NDN_THROW(Error("Invalid value '" + std::to_string(value) + "' for option '" + key +
Junxiao Shia6286a92021-02-23 06:43:52 -0700147 "' in section '" + sectionName + "': out of acceptable range [" +
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500148 std::to_string(min) + ", " + std::to_string(max) + "]"));
Junxiao Shia6286a92021-02-23 06:43:52 -0700149 }
150 }
151
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000152public: // setup and parsing
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400153 /// \brief Setup notification of configuration file sections.
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700154 void
155 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600156 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700157
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700158 /**
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 DiBenedetto84da5bf2014-03-11 14:51:29 -0600165 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700166
167 /**
168 * \param input configuration (as a string) to parse
169 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800170 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700171 * \throws ConfigFile::Error if file not found
172 * \throws ConfigFile::Error if parse error
173 */
174 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600175 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700176
177 /**
178 * \param input stream to parse
179 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800180 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700181 * \throws ConfigFile::Error if parse error
182 */
183 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600184 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700185
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800186 /**
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 DiBenedettobb75b552014-02-08 12:12:11 -0700195private:
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700196 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200197 process(bool isDryRun, const std::string& filename) const;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700198
199private:
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600200 UnknownConfigSectionHandler m_unknownSectionCallback;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200201 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700202 ConfigSection m_global;
203};
204
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200205} // namespace nfd
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700206
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400207#endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP