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