Kea  2.3.4-git
hooks_parser.cc
Go to the documentation of this file.
1 // Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <cc/data.h>
10 #include <cc/dhcp_config_error.h>
11 #include <hooks/hooks_parser.h>
12 #include <boost/algorithm/string.hpp>
13 #include <boost/foreach.hpp>
14 #include <util/strutil.h>
15 #include <vector>
16 
17 using namespace std;
18 using namespace isc::data;
19 using namespace isc::hooks;
20 using namespace isc::dhcp;
21 
22 namespace isc {
23 namespace hooks {
24 
25 // @todo use the flat style, split into list and item
26 
27 void
28 HooksLibrariesParser::parse(HooksConfig& libraries, ConstElementPtr value) {
29  // Initialize.
30  libraries.clear();
31 
32  if (!value) {
33  isc_throw(DhcpConfigError, "Tried to parse null hooks libraries");
34  }
35 
36  // This is the new syntax. Iterate through it and get each map.
37  BOOST_FOREACH(ConstElementPtr library_entry, value->listValue()) {
38  ConstElementPtr parameters;
39 
40  // Is it a map?
41  if (library_entry->getType() != Element::map) {
42  isc_throw(DhcpConfigError, "hooks library configuration error:"
43  " one or more entries in the hooks-libraries list is not"
44  " a map (" << library_entry->getPosition() << ")");
45  }
46 
47  // Iterate through each element in the map. We check
48  // whether we have found a library element.
49  bool lib_found = false;
50 
51  string libname = "";
52 
53  // Let's explicitly reset the parameters, so we won't cover old
54  // values from the previous loop round.
55  parameters.reset();
56 
57  BOOST_FOREACH(auto entry_item, library_entry->mapValue()) {
58  if (entry_item.first == "library") {
59  if (entry_item.second->getType() != Element::string) {
60  isc_throw(DhcpConfigError, "hooks library configuration"
61  " error: value of 'library' element is not a string"
62  " giving the path to a hooks library (" <<
63  entry_item.second->getPosition() << ")");
64  }
65 
66  // Get the name of the library and add it to the list after
67  // removing quotes.
68  libname = (entry_item.second)->stringValue();
69 
70  // Remove leading/trailing quotes and any leading/trailing
71  // spaces.
72  boost::erase_all(libname, "\"");
73  libname = isc::util::str::trim(libname);
74  if (libname.empty()) {
75  isc_throw(DhcpConfigError, "hooks library configuration"
76  " error: value of 'library' element must not be"
77  " blank (" <<
78  entry_item.second->getPosition() << ")");
79  }
80 
81  // Note we have found the library name.
82  lib_found = true;
83  continue;
84  }
85 
86  // If there are parameters, let's remember them.
87  if (entry_item.first == "parameters") {
88  parameters = entry_item.second;
89  continue;
90  }
91 
92  // For all other parameters we will throw.
93  isc_throw(DhcpConfigError, "unknown hooks library parameter: "
94  << entry_item.first << "("
95  << library_entry->getPosition() << ")");
96  }
97 
98  if (! lib_found) {
99  isc_throw(DhcpConfigError, "hooks library configuration error:"
100  " one or more hooks-libraries elements are missing the"
101  " name of the library" <<
102  " (" << library_entry->getPosition() << ")");
103  }
104 
105  libraries.add(libname, parameters);
106  }
107 }
108 
109 }
110 }
STL namespace.
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
void clear()
Removes all configured hooks libraries.
Definition: hooks_config.h:59
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
To be removed. Please use ConfigError instead.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Defines the logger used by the top-level component of kea-lfc.
void add(std::string libname, isc::data::ConstElementPtr parameters)
Adds additional hooks libraries.
Definition: hooks_config.h:46
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53