Kea 2.2.0
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>
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
17using namespace std;
18using namespace isc::data;
19using namespace isc::hooks;
20using namespace isc::dhcp;
21
22namespace isc {
23namespace hooks {
24
25// @todo use the flat style, split into list and item
26
27void
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}
To be removed. Please use ConfigError instead.
Wrapper class that holds hooks libraries configuration.
Definition: hooks_config.h:36
void clear()
Removes all configured hooks libraries.
Definition: hooks_config.h:59
void add(std::string libname, isc::data::ConstElementPtr parameters)
Adds additional hooks libraries.
Definition: hooks_config.h:46
void parse(HooksConfig &libraries, isc::data::ConstElementPtr value)
Parses parameters value.
Definition: hooks_parser.cc:28
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
Defines the logger used by the top-level component of kea-lfc.