blob: 363c66df20a6e8f11f281fd4a75739ea8adaad84 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, 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
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_CORE_CONFIG_FILE_HPP
27#define NFD_CORE_CONFIG_FILE_HPP
Steve DiBenedettobb75b552014-02-08 12:12:11 -070028
29#include "common.hpp"
30
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 */
Steve DiBenedettobb75b552014-02-08 12:12:11 -070037typedef boost::property_tree::ptree ConfigSection;
38
Junxiao Shi38b24c72017-01-05 02:59:31 +000039/** \brief an optional config file section
40 */
41typedef boost::optional<const ConfigSection&> OptionalConfigSection;
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 */
45typedef function<void(const ConfigSection& section,
46 bool isDryRun,
47 const std::string& filename)> ConfigSectionHandler;
48
49/** \brief callback to process a config file section without a \p ConfigSectionHandler
50 */
51typedef function<void(const std::string& filename,
52 const std::string& sectionName,
53 const ConfigSection& section,
54 bool isDryRun)> UnknownConfigSectionHandler;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070055
Junxiao Shi0cc125c2016-08-25 21:50:04 +000056/** \brief configuration file parsing utility
57 */
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060058class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070059{
60public:
Steve DiBenedettobb75b552014-02-08 12:12:11 -070061 class Error : public std::runtime_error
62 {
63 public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060064 explicit
Steve DiBenedettobb75b552014-02-08 12:12:11 -070065 Error(const std::string& what)
66 : std::runtime_error(what)
67 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070068 }
69 };
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
88 /** \brief parse a config option that can be either "yes" or "no"
89 * \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 /**
103 * \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 {
113 static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200114
Junxiao Shi0cc125c2016-08-25 21:50:04 +0000115 boost::optional<T> value = node.get_value_optional<T>();
116 if (value) {
117 return *value;
118 }
119 BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() +
120 "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
121 }
122
123 template <typename T>
124 static T
125 parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
126 {
127 return parseNumber<T>(option.second, option.first, sectionName);
128 }
129
130public: // setup and parsing
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700131 /// \brief setup notification of configuration file sections
132 void
133 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600134 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700135
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700136 /**
137 * \param filename file to parse
138 * \param isDryRun true if performing a dry run of configuration, false otherwise
139 * \throws ConfigFile::Error if file not found
140 * \throws ConfigFile::Error if parse error
141 */
142 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600143 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700144
145 /**
146 * \param input configuration (as a string) to parse
147 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800148 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700149 * \throws ConfigFile::Error if file not found
150 * \throws ConfigFile::Error if parse error
151 */
152 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600153 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700154
155 /**
156 * \param input stream to parse
157 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800158 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700159 * \throws ConfigFile::Error if parse error
160 */
161 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600162 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700163
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800164 /**
165 * \param config ConfigSection that needs to be processed
166 * \param isDryRun true if performing a dry run of configuration, false otherwise
167 * \param filename logical filename of the config file, can appear in error messages
168 * \throws ConfigFile::Error if parse error
169 */
170 void
171 parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
172
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700173private:
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700174 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200175 process(bool isDryRun, const std::string& filename) const;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700176
177private:
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600178 UnknownConfigSectionHandler m_unknownSectionCallback;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200179 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700180 ConfigSection m_global;
181};
182
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200183} // namespace nfd
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700184
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700185#endif // NFD_CORE_CONFIG_FILE_HPP