blob: 39cc2edf6b6182a7d612aa7e2c6751585c0e8b30 [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
Junxiao Shi38b24c72017-01-05 02:59:31 +000035/** \brief a config file section
36 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040037using ConfigSection = boost::property_tree::ptree;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070038
Junxiao Shi38b24c72017-01-05 02:59:31 +000039/** \brief an optional config file section
40 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040041using OptionalConfigSection = boost::optional<const ConfigSection&>;
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060042
Junxiao Shi38b24c72017-01-05 02:59:31 +000043/** \brief callback to process a config file section
44 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040045using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun,
46 const std::string& filename)>;
Junxiao Shi38b24c72017-01-05 02:59:31 +000047
48/** \brief callback to process a config file section without a \p ConfigSectionHandler
49 */
Davide Pesavento87fc0f82018-04-11 23:43:51 -040050using UnknownConfigSectionHandler = std::function<void(const std::string& filename,
51 const std::string& sectionName,
52 const ConfigSection& section,
53 bool isDryRun)>;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070054
Junxiao Shi0cc125c2016-08-25 21:50:04 +000055/** \brief configuration file parsing utility
56 */
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060057class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070058{
59public:
Steve DiBenedettobb75b552014-02-08 12:12:11 -070060 class Error : public std::runtime_error
61 {
62 public:
Davide Pesavento19779d82019-02-14 13:40:04 -050063 using std::runtime_error::runtime_error;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070064 };
65
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020066 explicit
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060067 ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
68
Junxiao Shi0cc125c2016-08-25 21:50:04 +000069public: // unknown section handlers
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060070 static void
71 throwErrorOnUnknownSection(const std::string& filename,
72 const std::string& sectionName,
73 const ConfigSection& section,
74 bool isDryRun);
75
76 static void
77 ignoreUnknownSection(const std::string& filename,
78 const std::string& sectionName,
79 const ConfigSection& section,
80 bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070081
Junxiao Shi0cc125c2016-08-25 21:50:04 +000082public: // parse helpers
83 /** \brief parse a config option that can be either "yes" or "no"
84 * \retval true "yes"
85 * \retval false "no"
86 * \throw Error the value is neither "yes" nor "no"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070087 */
88 static bool
Junxiao Shi0cc125c2016-08-25 21:50:04 +000089 parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
90
91 static bool
92 parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
93 {
94 return parseYesNo(option.second, option.first, sectionName);
95 }
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020097 /**
98 * \brief parse a numeric (integral or floating point) config option
Junxiao Shi0cc125c2016-08-25 21:50:04 +000099 * \tparam T an arithmetic type
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200100 *
101 * \return the numeric value of the parsed option
102 * \throw Error the value cannot be converted to the specified type
103 */
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000104 template<typename T>
105 static T
106 parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
107 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400108 static_assert(std::is_arithmetic_v<T>);
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200109
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400110 auto value = node.get_value_optional<T>();
Eric Newberrycb27d912020-04-08 19:38:18 -0700111 // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489
112 if (value &&
113 (std::is_signed<T>() || node.get_value<std::string>().find("-") == std::string::npos)) {
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000114 return *value;
115 }
Davide Pesavento19779d82019-02-14 13:40:04 -0500116 NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
117 "' for option '" + key + "' in section '" + sectionName + "'"));
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000118 }
119
Junxiao Shia6286a92021-02-23 06:43:52 -0700120 template<typename T>
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000121 static T
122 parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
123 {
124 return parseNumber<T>(option.second, option.first, sectionName);
125 }
126
Junxiao Shia6286a92021-02-23 06:43:52 -0700127 /**
128 * \brief check that a value is within the inclusive range [min, max]
129 * \throw Error the value is out of the acceptable range
130 */
131 template<typename T>
132 static void
133 checkRange(T value, T min, T max, const std::string& key, const std::string& sectionName)
134 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400135 static_assert(std::is_integral_v<T>);
Junxiao Shia6286a92021-02-23 06:43:52 -0700136
137 if (value < min || value > max) {
138 NDN_THROW(Error("Invalid value '" + to_string(value) + "' for option '" + key +
139 "' in section '" + sectionName + "': out of acceptable range [" +
140 to_string(min) + ", " + to_string(max) + "]"));
141 }
142 }
143
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000144public: // setup and parsing
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700145 /// \brief setup notification of configuration file sections
146 void
147 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600148 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700149
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700150 /**
151 * \param filename file to parse
152 * \param isDryRun true if performing a dry run of configuration, false otherwise
153 * \throws ConfigFile::Error if file not found
154 * \throws ConfigFile::Error if parse error
155 */
156 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600157 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700158
159 /**
160 * \param input configuration (as a string) to parse
161 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800162 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700163 * \throws ConfigFile::Error if file not found
164 * \throws ConfigFile::Error if parse error
165 */
166 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600167 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700168
169 /**
170 * \param input stream to parse
171 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800172 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700173 * \throws ConfigFile::Error if parse error
174 */
175 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600176 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700177
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800178 /**
179 * \param config ConfigSection that needs to be processed
180 * \param isDryRun true if performing a dry run of configuration, false otherwise
181 * \param filename logical filename of the config file, can appear in error messages
182 * \throws ConfigFile::Error if parse error
183 */
184 void
185 parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
186
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700187private:
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700188 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200189 process(bool isDryRun, const std::string& filename) const;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700190
191private:
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600192 UnknownConfigSectionHandler m_unknownSectionCallback;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200193 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700194 ConfigSection m_global;
195};
196
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200197} // namespace nfd
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700198
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400199#endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP