blob: 19bf1cb4ce240a44b93839ce9cd7acdd1eed79e8 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02003 * Copyright (c) 2014-2015, Regents of the University of California,
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 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
Steve DiBenedettobb75b552014-02-08 12:12:11 -070026#include "config-file.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "logger.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070028
29#include <boost/property_tree/info_parser.hpp>
Davide Pesavento52a18f92014-04-10 00:55:01 +020030#include <fstream>
Steve DiBenedettobb75b552014-02-08 12:12:11 -070031
32namespace nfd {
33
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020034ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
35 : m_unknownSectionCallback(unknownSectionCallback)
36{
37}
38
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060039void
40ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
41 const std::string& sectionName,
42 const ConfigSection& section,
43 bool isDryRun)
44{
45 std::string msg = "Error processing configuration file ";
46 msg += filename;
47 msg += ": no module subscribed for section \"" + sectionName + "\"";
48
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070049 BOOST_THROW_EXCEPTION(ConfigFile::Error(msg));
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060050}
51
52void
53ConfigFile::ignoreUnknownSection(const std::string& filename,
54 const std::string& sectionName,
55 const ConfigSection& section,
56 bool isDryRun)
57{
58 // do nothing
59}
60
Yanbiao Li698f4fe2015-08-19 16:30:16 -070061bool
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020062ConfigFile::parseYesNo(const ConfigSection::value_type& option,
63 const std::string& sectionName)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070064{
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020065 auto value = option.second.get_value<std::string>();
66
Yanbiao Li698f4fe2015-08-19 16:30:16 -070067 if (value == "yes") {
68 return true;
69 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020070 else if (value == "no") {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070071 return false;
72 }
73
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020074 BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
75 option.first + "\" in \"" + sectionName + "\" section"));
Steve DiBenedettobb75b552014-02-08 12:12:11 -070076}
77
78void
79ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060080 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070081{
82 m_subscriptions[sectionName] = subscriber;
83}
84
85void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060086ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070087{
88 std::ifstream inputFile;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060089 inputFile.open(filename.c_str());
90 if (!inputFile.good() || !inputFile.is_open())
Steve DiBenedettobb75b552014-02-08 12:12:11 -070091 {
92 std::string msg = "Failed to read configuration file: ";
93 msg += filename;
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070094 BOOST_THROW_EXCEPTION(Error(msg));
Steve DiBenedettobb75b552014-02-08 12:12:11 -070095 }
96 parse(inputFile, isDryRun, filename);
97 inputFile.close();
98}
99
100void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600101ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700102{
103 std::istringstream inputStream(input);
104 parse(inputStream, isDryRun, filename);
105}
106
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700107void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600108ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700109{
110 try
111 {
112 boost::property_tree::read_info(input, m_global);
113 }
114 catch (const boost::property_tree::info_parser_error& error)
115 {
116 std::stringstream msg;
117 msg << "Failed to parse configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600118 msg << " " << filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700119 msg << " " << error.message() << " line " << error.line();
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700120 BOOST_THROW_EXCEPTION(Error(msg.str()));
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700121 }
122
123 process(isDryRun, filename);
124}
125
126void
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800127ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
128{
129 m_global = config;
130 process(isDryRun, filename);
131}
132
133void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200134ConfigFile::process(bool isDryRun, const std::string& filename) const
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700135{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600136 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700137
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200138 if (m_global.begin() == m_global.end()) {
139 std::string msg = "Error processing configuration file: ";
140 msg += filename;
141 msg += " no data";
142 BOOST_THROW_EXCEPTION(Error(msg));
143 }
144
145 for (const auto& i : m_global) {
146 try {
147 ConfigSectionHandler subscriber = m_subscriptions.at(i.first);
148 subscriber(i.second, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700149 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200150 catch (const std::out_of_range&) {
151 m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700152 }
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200153 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700154}
155
156}